Skip to content

Commit

Permalink
Fixes for priority handling
Browse files Browse the repository at this point in the history
Filters for PRIORITY need to be in the type that systemd.journal
expects.

Fixes #39.
  • Loading branch information
twaugh committed Jul 24, 2016
1 parent 21e4a2c commit b7352b5
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 12 deletions.
2 changes: 1 addition & 1 deletion journal_brief/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
17 changes: 9 additions & 8 deletions journal_brief/filter.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {}
Expand Down
9 changes: 6 additions & 3 deletions tests/test_filter.py
Original file line number Diff line number Diff line change
Expand Up @@ -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']}
Expand Down Expand Up @@ -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']}
Expand Down Expand Up @@ -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',
Expand Down

0 comments on commit b7352b5

Please sign in to comment.