From 633a07509e3521a05b643037dd0f633095eeca8a Mon Sep 17 00:00:00 2001 From: Taka Date: Mon, 16 Jul 2018 10:03:15 +0000 Subject: [PATCH] Let a debug config file be specified from the command line. --- vasl_templates/main.py | 9 +++++++-- vasl_templates/webapp/__init__.py | 22 +++++++++++++++------- 2 files changed, 22 insertions(+), 9 deletions(-) diff --git a/vasl_templates/main.py b/vasl_templates/main.py index 2f1a69d..671b560 100755 --- a/vasl_templates/main.py +++ b/vasl_templates/main.py @@ -15,7 +15,7 @@ import click from vasl_templates.main_window import MainWindow from vasl_templates.webapp import app as webapp -from vasl_templates.webapp import generate +from vasl_templates.webapp import generate, load_debug_config # --------------------------------------------------------------------- @@ -40,7 +40,8 @@ class LoggerProxy: @click.command() @click.option( "--template-pack", help="Template pack to auto-load (ZIP file or directory)." ) -def main( template_pack ): +@click.option( "--debug", help="Debug config file." ) +def main( template_pack, debug ): """Main entry point for the application.""" # configure the autoload template pack @@ -54,6 +55,10 @@ def main( template_pack ): return 1 generate.autoload_template_pack = template_pack + # install the debug config file + if debug: + load_debug_config( debug ) + # connect stdout/stderr to Python logging # NOTE: Logging to the console causes crashes on Windows if we are frozen, so don't do that! sys.stdout = LoggerProxy( logging, logging.INFO ) diff --git a/vasl_templates/webapp/__init__.py b/vasl_templates/webapp/__init__.py index eaf15b4..bdb6a61 100644 --- a/vasl_templates/webapp/__init__.py +++ b/vasl_templates/webapp/__init__.py @@ -11,6 +11,13 @@ from vasl_templates.webapp.config.constants import APP_NAME, BASE_DIR # --------------------------------------------------------------------- +def load_debug_config( fname ): + """Configure the application.""" + config_parser.read( fname ) + app.config.update( dict( config_parser.items( "Debug" ) ) ) + +# --------------------------------------------------------------------- + # initialize Flask app = Flask( __name__ ) @@ -20,16 +27,17 @@ config_parser = configparser.ConfigParser() config_parser.optionxform = str # preserve case for the keys :-/ config_parser.read( os.path.join( config_dir, "app.cfg" ) ) app.config.update( dict( config_parser.items( "System" ) ) ) -fname = os.path.join( config_dir, "debug.cfg" ) -if os.path.isfile( fname ) : - config_parser.read( fname ) - app.config.update( dict( config_parser.items( "Debug" ) ) ) + +# load any debug configuration +_fname = os.path.join( config_dir, "debug.cfg" ) +if os.path.isfile( _fname ) : + load_debug_config( _fname ) # initialize logging -fname = os.path.join( config_dir, "logging.cfg" ) -if os.path.isfile( fname ): +_fname = os.path.join( config_dir, "logging.cfg" ) +if os.path.isfile( _fname ): import logging.config - logging.config.dictConfig( json.load( open(fname,"r") ) ) + logging.config.dictConfig( json.load( open(_fname,"r") ) ) # load the application import vasl_templates.webapp.main #pylint: disable=cyclic-import