Skip to content
This repository has been archived by the owner on Nov 29, 2022. It is now read-only.

Commit

Permalink
Add default rule for numbers; cleanup some rules
Browse files Browse the repository at this point in the history
  • Loading branch information
hSaria committed Oct 15, 2021
1 parent e8db354 commit 8607caf
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 8 deletions.
1 change: 1 addition & 0 deletions chromaterm/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,7 @@ def parse_rule(rule, rgb=False):
rule (dict): A dictionary representing the rule, formatted according to
`chromaterm.__main__.load_rules`.
'''
# pylint: disable=too-many-return-statements
if not isinstance(rule, dict):
return f'Rule {repr(rule)} not a dictionary'

Expand Down
16 changes: 11 additions & 5 deletions chromaterm/default_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,12 @@

# pylint: disable=line-too-long

RULE_NUMBERS = Rule(
r'\b(?<!\.)\d+(\.\d+)?(?!\.)\b',
Color('f#91490a'),
'Numbers',
)

RULE_IPV4 = Rule(
r'\b(?<!\.)((25[0-5]|(2[0-4]|[0-1]?\d)?\d)\.){3}(25[0-5]|(2[0-4]|[0-1]?\d)?\d)(/\d+)?(?!\.)\b',
Color('f#00ffff'),
Expand All @@ -13,7 +19,7 @@
)

RULE_IPV6 = Rule(
r'(?i)((?<=[\W])|^)(?<!:)(([\da-f]{1,4}:){7}[\da-f]{1,4}|([\da-f]{1,4}:){1,1}(:[\da-f]{1,4}){1,6}|([\da-f]{1,4}:){1,2}(:[\da-f]{1,4}){1,5}|([\da-f]{1,4}:){1,3}(:[\da-f]{1,4}){1,4}|([\da-f]{1,4}:){1,4}(:[\da-f]{1,4}){1,3}|([\da-f]{1,4}:){1,5}(:[\da-f]{1,4}){1,2}|([\da-f]{1,4}:){1,6}(:[\da-f]{1,4})|([\da-f]{1,4}:){1,7}:|:((:[\da-f]{1,4}){1,7}|:)|::(ffff(:0{1,4})?:)?((25[0-5]|(2[0-4]|[0-1]?\d)?\d)\.){3}(25[0-5]|(2[0-4]|[0-1]?\d)?\d)|([\da-f]{1,4}:){1,4}:((25[0-5]|(2[0-4]|[0-1]?\d)?\d)\.){3}(25[0-5]|(2[0-4]|[0-1]?\d)?\d))(%[\da-z]+)?(/\d+)?(?!:)((?=[\W])|$)',
r'(?i)(?<![\w:])(([\da-f]{1,4}:){7}[\da-f]{1,4}|[\da-f]{1,4}:(:[\da-f]{1,4}){1,6}|([\da-f]{1,4}:){1,2}(:[\da-f]{1,4}){1,5}|([\da-f]{1,4}:){1,3}(:[\da-f]{1,4}){1,4}|([\da-f]{1,4}:){1,4}(:[\da-f]{1,4}){1,3}|([\da-f]{1,4}:){1,5}(:[\da-f]{1,4}){1,2}|([\da-f]{1,4}:){1,6}:[\da-f]{1,4}|([\da-f]{1,4}:){1,7}:|:((:[\da-f]{1,4}){1,7}|:)|::(ffff(:0{1,4})?:)?((25[0-5]|(2[0-4]|[0-1]?\d)?\d)\.){3}(25[0-5]|(2[0-4]|[0-1]?\d)?\d)|([\da-f]{1,4}:){1,4}:((25[0-5]|(2[0-4]|[0-1]?\d)?\d)\.){3}(25[0-5]|(2[0-4]|[0-1]?\d)?\d))(%[\da-z]+)?(/\d+)?(?![\w:])',
Color('f#ff00ff'),
'IPv6 (boundaries don\'t work here as they can be in the start or end of the match, so using lookaheads and lookbehinds instead)',
exclusive=True,
Expand All @@ -27,16 +33,16 @@
)

RULE_DATE = Rule(
r'(?i)((?<=[\W])|^)((\d{2}|\d{4})\-((0)?[1-9]|1[0-2])\-(3[0-1]|([1-2]\d)|(0)?[1-9])|(jan|feb|mar|apr|may|jun|jul|aug|sep|oct|nov|dec)\s(\d{4}|\s\d|(3[0-1]|([0-2]\d)))|((\d|(3[0-1]|([0-2]\d)))\s(jan|feb|mar|apr|may|jun|jul|aug|sep|oct|nov|dec)(\s\d{4})?))((?=[\WT_])|$)',
r'(?i)((?<=\W)|^)((\d{2}|\d{4})\-(0?[1-9]|1[0-2])\-(3[0-1]|([1-2]\d)|0?[1-9])|(jan|feb|mar|apr|may|jun|jul|aug|sep|oct|nov|dec)\s+(\d{4}|(3[0-1]|([1-2]\d)|0?[1-9]))|((3[0-1]|([1-2]\d)|0?[1-9])\s(jan|feb|mar|apr|may|jun|jul|aug|sep|oct|nov|dec)(\s+\d{4})?))((?=[\WT_])|$)',
Color('b#af5f00'),
'Date in YYYY-MM-DD, YY-MM-DD, MMM (YYYY|DD), or DD MMM (YYYY)? formats',
exclusive=True,
)

RULE_TIME = Rule(
r'\b((?<!:)((2[0-3])|[0-1]\d):[0-5]\d(:[0-5]\d)?((\.|,)\d{3,6})?(?!:))\b',
r'(\b|(?<=T))(?<![\.:])((2[0-3])|[0-1]\d):[0-5]\d(:[0-5]\d([\.,]\d{3,6})?)?([\-\+](\d{2}|\d{4}))?(?![\.:])\b',
Color('b#af5f00'),
'Time in hh:mm:ss.SSSSSS format (sec, msec, and nsec optional)',
'Time in hh:mm:ss.SSSSSS-ZZZZ format (sec, msec, nsec, and timezone offset optional)',
exclusive=True,
)

Expand Down Expand Up @@ -76,7 +82,7 @@ def generate_default_rules_yaml():
data = 'rules:'

for rule in [
RULE_IPV4, RULE_IPV6, RULE_MAC, RULE_DATE, RULE_TIME,
RULE_NUMBERS, RULE_IPV4, RULE_IPV6, RULE_MAC, RULE_DATE, RULE_TIME,
RULE_GENERIC_BAD, RULE_GENERIC_AMBIGIOUS_BAD,
RULE_GENERIC_NOT_TOO_BAD, RULE_GENERIC_AMBIGIOUS_GOOD,
RULE_GENERIC_GOOD
Expand Down
15 changes: 12 additions & 3 deletions tests/test_default_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,15 @@ def permutate_data(data):
assert len(rule.get_matches(data)) == 0


def test_rule_numbers():
'''Default rule: Numbers.'''
positives = ['1', '123', '123.123']
negatives = ['1.1.', '.1.1', '1.2.3']
rule = chromaterm.default_config.RULE_NUMBERS

assert_matches(positives, negatives, rule)


def test_rule_ipv4():
'''Default rule: IPv4 addresses.'''
positives = ['192.168.2.1', '255.255.255.255', '=240.3.2.1', '1.2.3.4/32']
Expand Down Expand Up @@ -71,7 +80,7 @@ def test_rule_date():
'''Default rule: Date.'''
positives = [
'2019-12-31', '2019-12-31', 'jan 2019', 'feb 2019', 'Mar 2019',
'apr 2019', 'MAY 2019', 'Jun 2019', 'jul 2019', 'AUG 19', 'sep 20',
'apr 2019', 'MAY 2019', 'Jun 2019', 'jul 2019', 'AUG 19', 'sep 20',
'oct 21', 'nov 22', 'dec 23', '24 jan', '25 feb 2019'
]
negatives = [
Expand All @@ -85,8 +94,8 @@ def test_rule_date():

def test_rule_time():
'''Default rule: Time.'''
positives = ['23:59', '23:01', '23:01:01', '23:01:01.123']
negatives = ['24:59', '23:60', '23:1', '23:01:1', '23:01:01:']
positives = ['23:59', '23:01', '23:01:01', '23:01:01.123', '23:01:01-0800']
negatives = ['24:59', '23:60', '23:1', '23:01:1', '23:01:01:', '23:01.123']
rule = chromaterm.default_config.RULE_TIME

assert_matches(positives, negatives, rule)
Expand Down

0 comments on commit 8607caf

Please sign in to comment.