Spaces:
Runtime error
Runtime error
Hansimov
commited on
Commit
•
5b642ff
0
Parent(s):
:gem: [Feature] New enver and logger system
Browse files- utils/__init__.py +69 -0
- utils/enver.py +0 -0
- utils/logger.py +251 -0
utils/__init__.py
ADDED
@@ -0,0 +1,69 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import json
|
2 |
+
import requests
|
3 |
+
import os
|
4 |
+
|
5 |
+
from pathlib import Path
|
6 |
+
|
7 |
+
|
8 |
+
class OSEnver:
|
9 |
+
def __init__(self):
|
10 |
+
self.envs_stack = []
|
11 |
+
self.envs = os.environ.copy()
|
12 |
+
|
13 |
+
def store_envs(self):
|
14 |
+
self.envs_stack.append(self.envs)
|
15 |
+
|
16 |
+
def restore_envs(self):
|
17 |
+
self.envs = self.envs_stack.pop()
|
18 |
+
if self.global_scope:
|
19 |
+
os.environ = self.envs
|
20 |
+
|
21 |
+
def set_envs(self, secrets=True, proxies=None, store_envs=True):
|
22 |
+
# caller_info = inspect.stack()[1]
|
23 |
+
# logger.back(f"OS Envs is set by: {caller_info.filename}")
|
24 |
+
|
25 |
+
if store_envs:
|
26 |
+
self.store_envs()
|
27 |
+
|
28 |
+
if secrets:
|
29 |
+
secrets_path = Path(__file__).parents[1] / "secrets.json"
|
30 |
+
if secrets_path.exists():
|
31 |
+
with open(secrets_path, "r") as rf:
|
32 |
+
secrets = json.load(rf)
|
33 |
+
else:
|
34 |
+
secrets = {}
|
35 |
+
|
36 |
+
if proxies:
|
37 |
+
for proxy_env in ["http_proxy", "https_proxy"]:
|
38 |
+
if isinstance(proxies, str):
|
39 |
+
self.envs[proxy_env] = proxies
|
40 |
+
elif "http_proxy" in secrets.keys():
|
41 |
+
self.envs[proxy_env] = secrets["http_proxy"]
|
42 |
+
elif os.getenv("http_proxy"):
|
43 |
+
self.envs[proxy_env] = os.getenv("http_proxy")
|
44 |
+
else:
|
45 |
+
continue
|
46 |
+
|
47 |
+
self.proxy = (
|
48 |
+
self.envs.get("all_proxy")
|
49 |
+
or self.envs.get("http_proxy")
|
50 |
+
or self.envs.get("https_proxy")
|
51 |
+
or None
|
52 |
+
)
|
53 |
+
self.requests_proxies = {
|
54 |
+
"http": self.proxy,
|
55 |
+
"https": self.proxy,
|
56 |
+
}
|
57 |
+
|
58 |
+
# https://www.proxynova.com/proxy-server-list/country-us/
|
59 |
+
|
60 |
+
print(f"Using proxy: [{self.proxy}]")
|
61 |
+
# r = requests.get(
|
62 |
+
# "http://ifconfig.me/ip",
|
63 |
+
# proxies=self.requests_proxies,
|
64 |
+
# timeout=10,
|
65 |
+
# )
|
66 |
+
# print(f"[r.status_code] r.text")
|
67 |
+
|
68 |
+
|
69 |
+
enver = OSEnver()
|
utils/enver.py
ADDED
File without changes
|
utils/logger.py
ADDED
@@ -0,0 +1,251 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import datetime
|
2 |
+
import functools
|
3 |
+
import inspect
|
4 |
+
import logging
|
5 |
+
import os
|
6 |
+
import shutil
|
7 |
+
import subprocess
|
8 |
+
from termcolor import colored
|
9 |
+
|
10 |
+
|
11 |
+
def add_fillers(text, filler="=", fill_side="both"):
|
12 |
+
terminal_width = shutil.get_terminal_size().columns
|
13 |
+
text = text.strip()
|
14 |
+
text_width = len(text)
|
15 |
+
if text_width >= terminal_width:
|
16 |
+
return text
|
17 |
+
|
18 |
+
if fill_side[0].lower() == "b":
|
19 |
+
leading_fill_str = filler * ((terminal_width - text_width) // 2 - 1) + " "
|
20 |
+
trailing_fill_str = " " + filler * (
|
21 |
+
terminal_width - text_width - len(leading_fill_str) - 1
|
22 |
+
)
|
23 |
+
elif fill_side[0].lower() == "l":
|
24 |
+
leading_fill_str = filler * (terminal_width - text_width - 1) + " "
|
25 |
+
trailing_fill_str = ""
|
26 |
+
elif fill_side[0].lower() == "r":
|
27 |
+
leading_fill_str = ""
|
28 |
+
trailing_fill_str = " " + filler * (terminal_width - text_width - 1)
|
29 |
+
else:
|
30 |
+
raise ValueError("Invalid fill_side")
|
31 |
+
|
32 |
+
filled_str = f"{leading_fill_str}{text}{trailing_fill_str}"
|
33 |
+
return filled_str
|
34 |
+
|
35 |
+
|
36 |
+
class Logger:
|
37 |
+
LOG_METHODS = {
|
38 |
+
"err": ("error", "red"),
|
39 |
+
"warn": ("warning", "light_red"),
|
40 |
+
"note": ("info", "light_magenta"),
|
41 |
+
"mesg": ("info", "light_cyan"),
|
42 |
+
"file": ("info", "light_blue"),
|
43 |
+
"line": ("info", "white"),
|
44 |
+
"success": ("info", "light_green"),
|
45 |
+
"fail": ("info", "light_red"),
|
46 |
+
"back": ("debug", "light_cyan"),
|
47 |
+
}
|
48 |
+
INDENT_METHODS = [
|
49 |
+
"indent",
|
50 |
+
"set_indent",
|
51 |
+
"reset_indent",
|
52 |
+
"store_indent",
|
53 |
+
"restore_indent",
|
54 |
+
"log_indent",
|
55 |
+
]
|
56 |
+
LEVEL_METHODS = [
|
57 |
+
"set_level",
|
58 |
+
"store_level",
|
59 |
+
"restore_level",
|
60 |
+
"quiet",
|
61 |
+
"enter_quiet",
|
62 |
+
"exit_quiet",
|
63 |
+
]
|
64 |
+
LEVEL_NAMES = {
|
65 |
+
"critical": logging.CRITICAL,
|
66 |
+
"error": logging.ERROR,
|
67 |
+
"warning": logging.WARNING,
|
68 |
+
"info": logging.INFO,
|
69 |
+
"debug": logging.DEBUG,
|
70 |
+
}
|
71 |
+
|
72 |
+
def __init__(self, name=None, prefix=False):
|
73 |
+
if not name:
|
74 |
+
frame = inspect.stack()[1]
|
75 |
+
module = inspect.getmodule(frame[0])
|
76 |
+
name = module.__name__
|
77 |
+
|
78 |
+
self.logger = logging.getLogger(name)
|
79 |
+
self.logger.setLevel(logging.INFO)
|
80 |
+
|
81 |
+
if prefix:
|
82 |
+
formatter_prefix = "[%(asctime)s] - [%(name)s] - [%(levelname)s]\n"
|
83 |
+
else:
|
84 |
+
formatter_prefix = ""
|
85 |
+
|
86 |
+
self.formatter = logging.Formatter(formatter_prefix + "%(message)s")
|
87 |
+
|
88 |
+
self.handler = logging.StreamHandler()
|
89 |
+
self.handler.setLevel(logging.INFO)
|
90 |
+
|
91 |
+
self.handler.setFormatter(self.formatter)
|
92 |
+
self.logger.addHandler(self.handler)
|
93 |
+
|
94 |
+
self.log_indent = 0
|
95 |
+
self.log_indents = []
|
96 |
+
|
97 |
+
self.log_level = "info"
|
98 |
+
self.log_levels = []
|
99 |
+
|
100 |
+
self.bind_functions()
|
101 |
+
|
102 |
+
def indent(self, indent=2):
|
103 |
+
self.log_indent += indent
|
104 |
+
|
105 |
+
def set_indent(self, indent=2):
|
106 |
+
self.log_indent = indent
|
107 |
+
|
108 |
+
def reset_indent(self):
|
109 |
+
self.log_indent = 0
|
110 |
+
|
111 |
+
def store_indent(self):
|
112 |
+
self.log_indents.append(self.log_indent)
|
113 |
+
|
114 |
+
def restore_indent(self):
|
115 |
+
self.log_indent = self.log_indents.pop(-1)
|
116 |
+
|
117 |
+
def set_level(self, level):
|
118 |
+
self.log_level = level
|
119 |
+
self.logger.setLevel(self.LEVEL_NAMES[level])
|
120 |
+
|
121 |
+
def store_level(self):
|
122 |
+
self.log_levels.append(self.log_level)
|
123 |
+
|
124 |
+
def restore_level(self):
|
125 |
+
self.log_level = self.log_levels.pop(-1)
|
126 |
+
self.set_level(self.log_level)
|
127 |
+
|
128 |
+
def quiet(self):
|
129 |
+
self.set_level("critical")
|
130 |
+
|
131 |
+
def enter_quiet(self, quiet=False):
|
132 |
+
if quiet:
|
133 |
+
self.store_level()
|
134 |
+
self.quiet()
|
135 |
+
|
136 |
+
def exit_quiet(self, quiet=False):
|
137 |
+
if quiet:
|
138 |
+
self.restore_level()
|
139 |
+
|
140 |
+
def log(
|
141 |
+
self,
|
142 |
+
method,
|
143 |
+
msg,
|
144 |
+
indent=0,
|
145 |
+
fill=False,
|
146 |
+
fill_side="both",
|
147 |
+
end="\n",
|
148 |
+
*args,
|
149 |
+
**kwargs,
|
150 |
+
):
|
151 |
+
if type(msg) == str:
|
152 |
+
msg_str = msg
|
153 |
+
else:
|
154 |
+
msg_str = repr(msg)
|
155 |
+
quotes = ["'", '"']
|
156 |
+
if msg_str[0] in quotes and msg_str[-1] in quotes:
|
157 |
+
msg_str = msg_str[1:-1]
|
158 |
+
|
159 |
+
indent_str = " " * (self.log_indent + indent)
|
160 |
+
indented_msg = "\n".join([indent_str + line for line in msg_str.split("\n")])
|
161 |
+
|
162 |
+
if fill:
|
163 |
+
indented_msg = add_fillers(indented_msg, fill_side=fill_side)
|
164 |
+
|
165 |
+
handler = self.logger.handlers[0]
|
166 |
+
handler.terminator = end
|
167 |
+
|
168 |
+
level, color = self.LOG_METHODS[method]
|
169 |
+
getattr(self.logger, level)(colored(indented_msg, color), *args, **kwargs)
|
170 |
+
|
171 |
+
def bind_functions(self):
|
172 |
+
for method in self.LOG_METHODS:
|
173 |
+
setattr(self.logger, method, functools.partial(self.log, method))
|
174 |
+
|
175 |
+
for method in self.INDENT_METHODS:
|
176 |
+
setattr(self.logger, method, getattr(self, method))
|
177 |
+
|
178 |
+
for method in self.LEVEL_METHODS:
|
179 |
+
setattr(self.logger, method, getattr(self, method))
|
180 |
+
|
181 |
+
|
182 |
+
logger = Logger().logger
|
183 |
+
|
184 |
+
|
185 |
+
def shell_cmd(cmd, getoutput=False, showcmd=True, env=None):
|
186 |
+
if showcmd:
|
187 |
+
logger.info(colored(f"\n$ [{os.getcwd()}]", "light_blue"))
|
188 |
+
logger.info(colored(f" $ {cmd}\n", "light_cyan"))
|
189 |
+
if getoutput:
|
190 |
+
output = subprocess.getoutput(cmd, env=env)
|
191 |
+
return output
|
192 |
+
else:
|
193 |
+
subprocess.run(cmd, shell=True, env=env)
|
194 |
+
|
195 |
+
|
196 |
+
class Runtimer:
|
197 |
+
def __enter__(self):
|
198 |
+
self.t1, _ = self.start_time()
|
199 |
+
return self
|
200 |
+
|
201 |
+
def __exit__(self, exc_type, exc_value, traceback):
|
202 |
+
self.t2, _ = self.end_time()
|
203 |
+
self.elapsed_time(self.t2 - self.t1)
|
204 |
+
|
205 |
+
def start_time(self):
|
206 |
+
t1 = datetime.datetime.now()
|
207 |
+
self.logger_time("start", t1)
|
208 |
+
return t1, self.time2str(t1)
|
209 |
+
|
210 |
+
def end_time(self):
|
211 |
+
t2 = datetime.datetime.now()
|
212 |
+
self.logger_time("end", t2)
|
213 |
+
return t2, self.time2str(t2)
|
214 |
+
|
215 |
+
def elapsed_time(self, dt=None):
|
216 |
+
if dt is None:
|
217 |
+
dt = self.t2 - self.t1
|
218 |
+
self.logger_time("elapsed", dt)
|
219 |
+
return dt, self.time2str(dt)
|
220 |
+
|
221 |
+
def logger_time(self, time_type, t):
|
222 |
+
time_types = {
|
223 |
+
"start": "Start",
|
224 |
+
"end": "End",
|
225 |
+
"elapsed": "Elapsed",
|
226 |
+
}
|
227 |
+
time_str = add_fillers(
|
228 |
+
colored(
|
229 |
+
f"{time_types[time_type]} time: [ {self.time2str(t)} ]",
|
230 |
+
"light_magenta",
|
231 |
+
),
|
232 |
+
fill_side="both",
|
233 |
+
)
|
234 |
+
logger.line(time_str)
|
235 |
+
|
236 |
+
# Convert time to string
|
237 |
+
def time2str(self, t):
|
238 |
+
datetime_str_format = "%Y-%m-%d %H:%M:%S"
|
239 |
+
if isinstance(t, datetime.datetime):
|
240 |
+
return t.strftime(datetime_str_format)
|
241 |
+
elif isinstance(t, datetime.timedelta):
|
242 |
+
hours = t.seconds // 3600
|
243 |
+
hour_str = f"{hours} hr" if hours > 0 else ""
|
244 |
+
minutes = (t.seconds // 60) % 60
|
245 |
+
minute_str = f"{minutes:>2} min" if minutes > 0 else ""
|
246 |
+
seconds = t.seconds % 60
|
247 |
+
second_str = f"{seconds:>2} s"
|
248 |
+
time_str = " ".join([hour_str, minute_str, second_str]).strip()
|
249 |
+
return time_str
|
250 |
+
else:
|
251 |
+
return str(t)
|