Clean-up unused methods and reuse more common utils

This commit is contained in:
Lonami Exo 2018-04-14 17:22:39 +02:00
parent ab15f3699f
commit 200d3c0360
5 changed files with 30 additions and 53 deletions

View File

@ -12,6 +12,7 @@ from .common import (
) )
# This imports the base errors too, as they're imported there # This imports the base errors too, as they're imported there
from .rpc_base_errors import *
from .rpc_error_list import * from .rpc_error_list import *

View File

@ -1,7 +1,8 @@
import json import json
import re
from collections import defaultdict from collections import defaultdict
import re from ..utils import snake_to_camel_case
# Core base classes depending on the integer error code # Core base classes depending on the integer error code
KNOWN_BASE_CLASSES = { KNOWN_BASE_CLASSES = {
@ -45,13 +46,8 @@ def _get_class_name(error_code):
error_code, 'RPCError' + str(error_code).replace('-', 'Neg') error_code, 'RPCError' + str(error_code).replace('-', 'Neg')
) )
if 'FIRSTNAME' in error_code: return snake_to_camel_case(error_code.replace('FIRSTNAME', 'FIRST_NAME'),
error_code = error_code.replace('FIRSTNAME', 'FIRST_NAME') suffix='Error')
result = re.sub(
r'_([a-z])', lambda m: m.group(1).upper(), error_code.lower()
)
return result[:1].upper() + result[1:].replace('_', '') + 'Error'
class Error: class Error:

View File

@ -1,6 +1,8 @@
import re import re
from zlib import crc32 from zlib import crc32
from ..utils import snake_to_camel_case
CORE_TYPES = ( CORE_TYPES = (
0xbc799737, # boolFalse#bc799737 = Bool; 0xbc799737, # boolFalse#bc799737 = Bool;
0x997275b5, # boolTrue#997275b5 = Bool; 0x997275b5, # boolTrue#997275b5 = Bool;
@ -31,6 +33,7 @@ class TLObject:
self.args = args self.args = args
self.result = result self.result = result
self.is_function = is_function self.is_function = is_function
self.id = None
if object_id is None: if object_id is None:
self.id = self.infer_id() self.id = self.infer_id()
else: else:
@ -38,20 +41,8 @@ class TLObject:
assert self.id == self.infer_id(),\ assert self.id == self.infer_id(),\
'Invalid inferred ID for ' + repr(self) 'Invalid inferred ID for ' + repr(self)
def class_name(self): self.class_name = snake_to_camel_case(
"""Gets the class name following the Python style guidelines""" self.name, suffix='Request' if self.is_function else '')
return self.class_name_for(self.name, self.is_function)
@staticmethod
def class_name_for(typename, is_function=False):
"""Gets the class name following the Python style guidelines"""
# Courtesy of http://stackoverflow.com/a/31531797/4759433
result = re.sub(r'_([a-z])', lambda m: m.group(1).upper(), typename)
result = result[:1].upper() + result[1:].replace('_', '')
# If it's a function, let it end with "Request" to identify them
if is_function:
result += 'Request'
return result
def sorted_args(self): def sorted_args(self):
"""Returns the arguments properly sorted and ready to plug-in """Returns the arguments properly sorted and ready to plug-in
@ -62,11 +53,10 @@ class TLObject:
key=lambda x: x.is_flag or x.can_be_inferred) key=lambda x: x.is_flag or x.can_be_inferred)
def __repr__(self, ignore_id=False): def __repr__(self, ignore_id=False):
if getattr(self, 'id', None) is None or ignore_id: if self.id is None or ignore_id:
hex_id = '' hex_id = ''
else: else:
# Skip 0x and add 0's for padding hex_id = '#{:08x}'.format(self.id)
hex_id = '#' + hex(self.id)[2:].rjust(8, '0')
if self.args: if self.args:
args = ' ' + ' '.join([repr(arg) for arg in self.args]) args = ' ' + ' '.join([repr(arg) for arg in self.args])
@ -90,25 +80,6 @@ class TLObject:
) )
return crc32(representation.encode('ascii')) return crc32(representation.encode('ascii'))
def __str__(self):
# Some arguments are not valid for being represented,
# such as the flag indicator or generic definition
# (these have no explicit values until used)
valid_args = [arg for arg in self.args
if not arg.flag_indicator and not arg.generic_definition]
args = ', '.join(['{}={{}}'.format(arg.name) for arg in valid_args])
# Since Python's default representation for lists is using repr(),
# we need to str() manually on every item
args_format = ', '.join(
['str(self.{})'.format(arg.name) if not arg.is_vector else
'None if not self.{0} else [str(_) for _ in self.{0}]'.format(
arg.name) for arg in valid_args])
return ("'({} (ID: {}) = ({}))'.format({})"
.format(self.fullname, hex(self.id), args, args_format))
class TLArg: class TLArg:
def __init__(self, name, arg_type, generic_definition): def __init__(self, name, arg_type, generic_definition):

