Skip to content

Commit b7352b5

Browse files
committed
Fixes for priority handling
Filters for PRIORITY need to be in the type that systemd.journal expects. Fixes #39.
1 parent 21e4a2c commit b7352b5

File tree

3 files changed

+16
-12
lines changed

3 files changed

+16
-12
lines changed

journal_brief/constants.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,4 +28,4 @@
2828
value = getattr(journal, attr)
2929
svalue = str(value)
3030
for key in [value, svalue, attr[4:].lower()]:
31-
PRIORITY_MAP[key] = svalue
31+
PRIORITY_MAP[key] = value

journal_brief/filter.py

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -56,22 +56,23 @@ class FilterRule(dict):
5656
def __init__(self, mapping):
5757
assert isinstance(mapping, dict)
5858

59-
# Make sure everything is interpreted as a string
60-
str_mapping = {}
59+
# Make sure everything is interpreted as the appropriate type
60+
type_mapping = {}
6161
for field, matches in mapping.items():
62+
converter = DEFAULT_CONVERTERS.get(field, str)
6263
if field == 'PRIORITY':
6364
try:
6465
level = int(PRIORITY_MAP[matches])
6566
except (AttributeError, TypeError):
66-
str_mapping[field] = [PRIORITY_MAP[match]
67-
for match in matches]
67+
type_mapping[field] = [converter(PRIORITY_MAP[match])
68+
for match in matches]
6869
else:
69-
str_mapping[field] = list(range(level + 1))
70+
type_mapping[field] = [converter(prio)
71+
for prio in list(range(level + 1))]
7072
else:
71-
converter = DEFAULT_CONVERTERS.get(field, str)
72-
str_mapping[field] = [converter(match) for match in matches]
73+
type_mapping[field] = [converter(match) for match in matches]
7374

74-
super(FilterRule, self).__init__(str_mapping)
75+
super(FilterRule, self).__init__(type_mapping)
7576

7677
def __str__(self):
7778
rdict = {}

tests/test_filter.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,8 @@ def test_and_or(self):
109109

110110
def test_priority(self):
111111
inclusion = Inclusion({'PRIORITY': 'err'})
112-
assert inclusion.matches({'PRIORITY': 3})
112+
priority_type = journal.DEFAULT_CONVERTERS.get('PRIORITY', str)
113+
assert inclusion.matches({'PRIORITY': priority_type(3)})
113114

114115
def test_repr(self):
115116
incl = {'MESSAGE': ['include this']}
@@ -154,7 +155,8 @@ def test_regexp(self):
154155

155156
def test_priority(self):
156157
exclusion = Exclusion({'PRIORITY': 'err'})
157-
assert exclusion.matches({'PRIORITY': 3})
158+
priority_type = journal.DEFAULT_CONVERTERS.get('PRIORITY', str)
159+
assert exclusion.matches({'PRIORITY': priority_type(3)})
158160

159161
def test_str_without_comment(self):
160162
excl = {'MESSAGE': ['exclude this']}
@@ -216,10 +218,11 @@ def test_no_exclusions(self):
216218
assert lines == [entry['MESSAGE'] for entry in entries]
217219

218220
def test_exclusion(self):
221+
priority_type = journal.DEFAULT_CONVERTERS.get('PRIORITY', str)
219222
entries = [{'MESSAGE': 'exclude this',
220223
'SYSLOG_IDENTIFIER': 'from here'},
221224

222-
{'PRIORITY': '6',
225+
{'PRIORITY': priority_type(6),
223226
'MESSAGE': 'exclude this too'},
224227

225228
{'MESSAGE': 'message 1',

0 commit comments

Comments
 (0)