-
Notifications
You must be signed in to change notification settings - Fork 0
/
config.py
66 lines (57 loc) · 2.31 KB
/
config.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
import os
from dotenv import load_dotenv
import logging
import json
def loadConfiguration():
""" Function used to load the userdata"""
# load the json data
try:
with open(os.getenv('CONFIG_FILE_PATH')) as f:
data = json.loads(f.read())
if data.get('CONTROL_CHATID') != None and data.get('USER_DATA') != None:
return data
else:
log.warn('[CONFIG] The user data is not present. If this is the first start of the bot, this is expected.')
return None
except Exception as e:
log.warn('[CONFIG] The user data is not present. If this is the first start of the bot, this is expected.')
return None
def saveConfiguration():
try:
data = {}
data['CONTROL_CHATID'] = MASTER_CHATID
data['USER_DATA'] = USER_DATA
with open(os.getenv('CONFIG_FILE_PATH'), 'w') as f:
f.write(json.dumps(data))
log.info('[CONFIG] Configuration file written.')
except Exception as e:
log.error('[CONFIG] Writing data file failed. Reason: ' + str(e))
def setupLogging():
# Set up the logger with file and console handlers
logger = logging.getLogger(__name__)
logger.setLevel(logging.DEBUG)
# Create a console handler and set its level to WARNING
console_handler = logging.StreamHandler()
console_handler.setLevel(os.getenv('CONSOLE_LOGGING_LEVEL'))
console_handler.setFormatter(logging.Formatter('[%(levelname)s] %(message)s'))
# Create a file handler and set its level to DEBUG
file_handler = logging.FileHandler(os.getenv('LOG_FILE_PATH'), mode='a')
file_handler.setLevel(os.getenv('FILE_LOGGING_LEVEL'))
file_handler.setFormatter(logging.Formatter('%(asctime)s [%(levelname)s] %(message)s', datefmt='[%d.%m.%y %H:%M:%S]'))
# Add the handlers to the logger
logger.addHandler(console_handler)
logger.addHandler(file_handler)
return logger
# Run the confiuration, will be executed on first import, only once
load_dotenv() # load the .env keys
log = setupLogging()
data = loadConfiguration()
if data == None:
MASTER_CHATID = ""
USER_DATA = {}
else:
MASTER_CHATID = data['CONTROL_CHATID']
USER_DATA = data['USER_DATA']
log.debug(USER_DATA)
del data # free up namespace
log.info('[CONFIG] Sytsem Started, Configuration loaded!')