atk-common 1.2.0__py3-none-any.whl → 1.3.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 +24 -0
- atk_common/datetime_utils.py +7 -0
- atk_common/enums/__init__.py +8 -0
- atk_common/enums/command_status_enum.py +7 -0
- atk_common/enums/speed_control_status_enum.py +10 -0
- atk_common/env_utils.py +11 -0
- atk_common/error_utils.py +48 -0
- atk_common/http_utils.py +2 -0
- atk_common/log_utils.py +15 -0
- atk_common/rabbitmq_consumer.py +49 -0
- atk_common/response_utils.py +6 -0
- {atk_common-1.2.0.dist-info → atk_common-1.3.0.dist-info}/METADATA +1 -1
- atk_common-1.3.0.dist-info/RECORD +28 -0
- {atk_common-1.2.0.dist-info → atk_common-1.3.0.dist-info}/top_level.txt +1 -1
- atk_package/__init__.py +7 -7
- atk_package/enums/__init__.py +2 -2
- atk_package/env_utils.py +1 -1
- atk_package/error_utils.py +3 -3
- atk_package/log_utils.py +1 -1
- atk_common-1.2.0.dist-info/RECORD +0 -17
- {atk_common-1.2.0.dist-info → atk_common-1.3.0.dist-info}/WHEEL +0 -0
- {atk_common-1.2.0.dist-info → atk_common-1.3.0.dist-info}/license.txt +0 -0
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
|
+
]
|
atk_common/env_utils.py
ADDED
@@ -0,0 +1,11 @@
|
|
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
|
+
val = os.environ[key]
|
7
|
+
add_log_item(key + ':' + val)
|
8
|
+
return val
|
9
|
+
except (Exception) as error:
|
10
|
+
add_log_item(key + ':' + default_value)
|
11
|
+
return default_value
|
@@ -0,0 +1,48 @@
|
|
1
|
+
from datetime import datetime
|
2
|
+
import json
|
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
|
+
|
7
|
+
def get_message(error):
|
8
|
+
if hasattr(error, 'message'):
|
9
|
+
return str(error.message)
|
10
|
+
else:
|
11
|
+
return str(error)
|
12
|
+
|
13
|
+
def get_error_entity(app, error, component, method, error_type, status_code):
|
14
|
+
data = {}
|
15
|
+
data['statusCode'] = status_code
|
16
|
+
data['exceptionType'] = str(type(error))
|
17
|
+
data['errorType'] = error_type
|
18
|
+
data['message'] = get_message(error)
|
19
|
+
data['component'] = component
|
20
|
+
data['method'] = method
|
21
|
+
data['timestamp'] = get_utc_date_time()
|
22
|
+
return app.response_class(
|
23
|
+
response=json.dumps(data),
|
24
|
+
status=500,
|
25
|
+
mimetype='application/json'
|
26
|
+
)
|
27
|
+
|
28
|
+
def handle_error(resp, status):
|
29
|
+
if resp.status_code == 500:
|
30
|
+
add_log_item(resp.json().get('message'))
|
31
|
+
return create_save_resp(status, resp.status_code, resp.json())
|
32
|
+
else:
|
33
|
+
add_log_item(resp.text)
|
34
|
+
return create_save_resp(status, resp.status_code, resp.text)
|
35
|
+
|
36
|
+
def get_response_error(resp):
|
37
|
+
if resp.status_code == 500:
|
38
|
+
return resp.json()
|
39
|
+
else:
|
40
|
+
return resp.text
|
41
|
+
|
42
|
+
# Return values:
|
43
|
+
# 1 - Connection error
|
44
|
+
# 2 - Database error
|
45
|
+
def get_error_type(conn):
|
46
|
+
if conn is None:
|
47
|
+
return 1
|
48
|
+
return 2
|
atk_common/http_utils.py
ADDED
atk_common/log_utils.py
ADDED
@@ -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,49 @@
|
|
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, encoding, message_handler, log):
|
7
|
+
|
8
|
+
rabbit_url = 'amqp://' + user + ':' + pwd + '@' + host + '/' + vhost
|
9
|
+
|
10
|
+
self.connection = Connection(rabbit_url, heartbeat=10)
|
11
|
+
queue = None
|
12
|
+
if dlx is not None and dlq is not None:
|
13
|
+
queue = Queue(name=queue_name,
|
14
|
+
queue_arguments={
|
15
|
+
'x-dead-letter-exchange': dlx,
|
16
|
+
'x-dead-letter-routing-key': dlq},
|
17
|
+
)
|
18
|
+
else:
|
19
|
+
queue = Queue(name=queue_name)
|
20
|
+
self.consumer = Consumer(self.connection, queues=queue, callbacks=[message_handler], accept=[encoding])
|
21
|
+
self.consumer.consume()
|
22
|
+
|
23
|
+
self.message_handler = message_handler # Custom message handler
|
24
|
+
self.log = log
|
25
|
+
|
26
|
+
def consume(self):
|
27
|
+
new_conn = self.establish_connection()
|
28
|
+
while True:
|
29
|
+
try:
|
30
|
+
new_conn.drain_events(timeout=2)
|
31
|
+
except socket.timeout:
|
32
|
+
new_conn.heartbeat_check()
|
33
|
+
|
34
|
+
def establish_connection(self):
|
35
|
+
revived_connection = self.connection.clone()
|
36
|
+
revived_connection.ensure_connection(max_retries=3)
|
37
|
+
channel = revived_connection.channel()
|
38
|
+
self.consumer.revive(channel)
|
39
|
+
self.consumer.consume()
|
40
|
+
self.log("Connection revived!")
|
41
|
+
return revived_connection
|
42
|
+
|
43
|
+
def run(self):
|
44
|
+
while True:
|
45
|
+
try:
|
46
|
+
self.consume()
|
47
|
+
except self.connection.connection_errors:
|
48
|
+
pass
|
49
|
+
|
@@ -0,0 +1,28 @@
|
|
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/env_utils.py,sha256=bXOrxM3fZUslqfmZt75iphbEJHbG4riJa8XOVzPwIII,313
|
4
|
+
atk_common/error_utils.py,sha256=cBDe5zOzOOuzRCs5WHo55knx74-ZUnDvaqrQtXBuZKw,1421
|
5
|
+
atk_common/http_utils.py,sha256=5PjGYy-TiBbtNOElEDhsCs3BPDTfqg_aMQBNCuHfcrQ,90
|
6
|
+
atk_common/log_utils.py,sha256=DGGwXjD5jvpMZJNW1QBoJRdDaTY1e1Z69j0LSOOzsWA,419
|
7
|
+
atk_common/rabbitmq_consumer.py,sha256=wmZQieeONkfXB5RmM6yiNkwl0_QK4oWWDBSfiD47CdA,1753
|
8
|
+
atk_common/response_utils.py,sha256=ezqTSNdGwueFXzijKB7CKPWxZml39bqjAZOeIMWMntk,197
|
9
|
+
atk_common/enums/__init__.py,sha256=oW0aVnwewfH6sNu14jjZ5_uP4iRN4pRmGdTNC204rHo,236
|
10
|
+
atk_common/enums/command_status_enum.py,sha256=M2Nln27a_DbzI07-gfytWQk2X087JhkU6Fmard5qVHs,127
|
11
|
+
atk_common/enums/speed_control_status_enum.py,sha256=qpURh0K1L1tSpbrzVnckoe4hUn1illIkbo7k4mLfzIM,182
|
12
|
+
atk_package/__init__.py,sha256=EceDa352WFHTdC_LxYANXf_TS9pGzK3MCx87bwAKTvg,811
|
13
|
+
atk_package/datetime_utils.py,sha256=qsVF7l90P1-xukG2tV_jLqG9J_Yfl5wTpyfrdPBlyMo,239
|
14
|
+
atk_package/env_utils.py,sha256=bXOrxM3fZUslqfmZt75iphbEJHbG4riJa8XOVzPwIII,313
|
15
|
+
atk_package/error_utils.py,sha256=cBDe5zOzOOuzRCs5WHo55knx74-ZUnDvaqrQtXBuZKw,1421
|
16
|
+
atk_package/http_utils.py,sha256=5PjGYy-TiBbtNOElEDhsCs3BPDTfqg_aMQBNCuHfcrQ,90
|
17
|
+
atk_package/log_utils.py,sha256=DGGwXjD5jvpMZJNW1QBoJRdDaTY1e1Z69j0LSOOzsWA,419
|
18
|
+
atk_package/rabbitmq_consumer.py,sha256=wmZQieeONkfXB5RmM6yiNkwl0_QK4oWWDBSfiD47CdA,1753
|
19
|
+
atk_package/response_utils.py,sha256=ezqTSNdGwueFXzijKB7CKPWxZml39bqjAZOeIMWMntk,197
|
20
|
+
atk_package/enums/__init__.py,sha256=oW0aVnwewfH6sNu14jjZ5_uP4iRN4pRmGdTNC204rHo,236
|
21
|
+
atk_package/enums/command_status_enum.py,sha256=M2Nln27a_DbzI07-gfytWQk2X087JhkU6Fmard5qVHs,127
|
22
|
+
atk_package/enums/speed_control_status_enum.py,sha256=qpURh0K1L1tSpbrzVnckoe4hUn1illIkbo7k4mLfzIM,182
|
23
|
+
shared_python_atk_enforcement/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
24
|
+
atk_common-1.3.0.dist-info/METADATA,sha256=XAqiKPtEPWZ2NmEwxPp0BWwxo2zzhHeDzNscV3VdoCE,1193
|
25
|
+
atk_common-1.3.0.dist-info/WHEEL,sha256=PZUExdf71Ui_so67QXpySuHtCi3-J3wvF4ORK6k_S8U,91
|
26
|
+
atk_common-1.3.0.dist-info/license.txt,sha256=_0O6fWM00-wTurDjnZhUP_N5QiwGhItaQZqHq5eqadA,1063
|
27
|
+
atk_common-1.3.0.dist-info/top_level.txt,sha256=4CwRjkLnheIdI4jQwc4tK3dbRc58WqUmoqjkdDTWlME,41
|
28
|
+
atk_common-1.3.0.dist-info/RECORD,,
|
@@ -1,2 +1,2 @@
|
|
1
|
-
|
1
|
+
atk_common
|
2
2
|
shared_python_atk_enforcement
|
atk_package/__init__.py
CHANGED
@@ -1,11 +1,11 @@
|
|
1
1
|
# __init__.py
|
2
|
-
from
|
3
|
-
from
|
4
|
-
from
|
5
|
-
from
|
6
|
-
from
|
7
|
-
from
|
8
|
-
from
|
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',
|
atk_package/enums/__init__.py
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
# __init__.py
|
2
|
-
from
|
3
|
-
from
|
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
atk_package/error_utils.py
CHANGED
@@ -1,8 +1,8 @@
|
|
1
1
|
from datetime import datetime
|
2
2
|
import json
|
3
|
-
from
|
4
|
-
from
|
5
|
-
from
|
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,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,,
|
File without changes
|
File without changes
|