[mailman] Fix the yamlget script

Signed-off-by: Michal Konecny <mkonecny@redhat.com>
This commit is contained in:
Michal Konecny 2024-03-14 18:16:44 +01:00
parent 60dc16d7c9
commit 2867a08e75
2 changed files with 125 additions and 0 deletions

View file

@ -0,0 +1,110 @@
#!/usr/bin/env python
# vim: set fileencoding=utf-8 tabstop=4 shiftwidth=4 expandtab smartindent:
u"""
yamlget
-------
Output any key in a YAML-formatted file. The aim is to make such a
configuration file accessible to shell scripts.
.. :Authors:
Aurélien Bompard <aurelien@bompard.org> <http://aurelien.bompard.org>
.. :License:
GNU GPL v3 or later
"""
from __future__ import print_function
import os
import sys
from optparse import OptionParser
import yaml
def get_key(fullkey, data):
"""
Get the requested key from the parsed data.
:param fullkey: the key to get, nested values can be accessed by using a
colon (":") as the separator
:param data: the parsed data, from yaml.load()
Examples:
>>> data = {
... 'bool': [True, False, True, False],
... 'dict': {'hp': 13, 'sp': 5},
... 'float': 3.14159,
... 'int': 42,
... 'list': ['LITE', 'RES_ACID', 'SUS_DEXT'],
... 'none': [None, None],
... 'text': "The Set of Gauntlets 'Pauraegen'",
... }
>>> get_key('bool', data)
[True, False, True, False]
>>> get_key('bool:2', data)
False
>>> get_key('dict', data)
{'hp': 13, 'sp': 5}
>>> get_key('dict:hp', data)
13
>>> get_key('float', data)
3.14159
>>> get_key('int', data)
42
>>> get_key('list', data)
['LITE', 'RES_ACID', 'SUS_DEXT']
>>> get_key('list:2', data)
'RES_ACID'
>>> get_key('list:2:5', data)
'RES_ACID'
>>> get_key('none', data)
[None, None]
>>> get_key('none:1', data)
>>> get_key('text', data)
"The Set of Gauntlets 'Pauraegen'"
>>> get_key('2', ['item1', 'item2', 'item3'])
'item2'
"""
value = data
while value is not None:
key, _sep, fullkey = fullkey.partition(":")
if isinstance(value, list):
try:
key = int(key)
except TypeError:
print("Wrong key format: %s, it should be an integer" % key,
file=sys.stderr)
sys.exit(1)
value = value[key - 1] # start at 1, not 0
elif isinstance(value, dict):
value = value.get(key)
else:
break # we've got the value now
if not fullkey:
break # can't go any further
return value
def main():
parser = OptionParser(usage="%prog <key> <yaml-file>")
args = parser.parse_args()[1]
if len(args) != 2:
parser.error("wrong number of arguments")
fullkey, filepath = args
if not os.path.exists(filepath):
parser.error("no such file: %s" % filepath)
with open(filepath) as yamlfile:
data = next(yaml.safe_load_all(yamlfile))
#from pprint import pprint; pprint(data)
value = get_key(fullkey, data)
if value is not None:
print(value)
if __name__ == "__main__":
main()

View file

@ -541,6 +541,20 @@
owner=root group=root mode=0644
tags: mailman
- name: install the scripts
copy: src={{ item }} dest="{{ mailman_webui_basedir }}/bin/{{ item }}"
owner=root group=root mode=0755
tags: mailman
with_items:
- mailman3_yamlget
- pg-give-rights.py
- post-update.sh
- import-mm2.py
- periodic.py
- mailman-sar.py
- hyperkitty-delete-list.py
when: env == 'staging'
- name: install the scripts
copy: src={{ item }} dest="{{ mailman_webui_basedir }}/bin/{{ item }}"
owner=root group=root mode=0755
@ -553,6 +567,7 @@
- periodic.py
- mailman-sar.py
- hyperkitty-delete-list.py
when: env == 'production'
- name: install the templatized scripts
template: src={{ item }}.j2 dest="{{ mailman_webui_basedir }}/bin/{{ item }}"