python - Getting config variables outside of the application context -


i've extracted several of sqlalchemy models separate , installable package (../lib/site-packages), use across several applications. need to:

from models_package import mymodel 

from application needing access these models.

everything ok far, except cannot find satisfactory way of getting several application dependent config variables used of models, may vary application application. model need aware of variables, i've used application in.

neither

current_app.config['xyz'] 

or

config = localproxy(lambda: current_app.config['xyz']) 

have worked (outside of application context errors) i'm stuck right now. maybe poor programming and/or design on behalf, how clear up? there must way, haven't reasoned myself toward yet.

solution:

avoiding setting items occur on module load (like constant containing api key), both of above should work, , do. not using in context of model-in-the-application use of course error, methods returning values need should good.

well, since current_app can proxy of flask program when blueprint registered, , done @ run-time, can't use in models_package modules. (app tries import models_package, , models_package requires app's configs initalize things- import fails)

one option doing circular imports :

assuming in 'app' module:

__init__.py

import flask  application = flask.flask(__name__) application.config = #load configs  import models_package 

models_package.py

from app import application config = application.config 

or create own config object, somehow doubles complexity:

models_package.py

import flask  config = flask.config.config(defaults=flask.flask.default_config)  #pick 1 of , apply same config initialization in  #your __init__.py config.from_pyfile(..) #or config.from_object(..) #or config.from_envvar(..)  

Comments

Popular posts from this blog

Why does Ruby on Rails generate add a blank line to the end of a file? -

keyboard - Smiles and long press feature in Android -

node.js - Bad Request - node js ajax post -