+++ /dev/null
-#!/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
+++ /dev/null
-#!/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
--- /dev/null
+#!/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