mirror of
https://github.com/LonamiWebs/Telethon.git
synced 2025-03-28 13:54:13 +03:00
Add '_' key to TLObject's .to_dict() and remove recursive param
Closes #559
This commit is contained in:
parent
db698858e0
commit
2873dcf1c6
|
@ -20,15 +20,18 @@ class TLObject:
|
||||||
"""
|
"""
|
||||||
if indent is None:
|
if indent is None:
|
||||||
if isinstance(obj, TLObject):
|
if isinstance(obj, TLObject):
|
||||||
return '{}({})'.format(type(obj).__name__, ', '.join(
|
obj = obj.to_dict()
|
||||||
'{}={}'.format(k, TLObject.pretty_format(v))
|
|
||||||
for k, v in obj.to_dict(recursive=False).items()
|
|
||||||
))
|
|
||||||
if isinstance(obj, dict):
|
if isinstance(obj, dict):
|
||||||
return '{{{}}}'.format(', '.join(
|
if '_' in obj:
|
||||||
'{}: {}'.format(k, TLObject.pretty_format(v))
|
pre, left, right, sep = obj['_'], '(', ')', '{}={}'
|
||||||
for k, v in obj.items()
|
else:
|
||||||
))
|
pre, left, right, sep = '', '{', '}', '{}: {}'
|
||||||
|
|
||||||
|
mid = ', '.join(sep.format(k, TLObject.pretty_format(v))
|
||||||
|
for k, v in obj.items() if not pre or k != '_')
|
||||||
|
return '{}{}{}{}'.format(pre, left, mid, right)
|
||||||
|
|
||||||
elif isinstance(obj, str) or isinstance(obj, bytes):
|
elif isinstance(obj, str) or isinstance(obj, bytes):
|
||||||
return repr(obj)
|
return repr(obj)
|
||||||
elif hasattr(obj, '__iter__'):
|
elif hasattr(obj, '__iter__'):
|
||||||
|
@ -43,30 +46,33 @@ class TLObject:
|
||||||
return repr(obj)
|
return repr(obj)
|
||||||
else:
|
else:
|
||||||
result = []
|
result = []
|
||||||
if isinstance(obj, TLObject) or isinstance(obj, dict):
|
if isinstance(obj, TLObject):
|
||||||
if isinstance(obj, dict):
|
obj = obj.to_dict()
|
||||||
d = obj
|
|
||||||
start, end, sep = '{', '}', ': '
|
|
||||||
else:
|
|
||||||
d = obj.to_dict(recursive=False)
|
|
||||||
start, end, sep = '(', ')', '='
|
|
||||||
result.append(type(obj).__name__)
|
|
||||||
|
|
||||||
result.append(start)
|
if isinstance(obj, dict):
|
||||||
if d:
|
if '_' in obj:
|
||||||
|
pre, left, right, sep = obj['_'], '(', ')', '{}={}'
|
||||||
|
else:
|
||||||
|
pre, left, right, sep = '', '{', '}', '{}: {}'
|
||||||
|
|
||||||
|
result.append(pre)
|
||||||
|
result.append(left)
|
||||||
|
if obj:
|
||||||
result.append('\n')
|
result.append('\n')
|
||||||
indent += 1
|
indent += 1
|
||||||
for k, v in d.items():
|
for k, v in obj.items():
|
||||||
|
if pre and k == '_':
|
||||||
|
continue
|
||||||
result.append('\t' * indent)
|
result.append('\t' * indent)
|
||||||
result.append(k)
|
result.append(sep.format(
|
||||||
result.append(sep)
|
k, TLObject.pretty_format(v, indent)
|
||||||
result.append(TLObject.pretty_format(v, indent))
|
))
|
||||||
result.append(',\n')
|
result.append(',\n')
|
||||||
result.pop() # last ',\n'
|
result.pop() # last ',\n'
|
||||||
indent -= 1
|
indent -= 1
|
||||||
result.append('\n')
|
result.append('\n')
|
||||||
result.append('\t' * indent)
|
result.append('\t' * indent)
|
||||||
result.append(end)
|
result.append(right)
|
||||||
|
|
||||||
elif isinstance(obj, str) or isinstance(obj, bytes):
|
elif isinstance(obj, str) or isinstance(obj, bytes):
|
||||||
result.append(repr(obj))
|
result.append(repr(obj))
|
||||||
|
@ -158,7 +164,7 @@ class TLObject:
|
||||||
def resolve(self, client, utils):
|
def resolve(self, client, utils):
|
||||||
pass
|
pass
|
||||||
|
|
||||||
def to_dict(self, recursive=True):
|
def to_dict(self):
|
||||||
return {}
|
return {}
|
||||||
|
|
||||||
def __bytes__(self):
|
def __bytes__(self):
|
||||||
|
|
|
@ -297,17 +297,16 @@ class TLGenerator:
|
||||||
builder.end_block()
|
builder.end_block()
|
||||||
|
|
||||||
# Write the to_dict(self) method
|
# Write the to_dict(self) method
|
||||||
builder.writeln('def to_dict(self, recursive=True):')
|
builder.writeln('def to_dict(self):')
|
||||||
if args:
|
builder.writeln('return {')
|
||||||
builder.writeln('return {')
|
|
||||||
else:
|
|
||||||
builder.write('return {')
|
|
||||||
builder.current_indent += 1
|
builder.current_indent += 1
|
||||||
|
|
||||||
base_types = ('string', 'bytes', 'int', 'long', 'int128',
|
base_types = ('string', 'bytes', 'int', 'long', 'int128',
|
||||||
'int256', 'double', 'Bool', 'true', 'date')
|
'int256', 'double', 'Bool', 'true', 'date')
|
||||||
|
|
||||||
|
builder.write("'_': '{}'".format(tlobject.class_name()))
|
||||||
for arg in args:
|
for arg in args:
|
||||||
|
builder.writeln(',')
|
||||||
builder.write("'{}': ".format(arg.name))
|
builder.write("'{}': ".format(arg.name))
|
||||||
if arg.type in base_types:
|
if arg.type in base_types:
|
||||||
if arg.is_vector:
|
if arg.is_vector:
|
||||||
|
@ -318,17 +317,17 @@ class TLGenerator:
|
||||||
else:
|
else:
|
||||||
if arg.is_vector:
|
if arg.is_vector:
|
||||||
builder.write(
|
builder.write(
|
||||||
'([] if self.{0} is None else [None'
|
'[] if self.{0} is None else [None '
|
||||||
' if x is None else x.to_dict() for x in self.{0}]'
|
'if x is None else x.to_dict() for x in self.{0}]'
|
||||||
') if recursive else self.{0}'.format(arg.name)
|
.format(arg.name)
|
||||||
)
|
)
|
||||||
else:
|
else:
|
||||||
builder.write(
|
builder.write(
|
||||||
'(None if self.{0} is None else self.{0}.to_dict())'
|
'None if self.{0} is None else self.{0}.to_dict()'
|
||||||
' if recursive else self.{0}'.format(arg.name)
|
.format(arg.name)
|
||||||
)
|
)
|
||||||
builder.writeln(',')
|
|
||||||
|
|
||||||
|
builder.writeln()
|
||||||
builder.current_indent -= 1
|
builder.current_indent -= 1
|
||||||
builder.writeln("}")
|
builder.writeln("}")
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue
Block a user