Browse Source

add typing hint and reformat code

akkariiin/Experimental
Akkariiin 6 years ago
parent
commit
813e518c48
  1. 3
      shadowsocks/asyncdns.py
  2. 14
      shadowsocks/manager.py
  3. 3
      shadowsocks/server.py
  4. 6
      shadowsocks/shell.py

3
shadowsocks/asyncdns.py

@ -23,6 +23,7 @@ import socket
import struct import struct
import re import re
import logging import logging
import typing
if __name__ == '__main__': if __name__ == '__main__':
import sys import sys
@ -269,7 +270,7 @@ STATUS_IPV6 = 1
class DNSResolver(object): 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._loop = None
self._hosts = {} self._hosts = {}
self._hostname_status = {} self._hostname_status = {}

14
shadowsocks/manager.py

@ -24,17 +24,17 @@ import socket
import logging import logging
import json import json
import collections import collections
import typing
from shadowsocks import common, eventloop, tcprelay, udprelay, asyncdns, shell from shadowsocks import common, eventloop, tcprelay, udprelay, asyncdns, shell
BUF_SIZE = 1506 BUF_SIZE = 1506
STAT_SEND_LIMIT = 50 STAT_SEND_LIMIT = 50
class Manager(object): class Manager(object):
def __init__(self, config): def __init__(self, config: typing.Dict[str, any]):
self._config = config self._config = config
self._relays = {} # (tcprelay, udprelay) self._relays = {} # (tcprelay, udprelay)
self._loop = eventloop.EventLoop() self._loop = eventloop.EventLoop()
@ -168,7 +168,7 @@ class Manager(object):
send_data(r) send_data(r)
r.clear() r.clear()
i = 0 i = 0
if len(r) > 0 : if len(r) > 0:
send_data(r) send_data(r)
self._statistics.clear() self._statistics.clear()
@ -190,7 +190,7 @@ class Manager(object):
self._loop.run() self._loop.run()
def run(config): def run(config: typing.Dict[str, any]):
Manager(config).run() Manager(config).run()
@ -207,9 +207,9 @@ def test():
eventloop.TIMEOUT_PRECISION = 1 eventloop.TIMEOUT_PRECISION = 1
def run_server(): def run_server():
config = shell.get_config(True) config: typing.Dict[str, any] = shell.get_config(True)
config = config.copy() config = config.copy()
a_config = { a_config: typing.Dict[str, any] = {
'server': '127.0.0.1', 'server': '127.0.0.1',
'local_port': 1081, 'local_port': 1081,
'port_password': { 'port_password': {
@ -223,7 +223,7 @@ def test():
'verbose': 2 'verbose': 2
} }
config.update(a_config) config.update(a_config)
manager = Manager(config) manager: Manager = Manager(config)
enc.append(manager) enc.append(manager)
manager.run() manager.run()

3
shadowsocks/server.py

@ -22,6 +22,7 @@ import sys
import os import os
import logging import logging
import signal import signal
import typing
if __name__ == '__main__': if __name__ == '__main__':
import inspect import inspect
@ -36,7 +37,7 @@ from shadowsocks import shell, daemon, eventloop, tcprelay, udprelay, \
def main(): def main():
shell.check_python() shell.check_python()
config = shell.get_config(False) config: typing.Dict[str, any] = shell.get_config(False)
shell.log_shadowsocks_version() shell.log_shadowsocks_version()

6
shadowsocks/shell.py

@ -23,6 +23,8 @@ import json
import sys import sys
import getopt import getopt
import logging import logging
import typing
from shadowsocks.common import to_bytes, to_str, IPNetwork, PortRange from shadowsocks.common import to_bytes, to_str, IPNetwork, PortRange
from shadowsocks import encrypt from shadowsocks import encrypt
@ -132,9 +134,9 @@ def check_config(config, is_local):
encrypt.try_cipher(config['password'], config['method']) encrypt.try_cipher(config['password'], config['method'])
def get_config(is_local): def get_config(is_local) -> typing.Dict[str, any]:
global verbose global verbose
config = {} config: typing.Dict[str, any] = {}
config_path = None config_path = None
logging.basicConfig(level=logging.INFO, logging.basicConfig(level=logging.INFO,
format='%(levelname)-s: %(message)s') format='%(levelname)-s: %(message)s')

Loading…
Cancel
Save