From 1f0619e0cfe1aea6426a0d588966980db8df59f9 Mon Sep 17 00:00:00 2001 From: Chris Caron Date: Mon, 13 Nov 2023 17:10:17 -0500 Subject: [PATCH] Improved YAML Group support --- apprise/config/ConfigBase.py | 53 +++++++++++++++++++++++++++--------- test/test_apprise_config.py | 51 ++++++++++++++++++++++++++++++++++ 2 files changed, 91 insertions(+), 13 deletions(-) diff --git a/apprise/config/ConfigBase.py b/apprise/config/ConfigBase.py index adddc4f56c..1c60a850ae 100644 --- a/apprise/config/ConfigBase.py +++ b/apprise/config/ConfigBase.py @@ -898,19 +898,11 @@ def config_parse_yaml(content, asset=None): # groups root directive # groups = result.get('groups', None) - if not isinstance(groups, (list, tuple)): - # Not a problem; we simply have no group entry - groups = list() - - # Iterate over each group defined and store it - for no, entry in enumerate(groups): - if not isinstance(entry, dict): - ConfigBase.logger.warning( - 'No assignment for group {}, entry #{}'.format( - entry, no + 1)) - continue - - for _groups, tags in entry.items(): + if isinstance(groups, dict): + # + # Dictionary + # + for no, (_groups, tags) in enumerate(groups.items()): for group in parse_list(_groups, cast=str): if isinstance(tags, (list, tuple)): _tags = set() @@ -932,6 +924,41 @@ def config_parse_yaml(content, asset=None): else: group_tags[group] |= tags + elif isinstance(groups, (list, tuple)): + # + # List of Dictionaries + # + + # Iterate over each group defined and store it + for no, entry in enumerate(groups): + if not isinstance(entry, dict): + ConfigBase.logger.warning( + 'No assignment for group {}, entry #{}'.format( + entry, no + 1)) + continue + + for _groups, tags in entry.items(): + for group in parse_list(_groups, cast=str): + if isinstance(tags, (list, tuple)): + _tags = set() + for e in tags: + if isinstance(e, dict): + _tags |= set(e.keys()) + else: + _tags |= set(parse_list(e, cast=str)) + + # Final assignment + tags = _tags + + else: + tags = set(parse_list(tags, cast=str)) + + if group not in group_tags: + group_tags[group] = tags + + else: + group_tags[group] |= tags + # # include root directive # diff --git a/test/test_apprise_config.py b/test/test_apprise_config.py index a78300b494..a2910775b1 100644 --- a/test/test_apprise_config.py +++ b/test/test_apprise_config.py @@ -1250,6 +1250,57 @@ def test_config_base_parse_yaml_file04(tmpdir): assert sum(1 for _ in a.find('test1, test3')) == 2 +def test_config_base_parse_yaml_file05(tmpdir): + """ + API: ConfigBase.parse_yaml_file (#5) + + Test groups + + """ + t = tmpdir.mkdir("always-keyword").join("apprise.yml") + t.write(""" + version: 1 + groups: + mygroup: user1, user2 + + urls: + - json:///user:pass@localhost: + - to: user1@example.com + tag: user1 + - to: user2@example.com + tag: user2 + """) + + # Create ourselves a config object + ac = AppriseConfig(paths=str(t)) + + # The number of configuration files that exist + assert len(ac) == 1 + + # no notifications are loaded + assert len(ac.servers()) == 2 + + # Test our ability to add Config objects to our apprise object + a = Apprise() + + # Add our configuration object + assert a.add(servers=ac) is True + + # Detect our 3 entry as they should have loaded successfully + assert len(a) == 2 + + # No match + assert sum(1 for _ in a.find('no-match')) == 0 + # Match everything + assert sum(1 for _ in a.find('all')) == 2 + # Match user1 entry + assert sum(1 for _ in a.find('user1')) == 1 + # Match user2 entry + assert sum(1 for _ in a.find('user2')) == 1 + # Match mygroup + assert sum(1 for _ in a.find('mygroup')) == 2 + + def test_apprise_config_template_parse(tmpdir): """ API: AppriseConfig parsing of templates