Make lint happier

This commit is contained in:
Lonami Exo
2017-05-21 13:59:16 +02:00
parent 63c89af983
commit 02a847b64a
20 changed files with 92 additions and 84 deletions

View File

@@ -1,4 +1,4 @@
from .aes import AES
from .rsa import RSA, RSAServerKey
from .auth_key import AuthKey
from .factorizator import Factorizator
from .factorization import Factorization

View File

@@ -1,7 +1,7 @@
from random import randint
class Factorizator:
class Factorization:
@staticmethod
def find_small_multiplier_lopatin(what):
"""Finds the small multiplier by using Lopatin's method"""
@@ -25,7 +25,7 @@ class Factorizator:
x = c
z = y - x if x < y else x - y
g = Factorizator.gcd(z, what)
g = Factorization.gcd(z, what)
if g != 1:
break
@@ -58,5 +58,5 @@ class Factorizator:
@staticmethod
def factorize(pq):
"""Factorizes the given number and returns both the divisor and the number divided by the divisor"""
divisor = Factorizator.find_small_multiplier_lopatin(pq)
divisor = Factorization.find_small_multiplier_lopatin(pq)
return divisor, pq // divisor

View File

@@ -112,7 +112,7 @@ class RPCError(Exception):
'FILE_PARTS_INVALID': 'The number of file parts is invalid.',
'FILE_PART_(\d+)_MISSING':
'Part {} of the file is missing from storage.',
'MD5_CHECKSUM_INVALID': 'The MD5 checksums do not match.',
'MD5_CHECKSUM_INVALID': 'The MD5 check-sums do not match.',
'PHOTO_INVALID_DIMENSIONS': 'The photo dimensions are invalid.',
'FIELD_NAME_INVALID': 'The field with the name FIELD_NAME is invalid.',
'FIELD_NAME_EMPTY': 'The field with the name FIELD_NAME is missing.',

View File

