简体   繁体   English

在应用程序上下文之外获取配置变量

[英]Getting config variables outside of the application context

I've extracted several of my sqlalchemy models to a separate and installable package (../lib/site-packages), to use across several applications. 我已将几个sqlalchemy模型提取到一个单独的可安装包(../lib/site-packages),以便在多个应用程序中使用。 So I only need to: 所以我只需要:

from models_package import MyModel

from any application needing access to these models. 来自任何需要访问这些模型的应用程序。

Everything is ok so far, except I cannot find a satisfactory way of getting several application dependent config variables used by some of the models, which may vary from application to application. 到目前为止一切都还可以,除了我找不到令人满意的方法来获取一些模型使用的几个依赖于应用程序的配置变量,这些变量可能因应用程序而异。 So some model need to be aware of some variables, where previously I've used the application they were in. 所以有些模型需要注意一些变量,之前我已经使用过他们所使用的应用程序。

Neither 也不

current_app.config['XYZ']

or 要么

config = LocalProxy(lambda: current_app.config['XYZ'])

have worked ( outside of application context errors) so I'm stuck right now. 已经工作( outside of application context错误之外),所以我现在卡住了。 Maybe this is poor programming and/or design on my behalf, so how do clear this up? 也许这是代表我的糟糕的编程和/或设计,所以如何清除它? There must be some way, but I haven't reasoned myself toward it yet. 必须有某种方式,但我还没有推理自己。

SOLUTION: 解:

Avoiding setting items that would occur on module load (like a constant containing an api key), both of the above should work, and they do. 避免在模块加载时发生的设置项(如包含api键的常量),上述两种方法都可以正常工作,并且可以。 Anything not using those in the context of model-in-the-application use will of course error, methods returning the values you need should be good. 在应用程序模型的上下文中没有使用它们的任何东西当然都会出错,返回所需值的方法应该是好的。

well, since current_app can be a proxy of your flask program when the blueprint is registered, and that is done at run-time, you can't use it in your models_package modules. 好吧,因为当注册蓝图时, current_app可以作为您烧瓶程序的代理,并且在运行时完成,您不能在models_package模块中使用它。 (app tries to import models_package , and models_package requires app's configs to initalize things- thus import fails) (app尝试导入models_package ,而models_package需要应用程序的配置来models_package - 因此导入失败)

one option would be doing circular imports : 一种选择是进行循环进口:

assuming everything is in 'App' module: 假设一切都在“App”模块中:

__init__.py __init__.py

import flask

application = flask.Flask(__name__)
application.config = #load configs

import models_package

models_package.py models_package.py

from App import application
config = application.config

or create your own config object, but that somehow doubles complexity: 或者创建自己的config对象,但这会以某种方式使复杂性增加一倍:

models_package.py models_package.py

import flask

config = flask.config.Config(defaults=flask.Flask.default_config)

#pick one of those and apply the same config initialization as you do in 
#your __init__.py
config.from_pyfile(..) #or
config.from_object(..) #or
config.from_envvar(..) 

If you are using a configuration pattern utilising classes and inheritance as described here , you could simply import your config classes with their respective properties and access them anywhere you want: 如果使用的是利用类和继承配置样式这里描述 ,你可以简单地用各自的属性导入你的配置类和访问他们任何你想要的:

class Config(object):
    IMPORT = 'ME'
    DEBUG = False
    TESTING = False
    DATABASE_URI = 'sqlite:///:memory:'

class ProductionConfig(Config):
    DATABASE_URI = 'mysql://user@localhost/foo'

class DevelopmentConfig(Config):
    DEBUG = True

class TestingConfig(Config):
    TESTING = True

Now, in your foo.py : 现在,在你的foo.py

from config import Config

print(Config.IMPORT) # prints 'ME'

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM