diff --git a/journal_brief/constants.py b/journal_brief/constants.py index 1b4442f..cf5d279 100644 --- a/journal_brief/constants.py +++ b/journal_brief/constants.py @@ -28,4 +28,4 @@ value = getattr(journal, attr) svalue = str(value) for key in [value, svalue, attr[4:].lower()]: - PRIORITY_MAP[key] = svalue + PRIORITY_MAP[key] = value diff --git a/journal_brief/filter.py b/journal_brief/filter.py index 1ff9d22..da073ad 100644 --- a/journal_brief/filter.py +++ b/journal_brief/filter.py @@ -56,22 +56,23 @@ class FilterRule(dict): def __init__(self, mapping): assert isinstance(mapping, dict) - # Make sure everything is interpreted as a string - str_mapping = {} + # Make sure everything is interpreted as the appropriate type + type_mapping = {} for field, matches in mapping.items(): + converter = DEFAULT_CONVERTERS.get(field, str) if field == 'PRIORITY': try: level = int(PRIORITY_MAP[matches]) except (AttributeError, TypeError): - str_mapping[field] = [PRIORITY_MAP[match] - for match in matches] + type_mapping[field] = [converter(PRIORITY_MAP[match]) + for match in matches] else: - str_mapping[field] = list(range(level + 1)) + type_mapping[field] = [converter(prio) + for prio in list(range(level + 1))] else: - converter = DEFAULT_CONVERTERS.get(field, str) - str_mapping[field] = [converter(match) for match in matches] + type_mapping[field] = [converter(match) for match in matches] - super(FilterRule, self).__init__(str_mapping) + super(FilterRule, self).__init__(type_mapping) def __str__(self): rdict = {} diff --git a/tests/test_filter.py b/tests/test_filter.py index 2bc13ac..6622a56 100644 --- a/tests/test_filter.py +++ b/tests/test_filter.py @@ -109,7 +109,8 @@ def test_and_or(self): def test_priority(self): inclusion = Inclusion({'PRIORITY': 'err'}) - assert inclusion.matches({'PRIORITY': 3}) + priority_type = journal.DEFAULT_CONVERTERS.get('PRIORITY', str) + assert inclusion.matches({'PRIORITY': priority_type(3)}) def test_repr(self): incl = {'MESSAGE': ['include this']} @@ -154,7 +155,8 @@ def test_regexp(self): def test_priority(self): exclusion = Exclusion({'PRIORITY': 'err'}) - assert exclusion.matches({'PRIORITY': 3}) + priority_type = journal.DEFAULT_CONVERTERS.get('PRIORITY', str) + assert exclusion.matches({'PRIORITY': priority_type(3)}) def test_str_without_comment(self): excl = {'MESSAGE': ['exclude this']} @@ -216,10 +218,11 @@ def test_no_exclusions(self): assert lines == [entry['MESSAGE'] for entry in entries] def test_exclusion(self): + priority_type = journal.DEFAULT_CONVERTERS.get('PRIORITY', str) entries = [{'MESSAGE': 'exclude this', 'SYSLOG_IDENTIFIER': 'from here'}, - {'PRIORITY': '6', + {'PRIORITY': priority_type(6), 'MESSAGE': 'exclude this too'}, {'MESSAGE': 'message 1',