From: Frank Brehm Date: Thu, 12 Dec 2024 12:13:54 +0000 (+0100) Subject: Uniting plugins/filter/compare_lc_list.py and plugins/filter/cfg_389ds_to_dict.py... X-Git-Url: https://git.uhu-banane.org/?a=commitdiff_plain;h=4cdca40c8c52e068b350549a5226fbaa398f82fd;p=pixelpark%2Fpp-admin-tools.git Uniting plugins/filter/compare_lc_list.py and plugins/filter/cfg_389ds_to_dict.py to plugins/filter/filter.py --- diff --git a/plugins/filter/cfg_389ds_to_dict.py b/plugins/filter/cfg_389ds_to_dict.py deleted file mode 100644 index dfaea51..0000000 --- a/plugins/filter/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/plugins/filter/compare_lc_list.py b/plugins/filter/compare_lc_list.py deleted file mode 100644 index fe6f98c..0000000 --- a/plugins/filter/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/plugins/filter/filter.py b/plugins/filter/filter.py new file mode 100644 index 0000000..e34de3c --- /dev/null +++ b/plugins/filter/filter.py @@ -0,0 +1,124 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -*- +""" +@summary: Ansible filter module for different filters. + +@author: Frank Brehm +@contact: frank@brehm-online.com +@copyright: © 2024 by Frank Brehm, Berlin +""" + +import re + +try: + from collections.abc import Sequence +except ImportError: + from collections import Sequence + +# ============================================================================= +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, + '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 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 + + # ------------------ + 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