22from dateutil import tz
33from dateutil .relativedelta import relativedelta
44from enum import IntEnum , Enum , IntFlag
5- import yaml
65import traceback
76import os
87import html
98import re
109import hashlib
1110import math
1211import base64
13- import random
1412import struct
1513from itertools import chain
1614from pygal .style import DarkStyle
1715from api import ApiType
1816from consts import *
1917
2018
21- log_file : str = None
22- db_file = ""
23- host = "127.0.0.1"
24- port = 5000
25- num_threads = 2
26- uri = ""
27- embed_lichess = False
2819refresh_insights_times = {}
2920
3021STYLE_WORD_BREAK = "word-break:break-word;" # "word-break:break-all;"
@@ -120,65 +111,39 @@ def add_timeout_msg(timeouts, msg):
120111 timeouts [msg .username ] = msg
121112
122113
123- def load_config ():
124- global log_file , db_file , host , port , num_threads , uri , embed_lichess
125- if log_file is None :
126- try :
127- with open (os .path .abspath (f"./{ CONFIG_FILE } " )) as stream :
128- config = yaml .safe_load (stream )
129- log_file = config .get ('log' , "" )
130- db_file = config .get ('db' , "" )
131- host = config .get ('host' , host )
132- port = config .get ('port' , port )
133- num_threads = config .get ('num_threads' , num_threads )
134- uri = config .get ('url' , "" )
135- embed_lichess = config .get ('embed_lichess' , False )
136- except Exception as e :
137- print (f"There appears to be a syntax problem with { CONFIG_FILE } : { e } " )
138- log_file = ""
114+ def get_log_file ():
115+ return os .getenv ("LOG_FILE" , "./log/log.txt" )
139116
140117
141118def get_token ():
142- try :
143- with open (os .path .abspath (f"./{ CONFIG_FILE } " )) as stream :
144- config = yaml .safe_load (stream )
145- token = config .get ('token' , "" )
146- except Exception as e :
147- log (f"There appears to be a syntax problem with { CONFIG_FILE } : { e } " , to_print = True , to_save = True )
148- token = ""
119+ token = os .getenv ("TOKEN" )
149120 if token :
150121 log ("using token" , to_print = True , to_save = True )
151122 return token
152123
153124
154125def get_db ():
155- load_config ()
156- return db_file
126+ return os .getenv ("DB_FILE" , "./db/litools.sqlite" )
157127
158128
159129def get_host ():
160- load_config ()
161- return host
130+ return os .getenv ("HOST" , "0.0.0.0" )
162131
163132
164133def get_port ():
165- load_config ()
166- return port
134+ return int (os .getenv ("PORT" , 5000 ))
167135
168136
169137def get_num_threads ():
170- load_config ()
171- return num_threads
138+ return int (os .getenv ("NUM_THREADS" , 2 ))
172139
173140
174141def get_uri ():
175- load_config ()
176- return uri
142+ return os .getenv ("APP_URL" , "http://localhost:5000" )
177143
178144
179145def get_embed_lichess ():
180- load_config ()
181- return embed_lichess
146+ return bool (os .getenv ("EMBED_LICHESS" , False ))
182147
183148
184149def needs_to_refresh_insights (user_id , now = None ):
@@ -982,21 +947,17 @@ def to_Tag(reason):
982947def log (text , to_print = False , to_save = True , verbose = 1 ):
983948 if verbose > VERBOSE :
984949 return
985- global log_file
986- if log_file is None :
987- load_config ()
988950 now_utc = datetime .now (tz = tz .tzutc ())
989951 line = f"{ now_utc : %Y-%m-%d %H:%M:%S} UTC: { text } "
990952 if to_print :
991953 print (line )
992- if not log_file or not to_save :
954+ if not to_save :
993955 return
994956 try :
995- with open (log_file , "a" , encoding = "utf-8" ) as file :
957+ with open (get_log_file () , "a" , encoding = "utf-8" ) as file :
996958 file .write (f"{ line } \n " )
997959 except Exception as exception :
998960 traceback .print_exception (type (exception ), exception , exception .__traceback__ )
999- log_file = ""
1000961
1001962
1002963def log_exception (exception , to_print = True , to_save = True ):
@@ -1010,6 +971,7 @@ def log_read(lines_per_page=100, page=0, reverse=True):
1010971 if lines_per_page < 0 or page < 0 :
1011972 return ""
1012973 max_size = 50_000_000
974+ log_file = get_log_file ()
1013975 file_size = os .stat (log_file ).st_size
1014976 with open (log_file , "r" , encoding = "utf-8" ) as file :
1015977 if file_size > max_size :
0 commit comments