-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathframework.py
74 lines (64 loc) · 2.55 KB
/
framework.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
import logging
from datetime import datetime
from dotenv import dotenv_values
import json
# Define console class used for printing colored timestamped messages
class console:
def timestamp():
return datetime.now().strftime('%H:%M:%S.%f')[:-4]
def print(message, color=''):
print(f'[{console.timestamp()}] {color}{str(message)}{console.ENDC}')
HEADER = '\033[95m'
OKBLUE = '\033[94m'
OKGREEN = '\033[92m'
WARNING = '\033[93m'
FAIL = '\033[91m'
BOLD = '\033[1m'
UNDERLINE = '\033[4m'
ENDC = '\033[0m'
# Define env class used for loading env variables from ".env" file into one array
class env:
def empty(key):
e = f'Key "{key}" empty in Dotenv file!'
logger.error(e)
raise KeyError(e)
def load():
try:
envValues = dotenv_values()
if envValues != {}:
# If key is empty, raise KeyError using env.empty(key)
envHotspots = envValues['HELIUM_HOTSPOTS'] if envValues['HELIUM_HOTSPOTS'] != '[]' else env.empty('HELIUM_HOTSPOTS')
envDateStart = envValues['HELIUM_DATE_START'] if envValues['HELIUM_DATE_START'] != '' else env.empty('HELIUM_DATE_START')
envDateEnd = envValues['HELIUM_DATE_END'] if envValues['HELIUM_DATE_END'] != '' else env.empty('HELIUM_DATE_END')
try:
json.loads(envHotspots)
except json.decoder.JSONDecodeError as e:
raise KeyError('HELIUM_HOTSPOTS')
return {
'HELIUM_HOTSPOTS': envHotspots,
'HELIUM_DATE_START': envDateStart,
'HELIUM_DATE_END': envDateEnd,
}
else:
e_userFriendly = 'Dotenv file not found or empty!'
raise FileNotFoundError(e_userFriendly)
# Catch blank file or inexistent file
except FileNotFoundError as e:
e_userFriendly = f'{type(e).__name__}: {e.args[0]}'
console.print(e_userFriendly, color=console.FAIL)
logger.error(e_userFriendly)
exit()
# Catch miconfigured values (general)
except KeyError as e:
e_userFriendly = f'{type(e).__name__}: Key "{e.args[0]}" is misconfigured!'
console.print(e_userFriendly, color=console.FAIL)
logger.error(e_userFriendly)
exit()
# Define logger class used for logging events to a log file set on line :67
class logger:
logging.basicConfig(filename=f'result.log', filemode='w', level=logging.INFO, format='%(asctime)s § %(name)-24s § %(filename)-38s § %(threadName)-36s § %(lineno)-7s § %(levelname)-8s § %(message)s', datefmt='%Y-%m-%d %T%Z')
def start(filename='result.log', level=logging.INFO, loggername='helium.logger'):
logger = logging.getLogger(loggername); logger.setLevel(level)
assert logger.parent == logging.root
return logger
logger = logger.start()