From 6a4faa83a4e7fc91ddb23f3d4ff433edec815396 Mon Sep 17 00:00:00 2001 From: John Smith Date: Sat, 17 Jul 2021 13:03:45 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E4=B8=80=E4=BA=9BPyCharm?= =?UTF-8?q?=E8=AD=A6=E5=91=8A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- api/base.py | 17 ++++++++++------- config.py | 2 +- main.py | 1 + models/avatar.py | 2 +- models/database.py | 4 ++-- models/translate.py | 2 +- 6 files changed, 16 insertions(+), 12 deletions(-) diff --git a/api/base.py b/api/base.py index a4513b4..3d092ce 100644 --- a/api/base.py +++ b/api/base.py @@ -7,6 +7,10 @@ import tornado.web # noinspection PyAbstractClass class ApiHandler(tornado.web.RequestHandler): + def __init__(self, *args, **kwargs): + super().__init__(*args, **kwargs) + self.json_args = None + def set_default_headers(self): # 跨域测试用 if not self.application.settings['debug']: @@ -18,13 +22,12 @@ class ApiHandler(tornado.web.RequestHandler): self.request.headers['Access-Control-Request-Headers']) def prepare(self): - if self.request.headers.get('Content-Type', '').startswith('application/json'): - try: - self.json_args = json.loads(self.request.body) - except json.JSONDecodeError: - self.json_args = None - else: - self.json_args = None + if not self.request.headers.get('Content-Type', '').startswith('application/json'): + return + try: + self.json_args = json.loads(self.request.body) + except json.JSONDecodeError: + pass async def options(self, *_args, **_kwargs): # 跨域测试用 diff --git a/config.py b/config.py index 2653552..47aa3c8 100644 --- a/config.py +++ b/config.py @@ -121,7 +121,7 @@ class AppConfig: self.translator_configs = translator_configs -def _str_to_list(value, item_type: Type=str, container_type: Type=list): +def _str_to_list(value, item_type: Type = str, container_type: Type = list): value = value.strip() if value == '': return container_type() diff --git a/main.py b/main.py index d035417..3ca0e51 100644 --- a/main.py +++ b/main.py @@ -60,6 +60,7 @@ def init_logging(debug): file_handler = logging.handlers.TimedRotatingFileHandler( LOG_FILE_NAME, encoding='utf-8', when='midnight', backupCount=7, delay=True ) + # noinspection PyArgumentList logging.basicConfig( format='{asctime} {levelname} [{name}]: {message}', datefmt='%Y-%m-%d %H:%M:%S', diff --git a/models/avatar.py b/models/avatar.py index a8995c8..7865860 100644 --- a/models/avatar.py +++ b/models/avatar.py @@ -25,7 +25,7 @@ _avatar_url_cache: Dict[int, str] = {} # 正在获取头像的Future,user_id -> Future _uid_fetch_future_map: Dict[int, asyncio.Future] = {} # 正在获取头像的user_id队列 -_uid_queue_to_fetch = None +_uid_queue_to_fetch: Optional[asyncio.Queue] = None # 上次被B站ban时间 _last_fetch_banned_time: Optional[datetime.datetime] = None diff --git a/models/database.py b/models/database.py index 884d765..42228c0 100644 --- a/models/database.py +++ b/models/database.py @@ -13,7 +13,7 @@ engine = None DbSession: Optional[Type[sqlalchemy.orm.Session]] = None -def init(debug): +def init(_debug): cfg = config.get_config() global engine, DbSession # engine = sqlalchemy.create_engine(cfg.database_url, echo=debug) @@ -28,7 +28,7 @@ def get_session(): session = DbSession() try: yield session - except: + except BaseException: session.rollback() raise finally: diff --git a/models/translate.py b/models/translate.py index 6dad978..7df6ad0 100644 --- a/models/translate.py +++ b/models/translate.py @@ -23,7 +23,7 @@ NO_TRANSLATE_TEXTS = { } _main_event_loop = asyncio.get_event_loop() -_http_session = None +_http_session: Optional[aiohttp.ClientSession] = None _translate_providers: List['TranslateProvider'] = [] # text -> res _translate_cache: Dict[str, str] = {}