pandoraPlugintools-basic 1.0.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- pandoraPlugintools/__init__.py +7 -0
- pandoraPlugintools/agents.py +391 -0
- pandoraPlugintools/discovery.py +215 -0
- pandoraPlugintools/general.py +683 -0
- pandoraPlugintools/modules.py +348 -0
- pandoraPlugintools/output.py +137 -0
- pandoraPlugintools/threads.py +249 -0
- pandoraPlugintools/transfer.py +258 -0
- pandoraplugintools_basic-1.0.0.dist-info/METADATA +93 -0
- pandoraplugintools_basic-1.0.0.dist-info/RECORD +13 -0
- pandoraplugintools_basic-1.0.0.dist-info/WHEEL +5 -0
- pandoraplugintools_basic-1.0.0.dist-info/licenses/LICENSE +201 -0
- pandoraplugintools_basic-1.0.0.dist-info/top_level.txt +1 -0
|
@@ -0,0 +1,348 @@
|
|
|
1
|
+
####
|
|
2
|
+
# Internal: Alias for output.print_debug function
|
|
3
|
+
#########################################################################################
|
|
4
|
+
|
|
5
|
+
def _print_debug(
|
|
6
|
+
var = "",
|
|
7
|
+
print_errors: bool = False
|
|
8
|
+
):
|
|
9
|
+
"""
|
|
10
|
+
Print the provided variable as JSON format, supporting various data types.
|
|
11
|
+
|
|
12
|
+
Args:
|
|
13
|
+
var (any, optional): The variable to be printed as JSON. Defaults to an empty string.
|
|
14
|
+
print_errors (bool, optional): Set to True to print errors encountered during printing. Defaults to False.
|
|
15
|
+
"""
|
|
16
|
+
from .output import print_debug
|
|
17
|
+
print_debug(var, print_errors)
|
|
18
|
+
|
|
19
|
+
####
|
|
20
|
+
# Init module template
|
|
21
|
+
#########################################################################################
|
|
22
|
+
def init_module(
|
|
23
|
+
default_values: dict = {}
|
|
24
|
+
) -> dict:
|
|
25
|
+
"""
|
|
26
|
+
Initializes a module template with default values.
|
|
27
|
+
|
|
28
|
+
Args:
|
|
29
|
+
default_values (dict, optional): Dictionary containing default values to override template values. Defaults to an empty dictionary.
|
|
30
|
+
|
|
31
|
+
Returns:
|
|
32
|
+
dict: Dictionary representing the module template with default values.
|
|
33
|
+
"""
|
|
34
|
+
module = {
|
|
35
|
+
"name" : None,
|
|
36
|
+
"type" : "generic_data_string",
|
|
37
|
+
"data" : "0",
|
|
38
|
+
"value" : "0",
|
|
39
|
+
"description" : "",
|
|
40
|
+
"desc" : "",
|
|
41
|
+
"unit" : "",
|
|
42
|
+
"interval" : "",
|
|
43
|
+
"tags" : "",
|
|
44
|
+
"module_group" : "",
|
|
45
|
+
"module_parent" : "",
|
|
46
|
+
"min_warning" : "",
|
|
47
|
+
"min_warning_forced" : "",
|
|
48
|
+
"max_warning" : "",
|
|
49
|
+
"max_warning_forced" : "",
|
|
50
|
+
"min_critical" : "",
|
|
51
|
+
"min_critical_forced" : "",
|
|
52
|
+
"max_critical" : "",
|
|
53
|
+
"max_critical_forced" : "",
|
|
54
|
+
"str_warning" : "",
|
|
55
|
+
"str_warning_forced" : "",
|
|
56
|
+
"str_critical" : "",
|
|
57
|
+
"str_critical_forced" : "",
|
|
58
|
+
"critical_inverse" : "",
|
|
59
|
+
"warning_inverse" : "",
|
|
60
|
+
"max" : "",
|
|
61
|
+
"min" : "",
|
|
62
|
+
"post_process" : "",
|
|
63
|
+
"disabled" : "",
|
|
64
|
+
"min_ff_event" : "",
|
|
65
|
+
"status" : "",
|
|
66
|
+
"timestamp" : "",
|
|
67
|
+
"custom_id" : "",
|
|
68
|
+
"critical_instructions" : "",
|
|
69
|
+
"warning_instructions" : "",
|
|
70
|
+
"unknown_instructions" : "",
|
|
71
|
+
"quiet" : "",
|
|
72
|
+
"module_ff_interval" : "",
|
|
73
|
+
"crontab" : "",
|
|
74
|
+
"min_ff_event_normal" : "",
|
|
75
|
+
"min_ff_event_warning" : "",
|
|
76
|
+
"min_ff_event_critical" : "",
|
|
77
|
+
"ff_type" : "",
|
|
78
|
+
"ff_timeout" : "",
|
|
79
|
+
"each_ff" : "",
|
|
80
|
+
"module_parent_unlink" : "",
|
|
81
|
+
"extra_data" : "",
|
|
82
|
+
"alert" : []
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
for key, value in default_values.items():
|
|
86
|
+
if key in module:
|
|
87
|
+
module[key] = value
|
|
88
|
+
|
|
89
|
+
return module
|
|
90
|
+
|
|
91
|
+
####
|
|
92
|
+
# Returns module in XML format. Accepts only {dict}
|
|
93
|
+
#########################################################################################
|
|
94
|
+
def print_module(
|
|
95
|
+
module: dict = None,
|
|
96
|
+
print_flag: bool = False
|
|
97
|
+
) -> str:
|
|
98
|
+
"""
|
|
99
|
+
Returns module in XML format. Accepts only {dict}.
|
|
100
|
+
|
|
101
|
+
Args:
|
|
102
|
+
module (dict, optional): Dictionary containing module data. Defaults to None.
|
|
103
|
+
print_flag (bool, optional): Flag to print the module XML to STDOUT. Defaults to False.
|
|
104
|
+
|
|
105
|
+
Returns:
|
|
106
|
+
str: Module data in XML format.
|
|
107
|
+
"""
|
|
108
|
+
from .output import print_stdout
|
|
109
|
+
|
|
110
|
+
module_xml = ""
|
|
111
|
+
|
|
112
|
+
if module is not None:
|
|
113
|
+
data = dict(module)
|
|
114
|
+
|
|
115
|
+
if "description" in data:
|
|
116
|
+
data["desc"] = data["description"]
|
|
117
|
+
|
|
118
|
+
if "data" in data:
|
|
119
|
+
data["value"] = data["data"]
|
|
120
|
+
|
|
121
|
+
module_xml = ("<module>\n"
|
|
122
|
+
"\t<name><![CDATA[" + str(data["name"]) + "]]></name>\n"
|
|
123
|
+
"\t<type>" + str(data["type"]) + "</type>\n"
|
|
124
|
+
)
|
|
125
|
+
|
|
126
|
+
if type(data["type"]) is not str and "string" not in data["type"]: #### Strip spaces if module not generic_data_string
|
|
127
|
+
data["value"] = data["value"].strip()
|
|
128
|
+
|
|
129
|
+
if isinstance(data["value"], list): # Checks if value is a list
|
|
130
|
+
module_xml += "\t<datalist>\n"
|
|
131
|
+
for value in data["value"]:
|
|
132
|
+
if type(value) is dict and "value" in value:
|
|
133
|
+
module_xml += "\t<data>\n"
|
|
134
|
+
module_xml += "\t\t<value><![CDATA[" + str(value["value"]) + "]]></value>\n"
|
|
135
|
+
if "timestamp" in value:
|
|
136
|
+
module_xml += "\t\t<timestamp><![CDATA[" + str(value["timestamp"]) + "]]></timestamp>\n"
|
|
137
|
+
module_xml += "\t</data>\n"
|
|
138
|
+
module_xml += "\t</datalist>\n"
|
|
139
|
+
else:
|
|
140
|
+
module_xml += "\t<data><![CDATA[" + str(data["value"]) + "]]></data>\n"
|
|
141
|
+
|
|
142
|
+
if "desc" in data and len(str(data["desc"]).strip()) > 0:
|
|
143
|
+
module_xml += "\t<description><![CDATA[" + str(data["desc"]) + "]]></description>\n"
|
|
144
|
+
|
|
145
|
+
if "unit" in data and len(str(data["unit"]).strip()) > 0:
|
|
146
|
+
module_xml += "\t<unit><![CDATA[" + str(data["unit"]) + "]]></unit>\n"
|
|
147
|
+
|
|
148
|
+
if "interval" in data and len(str(data["interval"]).strip()) > 0:
|
|
149
|
+
module_xml += "\t<module_interval><![CDATA[" + str(data["interval"]) + "]]></module_interval>\n"
|
|
150
|
+
|
|
151
|
+
if "tags" in data and len(str(data["tags"]).strip()) > 0:
|
|
152
|
+
module_xml += "\t<tags>" + str(data["tags"]) + "</tags>\n"
|
|
153
|
+
|
|
154
|
+
if "module_group" in data and len(str(data["module_group"]).strip()) > 0:
|
|
155
|
+
module_xml += "\t<module_group>" + str(data["module_group"]) + "</module_group>\n"
|
|
156
|
+
|
|
157
|
+
if "module_parent" in data and len(str(data["module_parent"]).strip()) > 0:
|
|
158
|
+
module_xml += "\t<module_parent>" + str(data["module_parent"]) + "</module_parent>\n"
|
|
159
|
+
|
|
160
|
+
if "min_warning" in data and len(str(data["min_warning"]).strip()) > 0:
|
|
161
|
+
module_xml += "\t<min_warning><![CDATA[" + str(data["min_warning"]) + "]]></min_warning>\n"
|
|
162
|
+
|
|
163
|
+
if "min_warning_forced" in data and len(str(data["min_warning_forced"]).strip()) > 0:
|
|
164
|
+
module_xml += "\t<min_warning_forced><![CDATA[" + str(data["min_warning_forced"]) + "]]></min_warning_forced>\n"
|
|
165
|
+
|
|
166
|
+
if "max_warning" in data and len(str(data["max_warning"]).strip()) > 0:
|
|
167
|
+
module_xml += "\t<max_warning><![CDATA[" + str(data["max_warning"]) + "]]></max_warning>\n"
|
|
168
|
+
|
|
169
|
+
if "max_warning_forced" in data and len(str(data["max_warning_forced"]).strip()) > 0:
|
|
170
|
+
module_xml += "\t<max_warning_forced><![CDATA[" + str(data["max_warning_forced"]) + "]]></max_warning_forced>\n"
|
|
171
|
+
|
|
172
|
+
if "min_critical" in data and len(str(data["min_critical"]).strip()) > 0:
|
|
173
|
+
module_xml += "\t<min_critical><![CDATA[" + str(data["min_critical"]) + "]]></min_critical>\n"
|
|
174
|
+
|
|
175
|
+
if "min_critical_forced" in data and len(str(data["min_critical_forced"]).strip()) > 0:
|
|
176
|
+
module_xml += "\t<min_critical_forced><![CDATA[" + str(data["min_critical_forced"]) + "]]></min_critical_forced>\n"
|
|
177
|
+
|
|
178
|
+
if "max_critical" in data and len(str(data["max_critical"]).strip()) > 0:
|
|
179
|
+
module_xml += "\t<max_critical><![CDATA[" + str(data["max_critical"]) + "]]></max_critical>\n"
|
|
180
|
+
|
|
181
|
+
if "max_critical_forced" in data and len(str(data["max_critical_forced"]).strip()) > 0:
|
|
182
|
+
module_xml += "\t<max_critical_forced><![CDATA[" + str(data["max_critical_forced"]) + "]]></max_critical_forced>\n"
|
|
183
|
+
|
|
184
|
+
if "str_warning" in data and len(str(data["str_warning"]).strip()) > 0:
|
|
185
|
+
module_xml += "\t<str_warning><![CDATA[" + str(data["str_warning"]) + "]]></str_warning>\n"
|
|
186
|
+
|
|
187
|
+
if "str_warning_forced" in data and len(str(data["str_warning_forced"]).strip()) > 0:
|
|
188
|
+
module_xml += "\t<str_warning_forced><![CDATA[" + str(data["str_warning_forced"]) + "]]></str_warning_forced>\n"
|
|
189
|
+
|
|
190
|
+
if "str_critical" in data and len(str(data["str_critical"]).strip()) > 0:
|
|
191
|
+
module_xml += "\t<str_critical><![CDATA[" + str(data["str_critical"]) + "]]></str_critical>\n"
|
|
192
|
+
|
|
193
|
+
if "str_critical_forced" in data and len(str(data["str_critical_forced"]).strip()) > 0:
|
|
194
|
+
module_xml += "\t<str_critical_forced><![CDATA[" + str(data["str_critical_forced"]) + "]]></str_critical_forced>\n"
|
|
195
|
+
|
|
196
|
+
if "critical_inverse" in data and len(str(data["critical_inverse"]).strip()) > 0:
|
|
197
|
+
module_xml += "\t<critical_inverse><![CDATA[" + str(data["critical_inverse"]) + "]]></critical_inverse>\n"
|
|
198
|
+
|
|
199
|
+
if "warning_inverse" in data and len(str(data["warning_inverse"]).strip()) > 0:
|
|
200
|
+
module_xml += "\t<warning_inverse><![CDATA[" + str(data["warning_inverse"]) + "]]></warning_inverse>\n"
|
|
201
|
+
|
|
202
|
+
if "max" in data and len(str(data["max"]).strip()) > 0:
|
|
203
|
+
module_xml += "\t<max><![CDATA[" + str(data["max"]) + "]]></max>\n"
|
|
204
|
+
|
|
205
|
+
if "min" in data and len(str(data["min"]).strip()) > 0:
|
|
206
|
+
module_xml += "\t<min><![CDATA[" + str(data["min"]) + "]]></min>\n"
|
|
207
|
+
|
|
208
|
+
if "post_process" in data and len(str(data["post_process"]).strip()) > 0:
|
|
209
|
+
module_xml += "\t<post_process><![CDATA[" + str(data["post_process"]) + "]]></post_process>\n"
|
|
210
|
+
|
|
211
|
+
if "disabled" in data and len(str(data["disabled"]).strip()) > 0:
|
|
212
|
+
module_xml += "\t<disabled><![CDATA[" + str(data["disabled"]) + "]]></disabled>\n"
|
|
213
|
+
|
|
214
|
+
if "min_ff_event" in data and len(str(data["min_ff_event"]).strip()) > 0:
|
|
215
|
+
module_xml += "\t<min_ff_event><![CDATA[" + str(data["min_ff_event"]) + "]]></min_ff_event>\n"
|
|
216
|
+
|
|
217
|
+
if "status" in data and len(str(data["status"]).strip()) > 0:
|
|
218
|
+
module_xml += "\t<status><![CDATA[" + str(data["status"]) + "]]></status>\n"
|
|
219
|
+
|
|
220
|
+
if "timestamp" in data and len(str(data["timestamp"]).strip()) > 0:
|
|
221
|
+
module_xml += "\t<timestamp><![CDATA[" + str(data["timestamp"]) + "]]></timestamp>\n"
|
|
222
|
+
|
|
223
|
+
if "custom_id" in data and len(str(data["custom_id"]).strip()) > 0:
|
|
224
|
+
module_xml += "\t<custom_id><![CDATA[" + str(data["custom_id"]) + "]]></custom_id>\n"
|
|
225
|
+
|
|
226
|
+
if "critical_instructions" in data and len(str(data["critical_instructions"]).strip()) > 0:
|
|
227
|
+
module_xml += "\t<critical_instructions><![CDATA[" + str(data["critical_instructions"]) + "]]></critical_instructions>\n"
|
|
228
|
+
|
|
229
|
+
if "warning_instructions" in data and len(str(data["warning_instructions"]).strip()) > 0:
|
|
230
|
+
module_xml += "\t<warning_instructions><![CDATA[" + str(data["warning_instructions"]) + "]]></warning_instructions>\n"
|
|
231
|
+
|
|
232
|
+
if "unknown_instructions" in data and len(str(data["unknown_instructions"]).strip()) > 0:
|
|
233
|
+
module_xml += "\t<unknown_instructions><![CDATA[" + str(data["unknown_instructions"]) + "]]></unknown_instructions>\n"
|
|
234
|
+
|
|
235
|
+
if "quiet" in data and len(str(data["quiet"]).strip()) > 0:
|
|
236
|
+
module_xml += "\t<quiet><![CDATA[" + str(data["quiet"]) + "]]></quiet>\n"
|
|
237
|
+
|
|
238
|
+
if "module_ff_interval" in data and len(str(data["module_ff_interval"]).strip()) > 0:
|
|
239
|
+
module_xml += "\t<module_ff_interval><![CDATA[" + str(data["module_ff_interval"]) + "]]></module_ff_interval>\n"
|
|
240
|
+
|
|
241
|
+
if "crontab" in data and len(str(data["crontab"]).strip()) > 0:
|
|
242
|
+
module_xml += "\t<crontab><![CDATA[" + str(data["crontab"]) + "]]></crontab>\n"
|
|
243
|
+
|
|
244
|
+
if "min_ff_event_normal" in data and len(str(data["min_ff_event_normal"]).strip()) > 0:
|
|
245
|
+
module_xml += "\t<min_ff_event_normal><![CDATA[" + str(data["min_ff_event_normal"]) + "]]></min_ff_event_normal>\n"
|
|
246
|
+
|
|
247
|
+
if "min_ff_event_warning" in data and len(str(data["min_ff_event_warning"]).strip()) > 0:
|
|
248
|
+
module_xml += "\t<min_ff_event_warning><![CDATA[" + str(data["min_ff_event_warning"]) + "]]></min_ff_event_warning>\n"
|
|
249
|
+
|
|
250
|
+
if "min_ff_event_critical" in data and len(str(data["min_ff_event_critical"]).strip()) > 0:
|
|
251
|
+
module_xml += "\t<min_ff_event_critical><![CDATA[" + str(data["min_ff_event_critical"]) + "]]></min_ff_event_critical>\n"
|
|
252
|
+
|
|
253
|
+
if "ff_type" in data and len(str(data["ff_type"]).strip()) > 0:
|
|
254
|
+
module_xml += "\t<ff_type><![CDATA[" + str(data["ff_type"]) + "]]></ff_type>\n"
|
|
255
|
+
|
|
256
|
+
if "ff_timeout" in data and len(str(data["ff_timeout"]).strip()) > 0:
|
|
257
|
+
module_xml += "\t<ff_timeout><![CDATA[" + str(data["ff_timeout"]) + "]]></ff_timeout>\n"
|
|
258
|
+
|
|
259
|
+
if "each_ff" in data and len(str(data["each_ff"]).strip()) > 0:
|
|
260
|
+
module_xml += "\t<each_ff><![CDATA[" + str(data["each_ff"]) + "]]></each_ff>\n"
|
|
261
|
+
|
|
262
|
+
if "module_parent_unlink" in data and len(str(data["module_parent_unlink"]).strip()) > 0:
|
|
263
|
+
module_xml += "\t<module_parent_unlink><![CDATA[" + str(data["module_parent_unlink"]) + "]]></module_parent_unlink>\n"
|
|
264
|
+
|
|
265
|
+
if "extra_data" in data:
|
|
266
|
+
module_xml += "\t<extra_data><![CDATA[" + str(data["extra_data"]) + "]]></extra_data>\n"
|
|
267
|
+
|
|
268
|
+
if "alert" in data:
|
|
269
|
+
for alert in data["alert"]:
|
|
270
|
+
if len(str(alert).strip()) > 0:
|
|
271
|
+
module_xml += "\t<alert_template><![CDATA[" + str(alert) + "]]></alert_template>\n"
|
|
272
|
+
module_xml += "</module>\n"
|
|
273
|
+
|
|
274
|
+
if print_flag:
|
|
275
|
+
print_stdout(module_xml)
|
|
276
|
+
|
|
277
|
+
return module_xml
|
|
278
|
+
|
|
279
|
+
####
|
|
280
|
+
# Init log module template
|
|
281
|
+
#########################################################################################
|
|
282
|
+
def init_log_module(
|
|
283
|
+
default_values: dict = {}
|
|
284
|
+
) -> dict:
|
|
285
|
+
"""
|
|
286
|
+
Initializes a log module template with default values.
|
|
287
|
+
|
|
288
|
+
Args:
|
|
289
|
+
default_values (dict, optional): Default values to initialize the log module with. Defaults to an empty dictionary.
|
|
290
|
+
|
|
291
|
+
Returns:
|
|
292
|
+
dict: Dictionary representing the log module template with default values.
|
|
293
|
+
"""
|
|
294
|
+
module = {
|
|
295
|
+
"source" : None,
|
|
296
|
+
"value" : ""
|
|
297
|
+
}
|
|
298
|
+
|
|
299
|
+
for key, value in default_values.items():
|
|
300
|
+
if key in module:
|
|
301
|
+
module[key] = value
|
|
302
|
+
|
|
303
|
+
return module
|
|
304
|
+
|
|
305
|
+
####
|
|
306
|
+
# Returns log module in XML format. Accepts only {dict}
|
|
307
|
+
#########################################################################################
|
|
308
|
+
def print_log_module(
|
|
309
|
+
module: dict = None,
|
|
310
|
+
encoding: str = False,
|
|
311
|
+
print_flag: bool = False,
|
|
312
|
+
) -> str:
|
|
313
|
+
"""
|
|
314
|
+
Returns log module in XML format. Accepts only {dict}.
|
|
315
|
+
- Only works with one module at a time: otherwise iteration is needed.
|
|
316
|
+
- Module "value" field accepts str type.
|
|
317
|
+
- Use not_print_flag to avoid printing the XML (only populates variables).
|
|
318
|
+
|
|
319
|
+
Args:
|
|
320
|
+
module (dict, optional): Dictionary representing the log module. Defaults to None.
|
|
321
|
+
encoding (str, optional): Encoding format used for the log module.
|
|
322
|
+
print_flag (bool, optional): Flag to indicate whether to print the XML. Defaults to False.
|
|
323
|
+
|
|
324
|
+
Returns:
|
|
325
|
+
str: XML representation of the log module.
|
|
326
|
+
"""
|
|
327
|
+
|
|
328
|
+
from .output import print_stdout
|
|
329
|
+
|
|
330
|
+
module_xml = ""
|
|
331
|
+
|
|
332
|
+
if module is not None:
|
|
333
|
+
data = dict(module)
|
|
334
|
+
module_xml = ("<log_module>\n"
|
|
335
|
+
"\t<source><![CDATA[" + str(data["source"]) + "]]></source>\n"
|
|
336
|
+
"\t<data>\"" + str(data["value"]) + "\"</data>\n"
|
|
337
|
+
)
|
|
338
|
+
|
|
339
|
+
if encoding:
|
|
340
|
+
module_xml += "\t<encoding>" + str(encoding) + "</encoding>\n"
|
|
341
|
+
|
|
342
|
+
module_xml += "</log_module>\n"
|
|
343
|
+
|
|
344
|
+
if print_flag:
|
|
345
|
+
print_stdout(module_xml)
|
|
346
|
+
|
|
347
|
+
return module_xml
|
|
348
|
+
|
|
@@ -0,0 +1,137 @@
|
|
|
1
|
+
import sys
|
|
2
|
+
import os
|
|
3
|
+
import json
|
|
4
|
+
|
|
5
|
+
####
|
|
6
|
+
# Internal: Alias for output.print_debug function
|
|
7
|
+
#########################################################################################
|
|
8
|
+
|
|
9
|
+
def _print_debug(
|
|
10
|
+
var = "",
|
|
11
|
+
print_errors: bool = False
|
|
12
|
+
):
|
|
13
|
+
"""
|
|
14
|
+
Prints any list, dict, string, float or integer as a json
|
|
15
|
+
|
|
16
|
+
Args:
|
|
17
|
+
var (any, optional): Variable to be printed. Defaults to "".
|
|
18
|
+
print_errors (bool, optional): Flag to indicate whether to print errors. Defaults to False.
|
|
19
|
+
"""
|
|
20
|
+
print_debug(var, print_errors)
|
|
21
|
+
|
|
22
|
+
####
|
|
23
|
+
# Prints message in stdout
|
|
24
|
+
#########################################################################################
|
|
25
|
+
|
|
26
|
+
def print_stdout(
|
|
27
|
+
message: str = ""
|
|
28
|
+
)-> None:
|
|
29
|
+
"""
|
|
30
|
+
Prints message in stdout
|
|
31
|
+
|
|
32
|
+
Args:
|
|
33
|
+
message (str, optional): Message to be printed. Defaults to "".
|
|
34
|
+
|
|
35
|
+
Returns:
|
|
36
|
+
None
|
|
37
|
+
"""
|
|
38
|
+
print(message)
|
|
39
|
+
|
|
40
|
+
####
|
|
41
|
+
# Prints message in stderr
|
|
42
|
+
#########################################################################################
|
|
43
|
+
|
|
44
|
+
def print_stderr(
|
|
45
|
+
message: str = ""
|
|
46
|
+
)-> None:
|
|
47
|
+
"""
|
|
48
|
+
Prints message in stderr
|
|
49
|
+
|
|
50
|
+
Args:
|
|
51
|
+
message (str, optional): Message to be printed. Defaults to "".
|
|
52
|
+
|
|
53
|
+
Returns:
|
|
54
|
+
None
|
|
55
|
+
"""
|
|
56
|
+
print(message, file=sys.stderr)
|
|
57
|
+
|
|
58
|
+
####
|
|
59
|
+
# Prints dictionary in formatted json string.
|
|
60
|
+
#########################################################################################
|
|
61
|
+
|
|
62
|
+
def print_debug(
|
|
63
|
+
var = "",
|
|
64
|
+
print_errors: bool = False
|
|
65
|
+
)-> None:
|
|
66
|
+
"""
|
|
67
|
+
Prints any list, dict, string, float or integer as a json
|
|
68
|
+
|
|
69
|
+
Args:
|
|
70
|
+
var: Variable to be printed.
|
|
71
|
+
print_errors (bool, optional): Whether to print errors. Defaults to False.
|
|
72
|
+
|
|
73
|
+
Returns:
|
|
74
|
+
None
|
|
75
|
+
"""
|
|
76
|
+
try:
|
|
77
|
+
debug_json = json.dumps(var, indent=4)
|
|
78
|
+
print_stdout(debug_json)
|
|
79
|
+
except json.JSONDecodeError as e:
|
|
80
|
+
if print_errors:
|
|
81
|
+
print_stderr(f"debug_dict: Failed to dump. Error: {e}")
|
|
82
|
+
except Exception as e:
|
|
83
|
+
if print_errors:
|
|
84
|
+
print_stderr(f"debug_dict: Unexpected error: {e}")
|
|
85
|
+
|
|
86
|
+
####
|
|
87
|
+
# Add new line to log file
|
|
88
|
+
#########################################################################################
|
|
89
|
+
def logger(
|
|
90
|
+
log_file: str = "",
|
|
91
|
+
message: str = "",
|
|
92
|
+
log_level: str = "",
|
|
93
|
+
add_date: bool = True,
|
|
94
|
+
print_errors: bool = False
|
|
95
|
+
) -> bool:
|
|
96
|
+
'''
|
|
97
|
+
Add new line to log file
|
|
98
|
+
|
|
99
|
+
Args:
|
|
100
|
+
log_file (str): Path to the log file.
|
|
101
|
+
message (str): Message to be added to the log.
|
|
102
|
+
log_level (str): Log level, if applicable. Defaults to an empty string.
|
|
103
|
+
add_date (bool): Whether to add the current date and time to the log entry. Defaults to True.
|
|
104
|
+
print_errors (bool): Whether to print errors. Defaults to False.
|
|
105
|
+
|
|
106
|
+
Returns:
|
|
107
|
+
bool: True if the log entry was successfully added, False otherwise.
|
|
108
|
+
'''
|
|
109
|
+
from .general import now
|
|
110
|
+
|
|
111
|
+
try:
|
|
112
|
+
if not os.path.exists(log_file):
|
|
113
|
+
with open(log_file, 'w') as file:
|
|
114
|
+
pass # Creates an empty file
|
|
115
|
+
elif not os.access(log_file, os.W_OK):
|
|
116
|
+
if print_errors:
|
|
117
|
+
print_stderr(f"Log file '{log_file}' is not writable.")
|
|
118
|
+
return False
|
|
119
|
+
|
|
120
|
+
with open(log_file, 'a') as file:
|
|
121
|
+
final_message = ""
|
|
122
|
+
|
|
123
|
+
if add_date:
|
|
124
|
+
final_message += now() + " "
|
|
125
|
+
if log_level != "":
|
|
126
|
+
final_message += "[" + log_level + "] "
|
|
127
|
+
|
|
128
|
+
final_message += message + "\n"
|
|
129
|
+
|
|
130
|
+
file.write(final_message)
|
|
131
|
+
|
|
132
|
+
return True
|
|
133
|
+
|
|
134
|
+
except Exception as e:
|
|
135
|
+
if print_errors:
|
|
136
|
+
print_stderr(f"An error occurred while appending to the log: {e}")
|
|
137
|
+
return False
|