|
@@ -1,44 +1,54 @@
|
|
|
from subprocess import check_output
|
|
|
import yaml
|
|
|
import os
|
|
|
+import errno
|
|
|
import logging, logging.handlers
|
|
|
|
|
|
+currentdir = os.path.dirname(os.path.abspath(__file__))
|
|
|
|
|
|
-def config_load(filename: str):
|
|
|
+# Load must be called
|
|
|
+config = {}
|
|
|
+
|
|
|
+
|
|
|
+def load(filename: str):
|
|
|
global config
|
|
|
- with open(filename, "r") as fp:
|
|
|
- config = yaml.safe_load(fp)
|
|
|
|
|
|
+ if not os.path.isfile(filename):
|
|
|
+ raise FileNotFoundError(errno.ENOENT, os.strerror(errno.ENOENT), filename)
|
|
|
|
|
|
-if os.path.exists("config_dev.yaml"):
|
|
|
- config_load("config_dev.yaml")
|
|
|
-else:
|
|
|
- config_load("config.yaml")
|
|
|
+ with open(filename, "r") as fp:
|
|
|
+ config = yaml.safe_load(fp)
|
|
|
+ if "debug" in config and config["debug"]:
|
|
|
+ level = logging.DEBUG
|
|
|
+ else:
|
|
|
+ level = logging.INFO
|
|
|
|
|
|
-currentdir = os.path.dirname( os.path.abspath( __file__ ) )
|
|
|
+ root = logging.getLogger(None)
|
|
|
+ ch = logging.StreamHandler()
|
|
|
+ ch.setLevel(level)
|
|
|
+ formatter = logging.Formatter(
|
|
|
+ "%(asctime)s - %(filename)s (%(lineno)d) - %(name)s - %(levelname)s - %(message)s"
|
|
|
+ )
|
|
|
+ ch.setFormatter(formatter)
|
|
|
+ root.addHandler(ch)
|
|
|
|
|
|
-if 'debug' in config and config['debug']:
|
|
|
- level = logging.DEBUG
|
|
|
-else:
|
|
|
- level = logging.INFO
|
|
|
+ if "logfile" in config and config["logfile"]:
|
|
|
+ fhlog = logging.handlers.TimedRotatingFileHandler(
|
|
|
+ filename=os.path.join(currentdir, config["logfile"]),
|
|
|
+ when="midnight",
|
|
|
+ backupCount=7,
|
|
|
+ )
|
|
|
+ fhlog.setFormatter(formatter)
|
|
|
+ fhlog.setLevel(level)
|
|
|
+ root.addHandler(fhlog)
|
|
|
|
|
|
-root = logging.getLogger(None)
|
|
|
-ch = logging.StreamHandler()
|
|
|
-ch.setLevel( level )
|
|
|
-formatter = logging.Formatter( '%(asctime)s - %(filename)s (%(lineno)d) - %(name)s - %(levelname)s - %(message)s' )
|
|
|
-ch.setFormatter( formatter )
|
|
|
-root.addHandler( ch )
|
|
|
+ root.setLevel(level)
|
|
|
+ # print("config:", config)
|
|
|
|
|
|
-if 'logfile' in config and config['logfile']:
|
|
|
- fhlog = logging.handlers.TimedRotatingFileHandler( filename = os.path.join( currentdir, 'twgs_proxy.log' ), when = 'midnight', backupCount = 7 )
|
|
|
- fhlog.setFormatter( formatter )
|
|
|
- fhlog.setLevel( level )
|
|
|
- root.addHandler(fhlog)
|
|
|
|
|
|
-root.setLevel(level)
|
|
|
# with open('logging.config', 'r') as fp:
|
|
|
# LOGGING_CONFIG = yaml.safe_load(fp)
|
|
|
-
|
|
|
+
|
|
|
# Extract the version information from git.
|
|
|
# The match gives us only tags starting with v[0-9]* Using anything else trips up on double digits.
|
|
|
version = check_output(
|