From 2946a5a17e6bcd2f3808f4761cab2dd2aa6534c6 Mon Sep 17 00:00:00 2001 From: Didi Hoffmann Date: Thu, 21 Sep 2023 22:20:19 +0200 Subject: [PATCH] Adding pylint support --- .pylintrc | 38 ++++++++++++++++++++++++++++++++++++++ power_logger.py | 27 +++++++++++++-------------- 2 files changed, 51 insertions(+), 14 deletions(-) create mode 100644 .pylintrc diff --git a/.pylintrc b/.pylintrc new file mode 100644 index 0000000..97d1166 --- /dev/null +++ b/.pylintrc @@ -0,0 +1,38 @@ +[FORMAT] + +# Maximum number of characters on a single line. +max-line-length=120 + + +[MESSAGES CONTROL] + +disable=missing-function-docstring, + missing-module-docstring, + missing-class-docstring, + too-few-public-methods, + duplicate-code, + too-many-nested-blocks, + line-too-long, + too-many-boolean-expressions, + too-many-nested-blocks, + line-too-long, + protected-access, + too-many-lines, + multiple-statements, + pointless-string-statement, + too-many-locals, + too-many-public-methods, + too-many-branches, + too-many-statements, + too-many-arguments, + invalid-name, + wrong-import-position, + too-many-return-statements, + + +# import-error + +[MASTER] +ignore=env +ignore-patterns=^env.* +ignore-paths=^env.*$ \ No newline at end of file diff --git a/power_logger.py b/power_logger.py index 96190ff..bc94011 100755 --- a/power_logger.py +++ b/power_logger.py @@ -1,5 +1,6 @@ #!/usr/bin/env python3 +# pylint: disable=W0603,W0602 import json import subprocess import time @@ -12,18 +13,18 @@ import sys import uuid import os +import os.path import stat import urllib.request import configparser -import os.path import sqlite3 import http - -import libs.caribou as caribou - from datetime import timezone from pathlib import Path +from libs import caribou + + # Shared variable to signal the thread to stop stop_signal = False @@ -129,7 +130,7 @@ def process_lines(lines, debug): break if filename: - with open(filename, 'r') as file: + with open(filename, 'r', encoding='utf-8') as file: lines = file.readlines() process_lines(lines, debug) else: @@ -138,11 +139,11 @@ def process_lines(lines, debug): '-i', str(SETTINGS['powermetrics']), '-f', 'plist'] - process = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.DEVNULL, text=True) - process_lines(process.stdout, debug) + with subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.DEVNULL, text=True) as process: + process_lines(process.stdout, debug) - if stop_signal: - process.terminate() + if stop_signal: + process.terminate() # Make sure that all data has been uploaded when exiting upload_data_to_endpoint() @@ -229,9 +230,9 @@ def parse_powermetrics_output(output: str): data=plistlib.loads(data) data['timezone'] = time.tzname data['timestamp'] = int(data['timestamp'].replace(tzinfo=timezone.utc).timestamp() * 1e3) - except xml.parsers.expat.ExpatError: + except xml.parsers.expat.ExpatError as exc: print(data) - raise xml.parsers.expat.ExpatError + raise exc compressed_data = zlib.compress(str(json.dumps(data)).encode()) compressed_data_str = base64.b64encode(compressed_data).decode() @@ -268,7 +269,7 @@ def parse_powermetrics_output(output: str): cpu_energy_data['ane_energy'], cpu_energy_data['energy_impact'])) - for key in stats.keys(): + for key in stats: stats[key] += cpu_energy_data[key] @@ -354,5 +355,3 @@ def save_settings(): run_powermetrics(args.debug, args.file) c.close() - -