@@ -41,8 +41,8 @@ def calc_msg_key(data):
return sha1(data)[4:20]
def generate_key_data_from_nonces(server_nonce, new_nonce):
"""Generates the key data corresponding to the given nonces"""
def generate_key_data_from_nonce(server_nonce, new_nonce):
"""Generates the key data corresponding to the given nonce"""
hash1 = sha1(bytes(new_nonce + server_nonce))
hash2 = sha1(bytes(server_nonce + new_nonce))
hash3 = sha1(bytes(new_nonce + new_nonce))
@@ -68,10 +68,11 @@ def sha256(data):
def get_password_hash(pw, current_salt):
"""Gets the password hash for the two-step verification.
curent_salt should be the byte array provided by invoking GetPasswordRequest()"""
current_salt should be the byte array provided by invoking GetPasswordRequest()"""
# Passwords are encoded as UTF-8
# https://github.com/DrKLO/Telegram/blob/e31388/TMessagesProj/src/main/java/org/telegram/ui/LoginActivity.java#L2003
# At https://github.com/DrKLO/Telegram/blob/e31388
# src/main/java/org/telegram/ui/LoginActivity.java#L2003
data = pw.encode('utf-8')
pw_hash = current_salt + data + current_salt

View File

@@ -86,7 +86,7 @@ class InteractiveTelegramClient(TelegramClient):
# Display them so the user can choose
for i, entity in enumerate(entities):
i += 1 # 1-based index for normies
i += 1 # 1-based index
try:
print('{}. {}'.format(i, get_display_name(entity)))
except UnicodeEncodeError:

View File

@@ -2,7 +2,7 @@ import os
import time
from .. import helpers as utils
from ..crypto import AES, RSA, AuthKey, Factorizator
from ..crypto import AES, RSA, AuthKey, Factorization
from ..network import MtProtoPlainSender
from ..utils import BinaryReader, BinaryWriter
@@ -49,7 +49,7 @@ def do_authentication(transport):
# Step 2 sending: DH Exchange
new_nonce = os.urandom(32)
p, q = Factorizator.factorize(pq)
p, q = Factorization.factorize(pq)
with BinaryWriter() as pq_inner_data_writer:
pq_inner_data_writer.write_int(
0x83c95aec, signed=False) # PQ Inner Data
@@ -120,12 +120,12 @@ def do_authentication(transport):
encrypted_answer = reader.tgread_bytes()
# Step 3 sending: Complete DH Exchange
key, iv = utils.generate_key_data_from_nonces(server_nonce, new_nonce)
key, iv = utils.generate_key_data_from_nonce(server_nonce, new_nonce)
plain_text_answer = AES.decrypt_ige(encrypted_answer, key, iv)
g, dh_prime, ga, time_offset = None, None, None, None
with BinaryReader(plain_text_answer) as dh_inner_data_reader:
dh_inner_data_reader.read(20) # hashsum
dh_inner_data_reader.read(20) # hash sum
code = dh_inner_data_reader.read_int(signed=False)
if code != 0xb5890dba:
raise AssertionError('Invalid DH Inner Data code: {}'.format(code))

View File

@@ -46,7 +46,7 @@ class MtProtoPlainSender:
# See https://core.telegram.org/mtproto/description#message-identifier-msg-id
ms_time = int(time.time() * 1000)
new_msg_id = (((ms_time // 1000) << 32)
| # "must approximately equal unixtime*2^32"
| # "must approximately equal unix time*2^32"
((ms_time % 1000) << 22)
| # "approximate moment in time the message was created"
random.randint(0, 524288)

View File

@@ -282,9 +282,9 @@ class MtProtoSender:
def handle_pong(self, msg_id, sequence, reader, request):
self.logger.debug('Handling pong')
reader.read_int(signed=False) # code
recv_msg_id = reader.read_long(signed=False)
received_msg_id = reader.read_long(signed=False)
if recv_msg_id == request.msg_id:
if received_msg_id == request.msg_id:
self.logger.warning('Pong confirmed a request')
request.confirm_received = True
@@ -478,7 +478,7 @@ class MtProtoSender:
self.logger.info('Receiving updates cancelled')
except OSError:
self.logger.warning('OSError on updates thread, %s logging out',
'was' if self.logging_out else 'was not')
'was' if self.logging_out else 'was not')
if self.logging_out:
# This error is okay when logging out, means we got disconnected

View File

@@ -116,30 +116,30 @@ def parse_message_entities(msg):
del msg[entity.offset]
# Iterate over all the entities but the current
for subentity in [e for e in entities if e is not entity]:
for sub_entity in [e for e in entities if e is not entity]:
# First case, one in one out: so*me_th_in*g.
# In this case, the current entity length is decreased by two,
# and all the subentities offset decreases 1
if (subentity.offset > entity.offset and
subentity.offset + subentity.length <
# and all the sub_entities offset decreases 1
if (sub_entity.offset > entity.offset and
sub_entity.offset + sub_entity.length <
entity.offset + entity.length):
entity.length -= 2
subentity.offset -= 1
sub_entity.offset -= 1
# Second case, both inside: so*me_th*in_g.
# In this case, the current entity length is decreased by one,
# and all the subentities offset and length decrease 1
elif (entity.offset < subentity.offset < entity.offset +
entity.length < subentity.offset + subentity.length):
# and all the sub_entities offset and length decrease 1
elif (entity.offset < sub_entity.offset < entity.offset +
entity.length < sub_entity.offset + sub_entity.length):
entity.length -= 1
subentity.offset -= 1
subentity.length -= 1
sub_entity.offset -= 1
sub_entity.length -= 1
# Third case, both outside: so*me*th_in_g.
# In this case, the current entity is left untouched,
# and all the subentities offset decreases 2
elif subentity.offset > entity.offset + entity.length:
subentity.offset -= 2
# and all the sub_entities offset decreases 2
elif sub_entity.offset > entity.offset + entity.length:
sub_entity.offset -= 2
# Finally, we can join our poor mutilated message back and return
msg = ''.join(msg)

View File

@@ -17,8 +17,7 @@ from .tl.functions import InitConnectionRequest, InvokeWithLayerRequest
from .tl.functions.account import GetPasswordRequest
from .tl.functions.auth import (CheckPasswordRequest, LogOutRequest,
SendCodeRequest, SignInRequest,
SignUpRequest)
from .tl.functions.auth import ImportBotAuthorizationRequest
SignUpRequest, ImportBotAuthorizationRequest)
from .tl.functions.help import GetConfigRequest
from .tl.functions.messages import (
GetDialogsRequest, GetHistoryRequest, ReadHistoryRequest, SendMediaRequest,
@@ -34,7 +33,7 @@ from .tl.types import (
MessageMediaContact, MessageMediaDocument, MessageMediaPhoto,
UserProfilePhotoEmpty)
from .utils import (find_user_or_chat, get_input_peer,
get_appropiate_part_size, get_extension)
get_appropriated_part_size, get_extension)
class TelegramClient:
@@ -273,7 +272,7 @@ class TelegramClient:
self.session = None
return True
except:
except (RPCError, ConnectionError):
# Something happened when logging out, restore the state back
self.sender.logging_out = False
return False
@@ -282,8 +281,7 @@ class TelegramClient:
def list_sessions():
"""Lists all the sessions of the users who have ever connected
using this client and never logged out"""
return [path.splitext(path.basename(f))[
0] # splitext = split ext (not spli text!)
return [path.splitext(path.basename(f))[0]
for f in listdir('.') if f.endswith('.session')]
# endregion
@@ -424,7 +422,7 @@ class TelegramClient:
"""
file_size = path.getsize(file_path)
if not part_size_kb:
part_size_kb = get_appropiate_part_size(file_size)
part_size_kb = get_appropriated_part_size(file_size)
if part_size_kb > 512:
raise ValueError('The part size must be less or equal to 512KB')
@@ -534,7 +532,7 @@ class TelegramClient:
add_extension=True,
download_big=True):
"""Downloads the profile photo for an user or a chat (including channels).
Returns False if no photo was providen, or if it was Empty"""
Returns False if no photo was provided, or if it was Empty"""
if (not profile_photo or
isinstance(profile_photo, UserProfilePhotoEmpty) or
@@ -693,7 +691,7 @@ class TelegramClient:
if not file_size:
raise ValueError('A part size value must be provided')
else:
part_size_kb = get_appropiate_part_size(file_size)
part_size_kb = get_appropriated_part_size(file_size)
part_size = int(part_size_kb * 1024)
if part_size % 1024 != 0:

View File

@@ -31,7 +31,7 @@ class Session:
try:
os.remove('{}.session'.format(self.session_user_id))
return True
except:
except OSError:
return False
@staticmethod
@@ -54,7 +54,7 @@ class Session:
# Refer to mtproto_plain_sender.py for the original method, this is a simple copy
ms_time = int(time.time() * 1000)
new_msg_id = (((ms_time // 1000 + self.time_offset) << 32)
| # "must approximately equal unixtime*2^32"
| # "must approximately equal unix time*2^32"
((ms_time % 1000) << 22)
| # "approximate moment in time the message was created"
random.randint(0, 524288)

View File

@@ -77,8 +77,8 @@ def find_user_or_chat(peer, users, chats):
return None
def get_appropiate_part_size(file_size):
"""Gets the appropiate part size when uploading or downloading files,
def get_appropriated_part_size(file_size):
"""Gets the appropriated part size when uploading or downloading files,
given an initial file size"""
if file_size <= 1048576: # 1MB
return 32