From 5161044c0d20052b2d547c3ca1d3167a38690241 Mon Sep 17 00:00:00 2001 From: Florian Bezannier Date: Thu, 11 Nov 2021 23:54:44 +0100 Subject: [PATCH] clean code --- my_psacc.py | 17 ++++++----------- server.py | 2 +- trip.py | 8 +++----- web/abrp.py | 2 +- web/app.py | 3 +-- web/db.py | 7 +++---- web/figures.py | 3 +-- web/tools/figurefilter.py | 3 --- web/view/config_views.py | 2 +- 9 files changed, 17 insertions(+), 30 deletions(-) diff --git a/my_psacc.py b/my_psacc.py index ef03c12..28aaee0 100644 --- a/my_psacc.py +++ b/my_psacc.py @@ -46,7 +46,6 @@ def gen_correlation_id(date): return correlation_id -# pylint: disable=too-many-instance-attributes,too-many-public-methods class MyPSACC: def connect(self, user, password): self.manager.init_with_user_credentials_realm(user, password, self.realm) @@ -101,13 +100,10 @@ class MyPSACC: @rate_limit(6, 1800) def refresh_token(self): - try: - # pylint: disable=protected-access - self.manager._refresh_token() - self.save_config() - return True - except RequestException as e: - logger.error("Can't refresh token %s", e) + # pylint: disable=protected-access + self.manager._refresh_token() + self.save_config() + return True def api(self) -> psac.VehiclesApi: self.api_config.access_token = self.manager.access_token @@ -443,7 +439,7 @@ class MyPSACC: @staticmethod def load_config(name="config.json"): - with open(name, "r") as f: + with open(name, "r", encoding="utf-8") as f: config_str = f.read() config = dict(**json.loads(config_str)) if "country_code" not in config: @@ -493,10 +489,9 @@ class MyPSACC: yield key, value -# pylint: disable=arguments-differ class MyPeugeotEncoder(JSONEncoder): - def default(self, mp: MyPSACC): + def default(self, mp: MyPSACC): # pylint: disable=arguments-renamed data = dict(mp) mpd = {"proxies": data["_proxies"], "refresh_token": mp.manager.refresh_token, "client_secret": mp.service_information.client_secret, "abrp": dict(mp.abrp)} diff --git a/server.py b/server.py index d7b9602..905e20c 100755 --- a/server.py +++ b/server.py @@ -1,5 +1,4 @@ #!/usr/bin/env python3 -# pylint: disable=wrong-import-position import os import sys from threading import Thread @@ -12,6 +11,7 @@ if sys.version_info < (3, 6): TestRequirements(DIR + "/requirements.txt").test_requirements() +# pylint: disable=wrong-import-position import web.app from libs.config import Config from mylogger import logger diff --git a/trip.py b/trip.py index d034bd6..361904f 100644 --- a/trip.py +++ b/trip.py @@ -11,7 +11,6 @@ from web.db import Database class Points: - # pylint: disable= too-few-public-methods def __init__(self, latitude, longitude): self.latitude = latitude self.longitude = longitude @@ -21,7 +20,6 @@ class Points: class Trip: - # pylint: disable= too-many-instance-attributes def __init__(self): self.start_at = None self.end_at = None @@ -58,7 +56,7 @@ class Trip: try: self.consumption_km = 100 * self.consumption / self.distance # kw/100 km except TypeError: - raise ValueError("Distance not set") + raise ValueError("Distance not set") from TypeError return self.consumption_km def set_fuel_consumption(self, consumption) -> float: @@ -135,8 +133,8 @@ class Trips(list): logger.debugv("trip discarded") return False - @staticmethod # noqa: MC0001 - def get_trips(vehicles_list: Cars) -> Dict[str, "Trips"]: + @staticmethod + def get_trips(vehicles_list: Cars) -> Dict[str, "Trips"]: # noqa: MC0001 # pylint: disable=too-many-locals,too-many-statements,too-many-nested-blocks,too-many-branches conn = Database.get_db() vehicles = conn.execute("SELECT DISTINCT vin FROM position;").fetchall() diff --git a/web/abrp.py b/web/abrp.py index 861b214..ed721d4 100644 --- a/web/abrp.py +++ b/web/abrp.py @@ -14,7 +14,7 @@ class Abrp: def __init__(self, token: str = "", abrp_enable_vin=None): if abrp_enable_vin is None: - abrp_enable_vin = list() + abrp_enable_vin = [] self.token = token self.abrp_enable_vin = set(abrp_enable_vin) self.proxies = None diff --git a/web/app.py b/web/app.py index 1c9ef6d..0026b90 100644 --- a/web/app.py +++ b/web/app.py @@ -18,7 +18,6 @@ import importlib # pylint: disable=invalid-name app = None dash_app = None -dispatcher = None class MyProxyFix(ProxyFix): @@ -47,7 +46,7 @@ def start_app(*args, **kwargs): def config_flask(title, base_path, debug: bool, host, port, reloader=False, # pylint: disable=too-many-arguments unminified=False, view="web.view.views"): - global app, dash_app, dispatcher + global app, dash_app reload_view = app is not None app = Flask(__name__) try: diff --git a/web/db.py b/web/db.py index 7497068..116ff6a 100644 --- a/web/db.py +++ b/web/db.py @@ -129,10 +129,9 @@ class Database: db_file = Database.DEFAULT_DB_FILE conn = CustomSqliteConnection(db_file, detect_types=sqlite3.PARSE_DECLTYPES | sqlite3.PARSE_COLNAMES) conn.row_factory = sqlite3.Row - Database.__thread_lock.acquire() - if not Database.db_initialized: - Database.init_db(conn) - Database.__thread_lock.release() + with Database.__thread_lock: + if not Database.db_initialized: + Database.init_db(conn) if update_callback: conn.callbacks.append(Database.callback_fct) return conn diff --git a/web/figures.py b/web/figures.py index ccf2f41..331e382 100644 --- a/web/figures.py +++ b/web/figures.py @@ -51,8 +51,7 @@ SUMMARY_CARDS = {"Average consumption": {"text": [card_value_div(AVG_CONSUM_KW, # pylint: disable=too-many-locals def get_figures(car: Car): - global consumption_fig, consumption_df, trips_map, consumption_fig_by_speed, table_fig, info, \ - battery_table, consumption_fig_by_temp + global consumption_fig, trips_map, consumption_fig_by_speed, table_fig, battery_table, consumption_fig_by_temp lats = [42, 41] lons = [1, 2] names = ["undefined", "undefined"] diff --git a/web/tools/figurefilter.py b/web/tools/figurefilter.py index 7031dd3..a8a699d 100644 --- a/web/tools/figurefilter.py +++ b/web/tools/figurefilter.py @@ -64,9 +64,6 @@ class FigureFilter: res = {table.src: table.date_columns for table in self.tables} return res - def __get_table_src(self): - return [table.src for table in self.tables] - def __get_figures(self): return {"graph": [graph.figure for graph in self.graphs], "tables": [table.figure for table in self.tables], diff --git a/web/view/config_views.py b/web/view/config_views.py index 7d6872c..641be74 100644 --- a/web/view/config_views.py +++ b/web/view/config_views.py @@ -94,7 +94,7 @@ config_otp_layout = dbc.Row(dbc.Col(className="col-md-12 col-lg-2 m-3", children def log_layout(): - with open(LOG_FILE, "r") as f: + with open(LOG_FILE, "r", encoding="utf-8") as f: log_text = f.read() return html.H3(className="m-2", children=["Log:", dbc.Container( fluid=True,