From e285be4041bd3abcd8192b6ce57b19eeecf8e629 Mon Sep 17 00:00:00 2001 From: Olli Jarva Date: Mon, 25 Jan 2021 10:07:49 +0200 Subject: [PATCH] Run yapf & pylint fixes --- .pylintrc | 5 +++ .style.yapf | 10 +++++ openvpn_status_parser/__init__.py | 48 ++++++++++----------- openvpn_status_parser/openvpn-status-parser | 2 +- setup.py | 11 +++-- tests/__init__.py | 14 +++--- 6 files changed, 52 insertions(+), 38 deletions(-) create mode 100644 .pylintrc create mode 100644 .style.yapf diff --git a/.pylintrc b/.pylintrc new file mode 100644 index 0000000..f9eecf7 --- /dev/null +++ b/.pylintrc @@ -0,0 +1,5 @@ +[format] +max-line-length=125 + +[messages control] +disable=line-too-long,missing-docstring,no-self-use,fixme,bad-indentation,bad-continuation,invalid-name,too-many-locals,duplicate-code,too-many-branches,wrong-import-order diff --git a/.style.yapf b/.style.yapf new file mode 100644 index 0000000..93d64ae --- /dev/null +++ b/.style.yapf @@ -0,0 +1,10 @@ +[style] +based_on_style = pep8 +allow_split_before_dict_value = false +blank_line_before_nested_class_or_def = false +coalesce_brackets = true +column_limit = 125 +dedent_closing_brackets = true +each_dict_entry_on_separate_line = true +join_multiple_lines = true +spaces_around_power_operator = true diff --git a/openvpn_status_parser/__init__.py b/openvpn_status_parser/__init__.py index 17e0030..c56d94b 100644 --- a/openvpn_status_parser/__init__.py +++ b/openvpn_status_parser/__init__.py @@ -29,10 +29,11 @@ import datetime import logging import sys + from . import exceptions -class OpenVPNStatusParser(object): +class OpenVPNStatusParser: """ Usage: @@ -40,7 +41,6 @@ class OpenVPNStatusParser(object): parser = OpenVPNStatusParser(filename) pprint.pprint(parser.connected_clients) """ - def __init__(self, filename): self.filename = filename self._connected_clients = None @@ -59,34 +59,33 @@ def __init__(self, filename): def _process_title(self, row): try: self._details["title"] = row[1] - except IndexError: + except IndexError as err: logging.error("TITLE row is invalid: %s", row) - raise exceptions.MalformedFileException("TITLE row is invalid") + raise exceptions.MalformedFileException("TITLE row is invalid") from err def _process_time(self, row): try: self._details["timestamp"] = datetime.datetime.fromtimestamp(int(row[2])) - except (IndexError, ValueError): + except (IndexError, ValueError) as err: logging.error("TIME row is invalid: %s", row) - raise exceptions.MalformedFileException("TIME row is invalid") + raise exceptions.MalformedFileException("TIME row is invalid") from err def _process_header(self, row): try: self.topics_for[row[1]] = row[2:] - except IndexError: + except IndexError as err: logging.error("HEADER row is invalid: %s", row) - raise exceptions.MalformedFileException("HEADER row is invalid") + raise exceptions.MalformedFileException("HEADER row is invalid") from err def _process_client_list(self, row): try: self._connected_clients[row[1]] = dict(zip(self.topics_for["CLIENT_LIST"], row[1:])) - self._connected_clients[row[1]]["connected_since"] = ( - datetime.datetime.fromtimestamp(int(row[-1]))) - except IndexError: + self._connected_clients[row[1]]["connected_since"] = (datetime.datetime.fromtimestamp(int(row[-1]))) + except IndexError as err: logging.error("CLIENT_LIST row is invalid: %s", row) - raise exceptions.MalformedFileException("CLIENT_LIST row is invalid") - except KeyError: - raise exceptions.MalformedFileException("Topics for CLIENT_LIST are missing") + raise exceptions.MalformedFileException("CLIENT_LIST row is invalid") from err + except KeyError as err: + raise exceptions.MalformedFileException("Topics for CLIENT_LIST are missing") from err def _process_routing_table(self, row): if len(row[1:]) != len(self.topics_for.get("ROUTING_TABLE", [])): @@ -94,20 +93,20 @@ def _process_routing_table(self, row): try: self._routing_table[row[2]] = dict(zip(self.topics_for["ROUTING_TABLE"], row[1:])) self._routing_table[row[2]]["last_ref"] = datetime.datetime.fromtimestamp(int(row[-1])) - except IndexError: + except IndexError as err: logging.error("ROUTING_TABLE row is invalid: %s", row) - raise exceptions.MalformedFileException("ROUTING_TABLE row is invalid") - except ValueError: - raise exceptions.MalformedFileException("Invalid timestamp") - except KeyError: - raise exceptions.MalformedFileException("Topics for ROUTING_TABLE are missing") + raise exceptions.MalformedFileException("ROUTING_TABLE row is invalid") from err + except ValueError as err: + raise exceptions.MalformedFileException("Invalid timestamp") from err + except KeyError as err: + raise exceptions.MalformedFileException("Topics for ROUTING_TABLE are missing") from err def _process_global_stats(self, row): try: self._details[row[1]] = row[2] - except IndexError: + except IndexError as err: logging.error("GLOBAL_STATS row is invalid: %s", row) - raise exceptions.MalformedFileException("GLOBAL_STATS row is invalid") + raise exceptions.MalformedFileException("GLOBAL_STATS row is invalid") from err def _parse_file(self): self._details = {} @@ -122,9 +121,8 @@ def _parse_file(self): elif row_title == "END": return True else: - logging.warning("Line was not parsed. Keyword %s not recognized. %s", - row_title, row) - raise exceptions.MalformedFileException("Unhandled keyword %s", row_title) + logging.warning("Line was not parsed. Keyword %s not recognized. %s", row_title, row) + raise exceptions.MalformedFileException(f"Unhandled keyword {row_title}") logging.error("File was incomplete. END line was missing.") raise exceptions.MalformedFileException("END line was missing.") diff --git a/openvpn_status_parser/openvpn-status-parser b/openvpn_status_parser/openvpn-status-parser index 964c226..697ac0d 100644 --- a/openvpn_status_parser/openvpn-status-parser +++ b/openvpn_status_parser/openvpn-status-parser @@ -1,5 +1,4 @@ #!/usr/bin/env python - """OpenVPN status parser Usage: @@ -12,6 +11,7 @@ import pprint import sys import docopt + import openvpn_status_parser diff --git a/setup.py b/setup.py index 68611aa..7849cd9 100644 --- a/setup.py +++ b/setup.py @@ -1,7 +1,8 @@ -from setuptools import setup, find_packages from codecs import open from os import path +from setuptools import find_packages, setup + here = path.abspath(path.dirname(__file__)) with open(path.join(here, 'README.rst'), encoding='utf-8') as f: @@ -18,17 +19,16 @@ license='MIT', classifiers=[ 'Development Status :: 4 - Beta', - 'Intended Audience :: Developers', 'License :: OSI Approved :: MIT License', - - 'Programming Language :: Python :: 2.6', - 'Programming Language :: Python :: 2.7', 'Programming Language :: Python :: 3', 'Programming Language :: Python :: 3.2', 'Programming Language :: Python :: 3.3', 'Programming Language :: Python :: 3.4', 'Programming Language :: Python :: 3.5', + 'Programming Language :: Python :: 3.6', + 'Programming Language :: Python :: 3.7', + 'Programming Language :: Python :: 3.8', 'Programming Language :: Python :: Implementation :: PyPy', ], keywords='openvpn', @@ -36,7 +36,6 @@ install_requires=["docopt>=0.6.2"], test_suite="tests", scripts=["openvpn_status_parser/openvpn-status-parser"], - extras_require={ 'dev': ['twine', 'wheel'], }, diff --git a/tests/__init__.py b/tests/__init__.py index 25fe5e4..7e52626 100644 --- a/tests/__init__.py +++ b/tests/__init__.py @@ -1,6 +1,6 @@ +import datetime import glob import unittest -import datetime import openvpn_status_parser import openvpn_status_parser.exceptions @@ -11,17 +11,19 @@ def _test_file(self, filename): parsed = openvpn_status_parser.OpenVPNStatusParser(filename) def _call_details(): - parsed.details + parsed.details # pylint: disable=pointless-statement self.assertRaises(openvpn_status_parser.exceptions.MalformedFileException, _call_details) def ch(filename): - return lambda self: self._test_file(filename) + return lambda self: self._test_file(filename) # pylint: disable=protected-access + -for filename in glob.glob("tests/testfiles/broken/*.status"): - print("Adding %s" % filename) - setattr(TestBroken, "test_%s" % filename.replace(".", "_"), ch(filename)) +for fn in glob.glob("tests/testfiles/broken/*.status"): + print(f"Adding {fn}") + test_func_name = fn.replace(".", "_") + setattr(TestBroken, f"test_{test_func_name}", ch(fn)) if __name__ == '__main__': unittest.main()