atk-common 3.19.0__py3-none-any.whl → 3.20.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.
@@ -1,13 +1,17 @@
1
1
  import os
2
+ from pathlib import Path
2
3
  from typing import Optional
3
4
  from atk_common.interfaces import IEnvHandler
4
5
  from atk_common.interfaces import ILogger
5
6
 
6
- class EnvHandler(IEnvHandler):
7
- def __init__(self, logger: Optional[ILogger]): # type: ignore
7
+ class MyEnvHandler(IEnvHandler):
8
+ def __init__(self, logger: Optional[ILogger]):
8
9
  self.logger = logger
9
10
 
10
- def val_str(self, value):
11
+ def set_logger(self, logger: ILogger):
12
+ self.logger = logger
13
+
14
+ def val_str(self, value: Optional[object]) -> str:
11
15
  if value is None:
12
16
  return '<Empty>'
13
17
  if isinstance(value, str):
@@ -16,23 +20,57 @@ class EnvHandler(IEnvHandler):
16
20
  return value
17
21
  return str(value)
18
22
 
19
- def is_value_null_or_empty(self, value):
20
- if isinstance(value, str):
21
- return value.strip() == '' or value.lower() == 'null'
22
- return False
23
-
24
- def get_env_value(self, key, abort_on_error=True):
23
+ def is_value_null_or_empty(self, value: Optional[str]) -> bool:
24
+ if value is None:
25
+ return True
26
+ s = value.strip()
27
+ return s == '' or s.lower() == 'null'
28
+
29
+ def get_env_value(self, key: str) -> Optional[str]:
25
30
  val = os.environ.get(key)
31
+ if val is None:
32
+ err_msg = f"Environment key '{key}' is missing."
33
+ if self.logger:
34
+ self.logger.error(err_msg)
35
+ raise ValueError(err_msg)
26
36
  if self.logger:
27
37
  self.logger.info(key + ':' + self.val_str(val))
28
- if val is None and abort_on_error:
29
- err_msg = f"Environment variable '{key}' is not set."
38
+ if self.is_value_null_or_empty(val):
39
+ return None
40
+ return val
41
+
42
+ @staticmethod
43
+ def read_secret(path):
44
+ return Path(path).read_text().rstrip('\n')
45
+
46
+ def get_env_value_secret(self, key: str, file_key: str) -> str:
47
+ val = os.environ.get(key)
48
+ if val is None:
49
+ err_msg = f"Environment key '{key}' is missing."
30
50
  if self.logger:
31
51
  self.logger.error(err_msg)
32
52
  raise ValueError(err_msg)
33
53
  if self.is_value_null_or_empty(val):
34
- return None
54
+ secret_path = os.getenv(file_key)
55
+ if secret_path is None:
56
+ err_msg = f"Secret file key '{file_key}' is missing."
57
+ if self.logger:
58
+ self.logger.error(err_msg)
59
+ raise ValueError(err_msg)
60
+ if self.is_value_null_or_empty(secret_path):
61
+ err_msg = f"Secret file variable '{file_key}' is not set."
62
+ if self.logger:
63
+ self.logger.error(err_msg)
64
+ raise ValueError(err_msg)
65
+ if not Path(secret_path).is_file():
66
+ err_msg = f"Secret file '{secret_path}' not found."
67
+ if self.logger:
68
+ self.logger.error(err_msg)
69
+ raise ValueError(err_msg)
70
+ val = self.read_secret(secret_path)
71
+ if self.is_value_null_or_empty(val):
72
+ err_msg = f"Secret from key file '{secret_path}' is empty."
73
+ if self.logger:
74
+ self.logger.error(err_msg)
75
+ raise ValueError(err_msg)
35
76
  return val
36
-
37
- def set_logger(self, logger: ILogger):
38
- self.logger = logger
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: atk_common
3
- Version: 3.19.0
3
+ Version: 3.20.0
4
4
  Summary: ATK common methods
5
5
  Home-page: https://github.com/pypa/atk_common
6
6
  Author: Roger
@@ -19,7 +19,7 @@ atk_common/response_utils.py,sha256=AxlmwkFoDU5XcFOzBQiuZxAQgswihpKXHSo1T0JJw3Q,
19
19
  atk_common/classes/__init__.py,sha256=O_VHYxAilmoz3i9L6jkwS-JZ4UaTezdhFiA5liNl1lY,532
20
20
  atk_common/classes/bo_logger.py,sha256=wo1-BNWZjzSnlcM7c0LB7tK__2g3BENXvSvCLEiTOME,2065
