""" automatically invoked app factory """ import logging import os from flask import Flask from inventory_provider import environment def create_app(): """ overrides default settings with those found in the file read from env var SETTINGS_FILENAME :return: a new flask app instance """ if "SETTINGS_FILENAME" not in os.environ: assert False, \ "environment variable SETTINGS_FILENAME' must be defined" app = Flask(__name__) app.secret_key = "super secret session key" environment.setup_logging() from inventory_provider.routes import default app.register_blueprint(default.routes, url_prefix='/') from inventory_provider.routes import data app.register_blueprint(data.routes, url_prefix='/data') from inventory_provider.routes import jobs app.register_blueprint(jobs.routes, url_prefix='/jobs') from inventory_provider.routes import classifier app.register_blueprint(classifier.routes, url_prefix='/classifier') from inventory_provider.routes import poller app.register_blueprint(poller.routes, url_prefix='/poller') logging.info("initializing Flask with config from: %r" % os.environ["SETTINGS_FILENAME"]) app.config.from_envvar("SETTINGS_FILENAME") assert "INVENTORY_PROVIDER_CONFIG_FILENAME" in app.config, ( "INVENTORY_PROVIDER_CONFIG_FILENAME not defined in %s" % os.environ["SETTINGS_FILENAME"]) assert os.path.isfile(app.config["INVENTORY_PROVIDER_CONFIG_FILENAME"]), ( "config file '%s' not found" % app.config["INVENTORY_PROVIDER_CONFIG_FILENAME"]) if app.config.get('ENABLE_TESTING_ROUTES', False): from inventory_provider.routes import testing app.register_blueprint(testing.routes, url_prefix='/testing') logging.warning('DANGER!!! testing routes enabled') from inventory_provider import config with open(app.config["INVENTORY_PROVIDER_CONFIG_FILENAME"]) as f: # test the config file can be loaded logging.info("loading config from: %r" % app.config["INVENTORY_PROVIDER_CONFIG_FILENAME"]) app.config["INVENTORY_PROVIDER_CONFIG"] = config.load(f) logging.info('Inventory Provider Flask app initialized') return app