atk-common 1.2.0__py3-none-any.whl → 1.4.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.
atk_common/__init__.py ADDED
@@ -0,0 +1,24 @@
1
+ # __init__.py
2
+ from atk_common.datetime_utils import date_time_utc, get_utc_date_time
3
+ from atk_common.env_utils import get_env_value
4
+ from atk_common.error_utils import get_message, get_error_entity, handle_error, get_response_error, get_error_type
5
+ from atk_common.http_utils import is_status_code_ok
6
+ from atk_common.log_utils import add_log_item, add_log_item_http
7
+ from atk_common.rabbitmq_consumer import RabbitMQConsumer
8
+ from atk_common.response_utils import create_save_resp
9
+
10
+ __all__ = [
11
+ 'date_time_utc',
12
+ 'get_utc_date_time',
13
+ 'get_env_value',
14
+ 'get_message',
15
+ 'get_error_entity',
16
+ 'handle_error',
17
+ 'get_response_error',
18
+ 'get_error_type',
19
+ 'is_status_code_ok',
20
+ 'add_log_item',
21
+ 'add_log_item_http',
22
+ 'RabbitMQConsumer',
23
+ 'create_save_resp'
24
+ ]
@@ -0,0 +1,7 @@
1
+ from datetime import datetime, timezone
2
+
3
+ def date_time_utc(column):
4
+ return 'TO_CHAR({0} at time zone \'UTC\', \'yyyy-mm-dd hh24:mi:ss.ms"Z"\')'.format(column)
5
+
6
+ def get_utc_date_time():
7
+ return str(datetime.now(timezone.utc))
@@ -0,0 +1,65 @@
1
+ import docker
2
+ import os
3
+ import socket
4
+ from atk_common.env_utils import get_env_value
5
+
6
+ def find_image_prop_list_by_name(image_list, image_name):
7
+ for image in image_list:
8
+ if image.tags and len(image.tags) > 0:
9
+ # Check if the image name is in the first tag
10
+ if image_name in image.tags[0]:
11
+ return image.tags[0]
12
+ return None
13
+
14
+ def get_image_version(default_value=None):
15
+ """
16
+ Get the version of a Docker image.
17
+
18
+ :param image_name: Name of the Docker image
19
+ :return: Version of the Docker image
20
+ """
21
+ try:
22
+ image_name = get_env_value('DOCKER_IMAGE_NAME', default_value)
23
+ if image_name is None:
24
+ return None
25
+ client = docker.from_env()
26
+ image_list = client.images.list()
27
+ image_prop_list = find_image_prop_list_by_name(image_list, image_name)
28
+ if image_prop_list is None:
29
+ return None
30
+ image_props = image_prop_list.split(':')
31
+ if len(image_props) < 2:
32
+ return None
33
+ return image_props[1]
34
+ except docker.errors.ImageNotFound:
35
+ return None
36
+
37
+ def get_current_container_info():
38
+ try:
39
+ client = docker.from_env()
40
+
41
+ # Get current container's hostname (usually the container ID)
42
+ container_id = socket.gethostname()
43
+
44
+ # Fetch container object using partial ID
45
+ container = client.containers.get(container_id)
46
+
47
+ name = container.name
48
+ ports = container.attrs['NetworkSettings']['Ports']
49
+
50
+ print(f"🔍 Container name: {name}")
51
+ print("🌐 Port mappings:")
52
+ if ports:
53
+ for container_port, host_bindings in ports.items():
54
+ if host_bindings:
55
+ for binding in host_bindings:
56
+ print(f" {container_port} -> {binding['HostIp']}:{binding['HostPort']}")
57
+ else:
58
+ print(f" {container_port} -> Not bound to host")
59
+ else:
60
+ print("No exposed ports found.")
61
+ return None
62
+
63
+ except Exception as e:
64
+ print("❌ Error getting container info:", e)
65
+ return None
@@ -0,0 +1,8 @@
1
+ # __init__.py
2
+ from atk_common.enums.command_status_enum import CommandStatusType
3
+ from atk_common.enums.speed_control_status_enum import SpeedControlStatusType
4
+
5
+ __all__ = [
6
+ 'CommandStatusType',
7
+ 'SpeedControlStatusType'
8
+ ]
@@ -0,0 +1,7 @@
1
+ from enum import Enum
2
+
3
+ class CommandStatusType(Enum):
4
+ CREATED = 1
5
+ COMPLETED = 2
6
+ EXPIRED = 3
7
+ FAILED = 4
@@ -0,0 +1,10 @@
1
+ from enum import Enum
2
+
3
+ class SpeedControlStatusType(Enum):
4
+ CREATED = 1
5
+ CONFIRMED = 2
6
+ STARTED = 3
7
+ FAILED = 4
8
+ PAUSED = 5
9
+ STOPPED = 6
10
+ DELETED = 7
@@ -0,0 +1,13 @@
1
+ import os
2
+ from atk_common.log_utils import add_log_item
3
+
4
+ def get_env_value(key, default_value):
5
+ try:
6
+ if default_value is None:
7
+ return None
8
+ val = os.environ[key]
9
+ add_log_item(key + ':' + val)
10
+ return val
11
+ except (Exception) as error:
12
+ add_log_item(key + ':' + default_value)
13
+ return default_value
@@ -0,0 +1,50 @@
1
+ from datetime import datetime
2
+ import json
3
+ from atk_common.datetime_utils import get_utc_date_time
4
+ from atk_common.docker_utils import get_image_version
5
+ from atk_common.log_utils import add_log_item
6
+ from atk_common.response_utils import create_save_resp
7
+
8
+ def get_message(error):
9
+ if hasattr(error, 'message'):
10
+ return str(error.message)
11
+ else:
12
+ return str(error)
13
+
14
+ def get_error_entity(app, error, component, method, error_type, status_code):
15
+ data = {}
16
+ data['statusCode'] = status_code
17
+ data['exceptionType'] = str(type(error))
18
+ data['errorType'] = error_type
19
+ data['message'] = get_message(error)
20
+ data['component'] = component
21
+ data['method'] = method
22
+ data['imageVersion'] = get_image_version(component)
23
+ data['timestamp'] = get_utc_date_time()
24
+ return app.response_class(
25
+ response=json.dumps(data),
26
+ status=500,
27
+ mimetype='application/json'
28
+ )
29
+
30
+ def handle_error(resp, status):
31
+ if resp.status_code == 500:
32
+ add_log_item(resp.json().get('message'))
33
+ return create_save_resp(status, resp.status_code, resp.json())
34
+ else:
35
+ add_log_item(resp.text)
36
+ return create_save_resp(status, resp.status_code, resp.text)
37
+
38
+ def get_response_error(resp):
39
+ if resp.status_code == 500:
40
+ return resp.json()
41
+ else:
42
+ return resp.text
43
+
44
+ # Return values:
45
+ # 1 - Connection error
46
+ # 2 - Internal error
47
+ def get_error_type(conn):
48
+ if conn is None:
49
+ return 1
50
+ return 2
@@ -0,0 +1,24 @@
1
+ import json
2
+ from atk_common.datetime_utils import get_utc_date_time
3
+ from atk_common.docker_utils import get_current_container_info, get_image_version
4
+ from atk_common.env_utils import get_env_value
5
+
6
+ def is_status_code_ok(status_code):
7
+ return status_code >= 200 and status_code < 300
8
+
9
+ def get_test_response(default_value=None):
10
+ data = {}
11
+ container_info = get_current_container_info()
12
+ if container_info is not None:
13
+ data['containerName'] = container_info.name
14
+ data['containerPorts'] = container_info.ports
15
+ else:
16
+ data['containerName'] = None
17
+ data['containerPorts'] = None
18
+ data['imageName'] = get_env_value('DOCKER_IMAGE_NAME', default_value)
19
+ if data['imageName'] is None:
20
+ data['imageVersion'] = None
21
+ else:
22
+ data['imageVersion'] = get_image_version(data['imageName'])
23
+ data['timestamp'] = get_utc_date_time()
24
+ return json.dumps(data)
@@ -0,0 +1,15 @@
1
+ from atk_common.datetime_utils import get_utc_date_time
2
+
3
+ def create_date_time():
4
+ date_time = get_utc_date_time()
5
+ return '[' + date_time + '] '
6
+
7
+ def add_log_item(text):
8
+ print(create_date_time() + text)
9
+
10
+ def add_log_item_http(resp):
11
+ if resp.status_code == 500:
12
+ err_resp_json = resp.json().get('message')
13
+ add_log_item(err_resp_json)
14
+ else:
15
+ add_log_item(resp.text)
@@ -0,0 +1,51 @@
1
+ from kombu import Connection, Exchange, Queue, Consumer
2
+ import socket
3
+ import time
4
+
5
+ class RabbitMQConsumer:
6
+ def __init__(self, queue_name, user, pwd, host, vhost, dlx, dlq, content_type, message_handler, log):
7
+
8
+ rabbit_url = 'amqp://' + user + ':' + pwd + '@' + host + '/' + vhost
9
+
10
+ self.connection = Connection(rabbit_url, heartbeat=10)
11
+ if dlx is not None and dlq is not None:
12
+ queue = Queue(name=queue_name,
13
+ queue_arguments={
14
+ 'x-dead-letter-exchange': dlx,
15
+ 'x-dead-letter-routing-key': dlq},
16
+ )
17
+ else:
18
+ queue = Queue(name=queue_name)
19
+ if content_type is not None:
20
+ self.consumer = Consumer(self.connection, queues=queue, callbacks=[message_handler], accept=[content_type])
21
+ else:
22
+ self.consumer = Consumer(self.connection, queues=queue, callbacks=[message_handler], accept=None)
23
+ self.consumer.consume()
24
+
25
+ self.message_handler = message_handler # Custom message handler
26
+ self.log = log
27
+
28
+ def consume(self):
29
+ new_conn = self.establish_connection()
30
+ while True:
31
+ try:
32
+ new_conn.drain_events(timeout=2)
33
+ except socket.timeout:
34
+ new_conn.heartbeat_check()
35
+
36
+ def establish_connection(self):
37
+ revived_connection = self.connection.clone()
38
+ revived_connection.ensure_connection(max_retries=3)
39
+ channel = revived_connection.channel()
40
+ self.consumer.revive(channel)
41
+ self.consumer.consume()
42
+ self.log("Connection revived!")
43
+ return revived_connection
44
+
45
+ def run(self):
46
+ while True:
47
+ try:
48
+ self.consume()
49
+ except self.connection.connection_errors:
50
+ pass
51
+
@@ -0,0 +1,6 @@
1
+ def create_save_resp(status, status_code, response_msg):
2
+ data = {}
3
+ data['status'] = status
4
+ data['statusCode'] = status_code
5
+ data['responseMsg'] = response_msg
6
+ return data
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: atk_common
3
- Version: 1.2.0
3
+ Version: 1.4.0
4
4
  Summary: ATK common methods
