From: Frank Brehm Date: Thu, 12 Dec 2024 11:47:40 +0000 (+0100) Subject: Adding plugins/test/tests.py and moving filter plugins to plugins/filter. X-Git-Url: https://git.uhu-banane.org/?a=commitdiff_plain;h=ac8f50fcccc31aec4c6a5263b0277e8a3f391fff;p=pixelpark%2Fpp-admin-tools.git Adding plugins/test/tests.py and moving filter plugins to plugins/filter. --- diff --git a/filter_plugins/cfg_389ds_to_dict.py b/filter_plugins/cfg_389ds_to_dict.py deleted file mode 100644 index dfaea51..0000000 --- a/filter_plugins/cfg_389ds_to_dict.py +++ /dev/null @@ -1,73 +0,0 @@ -#!/usr/bin/env python -# -*- coding: utf-8 -*- -""" -@summary: Ansible filter module of filter 'compare_lc_list'. - -@author: Frank Brehm -@contact: frank@brehm-online.com -@copyright: © 2024 by Frank Brehm, Berlin -""" - -import re - -# ============================================================================= -class FilterModule(object): - """A filter module object.""" - - re_key = re.compile(r'nsslapd-', re.IGNORECASE) # noqa: W605 - re_sep = re.compile(r':\s+') # noqa: W605 - re_int = re.compile('^[+-]?\d+$') # noqa: W605 - re_float = re.compile('^[+-]?\d+\.\d*$') # noqa: W605 - - # ------------------ - def filters(self): - """Return all usable filter methods from this class.""" - return {'cfg_389ds_to_dict': self.cfg_389ds_to_dict} - - # ------------------ - def cfg_389ds_to_dict(self, the_list): - """ - Translate a list of strings of the form 'key: value' into a dictionary. - - In case of multiple values with the same key, they are assigned as a list to the key. - If a key is starting with 'nsslapd-', this will removed from the key. - The values are mangled with the method mangle_value(). - """ - result = {} - - for line in the_list: - (key, value) = self.re_sep.split(line, maxsplit=1) - key = self.re_key.sub('', key) - value = self.mangle_value(value) - if key in result: - old_val = result[key] - if isinstance(old_val, list): - result[key].append(value) - else: - result[key] = [old_val, value] - else: - result[key] = value - - return result - - # ------------------ - def mangle_value(self, value): - """Tape cast the given value to a boolean, integer of float value, if it is looking so.""" - if self.re_int.match(value): - return int(value) - if self.re_float.match(value): - return float(value) - if value.lower() == 'on': - return True - if value.lower() == 'yes': - return True - if value.lower() == 'off': - return False - if value.lower() == 'no': - return False - return value - - -# ============================================================================= - -# vim: ts=4 et list diff --git a/filter_plugins/compare_lc_list.py b/filter_plugins/compare_lc_list.py deleted file mode 100644 index fe6f98c..0000000 --- a/filter_plugins/compare_lc_list.py +++ /dev/null @@ -1,73 +0,0 @@ -#!/usr/bin/env python -# -*- coding: utf-8 -*- -""" -@summary: Ansible filter module of filter 'compare_lc_list'. - -@author: Frank Brehm -@contact: frank@brehm-online.com -@copyright: © 2024 by Frank Brehm, Berlin -""" - -try: - from collections.abc import Sequence -except ImportError: - from collections import Sequence - -# ============================================================================= -class FilterModule(object): - """A filter module object.""" - - # ------------------ - def filters(self): - """Return all usable filter methods from this class.""" - return { - 'compare_lc_list': self.compare_lc_list, - 'bool_to_on_off': self.bool_to_on_off, - 'bool_to_yes_no': self.bool_to_yes_no, - } - - # ------------------ - def compare_lc_list(self, list_one, list_two): - """Compare two lists with case-insensitive and position independend items.""" - if not isinstance(list_one, Sequence) or hasattr(list_one, 'strip'): - list_one = [list_one] - - if not isinstance(list_two, Sequence) or hasattr(list_two, 'strip'): - list_two = [list_two] - - if len(list_one) != len(list_two): - return False - - list_one_lc = [] - for item in list_one: - list_one_lc.append(str(item).lower()) - list_one_lc.sort() - - list_two_lc = [] - for item in list_two: - list_two_lc.append(str(item).lower()) - list_one_lc.sort() - - if list_one_lc == list_two_lc: - return True - - return False - - # ------------------ - def bool_to_on_off(self, value): - """Translate the given value to on or off respective.""" - if value: - return 'on' - return 'off' - - # ------------------ - def bool_to_yes_no(self, value): - """Translate the given value to yes or no respective.""" - if value: - return 'yes' - return 'no' - - -# ============================================================================= - -# vim: ts=4 et list diff --git a/playbooks/filter_plugins b/playbooks/filter_plugins index c954752..72b6fa6 120000 --- a/playbooks/filter_plugins +++ b/playbooks/filter_plugins @@ -1 +1 @@ -../filter_plugins/ \ No newline at end of file +../plugins/filter \ No newline at end of file diff --git a/playbooks/test_plugins b/playbooks/test_plugins new file mode 120000 index 0000000..cddf7f9 --- /dev/null +++ b/playbooks/test_plugins @@ -0,0 +1 @@ +../plugins/test \ No newline at end of file diff --git a/plugins/filter/cfg_389ds_to_dict.py b/plugins/filter/cfg_389ds_to_dict.py new file mode 100644 index 0000000..dfaea51 --- /dev/null +++ b/plugins/filter/cfg_389ds_to_dict.py @@ -0,0 +1,73 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -*- +""" +@summary: Ansible filter module of filter 'compare_lc_list'. + +@author: Frank Brehm +@contact: frank@brehm-online.com +@copyright: © 2024 by Frank Brehm, Berlin +""" + +import re + +# ============================================================================= +class FilterModule(object): + """A filter module object.""" + + re_key = re.compile(r'nsslapd-', re.IGNORECASE) # noqa: W605 + re_sep = re.compile(r':\s+') # noqa: W605 + re_int = re.compile('^[+-]?\d+$') # noqa: W605 + re_float = re.compile('^[+-]?\d+\.\d*$') # noqa: W605 + + # ------------------ + def filters(self): + """Return all usable filter methods from this class.""" + return {'cfg_389ds_to_dict': self.cfg_389ds_to_dict} + + # ------------------ + def cfg_389ds_to_dict(self, the_list): + """ + Translate a list of strings of the form 'key: value' into a dictionary. + + In case of multiple values with the same key, they are assigned as a list to the key. + If a key is starting with 'nsslapd-', this will removed from the key. + The values are mangled with the method mangle_value(). + """ + result = {} + + for line in the_list: + (key, value) = self.re_sep.split(line, maxsplit=1) + key = self.re_key.sub('', key) + value = self.mangle_value(value) + if key in result: + old_val = result[key] + if isinstance(old_val, list): + result[key].append(value) + else: + result[key] = [old_val, value] + else: + result[key] = value + + return result + + # ------------------ + def mangle_value(self, value): + """Tape cast the given value to a boolean, integer of float value, if it is looking so.""" + if self.re_int.match(value): + return int(value) + if self.re_float.match(value): + return float(value) + if value.lower() == 'on': + return True + if value.lower() == 'yes': + return True + if value.lower() == 'off': + return False + if value.lower() == 'no': + return False + return value + + +# ============================================================================= + +# vim: ts=4 et list diff --git a/plugins/filter/compare_lc_list.py b/plugins/filter/compare_lc_list.py new file mode 100644 index 0000000..fe6f98c --- /dev/null +++ b/plugins/filter/compare_lc_list.py @@ -0,0 +1,73 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -*- +""" +@summary: Ansible filter module of filter 'compare_lc_list'. + +@author: Frank Brehm +@contact: frank@brehm-online.com +@copyright: © 2024 by Frank Brehm, Berlin +""" + +try: + from collections.abc import Sequence +except ImportError: + from collections import Sequence + +# ============================================================================= +class FilterModule(object): + """A filter module object.""" + + # ------------------ + def filters(self): + """Return all usable filter methods from this class.""" + return { + 'compare_lc_list': self.compare_lc_list, + 'bool_to_on_off': self.bool_to_on_off, + 'bool_to_yes_no': self.bool_to_yes_no, + } + + # ------------------ + def compare_lc_list(self, list_one, list_two): + """Compare two lists with case-insensitive and position independend items.""" + if not isinstance(list_one, Sequence) or hasattr(list_one, 'strip'): + list_one = [list_one] + + if not isinstance(list_two, Sequence) or hasattr(list_two, 'strip'): + list_two = [list_two] + + if len(list_one) != len(list_two): + return False + + list_one_lc = [] + for item in list_one: + list_one_lc.append(str(item).lower()) + list_one_lc.sort() + + list_two_lc = [] + for item in list_two: + list_two_lc.append(str(item).lower()) + list_one_lc.sort() + + if list_one_lc == list_two_lc: + return True + + return False + + # ------------------ + def bool_to_on_off(self, value): + """Translate the given value to on or off respective.""" + if value: + return 'on' + return 'off' + + # ------------------ + def bool_to_yes_no(self, value): + """Translate the given value to yes or no respective.""" + if value: + return 'yes' + return 'no' + + +# ============================================================================= + +# vim: ts=4 et list diff --git a/plugins/test/tests.py b/plugins/test/tests.py new file mode 100644 index 0000000..112ad4b --- /dev/null +++ b/plugins/test/tests.py @@ -0,0 +1,37 @@ +# Copyright (c) 2020 Matthias Dellweg +# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt) + + +import typing as t + +from jinja2.runtime import Undefined + + +def empty_test(value: t.Any) -> bool: + """ + Check whether a value is false or an empty string, list or dict. + """ + if isinstance(value, Undefined): + return True + if isinstance(value, bool): + return not value + if value is None: + return True + if value == "": + return True + if value == []: + return True + if value == {}: + return True + if value == b"": + return True + return False + + +class TestModule: + def tests(self) -> t.Dict[str, t.Callable]: + return { + "empty": empty_test, + } + +# last line