From 4cdca40c8c52e068b350549a5226fbaa398f82fd Mon Sep 17 00:00:00 2001 From: Frank Brehm Date: Thu, 12 Dec 2024 13:13:54 +0100 Subject: [PATCH] Uniting plugins/filter/compare_lc_list.py and plugins/filter/cfg_389ds_to_dict.py to plugins/filter/filter.py --- plugins/filter/compare_lc_list.py | 73 ------------------- .../{cfg_389ds_to_dict.py => filter.py} | 55 +++++++++++++- 2 files changed, 53 insertions(+), 75 deletions(-) delete mode 100644 plugins/filter/compare_lc_list.py rename plugins/filter/{cfg_389ds_to_dict.py => filter.py} (59%) 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/cfg_389ds_to_dict.py b/plugins/filter/filter.py similarity index 59% rename from plugins/filter/cfg_389ds_to_dict.py rename to plugins/filter/filter.py index dfaea51..e34de3c 100644 --- a/plugins/filter/cfg_389ds_to_dict.py +++ b/plugins/filter/filter.py @@ -1,7 +1,7 @@ #!/usr/bin/env python # -*- coding: utf-8 -*- """ -@summary: Ansible filter module of filter 'compare_lc_list'. +@summary: Ansible filter module for different filters. @author: Frank Brehm @contact: frank@brehm-online.com @@ -10,6 +10,11 @@ import re +try: + from collections.abc import Sequence +except ImportError: + from collections import Sequence + # ============================================================================= class FilterModule(object): """A filter module object.""" @@ -22,7 +27,12 @@ class FilterModule(object): # ------------------ def filters(self): """Return all usable filter methods from this class.""" - return {'cfg_389ds_to_dict': self.cfg_389ds_to_dict} + 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): @@ -67,6 +77,47 @@ class FilterModule(object): 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' + # ============================================================================= -- 2.39.5