5
5
  Home-page: https://github.com/pypa/atk_common
6
6
  Author: Roger
@@ -0,0 +1,29 @@
1
+ atk_common/__init__.py,sha256=EceDa352WFHTdC_LxYANXf_TS9pGzK3MCx87bwAKTvg,811
2
+ atk_common/datetime_utils.py,sha256=qsVF7l90P1-xukG2tV_jLqG9J_Yfl5wTpyfrdPBlyMo,239
3
+ atk_common/docker_utils.py,sha256=BuulyBYuUoNS-iV4ZsknRifx5L2KWcKrrHDRqg6zqaY,2194
4
+ atk_common/env_utils.py,sha256=66v-S0vlkJNaun6ay1XcOGRtYJb4Ce4odJsWxuaHsQc,373
5
+ atk_common/error_utils.py,sha256=5OiL5nMBEkDc193m2cVMD2Bs-fwRvPeSlI7PwnqB0o4,1533
6
+ atk_common/http_utils.py,sha256=AzWuSkiH086JNsyAw1smOP6bNGyyeL4KkkxLYyNadOQ,936
7
+ atk_common/log_utils.py,sha256=DGGwXjD5jvpMZJNW1QBoJRdDaTY1e1Z69j0LSOOzsWA,419
8
+ atk_common/rabbitmq_consumer.py,sha256=4MhuwZs47Jt1fX4sUxr1MKRe7o2QRbPe9_utXEsa8QE,1907
9
+ atk_common/response_utils.py,sha256=ezqTSNdGwueFXzijKB7CKPWxZml39bqjAZOeIMWMntk,197
10
+ atk_common/enums/__init__.py,sha256=oW0aVnwewfH6sNu14jjZ5_uP4iRN4pRmGdTNC204rHo,236
11
+ atk_common/enums/command_status_enum.py,sha256=M2Nln27a_DbzI07-gfytWQk2X087JhkU6Fmard5qVHs,127
12
+ atk_common/enums/speed_control_status_enum.py,sha256=qpURh0K1L1tSpbrzVnckoe4hUn1illIkbo7k4mLfzIM,182
13
+ atk_package/__init__.py,sha256=EceDa352WFHTdC_LxYANXf_TS9pGzK3MCx87bwAKTvg,811
14
+ atk_package/datetime_utils.py,sha256=qsVF7l90P1-xukG2tV_jLqG9J_Yfl5wTpyfrdPBlyMo,239
15
+ atk_package/env_utils.py,sha256=bXOrxM3fZUslqfmZt75iphbEJHbG4riJa8XOVzPwIII,313
16
+ atk_package/error_utils.py,sha256=cBDe5zOzOOuzRCs5WHo55knx74-ZUnDvaqrQtXBuZKw,1421
17
+ atk_package/http_utils.py,sha256=5PjGYy-TiBbtNOElEDhsCs3BPDTfqg_aMQBNCuHfcrQ,90
18
+ atk_package/log_utils.py,sha256=DGGwXjD5jvpMZJNW1QBoJRdDaTY1e1Z69j0LSOOzsWA,419
19
+ atk_package/rabbitmq_consumer.py,sha256=wmZQieeONkfXB5RmM6yiNkwl0_QK4oWWDBSfiD47CdA,1753
20
+ atk_package/response_utils.py,sha256=ezqTSNdGwueFXzijKB7CKPWxZml39bqjAZOeIMWMntk,197
21
+ atk_package/enums/__init__.py,sha256=oW0aVnwewfH6sNu14jjZ5_uP4iRN4pRmGdTNC204rHo,236
22
+ atk_package/enums/command_status_enum.py,sha256=M2Nln27a_DbzI07-gfytWQk2X087JhkU6Fmard5qVHs,127
23
+ atk_package/enums/speed_control_status_enum.py,sha256=qpURh0K1L1tSpbrzVnckoe4hUn1illIkbo7k4mLfzIM,182
24
+ shared_python_atk_enforcement/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
25
+ atk_common-1.4.0.dist-info/METADATA,sha256=WDZvHM1C2qyv4CZcFatHogVf8xonI0FF1SFO6XNcuRc,1193
26
+ atk_common-1.4.0.dist-info/WHEEL,sha256=PZUExdf71Ui_so67QXpySuHtCi3-J3wvF4ORK6k_S8U,91
27
+ atk_common-1.4.0.dist-info/license.txt,sha256=_0O6fWM00-wTurDjnZhUP_N5QiwGhItaQZqHq5eqadA,1063
28
+ atk_common-1.4.0.dist-info/top_level.txt,sha256=4CwRjkLnheIdI4jQwc4tK3dbRc58WqUmoqjkdDTWlME,41
29
+ atk_common-1.4.0.dist-info/RECORD,,
@@ -1,2 +1,2 @@
1
- atk_package
1
+ atk_common
2
2
  shared_python_atk_enforcement
