From 813e518c48b0309f87eae5221413224356a42885 Mon Sep 17 00:00:00 2001 From: Akkariiin Date: Mon, 13 Aug 2018 00:21:12 +0800 Subject: [PATCH] add typing hint and reformat code --- shadowsocks/asyncdns.py | 5 +++-- shadowsocks/manager.py | 14 +++++++------- shadowsocks/server.py | 3 ++- shadowsocks/shell.py | 6 ++++-- 4 files changed, 16 insertions(+), 12 deletions(-) diff --git a/shadowsocks/asyncdns.py b/shadowsocks/asyncdns.py index 868ea61..f05a825 100644 --- a/shadowsocks/asyncdns.py +++ b/shadowsocks/asyncdns.py @@ -23,6 +23,7 @@ import socket import struct import re import logging +import typing if __name__ == '__main__': import sys @@ -269,7 +270,7 @@ STATUS_IPV6 = 1 class DNSResolver(object): - def __init__(self, black_hostname_list=None): + def __init__(self, black_hostname_list: typing.Optional[typing.List[bytes]] = None): self._loop = None self._hosts = {} self._hostname_status = {} @@ -388,7 +389,7 @@ class DNSResolver(object): ip = None for answer in response.answers: if answer[1] in (QTYPE_A, QTYPE_AAAA) and \ - answer[2] == QCLASS_IN: + answer[2] == QCLASS_IN: ip = answer[0] break if IPV6_CONNECTION_SUPPORT: diff --git a/shadowsocks/manager.py b/shadowsocks/manager.py index 80d0a32..d1e7235 100644 --- a/shadowsocks/manager.py +++ b/shadowsocks/manager.py @@ -24,17 +24,17 @@ import socket import logging import json import collections +import typing from shadowsocks import common, eventloop, tcprelay, udprelay, asyncdns, shell - BUF_SIZE = 1506 STAT_SEND_LIMIT = 50 class Manager(object): - def __init__(self, config): + def __init__(self, config: typing.Dict[str, any]): self._config = config self._relays = {} # (tcprelay, udprelay) self._loop = eventloop.EventLoop() @@ -168,7 +168,7 @@ class Manager(object): send_data(r) r.clear() i = 0 - if len(r) > 0 : + if len(r) > 0: send_data(r) self._statistics.clear() @@ -190,7 +190,7 @@ class Manager(object): self._loop.run() -def run(config): +def run(config: typing.Dict[str, any]): Manager(config).run() @@ -207,9 +207,9 @@ def test(): eventloop.TIMEOUT_PRECISION = 1 def run_server(): - config = shell.get_config(True) + config: typing.Dict[str, any] = shell.get_config(True) config = config.copy() - a_config = { + a_config: typing.Dict[str, any] = { 'server': '127.0.0.1', 'local_port': 1081, 'port_password': { @@ -223,7 +223,7 @@ def test(): 'verbose': 2 } config.update(a_config) - manager = Manager(config) + manager: Manager = Manager(config) enc.append(manager) manager.run() diff --git a/shadowsocks/server.py b/shadowsocks/server.py index dced0a3..75430d6 100755 --- a/shadowsocks/server.py +++ b/shadowsocks/server.py @@ -22,6 +22,7 @@ import sys import os import logging import signal +import typing if __name__ == '__main__': import inspect @@ -36,7 +37,7 @@ from shadowsocks import shell, daemon, eventloop, tcprelay, udprelay, \ def main(): shell.check_python() - config = shell.get_config(False) + config: typing.Dict[str, any] = shell.get_config(False) shell.log_shadowsocks_version() diff --git a/shadowsocks/shell.py b/shadowsocks/shell.py index a1547d0..c9911c2 100755 --- a/shadowsocks/shell.py +++ b/shadowsocks/shell.py @@ -23,6 +23,8 @@ import json import sys import getopt import logging +import typing + from shadowsocks.common import to_bytes, to_str, IPNetwork, PortRange from shadowsocks import encrypt @@ -132,9 +134,9 @@ def check_config(config, is_local): encrypt.try_cipher(config['password'], config['method']) -def get_config(is_local): +def get_config(is_local) -> typing.Dict[str, any]: global verbose - config = {} + config: typing.Dict[str, any] = {} config_path = None logging.basicConfig(level=logging.INFO, format='%(levelname)-s: %(message)s')