View File

@ -7,6 +7,7 @@ from collections import defaultdict
from .source_builder import SourceBuilder from .source_builder import SourceBuilder
from .parsers import TLObject, parse_tl, find_layer from .parsers import TLObject, parse_tl, find_layer
from .utils import snake_to_camel_case
AUTO_GEN_NOTICE = \ AUTO_GEN_NOTICE = \
'"""File generated by TLObjects\' generator. All changes will be ERASED"""' '"""File generated by TLObjects\' generator. All changes will be ERASED"""'
@ -113,7 +114,7 @@ class TLGenerator:
if tlobject.namespace: if tlobject.namespace:
builder.write('.' + tlobject.namespace) builder.write('.' + tlobject.namespace)
builder.writeln('.{},', tlobject.class_name()) builder.writeln('.{},', tlobject.class_name)
builder.current_indent -= 1 builder.current_indent -= 1
builder.writeln('}') builder.writeln('}')
@ -173,10 +174,10 @@ class TLGenerator:
pass pass
elif len(constructors) == 1: elif len(constructors) == 1:
type_defs.append('Type{} = {}'.format( type_defs.append('Type{} = {}'.format(
type_name, constructors[0].class_name())) type_name, constructors[0].class_name))
else: else:
type_defs.append('Type{} = Union[{}]'.format( type_defs.append('Type{} = Union[{}]'.format(
type_name, ','.join(c.class_name() type_name, ','.join(c.class_name
for c in constructors))) for c in constructors)))
imports = {} imports = {}
@ -238,7 +239,7 @@ class TLGenerator:
""" """
builder.writeln() builder.writeln()
builder.writeln() builder.writeln()
builder.writeln('class {}(TLObject):', tlobject.class_name()) builder.writeln('class {}(TLObject):', tlobject.class_name)
# Class-level variable to store its Telegram's constructor ID # Class-level variable to store its Telegram's constructor ID
builder.writeln('CONSTRUCTOR_ID = {:#x}', tlobject.id) builder.writeln('CONSTRUCTOR_ID = {:#x}', tlobject.id)
@ -297,10 +298,10 @@ class TLGenerator:
builder.writeln('This type has no constructors.') builder.writeln('This type has no constructors.')
elif len(constructors) == 1: elif len(constructors) == 1:
builder.writeln('Instance of {}.', builder.writeln('Instance of {}.',
constructors[0].class_name()) constructors[0].class_name)
else: else:
builder.writeln('Instance of either {}.', ', '.join( builder.writeln('Instance of either {}.', ', '.join(
c.class_name() for c in constructors)) c.class_name for c in constructors))
builder.writeln('"""') builder.writeln('"""')
@ -365,7 +366,7 @@ class TLGenerator:
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("'_': '{}'", tlobject.class_name()) builder.write("'_': '{}'", tlobject.class_name)
for arg in args: for arg in args:
builder.writeln(',') builder.writeln(',')
builder.write("'{}': ", arg.name) builder.write("'{}': ", arg.name)
@ -441,7 +442,7 @@ class TLGenerator:
builder.writeln( builder.writeln(
'return {}({})', 'return {}({})',
tlobject.class_name(), tlobject.class_name,
', '.join( ', '.join(
'{0}=_{0}'.format(a.name) for a in tlobject.sorted_args() '{0}=_{0}'.format(a.name) for a in tlobject.sorted_args()
if not a.flag_indicator and not a.generic_definition if not a.flag_indicator and not a.generic_definition
@ -709,7 +710,7 @@ class TLGenerator:
ns, t = '.', arg.type ns, t = '.', arg.type
else: else:
ns, t = '.' + arg.type[:sep_index], arg.type[sep_index+1:] ns, t = '.' + arg.type[:sep_index], arg.type[sep_index+1:]
class_name = TLObject.class_name_for(t) class_name = snake_to_camel_case(t)
# There would be no need to import the type if we're in the # There would be no need to import the type if we're in the
# file with the same namespace, but since it does no harm # file with the same namespace, but since it does no harm

View File

@ -0,0 +1,8 @@
import re
def snake_to_camel_case(name, suffix=None):
# Courtesy of http://stackoverflow.com/a/31531797/4759433
result = re.sub(r'_([a-z])', lambda m: m.group(1).upper(), name.lower())
result = result[:1].upper() + result[1:].replace('_', '')
return result + suffix if suffix else result