atk_package/__init__.py CHANGED
@@ -1,11 +1,11 @@
1
1
  # __init__.py
2
- from atk_package.datetime_utils import date_time_utc, get_utc_date_time
3
- from atk_package.env_utils import get_env_value
4
- from atk_package.error_utils import get_message, get_error_entity, handle_error, get_response_error, get_error_type
5
- from atk_package.http_utils import is_status_code_ok
6
- from atk_package.log_utils import add_log_item, add_log_item_http
7
- from atk_package.rabbitmq_consumer import RabbitMQConsumer
8
- from atk_package.response_utils import create_save_resp
2
+ from atk_common.datetime_utils import date_time_utc, get_utc_date_time
3
+ from atk_common.env_utils import get_env_value
4
+ from atk_common.error_utils import get_message, get_error_entity, handle_error, get_response_error, get_error_type
5
+ from atk_common.http_utils import is_status_code_ok
6
+ from atk_common.log_utils import add_log_item, add_log_item_http
7
+ from atk_common.rabbitmq_consumer import RabbitMQConsumer
8
+ from atk_common.response_utils import create_save_resp
9
9
 
10
10
  __all__ = [
11
11
  'date_time_utc',
@@ -1,6 +1,6 @@
1
1
  # __init__.py
2
- from atk_package.enums.command_status_enum import CommandStatusType
3
- from atk_package.enums.speed_control_status_enum import SpeedControlStatusType
2
+ from atk_common.enums.command_status_enum import CommandStatusType
3
+ from atk_common.enums.speed_control_status_enum import SpeedControlStatusType
4
4
 
5
5
  __all__ = [
6
6
  'CommandStatusType',
atk_package/env_utils.py CHANGED
@@ -1,5 +1,5 @@
1
1
  import os
2
- from atk_package.log_utils import add_log_item
2
+ from atk_common.log_utils import add_log_item
3
3
 
4
4
  def get_env_value(key, default_value):
5
5
  try:
@@ -1,8 +1,8 @@
1
1
  from datetime import datetime
2
2
  import json
3
- from atk_package.datetime_utils import get_utc_date_time
4
- from atk_package.log_utils import add_log_item
5
- from atk_package.response_utils import create_save_resp
3
+ from atk_common.datetime_utils import get_utc_date_time
4
+ from atk_common.log_utils import add_log_item
5
+ from atk_common.response_utils import create_save_resp
6
6
 
7
7
  def get_message(error):
8
8
  if hasattr(error, 'message'):
atk_package/log_utils.py CHANGED
@@ -1,4 +1,4 @@
1
- from atk_package.datetime_utils import get_utc_date_time
1
+ from atk_common.datetime_utils import get_utc_date_time
2
2
 
3
3
  def create_date_time():
4
4
  date_time = get_utc_date_time()
@@ -1,17 +0,0 @@
1
- atk_package/__init__.py,sha256=AkUS0g4hl_YQHI3hOUAxHk1B3KsdsJyDQn2qHuHcpd0,818
2
- atk_package/datetime_utils.py,sha256=qsVF7l90P1-xukG2tV_jLqG9J_Yfl5wTpyfrdPBlyMo,239
3
- atk_package/env_utils.py,sha256=4WRQGZz-j2eFqNK3wEK9k3wja0X4_mZmOPUPMX-vSDY,314
4
- atk_package/error_utils.py,sha256=70-bnNsd_ktHlrDB1o_d_qGUpYvUe25Ycz2myz8vU_o,1424
5
- atk_package/http_utils.py,sha256=5PjGYy-TiBbtNOElEDhsCs3BPDTfqg_aMQBNCuHfcrQ,90
6
- atk_package/log_utils.py,sha256=ihsbrooDT3cwdDa9VmSaY6bTPke4tTQm2LhZnmnmLQ4,420
7
- atk_package/rabbitmq_consumer.py,sha256=wmZQieeONkfXB5RmM6yiNkwl0_QK4oWWDBSfiD47CdA,1753
8
- atk_package/response_utils.py,sha256=ezqTSNdGwueFXzijKB7CKPWxZml39bqjAZOeIMWMntk,197
9
- atk_package/enums/__init__.py,sha256=t4f3eoekm9pF38yLLgg0s8niItF8Qi2ybC4MpgtxQOU,238
10
- atk_package/enums/command_status_enum.py,sha256=M2Nln27a_DbzI07-gfytWQk2X087JhkU6Fmard5qVHs,127
11
- atk_package/enums/speed_control_status_enum.py,sha256=qpURh0K1L1tSpbrzVnckoe4hUn1illIkbo7k4mLfzIM,182
12
- shared_python_atk_enforcement/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
13
- atk_common-1.2.0.dist-info/METADATA,sha256=7JlDlP_he9T9YWhKMRoj4Np6EP3huqB0aO1Al_RFp78,1193
14
- atk_common-1.2.0.dist-info/WHEEL,sha256=PZUExdf71Ui_so67QXpySuHtCi3-J3wvF4ORK6k_S8U,91
15
- atk_common-1.2.0.dist-info/license.txt,sha256=_0O6fWM00-wTurDjnZhUP_N5QiwGhItaQZqHq5eqadA,1063
16
- atk_common-1.2.0.dist-info/top_level.txt,sha256=4HLkJ8mtbeZikG_9tMb5-dw9UWuAQk3xUN_izlisloQ,42
17
- atk_common-1.2.0.dist-info/RECORD,,