Terarea  2
The automation project
Loading...
Searching...
No Matches
constants.py
Go to the documentation of this file.
1"""_summary_
2 This file contains the constants for the actions program.
3 These are used to standardise error logging and other sections of the program.
4"""
5# ---------------------------------- Imports ----------------------------------
6from typing import Union
7import operator
8from datetime import datetime, date
9from .secrets import Secrets
10
11# ---------------------------------- log type ----------------------------------
12
13TYPE_UNKNOWN = "UNKNOWN LOGGING TYPE"
14TYPE_API = "API"
15TYPE_SERVICE = "SERVICE"
16TYPE_SERVICE_TRIGGER = "SERVICE TRIGGER"
17TYPE_SERVICE_ACTION = "SERVICE ACTION"
18TYPE_ACTION = "ACTION"
19TYPE_UNDEFINED = "UNDEFINED"
20TYPE_MISMATCH = "MISMATCH"
21TYPE_BEFORE_ASSIGNEMENT = "REFERENCED BEFORE ASSIGNEMENT"
22TYPE_DIV_ZERO = "DIVISION BY ZERO"
23TYPE_SYNTAX_ERROR = "SYNTAX ERROR"
24TYPE_RUNTIME_ERROR = "RUNTIME ERROR"
25TYPE_INCOMPARABLE = "INCOMPARABLE TYPES"
26TYPE_OVERFLOW = "VALUE OVERFLOW"
27TYPE_UNDERFLOW = "VALUE UNDERFLOW"
28
29# -------------------------------- Error codes --------------------------------
30
31CODE_UNKNOWN = -1
32CODE_INFO = 0
33CODE_SUCCESS = 1
34CODE_DEBUG = 2
35CODE_WARNING = 3
36CODE_ERROR = 4
37CODE_CRITICAL = 5
38CODE_FATAL = 6
39
40# -------------------------------- Error level --------------------------------
41
42LEVEL_UNKNOWN = "UNKNOWN"
43LEVEL_INFO = "INFO"
44LEVEL_SUCCESS = "SUCCESS"
45LEVEL_DEBUG = "DEBUG"
46LEVEL_WARNING = "WARNING"
47LEVEL_ERROR = "ERROR"
48LEVEL_CRITICAL = "CRITICAL"
49LEVEL_FATAL = "FATAL"
50
51# ------------------------------- Error messages -------------------------------
52
53MSG_UNKNOWN = "Unknown: Operation executed with unknown status."
54MSG_INFO = "Information: Operation executed without any issues."
55MSG_SUCCESS = "Success: Operation completed successfully."
56MSG_DEBUG = "Debug: Tracking detailed operational data for diagnostics."
57MSG_WARNING = "Warning: Potential issue detected. Review is recommended."
58MSG_ERROR = "Error: Operation could not be completed successfully."
59MSG_CRITICAL = "Critical: Immediate attention required to prevent severe impact."
60MSG_FATAL = "Fatal: System failure imminent. Immediate intervention necessary."
61
62# ----------------------------- Error equivalence -----------------------------
63
64LOG_EQUIVALENCE = {
65 CODE_UNKNOWN: LEVEL_UNKNOWN,
66 CODE_INFO: LEVEL_INFO,
67 CODE_SUCCESS: LEVEL_SUCCESS,
68 CODE_DEBUG: LEVEL_DEBUG,
69 CODE_WARNING: LEVEL_WARNING,
70 CODE_ERROR: LEVEL_ERROR,
71 CODE_CRITICAL: LEVEL_CRITICAL,
72 CODE_FATAL: LEVEL_FATAL,
73}
74
75LOG_MESSAGE_EQUIVALENCE = {
76 CODE_UNKNOWN: MSG_UNKNOWN,
77 CODE_INFO: MSG_INFO,
78 CODE_SUCCESS: MSG_SUCCESS,
79 CODE_DEBUG: MSG_DEBUG,
80 CODE_WARNING: MSG_WARNING,
81 CODE_ERROR: MSG_ERROR,
82 CODE_CRITICAL: MSG_CRITICAL,
83 CODE_FATAL: MSG_FATAL,
84}
85
86# -------------------------------- List checks --------------------------------
87
88LIST_TYPE = [
89 TYPE_UNKNOWN,
90 TYPE_API,
91 TYPE_SERVICE,
92 TYPE_SERVICE_TRIGGER,
93 TYPE_SERVICE_ACTION,
94 TYPE_ACTION,
95 TYPE_UNDEFINED,
96 TYPE_MISMATCH,
97 TYPE_BEFORE_ASSIGNEMENT,
98 TYPE_DIV_ZERO,
99 TYPE_SYNTAX_ERROR,
100 TYPE_RUNTIME_ERROR,
101 TYPE_INCOMPARABLE,
102 TYPE_OVERFLOW,
103 TYPE_UNDERFLOW
104]
105
106LIST_CODE = [
107 CODE_UNKNOWN,
108 CODE_INFO,
109 CODE_SUCCESS,
110 CODE_DEBUG,
111 CODE_WARNING,
112 CODE_ERROR,
113 CODE_CRITICAL,
114 CODE_FATAL
115]
116
117LIST_LEVEL_INFO = [
118 LEVEL_UNKNOWN,
119 LEVEL_INFO,
120 LEVEL_SUCCESS,
121 LEVEL_DEBUG,
122 LEVEL_WARNING,
123 LEVEL_ERROR,
124 LEVEL_CRITICAL,
125 LEVEL_FATAL
126]
127
128LIST_MSG = [
129 MSG_UNKNOWN,
130 MSG_INFO,
131 MSG_SUCCESS,
132 MSG_DEBUG,
133 MSG_WARNING,
134 MSG_ERROR,
135 MSG_CRITICAL,
136 MSG_FATAL
137]
138
139# ---------------------------- Operator equivalence ----------------------------
140
141
142def _spaceship(a, b) -> int:
143 """Compares two values and returns:
144 -1 if a < b
145 0 if a == b
146 1 if a > b
147
148 Args:
149 a: The first value to compare.
150 b: The second value to compare.
151
152 Returns:
153 int: -1, 0, or 1 based on the comparison.
154 """
155 if a < b:
156 return -1
157 if a == b:
158 return 0
159 return 1
160
161
162OPERATOR_EXCHANGE = {
163 "==": operator.eq,
164 "===": operator.eq,
165 "=": operator.eq, # Bash string equality
166 "eq": operator.eq,
167 "-eq": operator.eq,
168 "!=": operator.ne,
169 "<>": operator.ne, # Not equal in SQL-like contexts
170 "ne": operator.ne,
171 "-ne": operator.ne,
172 "<": operator.lt,
173 "lt": operator.lt,
174 "-lt": operator.lt,
175 ">": operator.gt,
176 "gt": operator.gt,
177 "-gt": operator.gt,
178 "<=": operator.le,
179 "le": operator.le,
180 "-le": operator.le,
181 ">=": operator.ge,
182 "ge": operator.ge,
183 "-ge": operator.ge,
184 "<=>": _spaceship, # Custom spaceship operator
185 "equal to": operator.eq,
186 "less than": operator.lt,
187 "not equal to": operator.ne,
188 "greater than": operator.gt,
189 "less than or equal to": operator.le,
190 "greater than or equal to": operator.ge,
191}
192
193# ---------------------------------- Secrets ----------------------------------
194SECRETS_EQUIVALENCE = {
195 "secrets.now": Secrets.now_server,
196 "secrets.current_date": Secrets.current_date,
197 "secrets.current_time": Secrets.current_time,
198 "secrets.now_utc": Secrets.now_utc,
199 "secrets.current_date_utc": Secrets.current_date_utc,
200 "secrets.current_time_utc": Secrets.current_time_utc,
201 "secrets.now_server": Secrets.now_server,
202 "secrets.current_date_server": Secrets.current_date_server,
203 "secrets.current_time_server": Secrets.current_time_server,
204 "secret.now": Secrets.now_server,
205 "secret.current_date": Secrets.current_date,
206 "secret.current_time": Secrets.current_time,
207 "secret.now_utc": Secrets.now_utc,
208 "secret.current_date_utc": Secrets.current_date_utc,
209 "secret.current_time_utc": Secrets.current_time_utc,
210 "secret.now_server": Secrets.now_server,
211 "secret.current_date_server": Secrets.current_date_server,
212 "secret.current_time_server": Secrets.current_time_server,
213 "now": Secrets.now_server,
214 "current_date": Secrets.current_date,
215 "current_time": Secrets.current_time,
216 "now_utc": Secrets.now_utc,
217 "current_date_utc": Secrets.current_date_utc,
218 "current_time_utc": Secrets.current_time_utc,
219 "now_server": Secrets.now_server,
220 "current_date_server": Secrets.current_date_server,
221 "current_time_server": Secrets.current_time_server
222}
223
224
225# --------------------------------- Data types ---------------------------------
226
227CONTENT_TYPE_KEY = "type"
228CONTENT_KEY = "content"
229
230CONTENT_TYPES_JSON = {"application/json", "application/ld+json"}
231CONTENT_TYPES_TEXT = {
232 "text/html",
233 "text/plain",
234 "text/csv",
235 "text/xml",
236 "text/css"
237}
238CONTENT_TYPES_XML = {"application/xml", "application/xhtml+xml", "text/xml"}
239CONTENT_TYPES_BINARY = {
240 "application/octet-stream",
241 "application/pdf",
242 "application/zip",
243 "application/x-gzip",
244 "application/x-tar",
245 "application/x-7z-compressed",
246 "application/x-rar-compressed",
247 "application/x-bzip2",
248 "application/x-xz",
249 "application/x-lzip",
250 "application/x-lzma",
251 "application/x-lzop",
252 "application/x-snappy-framed",
253 "application/xz",
254 "application/x-arj",
255 "application/x-cpio",
256 "application/x-shar",
257 "application/x-compress",
258 "application/x-ace",
259 "application/x-stuffit",
260 "application/x-stuffitx",
261 "application/x-iso9660-image",
262 "application/x-nrg",
263 "application/x-gear",
264 "application/x-dms",
265 "application/x-cfs-compressed",
266 "application/x-astrotite-afa",
267 "application/x-squeeze",
268 "application/x-lzh-compressed",
269 "application/x-lha",
270 "application/x-lrzip",
271 "application/x-lrzip-compressed-tar",
272 "application/x-arc",
273 "application/x-ear",
274 "application/x-war",
275 "application/x-cab",
276 "application/x-msi",
277 "application/x-alz",
278 "application/x-ar",
279 "application/x-deb",
280 "application/x-rpm",
281 "application/x-sis",
282 "application/x-apk",
283 "application/x-ipk",
284 "application/x-xpi",
285 "application/x-java-archive",
286 "application/x-webarchive",
287 "application/x-b1",
288 "application/x-b6z",
289 "application/x-cbr",
290 "application/x-cb7",
291 "application/x-cbt",
292 "application/x-cbz",
293 "application/x-cba",
294 "application/java-archive",
295 "application/x-shockwave-flash",
296 "application/x-www-form-urlencoded",
297 "application/vnd.ms-excel",
298 "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",
299 "application/vnd.ms-powerpoint",
300 "application/vnd.openxmlformats-officedocument.presentationml.presentation",
301 "application/msword",
302 "application/vnd.openxmlformats-officedocument.wordprocessingml.document",
303 "application/vnd.android.package-archive"
304}
305CONTENT_TYPES_AUDIO = {
306 "audio/midi",
307 "audio/mpeg",
308 "audio/webm",
309 "audio/ogg",
310 "audio/wav",
311 "audio/flac",
312 "audio/aac",
313 "audio/mp4",
314 "audio/opus",
315 "audio/x-ms-wma",
316 "audio/vnd.rn-realaudio"
317}
318CONTENT_TYPES_IMAGES = {
319 "image/gif",
320 "image/jpeg",
321 "image/png",
322 "image/webp",
323 "image/svg+xml",
324 "image/bmp",
325 "image/vnd.microsoft.icon",
326 "image/tiff",
327 "image/x-icon",
328 "image/vnd.djvu"
329}
330CONTENT_TYPES_VIDEO = {
331 "video/mpeg",
332 "video/mp4",
333 "video/quicktime",
334 "video/x-ms-wmv",
335 "video/x-msvideo",
336 "video/x-flv",
337 "video/webm"
338}
339
340# ---------------- Compiled response nodes for the request info ----------------
341RESPONSE_NODE_BODY_KEY = "body"
342RESPONSE_NODE_BODY_TYPE_KEY = "body_type"
343RESPONSE_NODE_HEADERS_KEY = "headers"
344RESPONSE_NODE_HEADERS_TYPE_KEY = "headers_type"
345RESPONSE_NODE_ENCODING_KEY = "encoding"
346RESPONSE_NODE_HISTORY_KEY = "history"
347RESPONSE_NODE_COOKIES_KEY = "cookies"
348RESPONSE_NODE_ELAPSED_KEY = "elapsed"
349RESPONSE_NODE_REASON_KEY = "reason"
350RESPONSE_NODE_URL_KEY = "url"
351RESPONSE_NODE_METHOD_KEY = "method"
352RESPONSE_NODE_STATUS_CODE_KEY = "status_code"
353
354RESPONSE_NODE_KEY_EQUIVALENCE = {
355 "elapsed": RESPONSE_NODE_ELAPSED_KEY,
356 "url": RESPONSE_NODE_URL_KEY,
357 "urls": RESPONSE_NODE_URL_KEY,
358 "reason": RESPONSE_NODE_REASON_KEY,
359 "reasons": RESPONSE_NODE_REASON_KEY,
360 "encoding": RESPONSE_NODE_ENCODING_KEY,
361 "encodings": RESPONSE_NODE_ENCODING_KEY,
362 "cookie": RESPONSE_NODE_COOKIES_KEY,
363 "cookies": RESPONSE_NODE_COOKIES_KEY,
364 "header": RESPONSE_NODE_HEADERS_KEY,
365 "headers": RESPONSE_NODE_HEADERS_KEY,
366 "method": RESPONSE_NODE_METHOD_KEY,
367 "methods": RESPONSE_NODE_METHOD_KEY,
368 "history": RESPONSE_NODE_HISTORY_KEY,
369 "historie": RESPONSE_NODE_HISTORY_KEY,
370 "historys": RESPONSE_NODE_HISTORY_KEY,
371 "histore": RESPONSE_NODE_HISTORY_KEY,
372 "histores": RESPONSE_NODE_HISTORY_KEY,
373 "histories": RESPONSE_NODE_HISTORY_KEY,
374 "body_type": RESPONSE_NODE_BODY_TYPE_KEY,
375 "body_types": RESPONSE_NODE_BODY_TYPE_KEY,
376 "bodys_type": RESPONSE_NODE_BODY_TYPE_KEY,
377 "bodies_type": RESPONSE_NODE_BODY_TYPE_KEY,
378 "bodies_types": RESPONSE_NODE_BODY_TYPE_KEY,
379 "header_type": RESPONSE_NODE_HEADERS_TYPE_KEY,
380 "headers_type": RESPONSE_NODE_HEADERS_TYPE_KEY,
381 "header_types": RESPONSE_NODE_HEADERS_TYPE_KEY,
382 "headers_types": RESPONSE_NODE_HEADERS_TYPE_KEY,
383 "body": RESPONSE_NODE_BODY_KEY,
384 "bodys": RESPONSE_NODE_BODY_KEY,
385 "bodies": RESPONSE_NODE_BODY_KEY,
386 "bodie": RESPONSE_NODE_BODY_KEY,
387 "status_code": RESPONSE_NODE_STATUS_CODE_KEY,
388 "status_codes": RESPONSE_NODE_STATUS_CODE_KEY,
389 "statuses_codes": RESPONSE_NODE_STATUS_CODE_KEY,
390 "statuses_code": RESPONSE_NODE_STATUS_CODE_KEY,
391 "statuss_code": RESPONSE_NODE_STATUS_CODE_KEY,
392 "statuss_codes": RESPONSE_NODE_STATUS_CODE_KEY
393
394}
395
396# --------------------------------- Functions ---------------------------------
397
398
399def check_if_oauth_is_valid(oauth_token: str) -> bool:
400 from datetime import datetime
401
402 current_time = datetime.now().isoformat(sep="T", timespec="seconds")
403 expiration = datetime.fromisoformat(oauth_token)
404
405 if current_time >= expiration:
406 return False
407 return True
408
409
410# -------------------- Bruteforce type conversion attempts --------------------
411
412BRUTEFORCE_DATETIME_FORMATS = [
413 "%Y-%m-%dT%H:%M:%S.%f%z",
414 "%Y-%m-%dT%H:%M:%S%z",
415 "%Y-%m-%dT%H:%M:%S",
416 "%Y-%m-%d %H:%M:%S"
417]
418
419BRUTEFORCE_DATE_FORMATS = [
420 "%Y-%m-%d",
421 "%d/%m/%Y",
422 "%m/%d/%Y"
423]
424
425
426def detect_and_convert(value: str) -> Union[int, float, bool, None, datetime, date, str]:
427 """
428 Detects the type of the input string and converts it to the appropriate Python type.
429
430 Args:
431 value (str): The string to be converted.
432
433 Returns:
434 int, float, bool, None, datetime, date, or str: The converted value based on its detected type.
435 """
436 if isinstance(value, str) is False:
437 return value
438 value = value.strip()
439 lvalue = value.lower()
440 if lvalue == "none":
441 return None
442 if lvalue == "true":
443 return True
444 if lvalue == "false":
445 return False
446 try:
447 return int(value)
448 except ValueError:
449 pass
450 try:
451 return float(value)
452 except ValueError:
453 pass
454
455 for fmt in BRUTEFORCE_DATETIME_FORMATS:
456 try:
457 return datetime.strptime(value, fmt)
458 except ValueError:
459 continue
460
461 for fmt in BRUTEFORCE_DATE_FORMATS:
462 try:
463 return datetime.strptime(value, fmt).date()
464 except ValueError:
465 continue
466
467 return value
bool check_if_oauth_is_valid(str oauth_token)
Definition constants.py:399
Union[int, float, bool, None, datetime, date, str] detect_and_convert(str value)
Definition constants.py:426