Skip to content

Commit

Permalink
Improved YAML Group support
Browse files Browse the repository at this point in the history
  • Loading branch information
caronc committed Nov 13, 2023
1 parent 7e87807 commit 1f0619e
Show file tree
Hide file tree
Showing 2 changed files with 91 additions and 13 deletions.
53 changes: 40 additions & 13 deletions apprise/config/ConfigBase.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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
#
Expand Down
51 changes: 51 additions & 0 deletions test/test_apprise_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -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: [email protected]
tag: user1
- to: [email protected]
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
Expand Down

0 comments on commit 1f0619e

Please sign in to comment.