Flask之配置文件
一、配置文件概述
Flask中的配置参数有很多,它是flask中flask.config.Config对象(继承字典):
class Config(dict): """Works exactly like a dict but provides ways to fill it from files or special dictionaries. There are two common patterns to populate the config. Either you can fill the config from a config file:: app.config.from_pyfile('yourconfig.cfg') Or alternatively you can define the configuration options in the module that calls :meth:`from_object` or provide an import path to a module that should be loaded. It is also possible to tell it to use the same module and with that provide the configuration values just before the call:: DEBUG = True SECRET_KEY = 'development key' app.config.from_object(__name__) In both cases (loading from any Python file or loading from modules), only uppercase keys are added to the config. This makes it possible to use lowercase values in the config file for temporary values that are not added to the config or to define the config keys in the same file that implements the application. Probably the most interesting way to load configurations is from an environment variable pointing to a file:: app.config.from_envvar('YOURAPPLICATION_SETTINGS') In this case before launching the application you have to set this environment variable to the file you want to use. On Linux and OS X use the export statement:: export YOURAPPLICATION_SETTINGS='/path/to/config/file' On windows use `set` instead. :param root_path: path to which files are read relative from. When the config object is created by the application, this is the application's :attr:`~flask.Flask.root_path`. :param defaults: an optional dictionary of default values """ def __init__(self, root_path, defaults=None): dict.__init__(self, defaults or {}) self.root_path = root_path def from_envvar(self, variable_name, silent=False): """Loads a configuration from an environment variable pointing to a configuration file. This is basically just a shortcut with nicer error messages for this line of code:: app.config.from_pyfile(os.environ['YOURAPPLICATION_SETTINGS']) :param variable_name: name of the environment variable :param silent: set to ``True`` if you want silent failure for missing files. :return: bool. ``True`` if able to load config, ``False`` otherwise. """ rv = os.environ.get(variable_name) if not rv: if silent: return False raise RuntimeError( "The environment variable %r is not set " "and as such configuration could not be " "loaded. Set this variable and make it " "point to a configuration file" % variable_name ) return self.from_pyfile(rv, silent=silent) def from_pyfile(self, filename, silent=False): """Updates the values in the config from a Python file. This function behaves as if the file was imported as module with the :meth:`from_object` function. :param filename: the filename of the config. This can either be an absolute filename or a filename relative to the root path. :param silent: set to ``True`` if you want silent failure for missing files. .. versionadded:: 0.7 `silent` parameter. """ filename = os.path.join(self.root_path, filename) d = types.ModuleType("config") d.__file__ = filename try: with open(filename, mode="rb") as config_file: exec(compile(config_file.read(), filename, "exec"), d.__dict__) except IOError as e: if silent and e.errno in (errno.ENOENT, errno.EISDIR, errno.ENOTDIR): return False e.strerror = "Unable to load configuration file (%s)" % e.strerror raise self.from_object(d) return True def from_object(self, obj): """Updates the values from the given object. An object can be of one of the following two types: - a string: in this case the object with that name will be imported - an actual object reference: that object is used directly Objects are usually either modules or classes. :meth:`from_object` loads only the uppercase attributes of the module/class. A ``dict`` object will not work with :meth:`from_object` because the keys of a ``dict`` are not attributes of the ``dict`` class. Example of module-based configuration:: app.config.from_object('yourapplication.default_config') from yourapplication import default_config app.config.from_object(default_config) Nothing is done to the object before loading. If the object is a class and has ``@property`` attributes, it needs to be instantiated before being passed to this method. You should not use this function to load the actual configuration but rather configuration defaults. The actual config should be loaded with :meth:`from_pyfile` and ideally from a location not within the package because the package might be installed system wide. See :ref:`config-dev-prod` for an example of class-based configuration using :meth:`from_object`. :param obj: an import name or object """ if isinstance(obj, string_types): obj = import_string(obj) for key in dir(obj): if key.isupper(): self[key] = getattr(obj, key) def from_json(self, filename, silent=False): """Updates the values in the config from a JSON file. This function behaves as if the JSON object was a dictionary and passed to the :meth:`from_mapping` function. :param filename: the filename of the JSON file. This can either be an absolute filename or a filename relative to the root path. :param silent: set to ``True`` if you want silent failure for missing files. .. versionadded:: 0.11 """ filename = os.path.join(self.root_path, filename) try: with open(filename) as json_file: obj = json.loads(json_file.read()) except IOError as e: if silent and e.errno in (errno.ENOENT, errno.EISDIR): return False e.strerror = "Unable to load configuration file (%s)" % e.strerror raise return self.from_mapping(obj) def from_mapping(self, *mapping, **kwargs): """Updates the config like :meth:`update` ignoring items with non-upper keys. .. versionadded:: 0.11 """ mappings = [] if len(mapping) == 1: if hasattr(mapping[0], "items"): mappings.append(mapping[0].items()) else: mappings.append(mapping[0]) elif len(mapping) > 1: raise TypeError( "expected at most 1 positional argument, got %d" % len(mapping) ) mappings.append(kwargs.items()) for mapping in mappings: for (key, value) in mapping: if key.isupper(): self[key] = value return True def get_namespace(self, namespace, lowercase=True, trim_namespace=True): """Returns a dictionary containing a subset of configuration options that match the specified namespace/prefix. Example usage:: app.config['IMAGE_STORE_TYPE'] = 'fs' app.config['IMAGE_STORE_PATH'] = '/var/app/images' app.config['IMAGE_STORE_BASE_URL'] = 'http://img.website.com' image_store_config = app.config.get_namespace('IMAGE_STORE_') The resulting dictionary `image_store_config` would look like:: { 'type': 'fs', 'path': '/var/app/images', 'base_url': 'http://img.website.com' } This is often useful when configuration options map directly to keyword arguments in functions or class constructors. :param namespace: a configuration namespace :param lowercase: a flag indicating if the keys of the resulting dictionary should be lowercase :param trim_namespace: a flag indicating if the keys of the resulting dictionary should not include the namespace .. versionadded:: 0.11 """ rv = {} for k, v in iteritems(self): if not k.startswith(namespace): continue if trim_namespace: key = k[len(namespace) :] else: key = k if lowercase: key = key.lower() rv[key] = v return rv def __repr__(self): return "<%s %s>" % (self.__class__.__name__, dict.__repr__(self))
内置配置参数:
ENV #应用运行于什么环境,在生产环境中不要使用 development 。缺省值: 'production' DEBUG #是否开启调试模式,在生产环境中不要开启调试模式。缺省值:当 ENV 是 'development' 时,为 True ;否则为 False 。 TESTING #开启测试模式。缺省值: False PROPAGATE_EXCEPTIONS #当异常发生时,不要弹出请求情境。缺省值: None TRAP_HTTP_EXCEPTIONS #如果没有处理 HTTPException 类型异常的处理器,重新引发该异常用于被 交互调试器处理,
而不是作为一个简单的错误响应来返回。缺省值: False TRAP_BAD_REQUEST_ERRORS #尝试操作一个请求字典中不存在的键,如 args 和 form ,会返回一个
400 Bad Request error 页面。缺省值: None SECRET_KEY #密钥用于会话 cookie 的安全签名,并可用于应用或者扩展的其他安全需求。缺省值: None SESSION_COOKIE_NAME #会话 cookie 的名称。假如已存在同名 cookie ,本变量可改变。缺省值: 'session' SESSION_COOKIE_DOMAIN #认可会话 cookie 的域的匹配规则。缺省值: None SESSION_COOKIE_PATH #认可会话 cookie 的路径。缺省值: None SESSION_COOKIE_HTTPONLY #为了安全,浏览器不会允许 JavaScript 操作标记为“ HTTP only ”的 cookie 。缺省值: True SESSION_COOKIE_SECURE #如果 cookie 标记为“ secure ”,那么浏览器只会使用基于 HTTPS 的请求发 送 cookie 。缺省值: False SESSION_COOKIE_SAMESITE #限制来自外部站点的请求如何发送 cookie 。缺省值: None PERMANENT_SESSION_LIFETIME #如果 session.permanent 为真, cookie 的有效期为本变量设置的数字, 单位为秒。
缺省值: timedelta(days=31) ( 2678400 秒) SESSION_REFRESH_EACH_REQUEST #当 session.permanent 为真时,控制是否每个响应都发送 cookie 。缺省值: True SERVER_NAME #通知应用其所绑定的主机和端口。子域路由匹配需要本变量。缺省值: None APPLICATION_ROOT #通知应用应用的根路径是什么。缺省值: '/' ...
上面是一部分的内置的配置参数,配置文件可有下面三种方式。
二、直接配置
from flask import Flask app = Flask(__name__) #直接配置 app.config['DEBUG'] = True app.config['SECRET_KEY'] = 'ab32j56kgfsl_k' @app.route('/') def index(): return 'index' if __name__ == '__main__': app.run()
三、from_pyfile
导入一个.py文件:
from flask import Flask app = Flask(__name__) # 导入py文件 app.config.from_pyfile('settings.py') # settings.py路径 @app.route('/') def index(): return 'index' if __name__ == '__main__': app.run()
settings.py中的内容如下:
# settings.py DEBUG = True
四、from_object
导入一个对象:
from flask import Flask app = Flask(__name__) # 导入一个类的路径 app.config.from_object('"settings.DevelopmentConfig"') @app.route('/') def index(): return 'index' if __name__ == '__main__': app.run()
settings.py文件中:
class Config(object): DEBUG = False DATABASE_URI = 'sqlite://...' class ProductionConfig(Config): DATABASE_URI = 'mysql://...' class DevelopmentConfig(Config): DEBUG = True
作者:iveBoy
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须在文章页面给出原文连接,否则保留追究法律责任的权利。