21
21
  atk_common/classes/docker_handler.py,sha256=VNJecc2ZWcpwNBa61QTGM_kYrsoZTjktMcFRFGF4xI4,3048
22
- atk_common/classes/env_handler.py,sha256=h3snKwHwDvfc2dt1vgHasqv8n_vnsI9J2MAr-XM5oow,1283
22
+ atk_common/classes/env_handler.py,sha256=_Lv_sBUCwsjxO22oRSC4v2xrbVMv-DMW2Mer4OQKJmk,2877
23
23
  atk_common/classes/error_handler.py,sha256=DgRzWx4Yu8PKX1bqj55aWhrkqb92ebDaEwtxgKhTLls,3270
24
24
  atk_common/classes/http_response_handler.py,sha256=qgtGyEwUe4lIiayS6syWanr2VE6GaKHddazNL_PxEFM,2327
25
25
  atk_common/classes/rabbitmq_consumer.py,sha256=UySYDCMXCapi5XNQnsn7q7sVcVvZKKo73qj6OpEv8Ak,3004
@@ -82,7 +82,7 @@ atk_common/utils/http_utils.py,sha256=eSRuQeDgN0ISQdByZqE6cIGXoorcAXz7PEtVntHUKA
82
82
  atk_common/utils/internal_response_utils.py,sha256=2X9eLFEy1pO3Aesj1IRXg2yprwNcBDM5_dXaA5vfmMI,694
83
83
  atk_common/utils/mq_utils.py,sha256=DmVcXIZHG45p7cQVvgen6OT8QbW_UifFFJGJBybTkJQ,1835
84
84
  atk_common/utils/str_utils.py,sha256=sg3jwTTIfQvgGP-lcY5Xh4PXXhKWse_4HswBQYQKEo4,260
85
- atk_common-3.19.0.dist-info/licenses/license.txt,sha256=_0O6fWM00-wTurDjnZhUP_N5QiwGhItaQZqHq5eqadA,1063
85
+ atk_common-3.20.0.dist-info/licenses/license.txt,sha256=_0O6fWM00-wTurDjnZhUP_N5QiwGhItaQZqHq5eqadA,1063
86
86
  atk_package/__init__.py,sha256=okIFEefQhQrw6DZg6oCEVWsEdkVCk-57VXBW0IUG_wU,834
87
87
  atk_package/datetime_utils.py,sha256=qsVF7l90P1-xukG2tV_jLqG9J_Yfl5wTpyfrdPBlyMo,239
88
88
  atk_package/env_utils.py,sha256=bXOrxM3fZUslqfmZt75iphbEJHbG4riJa8XOVzPwIII,313
@@ -95,7 +95,7 @@ atk_package/enums/__init__.py,sha256=EtUr_--MQj1Rc_R0sF_ELXIThmhpfmhDWq3YaK9oQMk
95
95
  atk_package/enums/command_status_enum.py,sha256=M2Nln27a_DbzI07-gfytWQk2X087JhkU6Fmard5qVHs,127
96
96
  atk_package/enums/speed_control_status_enum.py,sha256=qpURh0K1L1tSpbrzVnckoe4hUn1illIkbo7k4mLfzIM,182
97
97
  shared_python_atk_enforcement/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
98
- atk_common-3.19.0.dist-info/METADATA,sha256=jyWhlPxZh1Z2aQFWHKW39nfvSh8tYY59FkRBZv_3jjo,1761
99
- atk_common-3.19.0.dist-info/WHEEL,sha256=_zCd3N1l69ArxyTb8rzEoP9TpbYXkqRFSNOD5OuxnTs,91
100
- atk_common-3.19.0.dist-info/top_level.txt,sha256=4CwRjkLnheIdI4jQwc4tK3dbRc58WqUmoqjkdDTWlME,41
101
- atk_common-3.19.0.dist-info/RECORD,,
98
+ atk_common-3.20.0.dist-info/METADATA,sha256=1-z5T2ctaAYb54SFV5AL8upYfRo7S--P5QdDYDXe5cQ,1761
99
+ atk_common-3.20.0.dist-info/WHEEL,sha256=_zCd3N1l69ArxyTb8rzEoP9TpbYXkqRFSNOD5OuxnTs,91
100
+ atk_common-3.20.0.dist-info/top_level.txt,sha256=4CwRjkLnheIdI4jQwc4tK3dbRc58WqUmoqjkdDTWlME,41
101
+ atk_common-3.20.0.dist-info/RECORD,,