def test_single_empty_groups(self): th = TrackerHelper() th.config.insert_regex( regex=r"(.*) / (.*)", regex_groups=["torrentName", "$g2"], ) var = announce_parser.parse(th, " / the_group") self.assertEqual(len(var), 1 + len(utils.get_default_variables())) self.assertEqual(var["$g2"], "the_group", "$g2 did not match") var = announce_parser.parse(th, "a_name / ") self.assertEqual(len(var), len(utils.get_default_variables())) self.assertEqual(var["torrentName"], "a_name", "$g2 did not match")
def test_single_non_capture_group(self): th = TrackerHelper() th.config.insert_regex( regex=r"(.*) /(?: (.*))?", regex_groups=["torrentName", "$g2"], ) var = announce_parser.parse(th, "name /") self.assertEqual(len(var), len(utils.get_default_variables())) self.assertEqual(var["torrentName"], "name", "Name did not match") var = announce_parser.parse(th, "name / group") self.assertEqual(len(var), 1 + len(utils.get_default_variables())) self.assertEqual(var["torrentName"], "name", "Name did not match") self.assertEqual(var["$g2"], "group", "g2 did not match")
def test_multi_line_pattern_parse_optional(self): th = TrackerHelper() th.config.insert_multi_regex( regex=r"Row1 name: (.*)", regex_groups=["torrentName"] ) th.config.insert_multi_regex( regex=r"Row2 g2: (.*)", regex_groups=["$g2"], optional=True ) th.config.insert_multi_regex( regex=r"Row3 g3: (.*)", regex_groups=["$g3"], ) th.config.insert_multi_regex( regex=r"Row4 g4: (.*)", regex_groups=["$g4"], optional=True ) var = announce_parser.parse(th, "Row1 name: another_name") self.assertEqual(var, None, "No match should return None") var = announce_parser.parse(th, "Row2 g2: g2_text") self.assertEqual(var, None, "No match should return None") var = announce_parser.parse(th, "Row3 g3: g3_text") self.assertEqual(len(var), 2 + len(utils.get_default_variables())) self.assertEqual(var["torrentName"], "another_name", "Name did not match") self.assertEqual(var["$g2"], "g2_text", "g2 did not match") self.assertEqual(var["$g3"], "g3_text", "g2 did not match")
def test_multi_line_pattern_simple(self): th = TrackerHelper() th.config.insert_multi_regex( regex=r"Row1 name: (.*)", regex_groups=["torrentName"] ) th.config.insert_multi_regex(regex=r"Row2 g2: (.*)", regex_groups=["$g2"]) th.config.insert_multi_regex(regex=r"Row3 g3: (.*)", regex_groups=["$g3"]) var = announce_parser.parse(th, "Row2 g2: g2_error") self.assertEqual(var, None, "Should return None if matched rows not in order") # torrentName has extra whitespace added var = announce_parser.parse(th, "Row1 name: the_name") self.assertEqual(var, None, "No match should return None") var = announce_parser.parse(th, "Row3 g3: g3_error") self.assertEqual(var, None, "Should return None if matched rows not in order") var = announce_parser.parse(th, "Row2 g2: g2_text") self.assertEqual(var, None, "Announcement is None") var = announce_parser.parse(th, "Row3 g3: g3_text") self.assertEqual(len(var), 2 + len(utils.get_default_variables())) self.assertEqual(var["torrentName"], "the_name", "Name did not match") self.assertEqual(var["$g2"], "g2_text", "g2 did not match") self.assertEqual(var["$g3"], "g3_text", "g3 did not match")
def test_single_default_variables(self): th = TrackerHelper() th.config.insert_regex( regex=r"Test g1: (.*) g2: (.*) g3: (.*)", regex_groups=["$g1", "$g2", "$g3"], ) var = announce_parser.parse(th, "Test g1: g1_text g2: g2_text& g3: g3_text&") self.assertEqual(len(var), 3 + len(utils.get_default_variables())) self.assertEqual(var["$g1"], "g1_text", "g1 did not match") self.assertEqual(var["$g2"], "g2_text&", "g2 did not match") self.assertEqual(var["$g3"], "g3_text&", "g3 did not match") for key in utils.get_default_variables(): self.assertEqual(var[key], "", "default value did not match")
def _parse_singleline_patterns(line_patterns, message): index, pattern_groups = _parse_message(line_patterns, message) if index == -1: return ParseStatus.NO_MATCH, {} else: variables = get_default_variables() variables.update(pattern_groups) return ParseStatus.MATCH, variables
def test_multi_default_variables(self): th = TrackerHelper() th.config.insert_multi_regex(regex=r"Row1 g1: (.*)", regex_groups=["$g1"]) th.config.insert_multi_regex(regex=r"Row2 g2: (.*)", regex_groups=["$g2"]) th.config.insert_multi_regex(regex=r"Row3 g3: (.*)", regex_groups=["$g3"]) var = announce_parser.parse(th, "Row1 g1: g1_text") self.assertEqual(var, None, "No match should return None") var = announce_parser.parse(th, "Row2 g2: g2_text") self.assertEqual(var, None, "Announcement is None") var = announce_parser.parse(th, "Row3 g3: g3_text") self.assertEqual(len(var), 3 + len(utils.get_default_variables())) self.assertEqual(var["$g1"], "g1_text", "g1 did not match") self.assertEqual(var["$g2"], "g2_text", "g2 did not match") self.assertEqual(var["$g3"], "g3_text", "g3 did not match") for key in utils.get_default_variables(): self.assertEqual(var[key], "", "default value did not match")
def test_multi_non_capture_group(self): th = TrackerHelper() th.config.insert_multi_regex( regex=r"Row1 name: (.*)", regex_groups=["torrentName"] ) th.config.insert_multi_regex(regex=r"Row2 g2:(?: (.*))?", regex_groups=["$g2"]) var = announce_parser.parse(th, "Row1 name: a_name") self.assertEqual(var, None, "First row in multi should return None") var = announce_parser.parse(th, "Row2 g2:") self.assertEqual(len(var), len(utils.get_default_variables())) self.assertEqual(var["torrentName"], "a_name", "Name did not match") var = announce_parser.parse(th, "Row1 name: a_name") self.assertEqual(var, None, "First row in multi should return None") var = announce_parser.parse(th, "Row2 g2: a_group") self.assertEqual(len(var), 1 + len(utils.get_default_variables())) self.assertEqual(var["torrentName"], "a_name", "Name did not match") self.assertEqual(var["$g2"], "a_group", "g2 did not match")
def test_multi_line_pattern_two_in_parallel(self): th = TrackerHelper() th.config.insert_multi_regex( regex=r"Row1 name: (.*)", regex_groups=["torrentName"] ) th.config.insert_multi_regex(regex=r"Row2 g2: (.*)", regex_groups=["$g2"]) var = announce_parser.parse(th, "Row1 name: first_name") self.assertEqual(var, None, "No match should return None") var = announce_parser.parse(th, "Row1 name: second_name") self.assertEqual(var, None, "No match should return None") var = announce_parser.parse(th, "Row2 g2: first_g2") self.assertEqual(len(var), 1 + len(utils.get_default_variables())) self.assertEqual(var["torrentName"], "first_name", "Name did not match") self.assertEqual(var["$g2"], "first_g2", "g2 did not match") var = announce_parser.parse(th, "Row2 g2: second_g2") self.assertEqual(len(var), 1 + len(utils.get_default_variables())) self.assertEqual(var["torrentName"], "second_name", "Name did not match") self.assertEqual(var["$g2"], "second_g2", "g2 did not match")
def test_multi_line_pattern_announcement_completed_just_in_time(self): th = TrackerHelper() th.config.insert_multi_regex( regex=r"Row1 name: (.*)", regex_groups=["torrentName"] ) th.config.insert_multi_regex(regex=r"Row2 g2: (.*)", regex_groups=["$g2"]) var = announce_parser.parse(th, "Row1 name: a_name") self.assertEqual(var, None, "No match should return None") var = announce_parser.parse(th, "Row2 g2: g2_text") self.assertEqual(len(var), 1 + len(utils.get_default_variables())) self.assertEqual(var["torrentName"], "a_name", "Name did not match") self.assertEqual(var["$g2"], "g2_text", "g2 did not match")
def test_single_line_pattern_with_category(self): th = TrackerHelper() th.config.insert_regex( regex=r"Test name: (.*) g2: (.*) category: (.*)", regex_groups=["torrentName", "$g2", "category"], ) var = announce_parser.parse( th, "Test name: a_name g2: g2_text category: this_is_category" ) self.assertEqual(len(var), 1 + len(utils.get_default_variables())) self.assertEqual(var["torrentName"], "a_name", "Name did not match") self.assertEqual(var["$g2"], "g2_text", "g2 did not match") self.assertEqual(var["category"], "this_is_category", "category did not match")
def test_single_line_ignore_unexpected(self): th = TrackerHelper() th.config.insert_regex( regex=r"(.*) / (.*)", regex_groups=["torrentName", "$g2"], ) th.config.insert_ignore(r".*/.*", False) var = announce_parser.parse(th, "something else") self.assertEqual(var, None, "Should return None when ignored match") var = announce_parser.parse(th, "a_name / a_group") self.assertEqual(len(var), 1 + len(utils.get_default_variables())) self.assertEqual(var["torrentName"], "a_name", "Name did not match") self.assertEqual(var["$g2"], "a_group", "g2 did not match")
def test_single_line_pattern_with_match(self): th = TrackerHelper() th.config.insert_regex( regex=r"Test name: (.*) g2: (.*) g3: (.*)", regex_groups=["torrentName", "$g2", "$g3"], ) # torrentName has extra whitespace added var = announce_parser.parse( th, "Test name: the_name g2: g2_text& g3: g3_text&" ) self.assertEqual(len(var), 2 + len(utils.get_default_variables())) self.assertEqual(var["torrentName"], "the_name", "Name did not match") self.assertEqual(var["$g2"], "g2_text&", "g2 did not match") self.assertEqual(var["$g3"], "g3_text&", "g3 did not match")
def __init__(self): self.time = time.time() self.pattern_groups = get_default_variables() self.matched_index = -1