j8yte-toolbox 1.0.0.dev0__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.

Potentially problematic release.


This version of j8yte-toolbox might be problematic. Click here for more details.

j8yte_toolbox/core.py ADDED
@@ -0,0 +1,1783 @@
1
+ # pylint: disable=too-many-lines
2
+ """
3
+ Author : Julien Blanc
4
+ Mail : j8ytes@gmail.com
5
+ Creation : 30/10/2026
6
+ License : MIT
7
+ """
8
+
9
+ ############## IMPORT MODULES
10
+ import base64
11
+ import json
12
+ import os
13
+ import re
14
+ import smtplib
15
+ import socket
16
+ import sys
17
+ import traceback
18
+ from datetime import datetime
19
+ from email.mime.multipart import MIMEMultipart
20
+ from email.mime.text import MIMEText
21
+ from email.utils import formatdate
22
+ from pathlib import Path
23
+ from urllib.parse import quote
24
+ from zoneinfo import ZoneInfo
25
+
26
+ import hvac
27
+ import pandas as pd
28
+ import requests
29
+ import xlsxwriter
30
+ from mailgun.client import Client as mgclient
31
+ from requests.adapters import HTTPAdapter
32
+ from urllib3 import disable_warnings
33
+ from urllib3.exceptions import InsecureRequestWarning
34
+ from urllib3.util import Retry
35
+ from xhtml2pdf import pisa
36
+
37
+
38
+ ############## API METHODS
39
+ class Api:
40
+ """
41
+ API requests methods
42
+ """
43
+ ###################### PRIVATE METHODS
44
+
45
+ ############## RETURN ADAPTER CONFIGURATION FOR REQUEST SESSION
46
+ @staticmethod
47
+ def __create_session(
48
+ retries: int,
49
+ protocol: str = "https://"
50
+ ) -> requests.Session:
51
+ """
52
+ Private method. Create a session with an adapter configuration for requests.
53
+
54
+ :param retries: Number of retries before timeout.
55
+ :type retries: int
56
+
57
+ :returns: Custom request session.
58
+ :rtype: Session
59
+ """
60
+ # DISABLE SSL WARNINGS MESSAGES
61
+ disable_warnings(InsecureRequestWarning)
62
+
63
+ # SET RETRIES PARAMETERS FOR HTTP(S) REQUESTS
64
+ retry_strategy = Retry(
65
+ total=retries,
66
+ status_forcelist=[429, 500, 502, 503, 504],
67
+ allowed_methods=frozenset(['HEAD','GET', 'OPTIONS'])
68
+ )
69
+
70
+ adapter = HTTPAdapter(max_retries=retry_strategy)
71
+
72
+ http = requests.Session()
73
+ http.mount(protocol, adapter)
74
+
75
+ return http
76
+
77
+ ###################### PUBLIC METHODS
78
+
79
+ ############## CREATES BASIC AUTH HTTP TOKEN
80
+ @staticmethod
81
+ def basic_auth_header(
82
+ user: str,
83
+ password: str
84
+ ) -> str:
85
+ """
86
+ Encrypt user:password to base64 for basic HTTP auth.
87
+
88
+ :param user: User
89
+ :type user: str
90
+ :param password: Password
91
+ :type password: str
92
+
93
+ :returns: Basic HTTP base64 encrypted token.
94
+ :rtype: str
95
+ """
96
+ cred_txt = f"{user}:{password}"
97
+ cred_bytes = cred_txt.encode('ascii')
98
+ cred_base64 = base64.b64encode(cred_bytes)
99
+
100
+ return cred_base64.decode('ascii')
101
+
102
+ ############## HTTP API GET
103
+ @staticmethod
104
+ def request( # pylint: disable=too-many-arguments
105
+ method: str,
106
+ host: str,
107
+ entry_point: str,
108
+ *,
109
+ headers: dict | None,
110
+ protocol: str = "https://",
111
+ params: dict | None = None,
112
+ auth: str | None = None,
113
+ retries: int=3,
114
+ data: dict | None = None,
115
+ json_data: dict | None = None,
116
+ timeout: float = 2.0,
117
+ verify: bool = False
118
+ ) -> None | dict:
119
+ """
120
+ Execute HTTP request. Returns None and prints error if a problem occurs.
121
+
122
+ :param method: HTTP method (can be 'GET', 'POST', 'PUT', 'PATCH' or 'DELETE')
123
+ :param method: str
124
+ :param url: URL to send request
125
+ :type url: str
126
+ :param headers: HTTP Headers.
127
+ :type headers: dict
128
+ :param protocol: (optional) Default to 'https://'
129
+ :type protocol: str
130
+ :param params: (optional) Dict or bytes to be sent in the query
131
+ string for the Request.
132
+ :type params: dict
133
+ :param auth: (optional) Auth tuple or callable to enable
134
+ Basic/Digest/Custom HTTP Auth.
135
+ :type auth: str
136
+ :param retries: (optional) Number of retries before timeout.
137
+ :type retries: int
138
+ :param data: (optional) Data to send.
139
+ :type data: dict
140
+ :param json_data: JSON body to send.
141
+ :type json_data: dict
142
+ :param timeout: How long to wait for the server to send
143
+ data before giving up.
144
+ :type float:
145
+ :param verify: Check certificate.
146
+ :type verify: bool
147
+
148
+ :returns: JSON response.
149
+ :rtype: dict | None
150
+ """
151
+ try:
152
+ http = Api.__create_session(retries=retries, protocol=protocol)
153
+ response = http.request(
154
+ method=method.upper(),
155
+ url=protocol + host + entry_point,
156
+ headers=headers,
157
+ params=params,
158
+ data=data,
159
+ auth=auth,
160
+ json=json_data,
161
+ timeout=timeout,
162
+ verify=verify
163
+ )
164
+
165
+ http.close()
166
+
167
+ return response
168
+
169
+ except requests.exceptions.Timeout:
170
+ print(
171
+ f"Error: The timeout has been exceeded ({timeout}s) for "
172
+ f"{protocol + host + entry_point}"
173
+ )
174
+ return None
175
+
176
+ except requests.exceptions.ConnectionError:
177
+ print(
178
+ f"Error: Connection error for "
179
+ f"{protocol + host + entry_point}"
180
+ )
181
+ return None
182
+
183
+ except Exception as e: # pylint: disable=broad-except
184
+ print(str(e))
185
+ print(traceback.format_exc())
186
+ return None
187
+
188
+
189
+ ############## BASIC METHODS
190
+ class Base:
191
+ """
192
+ Low-level methods
193
+ """
194
+ ############## CONVERT STRING BOOL TO NATIVE BOOL
195
+ @staticmethod
196
+ def str2bool(value: str) -> bool:
197
+ """
198
+ Convert a boolean into a string to a native boolean.
199
+
200
+ :param value: Value to convert.
201
+ :type value: str
202
+
203
+ :returns: Boolean from string.
204
+ :rtype: bool
205
+ """
206
+ return value.lower() in ("yes", "true", "t", "1")
207
+
208
+ ############## RETURNS ISO 8601 DATE WITH SUMMER/WINTER OFFSET
209
+ @staticmethod
210
+ def get_iso_8601(
211
+ date: str,
212
+ timezone: str='Europe/Paris'
213
+ ) -> str:
214
+ """
215
+ Returns ISO 8601 date with Summer/Winter offset for specified timezone.
216
+
217
+ :param date: ISO 8601 UTC formatted date (YYYY-MM-DDTHH:MM:SSZ).
218
+ :type date: str
219
+ :param timezone: Default to 'Europe/Paris'
220
+ :type timezone: str
221
+
222
+ :returns: Date with offset.
223
+ :rtype: str
224
+ """
225
+ try:
226
+ datetime_object = datetime.strptime(date, '%Y-%m-%d%H:%M:%SZ')
227
+ except ValueError:
228
+ datetime_object = datetime.strptime(date, '%Y-%m-%dT%H:%M:%SZ')
229
+
230
+ offset = int(ZoneInfo(timezone).dst(datetime_object).seconds / 3600)
231
+ new_date = f"{datetime_object.strftime('%Y-%m-%dT%H:%M:%S')}+0{offset + 1}:00"
232
+
233
+ return new_date
234
+
235
+ ############## CONVERTS A BINARY FILE TO BASE64 INTO TXT FILE
236
+ @staticmethod
237
+ def bin_to_base64(
238
+ src: str,
239
+ dest: str
240
+ ) -> bool:
241
+ """
242
+ Converts a binary file to base64 into a txt file.
243
+
244
+ :param src: Binary source file
245
+ :type src: str
246
+ :param dest: Txt destination file
247
+ :type dest: str
248
+
249
+ :returns: True if successful
250
+ :rtype: bool
251
+ """
252
+ exported = None
253
+
254
+ try:
255
+ with open(src, "rb") as f:
256
+ b64encoded = base64.b64encode(f.read())
257
+ exported = b64encoded.decode()
258
+ f.close()
259
+
260
+ with open(dest, "w", encoding="utf-8") as f:
261
+ f.write(exported)
262
+ f.close()
263
+
264
+ except Exception: # pylint: disable=broad-except
265
+ return False
266
+
267
+ return True
268
+
269
+ ############## CONVERTS A BINARY FILE TO BASE64 INTO TXT FILE
270
+ @staticmethod
271
+ def base64_to_bin(
272
+ text: str,
273
+ dest: str
274
+ ) -> bool:
275
+ """
276
+ Converts a base64 string to binary file.
277
+
278
+ :param src: Txt source file
279
+ :type src: str
280
+ :param dest: Binary destination file
281
+ :type dest: str
282
+
283
+ :returns: True if successful
284
+ :rtype: bool
285
+ """
286
+ try:
287
+ with open(dest, "wb") as f:
288
+ f.write(base64.b64decode(text))
289
+ f.close()
290
+
291
+ except Exception: # pylint: disable=broad-except
292
+ return False
293
+
294
+ return True
295
+
296
+
297
+ ############## EXCEL METHODS
298
+ class Excel:
299
+ """
300
+ Excel methods
301
+ """
302
+ ############## EXPORT TO XLSX
303
+ @staticmethod
304
+ def export(
305
+ file: str,
306
+ data: list[dict]
307
+ ) -> bool:
308
+ """
309
+ Export data to formatted XLSX file.
310
+
311
+ :param file: Exported file
312
+ :type file: str
313
+ :param data: Data list
314
+ :type data: list[dict]
315
+
316
+ :returns: True if successful
317
+ :rtype: bool
318
+ """
319
+ try:
320
+ workbook = xlsxwriter.Workbook(file)
321
+
322
+ ############## EXCEL TEMPLATE STUFF
323
+
324
+ # Cell format
325
+ header = workbook.add_format({
326
+ 'bold': True,
327
+ 'bg_color': '#0086C0',
328
+ 'font_color': 'white',
329
+ 'align': 'right',
330
+ 'valign': 'vcenter',
331
+ 'border': 2,
332
+ 'border_color': 'white',
333
+ 'font_name': 'Calibri'
334
+ })
335
+
336
+ base_cell = workbook.add_format({
337
+ 'bold': True,
338
+ 'bg_color': '#D9D9D9',
339
+ 'font_color': '#808080',
340
+ 'align': 'right',
341
+ 'valign': 'vcenter',
342
+ 'border': 2,
343
+ 'border_color': 'white',
344
+ 'font_name': 'Calibri'
345
+ })
346
+
347
+ empty_cell = workbook.add_format({
348
+ 'bold': True,
349
+ 'bg_color': 'white',
350
+ 'font_color': 'white',
351
+ 'align': 'right',
352
+ 'valign': 'vcenter',
353
+ 'border': 2,
354
+ 'border_color': 'white',
355
+ 'font_name': 'Calibri'
356
+ })
357
+
358
+ # Columns
359
+ col = [
360
+ 'A',
361
+ 'B',
362
+ 'C',
363
+ 'D',
364
+ 'E',
365
+ 'F',
366
+ 'G',
367
+ 'H',
368
+ 'I',
369
+ 'J',
370
+ 'K',
371
+ 'L',
372
+ 'M',
373
+ 'N',
374
+ 'O',
375
+ 'P',
376
+ 'Q',
377
+ 'R',
378
+ 'S',
379
+ 'T',
380
+ 'U',
381
+ 'V',
382
+ 'W',
383
+ 'X',
384
+ 'Y',
385
+ 'Z'
386
+ ]
387
+
388
+ # Adds tab in file
389
+ worksheet = workbook.add_worksheet("Data")
390
+
391
+ # White background
392
+ worksheet.set_column('A:ZZ', cell_format=empty_cell)
393
+
394
+ # Line height
395
+ for index in range(1, len(data) + 1):
396
+ worksheet.set_row(index, 20)
397
+
398
+ # Headers
399
+ col_index = 0
400
+
401
+ if len(data) > 0:
402
+ for attr in data[0]:
403
+ worksheet.write(col[col_index] + '1', attr + " ", header)
404
+ col_index += 1
405
+
406
+ # Set column filter
407
+ worksheet.autofilter('A1:' + col[col_index - 1] + '1')
408
+
409
+ line_count = 2
410
+
411
+ # Parse data
412
+ for item in data:
413
+
414
+ col_index = 0
415
+
416
+ # Write cells
417
+ for attr in item:
418
+
419
+ worksheet.write(
420
+ col[col_index] + str(line_count),
421
+ item[attr],
422
+ base_cell
423
+ )
424
+ col_index += 1
425
+
426
+ line_count +=1
427
+
428
+ # Column size
429
+ worksheet.set_column('A:ZZ', width=40)
430
+
431
+ # Save file
432
+ workbook.close()
433
+
434
+ return True
435
+
436
+ except Exception as e: # pylint: disable=broad-except
437
+ print(e)
438
+ return False
439
+
440
+ ############## IMPORT FROM XLSX
441
+ @staticmethod
442
+ def read(
443
+ file: str,
444
+ sheet: int=0,
445
+ no_headers: bool=False
446
+ ) -> list[dict] | None:
447
+ """
448
+ Import data from XLSX file.
449
+
450
+ :param file: File to import
451
+ :type file: str
452
+ :param sheet: Sheet to load (default to 0 -> first)
453
+ :type sheet: int
454
+ :param no_headers: Set to true if no headers
455
+ :type no_headers: bool
456
+
457
+ :returns: A list[dict] that contains data
458
+ :rtype: list[dict] | None
459
+ """
460
+ try:
461
+ header = None if no_headers else 0
462
+ df = pd.read_excel(file, sheet_name=sheet, header=header)
463
+
464
+ if no_headers:
465
+ df.columns = [f"col_{i:03d}" for i in range(len(df.columns))]
466
+ else:
467
+ df.columns = [re.sub(r'\s{2,}', '', str(col)) for col in df.columns]
468
+
469
+ return df.to_dict(orient="records")
470
+
471
+ except Exception as e: # pylint: disable=broad-except
472
+ print(e)
473
+ return None
474
+
475
+
476
+ ############## HTML METHODS
477
+ class Html:
478
+ """
479
+ Methods for generating HTML.
480
+ """
481
+ ############## PRIVATE - STATIC VARS
482
+ MAPPING_CSS = {
483
+ # OK
484
+ "resolved": "class='ok'",
485
+ "approved": "class='ok'",
486
+ "planned": "class='ok'",
487
+ "ok": "class='ok'",
488
+ "p3": "class='ok'",
489
+ "ready": "class='ok'",
490
+ "completed": "class='ok'",
491
+ "healthy": "class='ok'",
492
+ "protected": "class='ok'",
493
+ "succeeded": "class='ok'",
494
+ "installed": "class='ok'",
495
+ # ONGOING
496
+ "inprogress": "class='ongoing'",
497
+ "in progress": "class='ongoing'",
498
+ "notneeded": "class='ongoing'",
499
+ "not needed": "class='ongoing'",
500
+ "excluded": "class='ongoing'",
501
+ "cab": "class='ongoing'",
502
+ "p4": "class='ongoing'",
503
+ # WARN
504
+ "to be planned": "class='warn'",
505
+ "p2": "class='warn'",
506
+ "to be filled out": "class='warn'",
507
+ "completedwithwarnings":
508
+ "class='warn'",
509
+ "completed with warnings": "class='warn'",
510
+ "required": "class='warn'",
511
+ "notselected": "class='warn'",
512
+ "not selected": "class='warn'",
513
+ "warning": "class='warn'",
514
+ # CRIT
515
+ "canceled": "class='crit'",
516
+ "failed": "class='crit'",
517
+ "nok": "class='crit'",
518
+ "p1": "class='crit'",
519
+ "r-.-": "class='crit'",
520
+ "unknown": "class='crit'",
521
+ "suspended": "class='crit'",
522
+ "not ready": "class='crit'",
523
+ "no agent": "class='crit'",
524
+ "unhealthy": "class='crit'",
525
+ "error": "class='crit'",
526
+ # NONE & TOTAL
527
+ "na": "class='none'",
528
+ "none": "class='none'",
529
+ "no data": "class='none'",
530
+ "empty": "class='none'",
531
+ "total": "class='total'"
532
+ }
533
+
534
+ PATTERNS_REGEX = [
535
+ (
536
+ re.compile(r"^-(?:[0-9]+h [0-9]+m|[0-9]\s[0-9]+h [0-9]+m|[0-9]+h|[0-9]+m)"),
537
+ "class='crit'"
538
+ ),
539
+ (
540
+ re.compile(r"^(?:[0-9]+h [0-9]+m|[0-9]+h|[0-9]+m)"),
541
+ "class='ok'"
542
+ ),
543
+ (
544
+ re.compile(r"^r1.0"),
545
+ "class='ok'"
546
+ ),
547
+ (
548
+ re.compile(r"^r0.[1-9]"),
549
+ "class='warn'"
550
+ ),
551
+ (
552
+ re.compile(r"^r0.0"),
553
+ "class='crit'"
554
+ ),
555
+ ]
556
+
557
+ ############## PRIVATE - PROCESS STRING VALUES TO OUTPUT CSS
558
+ @staticmethod
559
+ def __string_value_to_css(
560
+ value: str,
561
+ empty_blank: bool
562
+ ) -> str:
563
+ """
564
+ Process string value to determine the most appropriate CSS class.
565
+
566
+ :param value: String value to handle
567
+ :type value: str
568
+ :param empty_blank: Forces to return class 'blank' if empty value
569
+ :type empty_blank: str
570
+
571
+ :returns: CSS class
572
+ :rtype: str
573
+ """
574
+ if empty_blank and value == "":
575
+ return "class='blank'"
576
+
577
+ val_lower = value.lower()
578
+
579
+ if val_lower in Html.MAPPING_CSS:
580
+ return Html.MAPPING_CSS[val_lower]
581
+
582
+ if any(x in val_lower for x in ["pending", "deprecation", "retirement"]):
583
+ return "class='warn'"
584
+
585
+ for pattern, css_class in Html.PATTERNS_REGEX:
586
+ if pattern.search(value):
587
+ return css_class
588
+
589
+ return ""
590
+
591
+ ############## PRIVATE - PROCESS NUMERIC VALUES TO OUTPUT CSS
592
+ @staticmethod
593
+ def __numeric_value_to_css(
594
+ value: float | int,
595
+ invert: bool
596
+ ) -> str:
597
+ """
598
+ Process numeric value to determine the most appropriate CSS class.
599
+
600
+ :param value: Numeric value to handle
601
+ :type value: float | int
602
+ :param invert: Invert 'crit' & 'ok'
603
+ :type invert: bool
604
+
605
+ :returns: CSS class
606
+ :rtype: str
607
+ """
608
+ if 80 <= value <= 89:
609
+ return "class='warn'"
610
+
611
+ if value >= 90:
612
+ return "class='ok'" if invert else "class='crit'"
613
+
614
+ return "class='crit'" if invert else "class='ok'"
615
+
616
+ ############## CREATE HTML FOR MAIL
617
+ @staticmethod
618
+ def create_doc(
619
+ body: str,
620
+ layout: str = "A3 landscape",
621
+ *,
622
+ font_size: int = 12
623
+ ) -> str:
624
+ """
625
+ Create HTML document for email usage with CSS template integrated.
626
+
627
+ :param body: HTML contents inside body tags
628
+ :type body: str
629
+ :param layout: (optional) CSS layout, default to 'A3 landscape'
630
+ :type layout: str
631
+ :param font_size: Default font size for all content
632
+ :type font_size: int
633
+
634
+ :returns: Complete HTML document that includes CSS elements
635
+ :rtype: str
636
+ """
637
+ return f"""
638
+ <html>
639
+ <head>
640
+ <style>
641
+ @page {{
642
+ size: {layout};
643
+ margin: 1cm;
644
+ }}
645
+ body {{
646
+ font-family: Tahoma, Arial, sans-serif;
647
+ font-size: {font_size}px;
648
+ }}
649
+ table {{
650
+ width: 100%;
651
+ }}
652
+ thead tr {{
653
+ background-color: #07729C;
654
+ color: white;
655
+ border: 1px solid white;
656
+ }}
657
+ tbody {{
658
+ color: black;
659
+ border: 1px solid white;
660
+ }}
661
+ td, th {{
662
+ padding: 5px;
663
+ border: 2px solid white;
664
+ line-height: auto;
665
+ vertical-align: middle;
666
+ font-size: {font_size}px;
667
+ }}
668
+ td {{
669
+ background-color: #ECECEC;
670
+ color: #494949;
671
+ }}
672
+ td.ok {{
673
+ background-color: #DAFFD0;
674
+ }}
675
+ td.ongoing {{
676
+ background-color: #D0EBFF;
677
+ }}
678
+ td.warn {{
679
+ background-color: #FFF6D0;
680
+ }}
681
+ td.crit {{
682
+ background-color: #FFD0D0;
683
+ }}
684
+ td.none {{
685
+ color: #D0D0D0;
686
+ }}
687
+ td.blank {{
688
+ background-color: white;
689
+ }}
690
+ td.total {{
691
+ color: white;
692
+ background-color: #858585;
693
+ text-align: right;
694
+ font-weight: bold;
695
+ }}
696
+ h1 {{
697
+ font-size: 20px;
698
+ margin-bottom: 10px;
699
+ }}
700
+ h1 a {{
701
+ text-decoration: none;
702
+ }}
703
+ hr {{
704
+ height: 1px;
705
+ border-width: 0;
706
+ }}
707
+ p {{
708
+ margin-top: 0;
709
+ }}
710
+ a {{
711
+ color: #494949;
712
+ }}
713
+ a:hover {{
714
+ color: #0086C0;
715
+ }}
716
+ .title {{
717
+ margin: 0;
718
+ padding: 5px;
719
+ color: #494949;
720
+ }}
721
+ .value {{
722
+ margin: 0;
723
+ padding: 5px;
724
+ font-weight: bold;
725
+ font-size: 20px;
726
+ color: #0086C0;
727
+ }}
728
+ </style>
729
+ </head>
730
+ <body>
731
+ {body}
732
+ </body>
733
+ </html>
734
+ """
735
+
736
+ ############## CREATE HTML TABLE
737
+ @staticmethod
738
+ def create_table(
739
+ data: list[dict],
740
+ empty_blank: bool = False
741
+ ) -> str:
742
+ """
743
+ Create HTML table from a dictionary list.
744
+ Useful with create_html_doc function for formatted tables.
745
+
746
+ :param data: Object that will be transformed into HTML table
747
+ :type data: str
748
+ :param empty_blank: If empty value, apply no specific CSS. Default to False.
749
+ :type empty_blank: bool
750
+
751
+ :returns: HTML table
752
+ :rtype: str
753
+ """
754
+
755
+ # HTML TEMPLATE
756
+ html_table_thead_head = """
757
+ <table cellspacing='0'>
758
+ <thead>
759
+ <tr>
760
+ """
761
+
762
+ html_table_thead_data = ""
763
+
764
+ html_table_thead_end = """
765
+ </tr>
766
+ </thead>
767
+ """
768
+
769
+ html_table_tbody_head = """
770
+ <tbody>
771
+ """
772
+
773
+ html_table_tbody_data = ""
774
+
775
+ html_table_tbody_end = """
776
+ </tbody>
777
+ </table>
778
+ """
779
+
780
+ # KEYS COUNT
781
+ head_keys_count = 0
782
+
783
+ # THEAD
784
+ for key in data[0]:
785
+ html_table_thead_data += f"<th>{key}</th>"
786
+ head_keys_count += 1
787
+
788
+ # TBODY
789
+ for item in data:
790
+ if len(item.keys()) == head_keys_count:
791
+ html_table_tbody_data += "<tr>"
792
+ for key in item:
793
+ html_table_tbody_data += (
794
+ f"<td {Html.color_class(item[key], empty_blank=empty_blank)}>"
795
+ f"{item[key]}</td>"
796
+ )
797
+ html_table_tbody_data += "</tr>"
798
+
799
+ return f"""
800
+ {html_table_thead_head}
801
+ {html_table_thead_data}
802
+ {html_table_thead_end}
803
+ {html_table_tbody_head}
804
+ {html_table_tbody_data}
805
+ {html_table_tbody_end}
806
+ """
807
+
808
+ ############## CREATE HTML SEPARATOR
809
+ @staticmethod
810
+ def create_separator() -> str:
811
+ """
812
+ Create HTML separator compatible with old outlook client.
813
+
814
+ :returns: HTML code
815
+ :rtype: str
816
+ """
817
+ return (
818
+ "<div style='height: 10px; border:0; "
819
+ "border-top: 1px solid #D0D0D0;'></div>"
820
+ )
821
+
822
+ ############## CREATE PIE CHART
823
+ @staticmethod
824
+ def create_pie_chart(
825
+ labels: list[str],
826
+ data: list[int | float],
827
+ colors: list[str],
828
+ size: tuple[int, int],
829
+ *,
830
+ pixel_ratio: float = 0.9,
831
+ quickchart_host: str="quickchart.io"
832
+ ) -> str:
833
+ """
834
+ Returns an HTML image thats points to quickchart.io.
835
+ Useful to integrate charts in mails.
836
+
837
+ :param labels: String list that contains labels
838
+ :type labels: list[str]
839
+ :param data: Integer or float list that contains data
840
+ :type data: list[int|float]
841
+ :param colors: List of hexadecimal colors for
842
+ each section of a pie chart
843
+ :type colors: list[str]
844
+ :param size: Width of chart
845
+ :type size: tuple[int, int]
846
+ :param pixel_ratio: Graph pixel ratio (default to 0.9)
847
+ :type pixel_ratio: float
848
+ :param quickchart_host: Default to quickchart.io
849
+ :type quickchart_host: str
850
+
851
+ :returns: quickchart.io chart
852
+ :rtype: str
853
+ """
854
+ chart_config = {
855
+ "type": "doughnut",
856
+ "data": {
857
+ "labels": labels,
858
+ "datasets": [{
859
+ "data": data,
860
+ "backgroundColor": colors
861
+ }]
862
+ },
863
+ "options": {
864
+ "plugins": {
865
+ "legend": {
866
+ "position": "right"
867
+ },
868
+ "datalabels": {
869
+ "formatter": "function (value, context) { return value + '%'; }"
870
+ }
871
+ },
872
+ "layout": {
873
+ "padding": 10
874
+ }
875
+ }
876
+ }
877
+
878
+ # ENCODE & CREATE IMAGE WITH URL
879
+ width, height = size
880
+ encoded_config = quote(json.dumps(chart_config))
881
+ chart_url = (
882
+ f'https://{quickchart_host}/chart?v=4&devicePixelRatio={pixel_ratio}'
883
+ f'&w={width}&h={height}&bkg=white&c={encoded_config}'
884
+ )
885
+
886
+ return f"<img src='{chart_url}'/>"
887
+
888
+ ############## CREATE BAR CHART
889
+ @staticmethod
890
+ def create_bar_chart( # pylint: disable=too-many-arguments
891
+ label: str,
892
+ xlabels: list[str],
893
+ data: list[int | float],
894
+ color: str,
895
+ size: tuple[int, int],
896
+ *,
897
+ yscale: tuple[int, int] = (-20, 20),
898
+ pixel_ratio: float = 0.9,
899
+ quickchart_host: str="quickchart.io"
900
+ ) -> str:
901
+ """
902
+ Returns an HTML image thats points to quickchart.io.
903
+ Useful to integrate charts in mails.
904
+
905
+ :param label: Label for color
906
+ :type label: str
907
+ :param xlabels: String list that contains labels
908
+ :type xlabels: list[str]
909
+ :param data: Integer or float list that contains data
910
+ :type date: list[int|float],
911
+ :param color: Hexadecimal color for bars
912
+ :type color: str
913
+ :param size: Width of chart
914
+ :type size: tuple[int, int]
915
+ :param yscale: Y axis scale size (default to (-20, 20))
916
+ :type yscale: tuple[int, int]
917
+ :param pixel_ratio: Graph pixel ratio (default to 0.9)
918
+ :type pixel_ratio: float
919
+ :param quickchart_host: Default to quickchart.io
920
+ :type quickchart_host: str
921
+
922
+ :returns: quickchart.io chart
923
+ :rtype: str
924
+ """
925
+ ymin, ymax = yscale
926
+ chart_config = {
927
+ "type": "bar",
928
+ "data": {
929
+ "labels": xlabels,
930
+ "datasets": [
931
+ {
932
+ "label": label,
933
+ "backgroundColor": color,
934
+ "data": data
935
+ }
936
+ ]
937
+ },
938
+ "options": {
939
+ "title": {
940
+ "display": False
941
+ },
942
+ "scales": {
943
+ "y": {
944
+ "offset": True,
945
+ "min": ymin,
946
+ "max": ymax
947
+ }
948
+ },
949
+ "plugins": {
950
+ "datalabels": {
951
+ "formatter": "function (value, context) { return value }"
952
+ }
953
+ },
954
+ "layout": {
955
+ "padding": 10
956
+ }
957
+ }
958
+ }
959
+
960
+ # ENCODE & CREATE IMAGE WITH URL
961
+ width, height = size
962
+ encoded_config = quote(json.dumps(chart_config))
963
+ chart_url = (
964
+ f'https://{quickchart_host}/chart?v=4&devicePixelRatio={pixel_ratio}'
965
+ f'&w={width}&h={height}&bkg=white&c={encoded_config}'
966
+ )
967
+
968
+ return f"<img src='{chart_url}'/>"
969
+
970
+ ############## HTML COLORS
971
+ @staticmethod
972
+ def color_class(
973
+ value: str,
974
+ invert=False,
975
+ empty_blank=False
976
+ ) -> str:
977
+ """
978
+ Returns HTML class from value.
979
+ Must be used with create_html_doc function for formatted tables.
980
+
981
+ :param value: String or numeric value
982
+ :type value: str
983
+ :param invert: For numerical value, invert threshold
984
+ interpretation (default to False)
985
+ :type invert: bool
986
+ :param empty_blank: If empty value, apply no specific CSS. Default to False.
987
+ :type empty_blank: bool
988
+
989
+ :returns: CSS class
990
+ :rtype: str
991
+ """
992
+ if isinstance(value, str):
993
+ return Html.__string_value_to_css(value, empty_blank)
994
+
995
+ if isinstance(value, (float, int)):
996
+ return Html.__numeric_value_to_css(value, invert)
997
+
998
+ return ""
999
+
1000
+
1001
+ ############## JSM METHODS
1002
+ class Jsm:
1003
+ """
1004
+ JSM methods
1005
+ """
1006
+ @staticmethod
1007
+ def form_select(
1008
+ field: str,
1009
+ data: list,
1010
+ return_unknown_if_empty: bool = False
1011
+ ) -> list:
1012
+ """
1013
+ Search data in Jira form's answers list from label name.
1014
+
1015
+ :param field: Label name
1016
+ :type field: str
1017
+ :param data: Jira form's answers list
1018
+ :type data: list
1019
+
1020
+ :returns: Answer
1021
+ :rtype: list
1022
+ """
1023
+ try:
1024
+ raw = [x["answer"] for x in data if x["label"] == field][0]
1025
+ return str(raw).replace(" ", "").split(",")
1026
+
1027
+ except Exception:
1028
+ if return_unknown_if_empty:
1029
+ return ["unknown"]
1030
+ return []
1031
+
1032
+ @staticmethod
1033
+ def create_table(
1034
+ headers: list,
1035
+ data: list,
1036
+ text: str | None = None,
1037
+ colwidth: list | None = None
1038
+ ) -> dict:
1039
+ """
1040
+ Creates a table with data to insert in JSM comment.
1041
+
1042
+ :param headers: Table headers
1043
+ :type headers: list
1044
+ :param data: Data to insert into table
1045
+ :type data: list
1046
+
1047
+ :returns: JSON structure compatible with JSM API
1048
+ :rtype: dict
1049
+ """
1050
+ fheaders = []
1051
+ frows = []
1052
+
1053
+ colindex = 0
1054
+
1055
+ for col_text in headers:
1056
+
1057
+ if colwidth is not None:
1058
+ attrs = {
1059
+ "colwidth": [colwidth[colindex]]
1060
+ }
1061
+ else:
1062
+ attrs = {}
1063
+
1064
+ fheaders.append({
1065
+ "type": "tableHeader",
1066
+ "attrs": attrs,
1067
+ "content": [
1068
+ {
1069
+ "type": "paragraph",
1070
+ "content": [
1071
+ {
1072
+ "type": "text",
1073
+ "text": col_text,
1074
+ "marks": [
1075
+ {
1076
+ "type": "strong"
1077
+ }
1078
+ ]
1079
+ }
1080
+ ]
1081
+ }
1082
+ ]
1083
+ })
1084
+
1085
+ colindex += 1
1086
+
1087
+ for row in data:
1088
+ base = {
1089
+ "type": "tableRow",
1090
+ "content": []
1091
+ }
1092
+
1093
+ for cell_text in row:
1094
+ base["content"].append({
1095
+ "type": "tableCell",
1096
+ "attrs": {},
1097
+ "content": [
1098
+ {
1099
+ "type": "paragraph",
1100
+ "content": [
1101
+ {
1102
+ "type": "text",
1103
+ "text": cell_text
1104
+ }
1105
+ ]
1106
+ }
1107
+ ]
1108
+ })
1109
+
1110
+ frows.append(base)
1111
+
1112
+ table = [
1113
+ {
1114
+ "type": "tableRow",
1115
+ "content": fheaders
1116
+ }
1117
+ ]
1118
+
1119
+ table.extend(frows)
1120
+
1121
+ if text is not None:
1122
+ return {
1123
+ "version": 1,
1124
+ "type": "doc",
1125
+ "content": [
1126
+ {
1127
+ "type": "paragraph",
1128
+ "content": [
1129
+ {
1130
+ "type": "text",
1131
+ "text": text
1132
+ }
1133
+ ]
1134
+ },
1135
+ {
1136
+ "type": "table",
1137
+ "attrs": {
1138
+ "isNumberColumnEnabled": False,
1139
+ "layout": "default"
1140
+ },
1141
+ "content": table
1142
+ }
1143
+ ]
1144
+ }
1145
+
1146
+ return {
1147
+ "version": 1,
1148
+ "type": "doc",
1149
+ "content": [
1150
+ {
1151
+ "type": "table",
1152
+ "attrs": {
1153
+ "isNumberColumnEnabled": False,
1154
+ "layout": "default"
1155
+ },
1156
+ "content": table
1157
+ }
1158
+ ]
1159
+ }
1160
+
1161
+
1162
+ ############## MAIL METHODS
1163
+ class Mail: # pylint: disable=too-few-public-methods
1164
+ """
1165
+ Mail methods
1166
+ """
1167
+ ############## SEND MAIL
1168
+ @staticmethod
1169
+ def send( # pylint: disable=too-many-arguments
1170
+ *,
1171
+ subject: str,
1172
+ to: str,
1173
+ body: str,
1174
+ server: str,
1175
+ port: int,
1176
+ user: str,
1177
+ password=None,
1178
+ secure=True
1179
+ ) -> bool:
1180
+ """
1181
+ Sendmail function.
1182
+
1183
+ :param subject: Mail subject
1184
+ :type subject: str
1185
+ :param to: Comma separated recipients
1186
+ :type to: str
1187
+ :param body: HTML code to include in mail body
1188
+ :type body: str
1189
+ :param server: Mail server
1190
+ :type server: str
1191
+ :param port: Port mail server
1192
+ :type port: int
1193
+ :param user: Mail user
1194
+ :type user: str
1195
+ :param password: Mail password
1196
+ :type password: str
1197
+ :param secure: Is using secure connection ?
1198
+ :type secure: bool
1199
+
1200
+ :returns: True if successful
1201
+ :rtype: bool
1202
+ """
1203
+ try:
1204
+ # SMTP PARAMETERS
1205
+ smtp = smtplib.SMTP(host=server, port=port)
1206
+ smtp.local_hostname = socket.gethostname().lower()
1207
+
1208
+ if secure is True:
1209
+ smtp.starttls()
1210
+
1211
+ if password is not None:
1212
+ smtp.login(user, password)
1213
+
1214
+ # CREATE MESSAGE OBJECT
1215
+ msg = MIMEMultipart()
1216
+
1217
+ # CREATE MESSAGE HEADERS
1218
+ msg['From'] = user
1219
+ msg['To'] = to
1220
+ msg['Date'] = formatdate(localtime = True)
1221
+ msg['Subject'] = subject
1222
+
1223
+ # FINALIZE MESSAGE
1224
+ part1 = MIMEText(body, 'html')
1225
+ msg.attach(part1)
1226
+
1227
+ # SEND
1228
+ smtp.send_message(msg)
1229
+
1230
+ return True
1231
+
1232
+ except Exception as e: # pylint: disable=broad-except
1233
+ print(f"Error: Mail.send -> {str(e)}")
1234
+ return False
1235
+
1236
+
1237
+ ############## MAILGUN METHODS
1238
+ class Mailgun: # pylint: disable=too-few-public-methods
1239
+ """
1240
+ Mailgun methods
1241
+ """
1242
+ ############## SEND MAIL
1243
+ @staticmethod
1244
+ def send( # pylint: disable=too-many-arguments
1245
+ *,
1246
+ subject: str,
1247
+ to: str,
1248
+ body: str,
1249
+ user: str,
1250
+ token: str,
1251
+ domain: str,
1252
+ file: list[str] | None = None
1253
+ ) -> bool:
1254
+ """
1255
+ Sendmail function.
1256
+
1257
+ :param subject: Mail subject
1258
+ :type subject: str
1259
+ :param to: Comma separated recipients
1260
+ :type to: str
1261
+ :param body: HTML code to include in mail body
1262
+ :type body: str
1263
+ :param user: Mail user
1264
+ :type user: str
1265
+ :param token: Mailgun token
1266
+ :type token: str
1267
+ :param domain: Domain (ex: domain.com)
1268
+ :type domain: str
1269
+ :param file: Attachment list (optional), must be a list even with one item
1270
+ :type file: list[str]
1271
+
1272
+ :returns: Nothing
1273
+ :rtype: None
1274
+ """
1275
+ client: mgclient = mgclient(auth=("api", token))
1276
+
1277
+ data = {
1278
+ "from": user,
1279
+ "to": to,
1280
+ "subject": subject,
1281
+ "html": body
1282
+ }
1283
+
1284
+ files: list = []
1285
+
1286
+ if file is not None:
1287
+ for f in file:
1288
+ files.append(
1289
+ (
1290
+ "attachment",
1291
+ (f, Path(f).read_bytes()),
1292
+ )
1293
+ )
1294
+
1295
+ if file is not None:
1296
+ return client.messages.create(
1297
+ data=data,
1298
+ files=files,
1299
+ domain=domain
1300
+ )
1301
+
1302
+ return client.messages.create(
1303
+ data=data,
1304
+ domain=domain
1305
+ )
1306
+
1307
+
1308
+ ############## EXCEL METHODS
1309
+ class Pdf: # pylint: disable=too-few-public-methods
1310
+ """
1311
+ Excel methods
1312
+ """
1313
+ ############## EXPORT TO XLSX
1314
+ @staticmethod
1315
+ def export(
1316
+ file: str,
1317
+ html_doc: str
1318
+ ) -> bool:
1319
+ """
1320
+ Export data to formatted XLSX file.
1321
+
1322
+ :param file: Exported file
1323
+ :type file: str
1324
+ :param html_doc: String that contains HTML doc
1325
+ :type html_doc: str
1326
+
1327
+ :returns: True if successful
1328
+ :rtype: bool
1329
+ """
1330
+ try:
1331
+ with open(file, "wb") as pdf_file:
1332
+ pisa.CreatePDF(html_doc, dest=pdf_file)
1333
+
1334
+ except Exception as e:
1335
+ print(f"Error: Can't export PDF -> {e}")
1336
+ return False
1337
+
1338
+ return True
1339
+
1340
+
1341
+ ############## SLACK METHODS
1342
+ class Slack:
1343
+ """
1344
+ Slack methods
1345
+ """
1346
+ ############## SEND MESSAGE TO SLACK
1347
+ @staticmethod
1348
+ def send( # pylint: disable=too-many-arguments
1349
+ host: str,
1350
+ token: str,
1351
+ channel: str,
1352
+ blocks: list,
1353
+ *,
1354
+ text: str | None = None,
1355
+ retries: int = 0,
1356
+ timeout: float = 2.0
1357
+ ) -> dict | None:
1358
+ """
1359
+ Send Slack message to channel.
1360
+
1361
+ :param host: Slack host
1362
+ :type host: str
1363
+ :param token: Slack API token
1364
+ :type token: str
1365
+ :param channel: Slack channel
1366
+ :type channel: str
1367
+ :param blocks: Slack blocks, ignored if 'text' is set
1368
+ :type blocks: list
1369
+ :param text: Markdown text (optional)
1370
+ :type text: str
1371
+ :param retries: Number of retries (optional)
1372
+ :type retries: int
1373
+ :param timeout: Maximum timeout when sending message (optional)
1374
+ :type timeout: float
1375
+
1376
+ :returns: Slack formatted message
1377
+ :rtype: dict | None
1378
+ """
1379
+ if text is None:
1380
+ return Api.request(
1381
+ method='POST',
1382
+ host=host,
1383
+ headers={
1384
+ "Content-type": "application/json",
1385
+ "Authorization": f"Bearer {token}"
1386
+ },
1387
+ entry_point="/api/chat.postMessage",
1388
+ data=json.dumps({
1389
+ "channel": channel,
1390
+ "blocks": blocks
1391
+ }),
1392
+ retries=retries,
1393
+ timeout=timeout
1394
+ )
1395
+
1396
+ return Api.request(
1397
+ method='POST',
1398
+ host=host,
1399
+ headers={
1400
+ "Content-type": "application/json",
1401
+ "Authorization": f"Bearer {token}"
1402
+ },
1403
+ entry_point="/api/chat.postMessage",
1404
+ data=json.dumps({
1405
+ "channel": channel,
1406
+ "text": text
1407
+ }),
1408
+ retries=retries,
1409
+ timeout=timeout
1410
+ )
1411
+
1412
+ ############## DELETE A MESSAGE FROM SLACK CHANNEL
1413
+ @staticmethod
1414
+ def delete( # pylint: disable=too-many-arguments
1415
+ host: str,
1416
+ token: str,
1417
+ channel: str,
1418
+ timestamp: str,
1419
+ *,
1420
+ retries: int = 0,
1421
+ timeout: float = 60.0
1422
+ ) -> dict | None:
1423
+ """
1424
+ Delete a message in a Slack channel with his timestamp
1425
+
1426
+ :param host: Slack host
1427
+ :type host: str
1428
+ :param token: Slack API token
1429
+ :type token: str
1430
+ :param channel: Slack channel
1431
+ :type channel: str
1432
+ :param timestamp: Message timestamp
1433
+ :type timestamp: str
1434
+ :param retries: (optional) Number of retries
1435
+ :type retries: int
1436
+ :param timeout: (optional) Maximum timeout when sending attachment
1437
+ :type timeout: float
1438
+
1439
+ :returns: API response
1440
+ :rtype: dict | None
1441
+ """
1442
+ return Api.request(
1443
+ method='POST',
1444
+ host=host,
1445
+ headers={
1446
+ "Content-type": "application/x-www-form-urlencoded",
1447
+ "Authorization": f"Bearer {token}"
1448
+ },
1449
+ entry_point=f"/api/chat.delete?ts={timestamp}&channel={channel}",
1450
+ retries=retries,
1451
+ timeout=timeout
1452
+ )
1453
+
1454
+ ############## SEND ATTACHMENT TO SLACK
1455
+ @staticmethod
1456
+ def send_attachment( # pylint: disable=too-many-arguments
1457
+ host: str,
1458
+ token: str,
1459
+ channel: str,
1460
+ filepath: str,
1461
+ blocks: list | None,
1462
+ *,
1463
+ retries: int = 0,
1464
+ timeout: float = 60.0
1465
+ ) -> dict | None:
1466
+ """
1467
+ Send attachment to Slack channel.
1468
+
1469
+ :param host: Slack host
1470
+ :type host: str
1471
+ :param token: Slack API token
1472
+ :type token: str
1473
+ :param channel: Slack channel
1474
+ :type channel: str
1475
+ :param filepath: Name & path of the file
1476
+ :type filepath: str
1477
+ :param blocks: Slack blocks
1478
+ :type blocks: list
1479
+ :param retries: (optional) Number of retries
1480
+ :type retries: int
1481
+ :param timeout: (optional) Maximum timeout
1482
+ when sending attachment, default to 60s
1483
+ :type timeout: float
1484
+
1485
+ :returns: Slack formatted message
1486
+ :rtype: dict | None
1487
+ """
1488
+ try:
1489
+ length: int = Path(filepath).stat().st_size
1490
+
1491
+ resp1 = Api.request(
1492
+ method='POST',
1493
+ host=host,
1494
+ headers={
1495
+ "Content-type": "application/json",
1496
+ "Authorization": f"Bearer {token}"
1497
+ },
1498
+ entry_point=f"/api/files.getUploadURLExternal?length={length}&filename={filepath}",
1499
+ retries=retries,
1500
+ timeout=timeout
1501
+ )
1502
+
1503
+ except Exception: # pylint: disable=broad-except
1504
+ return { "ok": False }
1505
+
1506
+ if resp1["ok"]:
1507
+
1508
+ with open(filepath, 'rb') as f:
1509
+ data = f.read()
1510
+ f.close()
1511
+
1512
+ resp2 = Api.request(
1513
+ method='POST',
1514
+ host="files.slack.com",
1515
+ headers={
1516
+ "Content-type": "application/octet-stream",
1517
+ "Authorization": f"Bearer {token}"
1518
+ },
1519
+ params={
1520
+ "filename": filepath
1521
+ },
1522
+ entry_point=str(
1523
+ resp1['upload_url']).replace("https://files.slack.com",
1524
+ ""
1525
+ ),
1526
+ retries=retries,
1527
+ data=data,
1528
+ timeout=timeout
1529
+ )
1530
+
1531
+ if str(resp2) == "<Response [200]>":
1532
+ return Api.request(
1533
+ method='POST',
1534
+ host=host,
1535
+ headers={
1536
+ "Content-type": "application/json",
1537
+ "Authorization": f"Bearer {token}"
1538
+ },
1539
+ entry_point="/api/files.completeUploadExternal",
1540
+ data=json.dumps({
1541
+ "channel_id": channel,
1542
+ "files": [{ "id": resp1["file_id"] }],
1543
+ "blocks": blocks
1544
+ }),
1545
+ retries=retries,
1546
+ timeout=timeout
1547
+ ) # pragma: no cover
1548
+
1549
+ return { "ok": False }
1550
+
1551
+ ############## DELETE ATTACHMENT
1552
+ @staticmethod
1553
+ def delete_attachment(
1554
+ host: str,
1555
+ token: str,
1556
+ file_id: str,
1557
+ *,
1558
+ retries: int = 0,
1559
+ timeout: float = 60.0
1560
+ ) -> dict | None:
1561
+ """
1562
+ Delete an attachment in Slack.
1563
+
1564
+ :param host: Slack host
1565
+ :type host: str
1566
+ :param token: Slack API token
1567
+ :type token: str
1568
+ :param file_id: File ID
1569
+ :type file_id: str
1570
+ :param retries: (optional) Number of retries
1571
+ :type retries: int
1572
+ :param timeout: (optional) Maximum timeout when sending attachment
1573
+ :type timeout: float
1574
+
1575
+ :returns: API response
1576
+ :rtype: dict | None
1577
+ """
1578
+ return Api.request(
1579
+ method='POST',
1580
+ host=host,
1581
+ headers={
1582
+ "Content-type": "application/json",
1583
+ "Authorization": f"Bearer {token}"
1584
+ },
1585
+ entry_point="/api/files.delete",
1586
+ data=json.dumps({
1587
+ "file": file_id
1588
+ }),
1589
+ retries=retries,
1590
+ timeout=timeout
1591
+ ) # pragma: no cover
1592
+
1593
+ ############## CREATE TABLE USING BLOCKS
1594
+ @staticmethod
1595
+ def create_table(data: list) -> list:
1596
+ """
1597
+ Create pseudo table with blocks.
1598
+
1599
+ :param data: Data to convert (only 2 columns are supported!)
1600
+ :type data: list
1601
+
1602
+ :returns: Slack blocks
1603
+ :rtype: list
1604
+ """
1605
+
1606
+ header: list = []
1607
+ lines: list = []
1608
+
1609
+ # TABLE HEADER
1610
+ key_count: int = 0
1611
+
1612
+ for key in data[0]:
1613
+
1614
+ if key_count < 2:
1615
+ header.append({
1616
+ "type": "mrkdwn",
1617
+ "text": f"*{key}*"
1618
+ })
1619
+ key_count += 1
1620
+
1621
+ lines.append({
1622
+ "type": "section",
1623
+ "fields": header
1624
+ })
1625
+
1626
+ lines.append({
1627
+ "type": "divider"
1628
+ })
1629
+
1630
+ for item in data:
1631
+ line: list = []
1632
+ key_count: int = 0
1633
+
1634
+ for key in item.keys():
1635
+ if key_count < 2:
1636
+ line.append({
1637
+ "type": "mrkdwn",
1638
+ "text": f"{item[key]}"
1639
+ })
1640
+ key_count += 1
1641
+
1642
+ lines.append({
1643
+ "type": "section",
1644
+ "fields": line
1645
+ })
1646
+
1647
+ return lines
1648
+
1649
+ ############## CREATE LIST USING BLOCK
1650
+ @staticmethod
1651
+ def create_list(
1652
+ data: list,
1653
+ simple: bool=False
1654
+ ) -> dict:
1655
+ """
1656
+ Create list from a Python list.
1657
+
1658
+ :param data: Data to convert (only 2 properties are supported)
1659
+ :type data: list
1660
+ :param simple: (optional) Simple list based on array
1661
+ :type simple: bool
1662
+
1663
+ :returns: Slack block
1664
+ :rtype: dict
1665
+ """
1666
+ if simple is not True:
1667
+ block = {
1668
+ "type": "rich_text",
1669
+ "elements": [
1670
+ {
1671
+ "type": "rich_text_list",
1672
+ "style": "bullet",
1673
+ "indent": 0,
1674
+ "elements": []
1675
+ }
1676
+ ]
1677
+ }
1678
+
1679
+ for item in data:
1680
+ key_count: int = 0
1681
+ elements = []
1682
+
1683
+ for key in item.keys():
1684
+ if key_count < 2:
1685
+ if key_count == 0:
1686
+ elements.append({
1687
+ "type": "text",
1688
+ "text": f"{item[key]}"
1689
+ })
1690
+
1691
+ if key_count == 1: # pragma: no cover
1692
+ elements.append({
1693
+ "type": "text",
1694
+ "text": f"{item[key]}"
1695
+ })
1696
+
1697
+ block["elements"].append({
1698
+ "type": "rich_text_section",
1699
+ "elements": elements
1700
+ })
1701
+ else:
1702
+ block = {
1703
+ "type": "section",
1704
+ "text": {
1705
+ "type": "mrkdwn",
1706
+ "text": ""
1707
+ }
1708
+ }
1709
+
1710
+ for item in data:
1711
+ block["text"]["text"] += f"- {item}\n"
1712
+
1713
+ return block
1714
+
1715
+
1716
+ ############## HASHICORP VAULT METHODS
1717
+ class Vault: # pylint: disable=too-few-public-methods
1718
+ """
1719
+ Hashicorp Vault methods
1720
+ """
1721
+ @staticmethod
1722
+ def get(
1723
+ reqs: list[dict],
1724
+ url: str | None = None,
1725
+ role: str | None = None,
1726
+ token: str | None = None,
1727
+ mount_point: str = "kv"
1728
+ ) -> dict | None:
1729
+ """
1730
+ Get secrets from Hashicorp Vault within Gitlab pipelines.
1731
+
1732
+ :param reqs: List of dict that contains requests for getting secrets.
1733
+ Ex: ```[{"path":"my/path","key":"my_secret_key"}]```
1734
+ :type reqs: list[dict]
1735
+ :param url: (optional) Vault URL.
1736
+ If not specified, search for VAULT_SERVER_URL env var.
1737
+ :type url: str
1738
+ :param role: (optional) Vault auth role.
1739
+ If not specified, search for VAULT_AUTH_ROLE env var.
1740
+ :type role: str
1741
+ :param token: (optional) Vault JWT token.
1742
+ If not specified, search for VAULT_ID_TOKEN env var.
1743
+ :type token: str
1744
+ :param mount_point: (optional) Vault engine. Default to 'kv'.
1745
+ :type mount_point: str
1746
+
1747
+ :returns: A dict that contains screts. Returns None and
1748
+ prints error if problem occurs.
1749
+ :rtype: dict | None
1750
+ """
1751
+ vault_url = url or os.environ.get("VAULT_SERVER_URL")
1752
+ vault_role = role or os.environ.get("VAULT_AUTH_ROLE")
1753
+ vault_token = token or os.environ.get("VAULT_ID_TOKEN")
1754
+
1755
+ result = {}
1756
+
1757
+ if vault_token is None:
1758
+ print("Error : 'vault_token' is None.")
1759
+ return None
1760
+
1761
+ if vault_url is None:
1762
+ print("Error : 'vault_url' is None.")
1763
+ return None
1764
+
1765
+ client = hvac.Client(url=vault_url)
1766
+ login_params = {"jwt": vault_token, "role": vault_role}
1767
+ client.login(url="/v1/auth/jwt/login", json=login_params)
1768
+
1769
+ if not client.is_authenticated():
1770
+ print("Error: Vault authentication failed.", file=sys.stderr)
1771
+ return None
1772
+
1773
+ for req in reqs:
1774
+ secret_response = client.secrets.kv.v2.read_secret_version(
1775
+ mount_point=mount_point,
1776
+ path=req['path'],
1777
+ raise_on_deleted_version=True
1778
+ )
1779
+
1780
+ result[req['key']] = secret_response["data"]["data"][req['key']]
1781
+
1782
+ client.logout()
1783
+ return result