def test_dotenv_path(monkeypatch): for item in ("FOO", "BAR", "EGGS"): monkeypatch._setitem.append((os.environ, item, notset)) load_dotenv(test_path / ".flaskenv") assert Path.cwd() == cwd assert "FOO" in os.environ
def create_app(config_name=Config): load_dotenv() app = Flask(__name__.split('.')[0]) app.config.from_object(config_name) app.redis = redis.from_url(app.config['REDIS_URL']) app.config['SITEMAP_VIEW_DECORATORS'] = [load_page] # Don't cache js bundles if in development if app.config['ENV'] == 'development': app.config['SEND_FILE_MAX_AGE_DEFAULT'] = 0 # Register before requests mixins prior to those that are inside extensions register_extensions(app) register_url_rules(app) register_blueprints(app) register_errorhandlers(app) app.shell_context_processor( lambda: { 'db': db, 'User': main.models.User, 'CourseColor': calendar.models.CourseColor, 'CourseFilter': calendar.models.CourseFilter, 'CourseIdentifier': calendar.models.CourseIdentifier }) app.context_processor(inject_date) return app
def test_cmd(algorithm=None): ''' \b Test algorithms in your project. You may specify an algorithm to test: >>> alg test my_algorithm \b or test all algorithms: >>> alg test ''' from flask.cli import load_dotenv load_dotenv() os.environ['FLASK_APP'] = 'run' os.environ['ATK_CONFIG'] = os.path.join(os.getcwd(), 'config.py') if algorithm: base_dir = os.path.join('algorithms', algorithm) else: base_dir = '.' try: suite = unittest.TestLoader().discover(base_dir) unittest.TextTestRunner(verbosity=2).run(suite) except ImportError: click.echo('Algorithm does not exist')
def test_dotenv_path(monkeypatch): for item in ("FOO", "BAR", "EGGS"): monkeypatch._setitem.append((os.environ, item, notset)) cwd = os.getcwd() load_dotenv(os.path.join(test_path, ".flaskenv")) assert os.getcwd() == cwd assert "FOO" in os.environ
def test_dotenv_path(monkeypatch): for item in ('FOO', 'BAR', 'EGGS'): monkeypatch._setitem.append((os.environ, item, notset)) cwd = os.getcwd() load_dotenv(os.path.join(test_path, '.flaskenv')) assert os.getcwd() == cwd assert 'FOO' in os.environ
def test_dotenv_path(monkeypatch): for item in ('FOO', 'BAR', 'EGGS'): monkeypatch._setitem.append((os.environ, item, notset)) cwd = os.getcwd() load_dotenv(os.path.join(test_path, '.flaskenv')) assert os.getcwd() == cwd assert 'FOO' in os.environ
def create_app(): app = Flask(__name__) cli.load_dotenv() app.config.from_object(os.environ['APP_SETTINGS']) # Init app db.init_app(app) routes.init_app(app) scripts.init_app(app) # Migarate Migrate(app, db) return app
def __init__(self): load_dotenv() self.APP_ENABLE_FILE_LOGGING = str2bool( os.environ.get('APP_ENABLE_FILE_LOGGING')) or False self.APP_ENABLE_SENTRY = str2bool( os.environ.get('APP_ENABLE_SENTRY')) or True self.LOG_FORMAT = '[%(asctime)s] %(levelname)s [%(name)s.%(funcName)s:%(lineno)d] %(message)s' self.LOG_FILE_PATH = Path( os.environ.get('LOG_FILE_PATH') or '/var/log/app/app.log')
def create_app() -> Flask: app = Flask(__name__) # Load application settings from environment variable. # If environment variable is not set will load development settings by default. app_settings = os.getenv("APP_SETTINGS", "settings.DevelopmentConfig") env_path = Path(".env.development").absolute() if not app_settings: raise ValueError('"APP_SETTINGS" environment variable not set') if app_settings == "settings.TestingConfig": env_path = Path(".env.testing") elif app_settings == "settings.ProductionConfig": env_path = Path(".env.production") # Load environment variables depending on application settings (Dev/Test/Prod) load_dotenv(path=env_path) app.config.from_object(app_settings) # Initialize Flask-SQLAlchemy ORM db.init_app(app) db.app = app # Initialize Flask-Migrate Migrate(app, db) # Initialize flask_resty api api = Api(app, prefix="/api") initialize_routes(api) # Initialize Flask-Bcrypt bcrypt_.init_app(app) # Initialize swagger template = { "components": { "securitySchemes": { "basicAuth": { "type": "http", "scheme": "basic" }, "bearerAuth": { "type": "http", "scheme": "bearer", "bearerFormat": "JWT", }, } } } Swagger(app, template=template) return app
def run(): click.echo('Running archivy...') load_dotenv() watcher = Watcher(app) watcher.start() port = int(os.environ.get("ARCHIVY_PORT", 5000)) os.environ["FLASK_RUN_FROM_CLI"] = "false" app_with_cli = create_click_web_app(click, cli, app) app_with_cli.run(host='0.0.0.0', port=port) click.echo("Stopping archivy watcher") watcher.stop() watcher.join()
def load_config(app: Flask): """ Loads the configuration for your zemfrog application based on the environment ``ZEMFROG_ENV``, change your application environment in the file ``.flaskenv``. """ path = os.path.join(app.root_path, ".flaskenv") load_dotenv(path) env = os.getenv("ZEMFROG_ENV") if not env: raise ZemfrogEnvironment("environment not found") import_name = get_import_name(app) app.config.from_object(import_name + "config." + env.capitalize())
def app(): cli.load_dotenv() os.environ["MONGODB_URI"] = "%s_test" % os.environ["MONGODB_URI"] app = create_app() app.config["TESTING"] = True app_context = app.test_request_context() app_context.push() yield app teardown_database(app)
def load_envvars(app): if not app: app = current_app cli.load_dotenv() for key in app.config: if key in ["FLASK_ENV", "FLASK_DEBUG", "ENV", "DEBUG"]: continue if key in os.environ: value = os.environ[key] if not isinstance(app.config[key], str): value = ast.literal_eval(value) app.config[key] = value
def create_app( additional_config={}, name="atlas_core", standalone=False, custom_json_encoder=False, load_dotenv=False, ): """App factory. Creates a Flask `app` object and imports extensions, sets config variables etc.""" app = Flask(name) # Load environment variables from .env and .flaskenv including FLASK_APP # and FLASK_CONFIG etc etc. The flask 1.0+ CLI (`flask commandname`) does # this automatically if you have python-dotenv installed, but if you call # create_app() manually from your own code you need this. This needs to # happen before pretty much anything, so that we can customize even the # flask config location with this. if load_dotenv: cli.load_dotenv() # Load config from FLASK_CONFIG env variable. app = load_config(app, overrides=additional_config) # Load extensions db.init_app(app) # Debug tools if app.debug: app = add_profiler(app) if standalone: create_db(app, db) if app.config.get("CATCH_API_EXCEPTIONS", True): app.errorhandler(APIError)(handle_api_error) # For flask's jsonify if custom_json_encoder: app.json_encoder = custom_json_encoder # Register custom serializers like json, csv, msgpack, bson etc to use with # helpers.serialize() app.serializers = {"json": JsonifySerializer()} if "default_serializer" not in app.config: app.config["default_serializer"] = "json" return app
def create_app(): cli.load_dotenv() from .settings import BaseSettings, LOGGING dictConfig(LOGGING) app = Flask(__name__) app.config.from_object(BaseSettings) app.url_map.strict_slashes = False configure_commands(app) configure_extensions(app) configure_rules(app) return app
def create_app(): load_dotenv() server = Flask(__name__) if os.getenv("FLASK_ENV") == 'development': server.config.from_object(config.DevelopmentConfig) elif os.getenv("FLASK_ENV") == 'production': server.config.from_object(config.ProductionConfig) else: raise ValueError('FLASK_ENV environment variable not set') register_dashapps(server) register_extensions(server) register_blueprints(server) return server
def run(): from flask.helpers import get_load_dotenv from flask.cli import ScriptInfo, load_dotenv from mini_fiction.management import manager manager.populate_commands() # Необходимо создать приложение заранее для загрузки команд из плагинов if get_load_dotenv(manager.cli.load_dotenv): load_dotenv() obj = ScriptInfo(create_app=manager.cli.create_app) obj.load_app() return manager.cli(obj=obj)
class Base: """ Base configuration """ PROJECT_DIR = os.path.abspath(os.path.dirname(os.path.dirname(__file__))) load_dotenv(os.path.join(PROJECT_DIR, '.flaskenv')) DEBUG = False TESTING = False SECRET_KEY = os.environ.get('SECRET_KEY') DB = os.environ.get('POSTGRES_DB') DB_USER = os.environ.get('POSTGRES_USER') DB_PASS = os.environ.get('POSTGRES_PASSWORD') DB_HOST = os.environ.get('POSTGRES_HOST') DB_TEST_HOST = os.environ.get('POSTGRES_TEST_HOST', 'localhost') DB_PORT = os.environ.get('POSTGRES_PORT') SQLALCHEMY_TRACK_MODIFICATIONS = False # Redis configuration REDIS_URI = 'redis://redis:6379/0' # Celery configuration CELERY_TASK_STARTED = True CELERY_SEND_TASK_ERROR_EMAILS = True CELERY_TASK_SERIALIZER = 'json' CELERY_RESULT_SERIALIZER = 'json' CELERY_ACCEPT_CONTENT = ['application/json'] CELERY_BROKER_URL = REDIS_URI CELERY_RESULT_BACKEND = CELERY_BROKER_URL # deal with configuration requiring flask application instance def init_app(self, app): # e.g. --> app.url_map.strict_slashes = False pass
def __init__(self): load_dotenv() self.APP_ENABLE_FILE_LOGGING = str2bool( os.environ.get("APP_ENABLE_FILE_LOGGING")) or False self.APP_ENABLE_SENTRY = str2bool( os.environ.get("APP_ENABLE_SENTRY")) or True self.LOG_FORMAT = "[%(asctime)s] %(levelname)s [%(name)s.%(funcName)s:%(lineno)d] %(message)s" self.LOG_FILE_PATH = Path( os.environ.get("APP_LOG_FILE_PATH") or "/var/log/app/app.log") self.SENTRY_DSN = os.environ.get("SENTRY_DSN") or None self.AIRTABLE_API_KEY = os.environ.get("AIRTABLE_API_KEY") self.AIRTABLE_BASE_ID = os.environ.get("AIRTABLE_BASE_ID")
def test_load_dotenv(monkeypatch): # can't use monkeypatch.delitem since the keys don't exist yet for item in ('FOO', 'BAR', 'SPAM'): monkeypatch._setitem.append((os.environ, item, notset)) monkeypatch.setenv('EGGS', '3') monkeypatch.chdir(os.path.join(test_path, 'cliapp', 'inner1')) load_dotenv() assert os.getcwd() == test_path # .flaskenv doesn't overwrite .env assert os.environ['FOO'] == 'env' # set only in .flaskenv assert os.environ['BAR'] == 'bar' # set only in .env assert os.environ['SPAM'] == '1' # set manually, files don't overwrite assert os.environ['EGGS'] == '3'
def test_load_dotenv(monkeypatch): # can't use monkeypatch.delitem since the keys don't exist yet for item in ("FOO", "BAR", "SPAM"): monkeypatch._setitem.append((os.environ, item, notset)) monkeypatch.setenv("EGGS", "3") monkeypatch.chdir(os.path.join(test_path, "cliapp", "inner1")) load_dotenv() assert os.getcwd() == test_path # .flaskenv doesn't overwrite .env assert os.environ["FOO"] == "env" # set only in .flaskenv assert os.environ["BAR"] == "bar" # set only in .env assert os.environ["SPAM"] == "1" # set manually, files don't overwrite assert os.environ["EGGS"] == "3"
class Production(Development): """Production configuration""" DEBUG = False load_dotenv(os.path.join(Base.PROJECT_DIR, '.env')) DB_URI = 'postgresql://{}:{}@db:{}/{}'.format( Base.DB_USER, Base.DB_PASS, Base.DB_PORT, Base.DB ) SQLALCHEMY_DATABASE_URI = DB_URI
def test_load_dotenv(monkeypatch): # can't use monkeypatch.delitem since the keys don't exist yet for item in ('FOO', 'BAR', 'SPAM'): monkeypatch._setitem.append((os.environ, item, notset)) monkeypatch.setenv('EGGS', '3') monkeypatch.chdir(os.path.join(test_path, 'cliapp', 'inner1')) load_dotenv() assert os.getcwd() == test_path # .flaskenv doesn't overwrite .env assert os.environ['FOO'] == 'env' # set only in .flaskenv assert os.environ['BAR'] == 'bar' # set only in .env assert os.environ['SPAM'] == '1' # set manually, files don't overwrite assert os.environ['EGGS'] == '3'
def test_load_dotenv(monkeypatch): # can't use monkeypatch.delitem since the keys don't exist yet for item in ("FOO", "BAR", "SPAM"): monkeypatch._setitem.append((os.environ, item, notset)) monkeypatch.setenv("EGGS", "3") monkeypatch.chdir(test_path) assert load_dotenv() assert os.getcwd() == test_path # .flaskenv doesn't overwrite .env assert os.environ["FOO"] == "env" # set only in .flaskenv assert os.environ["BAR"] == "bar" # set only in .env assert os.environ["SPAM"] == "1" # set manually, files don't overwrite assert os.environ["EGGS"] == "3" # Non existent file should not load assert not load_dotenv("non-existent-file")
def create_app(config=None): load_dotenv() app = Flask(__name__) register_blueprints(app) register_commands(app) load_configs(app) mail.init_app(app) socketio.init_app(app, message_queue=app.config["REDIS"]) app.redis = socketio.server_options["client_manager"].redis db.init_app(app) db.app = app @app.before_request def before_request(): g.member = current_member() @app.after_request def add_header(response): return response return app
def __init__(self): load_dotenv() """ APP_ENABLE_SENTRY - Whether to enable Sentry error reporting If true errors and uncaught exceptions will be reported to Sentry. A default value is set on an per-environment basis (off in development/testing) by overriding the attribute, however it can be also be set at runtime. """ self.APP_ENABLE_SENTRY = str2bool( os.environ.get("APP_ENABLE_SENTRY") or str(self._APP_ENABLE_SENTRY)) """ AUTH_SESSION_FILE_PATH - Path to the file used to store authentication information When ran as a CLI using containers, this application becomes stateless. Therefore user auth information (access token etc.) needs to persisted elsewhere, in this case as a file written to the path set by this config option. Note: As this file stores authentication information its contents should be considered sensitive, meaning restricted read/write permissions should be set for example. Note that as OAuth is used for authentication, no long-lived credentials (e.g. passwords) will be stored in this file. """ self.AUTH_SESSION_FILE_PATH = Path( os.environ.get("APP_AUTH_SESSION_FILE_PATH") or self._AUTH_SESSION_FILE_PATH)
def test_dotenv_optional(monkeypatch): monkeypatch.setattr('flask.cli.dotenv', None) monkeypatch.chdir(test_path) load_dotenv() assert 'FOO' not in os.environ
# -*- coding: utf-8 -*- import os from flask import request from flask.cli import load_dotenv from app import models from app import create_app load_dotenv() app = create_app(os.getenv("FLASK_ENV")) @app.before_request def before_request(): app.jinja_env.cache = None if request.blueprint is not None: bp = app.blueprints[request.blueprint] if bp.jinja_loader is not None: newsearchpath = bp.jinja_loader.searchpath + app.jinja_loader.searchpath app.jinja_loader.searchpath = newsearchpath else: app.jinja_loader.searchpath = app.jinja_loader.searchpath[-1:] else: app.jinja_loader.searchpath = app.jinja_loader.searchpath[-1:] @app.cli.command("create-user") def create_user(): """
def run(self, host=None, port=None, debug=None, load_dotenv=True, **options): if get_load_dotenv(load_dotenv): cli.load_dotenv() # if set, let env vars override previous values if "FLASK_ENV" in os.environ: self.env = get_env() self.debug = get_debug_flag() elif "FLASK_DEBUG" in os.environ: self.debug = get_debug_flag() # debug passed to method overrides all other sources if debug is not None: self.debug = bool(debug) server_name = self.config.get("SERVER_NAME") sn_host = sn_port = None if server_name: sn_host, _, sn_port = server_name.partition(":") if not host: if sn_host: host = sn_host else: host = "127.0.0.1" if port or port == 0: port = int(port) elif sn_port: port = int(sn_port) else: port = 5000 options.setdefault("use_reloader", self.debug) options.setdefault("use_debugger", self.debug) options.setdefault("threaded", True) certfile = None keyfile = None cert = options.get('ssl_context') if cert is not None and len(cert) == 2: certfile = cert[0] keyfile = cert[1] elif cert == 'adhoc': raise RuntimeError( 'Aad-hoc certificates are not supported by aioflask.') if debug: os.environ['FLASK_DEBUG'] = 'true' if options['use_debugger']: os.environ['AIOFLASK_USE_DEBUGGER'] = 'true' show_server_banner(self.env, self.debug, self.name, False) uvicorn.run( self.import_name + ':app', host=host, port=port, reload=options['use_reloader'], workers=1, log_level='debug' if self.debug else 'info', ssl_certfile=certfile, ssl_keyfile=keyfile, )
def run(): click.echo("Running archivy...") load_dotenv() environ["FLASK_RUN_FROM_CLI"] = "false" app_with_cli = create_click_web_app(click, cli, app) app_with_cli.run(host=app.config["HOST"], port=app.config["PORT"])
def create(env=None) -> Flask: """ Create a Flask app and configure it. Set the environment by setting FLASK_ENV as environment variable (also possible in .env). Or, overwrite any FLASK_ENV setting by passing an env in directly (useful for testing for instance). """ # Create app configure_logging() # do this first, see http://flask.pocoo.org/docs/dev/logging/ # we're loading dotenv files manually & early (can do Flask.run(load_dotenv=False)), # as we need to know the ENV now (for it to be recognised by Flask()). load_dotenv() app = Flask("flexmeasures") if env is not None: # overwrite app.env = env if env == "testing": app.testing = True # App configuration read_config(app) if app.debug and not app.testing and not app.cli: print(app.config) add_basic_error_handlers(app) app.mail = Mail(app) FlaskJSON(app) # configure Redis (for redis queue) if app.testing: from fakeredis import FakeStrictRedis app.queues = dict( forecasting=Queue(connection=FakeStrictRedis(), name="forecasting"), scheduling=Queue(connection=FakeStrictRedis(), name="scheduling"), ) else: redis_conn = Redis( app.config["FLEXMEASURES_REDIS_URL"], port=app.config["FLEXMEASURES_REDIS_PORT"], db=app.config["FLEXMEASURES_REDIS_DB_NR"], password=app.config["FLEXMEASURES_REDIS_PASSWORD"], ) """ FWIW, you could use redislite like this (not on non-recent os.name=="nt" systems or PA, sadly): from redislite import Redis redis_conn = Redis("MY-DB-NAME", unix_socket_path="/tmp/my-redis.socket", ) """ app.queues = dict( forecasting=Queue(connection=redis_conn, name="forecasting"), scheduling=Queue(connection=redis_conn, name="scheduling"), ) # Some basic security measures install_secret_key(app) SSLify(app) # Register database and models, including user auth security measures from flexmeasures.data import register_at as register_db_at register_db_at(app) # Register the UI from flexmeasures.ui import register_at as register_ui_at register_ui_at(app) # Register the API from flexmeasures.api import register_at as register_api_at register_api_at(app) # Profile endpoints (if needed, e.g. during development) @app.before_request def before_request(): if app.config.get("FLEXMEASURES_PROFILE_REQUESTS", False): g.start = time.time() @app.teardown_request def teardown_request(exception=None): if app.config.get("FLEXMEASURES_PROFILE_REQUESTS", False): diff = time.time() - g.start if all([kw not in request.url for kw in ["/static", "favicon.ico"]]): app.logger.info( f"[PROFILE] {str(round(diff, 2)).rjust(6)} seconds to serve {request.url}." ) return app
import datetime import os import random import numpy import requests from flask.cli import load_dotenv assert load_dotenv(), "Unable to load .env" BEARER_TOKEN = os.getenv("TMDB_TOKEN") HEADERS = {"Authorization": f"Bearer {BEARER_TOKEN}"} PARAMS = { "language": "fr-FR", "region": "FR", } DISCOVER_PARAMS = { "with_runtime.gte": 15, "release_date.lte": datetime.date.today().isoformat(), } BASE_API = "https://api.themoviedb.org/3/" def get_movies(n): """Get n popular movies.""" s = requests.session() movies = [] for i in range(7 * n // 20 + 1): r = s.get(f"{BASE_API}/discover/movie", headers=HEADERS,
def test_dotenv_optional(monkeypatch): monkeypatch.setattr('flask.cli.dotenv', None) monkeypatch.chdir(test_path) load_dotenv() assert 'FOO' not in os.environ