""" 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 """ required_env_vars = [ 'FLASK_SETTINGS_FILENAME', 'INVENTORY_PROVIDER_CONFIG_FILENAME'] assert all([n in os.environ for n in required_env_vars]), \ 'environment variables %r must be defined' % required_env_vars assert os.path.isfile(os.environ['INVENTORY_PROVIDER_CONFIG_FILENAME']), ( 'config file %r not found' % os.environ['INVENTORY_PROVIDER_CONFIG_FILENAME']) from inventory_provider import config with open(os.environ['INVENTORY_PROVIDER_CONFIG_FILENAME']) as f: logging.info( 'loading config from: %r' % os.environ['INVENTORY_PROVIDER_CONFIG_FILENAME']) inventory_provider_config = config.load(f) app = Flask(__name__) app.secret_key = 'super secret session key' logging.info( 'initializing Flask with config from: %r' % os.environ['FLASK_SETTINGS_FILENAME']) app.config.from_envvar('FLASK_SETTINGS_FILENAME') app.config['INVENTORY_PROVIDER_CONFIG'] = inventory_provider_config 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') 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') logging.info('Inventory Provider Flask app initialized') environment.setup_logging() return app