1
|
|
|
import os |
2
|
|
|
import sys |
3
|
|
|
import time |
4
|
|
|
import traceback |
5
|
|
|
|
6
|
|
|
""" |
7
|
|
|
Copyright (c) 2020 Star Inc.(https://starinc.xyz) |
8
|
|
|
|
9
|
|
|
This Source Code Form is subject to the terms of the Mozilla Public |
10
|
|
|
License, v. 2.0. If a copy of the MPL was not distributed with this |
11
|
|
|
file, You can obtain one at http://mozilla.org/MPL/2.0/. |
12
|
|
|
""" |
13
|
|
|
|
14
|
|
|
check_file = ".alive" |
15
|
|
|
|
16
|
|
|
|
17
|
|
|
class Tools: |
18
|
|
|
@staticmethod |
19
|
|
|
def get_time(time_format: str = "%b %d %Y %H:%M:%S %Z"): |
20
|
|
|
""" |
21
|
|
|
Get datetime with format |
22
|
|
|
|
23
|
|
|
:param time_format: string of format codes |
24
|
|
|
:return: |
25
|
|
|
""" |
26
|
|
|
time_ = time.localtime(time.time()) |
27
|
|
|
return time.strftime(time_format, time_) |
28
|
|
|
|
29
|
|
|
@staticmethod |
30
|
|
|
def error_report(): |
31
|
|
|
""" |
32
|
|
|
Report errors as message |
33
|
|
|
|
34
|
|
|
:return: string |
35
|
|
|
""" |
36
|
|
|
occur_time = Tools.get_time() |
37
|
|
|
err1, err2, err3 = sys.exc_info() |
38
|
|
|
traceback.print_tb(err3) |
39
|
|
|
tb_info = traceback.extract_tb(err3) |
40
|
|
|
filename, line, _, text = tb_info[-1] |
41
|
|
|
error_info = "occurred in\n{}\non line {}\nin statement {}".format(filename, line, text) |
42
|
|
|
return "%s\nSystem Error:\n%s\n%s\n%s\n%s\n" % (occur_time, err1, err2, err3, error_info) |
43
|
|
|
|
44
|
|
|
@staticmethod |
45
|
|
|
def logger(error_msg, silent: bool = True): |
46
|
|
|
""" |
47
|
|
|
Journal or print error message |
48
|
|
|
|
49
|
|
|
:return: |
50
|
|
|
""" |
51
|
|
|
if silent: |
52
|
|
|
with open("service.log", "a+") as log: |
53
|
|
|
log.write(error_msg + "\n") |
54
|
|
|
else: |
55
|
|
|
print(error_msg) |
56
|
|
|
|
57
|
|
|
@staticmethod |
58
|
|
|
def set_ready(status: bool): |
59
|
|
|
""" |
60
|
|
|
Set status whether service is ready or not |
61
|
|
|
|
62
|
|
|
:param status: bool of status |
63
|
|
|
:return: |
64
|
|
|
""" |
65
|
|
|
if status and not os.path.exists(check_file): |
66
|
|
|
with open(check_file, "w") as f: |
67
|
|
|
f.write("") |
68
|
|
|
elif not status and os.path.exists(check_file): |
69
|
|
|
os.remove(check_file) |
70
|
|
|
|
71
|
|
|
@staticmethod |
72
|
|
|
def check_ready(): |
73
|
|
|
""" |
74
|
|
|
Check status that service is ready or not |
75
|
|
|
|
76
|
|
|
:return: bool of status |
77
|
|
|
""" |
78
|
|
|
return os.path.isfile(check_file) |
79
|
|
|
|
80
|
|
|
@staticmethod |
81
|
|
|
def lists_separate(lists: list, numbers: int): |
82
|
|
|
""" |
83
|
|
|
Split lists to average |
84
|
|
|
|
85
|
|
|
:param lists: list you want to separate |
86
|
|
|
:param numbers: numbers in part you want |
87
|
|
|
:return: |
88
|
|
|
""" |
89
|
|
|
for count in range(0, len(lists), numbers): |
90
|
|
|
yield lists[count:count + numbers] |
91
|
|
|
|