From 1a314ffd6bec48692375b3001953e9def4291f0c Mon Sep 17 00:00:00 2001 From: Fabio Manganiello Date: Fri, 17 Sep 2021 22:21:06 +0200 Subject: [PATCH] Fixed LGTM errors and warnings --- platypush/backend/http/__init__.py | 6 +++--- platypush/backend/websocket/__init__.py | 19 +++++++------------ platypush/message/event/chat/slack.py | 2 +- platypush/plugins/__init__.py | 2 +- platypush/plugins/websocket/__init__.py | 6 +++--- platypush/user/__init__.py | 8 ++++---- 6 files changed, 19 insertions(+), 24 deletions(-) diff --git a/platypush/backend/http/__init__.py b/platypush/backend/http/__init__.py index 27336cfbe..a6a365ca9 100644 --- a/platypush/backend/http/__init__.py +++ b/platypush/backend/http/__init__.py @@ -6,8 +6,9 @@ from multiprocessing import Process try: from websockets.exceptions import ConnectionClosed + from websockets import serve as websocket_serve except ImportError: - from websockets import ConnectionClosed + from websockets import ConnectionClosed, serve as websocket_serve from platypush.backend import Backend from platypush.backend.http.app import application @@ -354,7 +355,6 @@ class HttpBackend(Backend): def websocket(self): """ Websocket main server """ - import websockets set_thread_name('WebsocketServer') async def register_websocket(websocket, path): @@ -378,7 +378,7 @@ class HttpBackend(Backend): self._websocket_loop = get_or_create_event_loop() self._websocket_loop.run_until_complete( - websockets.serve(register_websocket, self.bind_address, self.websocket_port, + websocket_serve(register_websocket, self.bind_address, self.websocket_port, **websocket_args)) self._websocket_loop.run_forever() diff --git a/platypush/backend/websocket/__init__.py b/platypush/backend/websocket/__init__.py index fc5e2fac3..1fd8a6b43 100644 --- a/platypush/backend/websocket/__init__.py +++ b/platypush/backend/websocket/__init__.py @@ -1,4 +1,10 @@ from platypush.backend import Backend +try: + from websockets.exceptions import ConnectionClosed + from websockets import serve as websocket_serve +except ImportError: + from websockets import ConnectionClosed, serve as websocket_serve + from platypush.context import get_plugin, get_or_create_event_loop from platypush.message import Message from platypush.message.request import Request @@ -73,10 +79,6 @@ class WebsocketBackend(Backend): def notify_web_clients(self, event): """ Notify all the connected web clients (over websocket) of a new event """ - try: - from websockets.exceptions import ConnectionClosed - except ImportError: - from websockets import ConnectionClosed async def send_event(websocket): try: @@ -96,12 +98,6 @@ class WebsocketBackend(Backend): def run(self): import asyncio - import websockets - - try: - from websockets.exceptions import ConnectionClosed - except ImportError: - from websockets import ConnectionClosed super().run() self.register_service(port=self.port, name='ws') @@ -152,8 +148,7 @@ class WebsocketBackend(Backend): websocket_args['ssl'] = self.ssl_context self._loop = get_or_create_event_loop() - server = websockets.serve(serve_client, self.bind_address, self.port, - **websocket_args) + server = websocket_serve(serve_client, self.bind_address, self.port, **websocket_args) self._loop.run_until_complete(server) self._loop.run_forever() diff --git a/platypush/message/event/chat/slack.py b/platypush/message/event/chat/slack.py index fa103277f..42847ce79 100644 --- a/platypush/message/event/chat/slack.py +++ b/platypush/message/event/chat/slack.py @@ -26,7 +26,7 @@ class SlackEvent(Event, ABCMeta): return datetime.fromtimestamp(timestamp, tz=gettz()) # lgtm [py/call-to-non-callable] -class SlackMessageEvent(SlackEvent, ABCMeta): +class SlackMessageEvent(SlackEvent, ABCMeta): # lgtm [py/conflicting-attributes] """ Base class for message-related events. """ diff --git a/platypush/plugins/__init__.py b/platypush/plugins/__init__.py index d5853cb73..6a55f4b95 100644 --- a/platypush/plugins/__init__.py +++ b/platypush/plugins/__init__.py @@ -39,7 +39,7 @@ def action(f): return _execute_action -class Plugin(EventGenerator, ExtensionWithManifest): +class Plugin(EventGenerator, ExtensionWithManifest): # lgtm [py/missing-call-to-init] """ Base plugin class """ def __init__(self, **kwargs): diff --git a/platypush/plugins/websocket/__init__.py b/platypush/plugins/websocket/__init__.py index 0941e376c..1cfa7c0e5 100644 --- a/platypush/plugins/websocket/__init__.py +++ b/platypush/plugins/websocket/__init__.py @@ -1,10 +1,10 @@ import json -import websockets try: from websockets.exceptions import ConnectionClosed + from websockets import connect as websocket_connect except ImportError: - from websockets import ConnectionClosed + from websockets import ConnectionClosed, connect as websocket_connect from platypush.context import get_or_create_event_loop from platypush.message import Message @@ -52,7 +52,7 @@ class WebsocketPlugin(Plugin): ssl_cafile=ssl_cafile, ssl_capath=ssl_capath) - async with websockets.connect(url, **websocket_args) as websocket: + async with websocket_connect(url, **websocket_args) as websocket: try: await websocket.send(str(msg)) except ConnectionClosed as err: diff --git a/platypush/user/__init__.py b/platypush/user/__init__.py index 981a4e464..ccc2040aa 100644 --- a/platypush/user/__init__.py +++ b/platypush/user/__init__.py @@ -5,12 +5,12 @@ import time from typing import Optional, Dict import bcrypt -import jwt try: from jwt.exceptions import PyJWTError + from jwt import encode as jwt_encode, decode as jwt_decode except ImportError: - from jwt import PyJWTError + from jwt import PyJWTError, encode as jwt_encode, decode as jwt_decode from sqlalchemy import Column, Integer, String, DateTime, ForeignKey from sqlalchemy.orm import sessionmaker, scoped_session @@ -204,7 +204,7 @@ class UserManager: 'expires_at': expires_at.timestamp() if expires_at else None, } - token = jwt.encode(payload, priv_key, algorithm='RS256') + token = jwt_encode(payload, priv_key, algorithm='RS256') if isinstance(token, bytes): token = token.decode() return token @@ -230,7 +230,7 @@ class UserManager: pub_key, priv_key = get_or_generate_jwt_rsa_key_pair() try: - payload = jwt.decode(token.encode(), pub_key, algorithms=['RS256']) + payload = jwt_decode(token.encode(), pub_key, algorithms=['RS256']) except PyJWTError as e: raise InvalidJWTTokenException(str(e))