From fd989e2a9c5c800d25c473d5b9d4a644cfd0d5f3 Mon Sep 17 00:00:00 2001 From: Jochen Kupperschmidt Date: Wed, 3 Mar 2021 19:50:30 +0100 Subject: [PATCH] Format code (more) according to Black --- pyproject.toml | 5 +++++ src/syslogmp/parser.py | 46 +++++++++++++++++++++++++++--------------- src/syslogmp/stream.py | 2 +- tests/test_parser.py | 15 +++++++------- 4 files changed, 44 insertions(+), 24 deletions(-) diff --git a/pyproject.toml b/pyproject.toml index 63e6503..754a11b 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -5,5 +5,10 @@ requires = [ ] build-backend = "setuptools.build_meta" +[tool.black] +line-length = 80 +skip-string-normalization = true +target-version = ['py36'] + [tool.pytest.ini_options] testpaths = ["tests"] diff --git a/src/syslogmp/parser.py b/src/syslogmp/parser.py index fce2ea4..9736395 100644 --- a/src/syslogmp/parser.py +++ b/src/syslogmp/parser.py @@ -47,14 +47,21 @@ class Parser(object): timestamp, hostname = parser._parse_header_part() message = parser._parse_msg_part() - return Message(priority_value.facility, priority_value.severity, - timestamp, hostname, message) + return Message( + priority_value.facility, + priority_value.severity, + timestamp, + hostname, + message, + ) def __init__(self, data): ensure(isinstance(data, bytes), 'Data must be a byte string.') - ensure(len(data) <= MAX_MESSAGE_LENGTH, - 'Message must not be longer than 1024 bytes.') + ensure( + len(data) <= MAX_MESSAGE_LENGTH, + 'Message must not be longer than 1024 bytes.', + ) self.stream = Stream(data) @@ -70,8 +77,9 @@ class Parser(object): # Advance to hostname. nothing = self.stream.read_until(b' ') - ensure(nothing == b'', - 'Timestamp must be followed by a space character.') + ensure( + nothing == b'', 'Timestamp must be followed by a space character.' + ) hostname = self._parse_hostname() @@ -90,8 +98,9 @@ class Parser(object): timestamp_ascii_with_year = f'{current_year:d} {timestamp_ascii}' try: - timestamp = datetime.strptime(timestamp_ascii_with_year, - '%Y %b %d %H:%M:%S') + timestamp = datetime.strptime( + timestamp_ascii_with_year, '%Y %b %d %H:%M:%S' + ) except ValueError as e: raise MessageFormatError(e) @@ -106,18 +115,22 @@ class Parser(object): class PriorityValue(namedtuple('PriorityValue', 'facility severity')): - @classmethod def from_pri_part(cls, pri_part): """Create instance from PRI part.""" - ensure(len(pri_part) in {3, 4, 5}, - 'PRI part must have 3, 4, or 5 bytes.') + ensure( + len(pri_part) in {3, 4, 5}, 'PRI part must have 3, 4, or 5 bytes.' + ) - ensure(pri_part.startswith(b'<'), - 'PRI part must start with an opening angle bracket (`<`).') + ensure( + pri_part.startswith(b'<'), + 'PRI part must start with an opening angle bracket (`<`).', + ) - ensure(pri_part.endswith(b'>'), - 'PRI part must end with a closing angle bracket (`>`).') + ensure( + pri_part.endswith(b'>'), + 'PRI part must end with a closing angle bracket (`>`).', + ) priority_value = pri_part[1:-1] @@ -125,7 +138,8 @@ class PriorityValue(namedtuple('PriorityValue', 'facility severity')): priority_value_number = int(priority_value) except ValueError: raise MessageFormatError( - "Priority value must be a number, but is '{priority_value}'.") + "Priority value must be a number, but is '{priority_value}'." + ) facility_id, severity_id = divmod(priority_value_number, 8) diff --git a/src/syslogmp/stream.py b/src/syslogmp/stream.py index d49fea1..9e77911 100644 --- a/src/syslogmp/stream.py +++ b/src/syslogmp/stream.py @@ -13,7 +13,6 @@ from itertools import islice, takewhile class Stream(object): - def __init__(self, data): self.iterator = iter(data) @@ -34,6 +33,7 @@ class Stream(object): """Return bytes until, and including, the first occurrence of the stop byte. """ + def inner(): predicate = create_match_predicate(stop_byte) for x in self.iterator: diff --git a/tests/test_parser.py b/tests/test_parser.py index 90e825c..41a88b5 100644 --- a/tests/test_parser.py +++ b/tests/test_parser.py @@ -79,13 +79,14 @@ def pad_data(value, width, fillbyte): ], ) def test_parse( - data, - expected_facility, - expected_facility_description, - expected_severity, - expected_timestamp, - expected_hostname, - expected_message): + data, + expected_facility, + expected_facility_description, + expected_severity, + expected_timestamp, + expected_hostname, + expected_message, +): """Test parsing of a syslog message.""" actual = parse(data) -- 2.45.2