From 5bf6cbc0e61d3ac90cf1139cf4dc34b5f7079ee2 Mon Sep 17 00:00:00 2001 From: Jochen Kupperschmidt Date: Sat, 3 Apr 2021 19:49:14 +0200 Subject: [PATCH] Turn `PriorityValue` class factory method into function --- src/syslogmp/parser.py | 53 +++++++++++++++++++++--------------------- 1 file changed, 26 insertions(+), 27 deletions(-) diff --git a/src/syslogmp/parser.py b/src/syslogmp/parser.py index 2768f99..3db8338 100644 --- a/src/syslogmp/parser.py +++ b/src/syslogmp/parser.py @@ -68,8 +68,7 @@ class Parser: def _parse_pri_part(self): """Extract facility and severity from the PRI part.""" pri_part = self.stream.read_until_inclusive(b'>') - - return PriorityValue.from_pri_part(pri_part) + return _create_priority_value_from_pri_part(pri_part) def _parse_header_part(self): """Extract timestamp and hostname from the HEADER part.""" @@ -115,38 +114,38 @@ class Parser: 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.' - ) + pass - 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 (`>`).', - ) +def _create_priority_value_from_pri_part(pri_part): + """Create priority value from PRI part.""" + ensure(len(pri_part) in {3, 4, 5}, 'PRI part must have 3, 4, or 5 bytes.') - priority_value = pri_part[1:-1] + ensure( + pri_part.startswith(b'<'), + 'PRI part must start with an opening angle bracket (`<`).', + ) - try: - priority_value_number = int(priority_value) - except ValueError: - raise MessageFormatError( - "Priority value must be a number, but is '{priority_value}'." - ) + ensure( + pri_part.endswith(b'>'), + 'PRI part must end with a closing angle bracket (`>`).', + ) + + priority_value = pri_part[1:-1] + + try: + priority_value_number = int(priority_value) + except ValueError: + raise MessageFormatError( + "Priority value must be a number, but is '{priority_value}'." + ) - facility_id, severity_id = divmod(priority_value_number, 8) + facility_id, severity_id = divmod(priority_value_number, 8) - facility = Facility(facility_id) - severity = Severity(severity_id) + facility = Facility(facility_id) + severity = Severity(severity_id) - return cls(facility, severity) + return PriorityValue(facility=facility, severity=severity) def ensure(expression, error_message): -- 2.45.2