Debian Codegraph @

ansible_collections/community/zabbix/plugins/module_utils/helpers.py

raw
1 #!/usr/bin/env python
2 # -*- coding: utf-8 -*-
3
4 # GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
5
6
7 from __future__ import absolute_import, division, print_function
8 __metaclass__ = type
9
10
11 def zabbix_common_argument_spec():
12 """
13 Return a dictionary with connection options.
14 The options are commonly used by most of Zabbix modules.
15 """
16 return dict(
17 http_login_user=dict(
18 type='str',
19 required=False,
20 default=None
21 ),
22 http_login_password=dict(
23 type='str',
24 required=False,
25 default=None,
26 no_log=True
27 )
28 )
29
30
31 def helper_cleanup_data(obj):
32 """
33 Removes the None values from the object and returns the object
34 Args:
35 obj: object to cleanup
36
37 Returns:
38 object: cleaned object
39 """
40 if isinstance(obj, (list, tuple, set)):
41 return type(obj)(helper_cleanup_data(x) for x in obj if x is not None)
42 elif isinstance(obj, dict):
43 return type(obj)((helper_cleanup_data(k), helper_cleanup_data(v))
44 for k, v in obj.items() if k is not None and v is not None)
45 else:
46 return obj
47
48
49 def helper_to_numeric_value(elements, value):
50 """Converts string values to integers
51
52 Parameters:
53 elements: list of elements to enumerate
54 value: string value
55
56 Returns:
57 int: converted integer
58 """
59 if value is None:
60 return None
61 for index, element in enumerate(elements):
62 if isinstance(element, str) and element.lower() == value.lower():
63 return index
64 if isinstance(element, list):
65 for deep_element in element:
66 if isinstance(deep_element, str) and deep_element.lower() == value.lower():
67 return index
68
69
70 def helper_convert_unicode_to_str(data):
71 """Converts unicode objects to strings in dictionary
72
73 Parameters:
74 data: unicode object
75
76 Returns:
77 dict: strings in dictionary
78 """
79 if isinstance(data, dict):
80 return dict(map(helper_convert_unicode_to_str, data.items()))
81 elif isinstance(data, (list, tuple, set)):
82 return type(data)(map(helper_convert_unicode_to_str, data))
83 elif data is None:
84 return data
85 else:
86 return str(data)
87
88
89 def helper_compare_lists(l1, l2, diff_dict):
90 """
91 Compares l1 and l2 lists and adds the items that are different
92 to the diff_dict dictionary.
93 Used in recursion with helper_compare_dictionaries() function.
94
95 Parameters:
96 l1: first list to compare
97 l2: second list to compare
98 diff_dict: dictionary to store the difference
99
100 Returns:
101 dict: items that are different
102 """
103 if len(l1) != len(l2):
104 diff_dict.append(l1)
105 return diff_dict
106 for i, item in enumerate(l1):
107 if isinstance(item, dict):
108 for item2 in l2:
109 diff_dict2 = {}
110 diff_dict2 = helper_compare_dictionaries(item, item2, diff_dict2)
111 if len(diff_dict2) == 0:
112 break
113 if len(diff_dict2) != 0:
114 diff_dict.insert(i, item)
115 else:
116 if item != l2[i]:
117 diff_dict.append(item)
118 while {} in diff_dict:
119 diff_dict.remove({})
120 return diff_dict
121
122
123 def helper_compare_dictionaries(d1, d2, diff_dict):
124 """
125 Compares d1 and d2 dictionaries and adds the items that are different
126 to the diff_dict dictionary.
127 Used in recursion with helper_compare_lists() function.
128
129 Parameters:
130 d1: first dictionary to compare
131 d2: second dictionary to compare
132 diff_dict: dictionary to store the difference
133
134 Returns:
135 dict: items that are different
136 """
137 for k, v in d1.items():
138 if k not in d2:
139 diff_dict[k] = v
140 continue
141 if isinstance(v, dict):
142 diff_dict[k] = {}
143 helper_compare_dictionaries(v, d2[k], diff_dict[k])
144 if diff_dict[k] == {}:
145 del diff_dict[k]
146 else:
147 diff_dict[k] = v
148 elif isinstance(v, list):
149 diff_dict[k] = []
150 helper_compare_lists(v, d2[k], diff_dict[k])
151 if diff_dict[k] == []:
152 del diff_dict[k]
153 else:
154 diff_dict[k] = v
155 else:
156 if v != d2[k]:
157 diff_dict[k] = v
158 return diff_dict
159
160
161 def helper_normalize_data(data, del_keys=None):
162 """
163 Delete None parameter or specified keys from data.
164
165 Parameters:
166 data: dictionary
167
168 Returns:
169 data: falsene parameter removed data
170 del_keys: deleted keys
171 """
172 if del_keys is None:
173 del_keys = []
174
175 for key, value in data.items():
176 if value is None:
177 del_keys.append(key)
178
179 for key in del_keys:
180 if key in data.keys():
181 del data[key]
182
183 return data, del_keys