Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100644 153 lines (115 sloc) 5.542 kB
4f8ee8f @mitsuhiko Added missing comments, fixed setup.py and made tests pass
authored
1 # -*- coding: utf-8 -*-
2 """
3 flask.config
4 ~~~~~~~~~~~~
5
6 Implements the configuration related objects.
7
8 :copyright: (c) 2010 by Armin Ronacher.
9 :license: BSD, see LICENSE for more details.
10 """
11
b551f15 @mitsuhiko Restored 2.5 compatibility{
authored
12 from __future__ import with_statement
13
c4f64c1 @justquick working import layout for module
justquick authored
14 import os
15 import sys
16
17 from werkzeug import import_string
d0dc89e @justquick in with the new. i have the bits in places where i think they should …
justquick authored
18
19
20 class ConfigAttribute(object):
21 """Makes an attribute forward to the config"""
22
23 def __init__(self, name):
24 self.__name__ = name
25
26 def __get__(self, obj, type=None):
27 if obj is None:
28 return self
29 return obj.config[self.__name__]
30
31 def __set__(self, obj, value):
32 obj.config[self.__name__] = value
33
34
35 class Config(dict):
36 """Works exactly like a dict but provides ways to fill it from files
37 or special dictionaries. There are two common patterns to populate the
38 config.
39
40 Either you can fill the config from a config file::
41
42 app.config.from_pyfile('yourconfig.cfg')
43
44 Or alternatively you can define the configuration options in the
45 module that calls :meth:`from_object` or provide an import path to
46 a module that should be loaded. It is also possible to tell it to
47 use the same module and with that provide the configuration values
48 just before the call::
49
50 DEBUG = True
51 SECRET_KEY = 'development key'
52 app.config.from_object(__name__)
53
54 In both cases (loading from any Python file or loading from modules),
55 only uppercase keys are added to the config. This makes it possible to use
56 lowercase values in the config file for temporary values that are not added
57 to the config or to define the config keys in the same file that implements
58 the application.
59
60 Probably the most interesting way to load configurations is from an
61 environment variable pointing to a file::
62
63 app.config.from_envvar('YOURAPPLICATION_SETTINGS')
64
65 In this case before launching the application you have to set this
66 environment variable to the file you want to use. On Linux and OS X
67 use the export statement::
68
69 export YOURAPPLICATION_SETTINGS='/path/to/config/file'
70
71 On windows use `set` instead.
72
73 :param root_path: path to which files are read relative from. When the
74 config object is created by the application, this is
75 the application's :attr:`~flask.Flask.root_path`.
76 :param defaults: an optional dictionary of default values
77 """
78
79 def __init__(self, root_path, defaults=None):
80 dict.__init__(self, defaults or {})
81 self.root_path = root_path
82
83 def from_envvar(self, variable_name, silent=False):
84 """Loads a configuration from an environment variable pointing to
85 a configuration file. This basically is just a shortcut with nicer
86 error messages for this line of code::
87
88 app.config.from_pyfile(os.environ['YOURAPPLICATION_SETTINGS'])
89
90 :param variable_name: name of the environment variable
91 :param silent: set to `True` if you want silent failing for missing
92 files.
93 :return: bool. `True` if able to load config, `False` otherwise.
94 """
95 rv = os.environ.get(variable_name)
96 if not rv:
97 if silent:
98 return False
99 raise RuntimeError('The environment variable %r is not set '
100 'and as such configuration could not be '
101 'loaded. Set this variable and make it '
102 'point to a configuration file' %
103 variable_name)
104 self.from_pyfile(rv)
105 return True
106
107 def from_pyfile(self, filename):
108 """Updates the values in the config from a Python file. This function
109 behaves as if the file was imported as module with the
110 :meth:`from_object` function.
111
112 :param filename: the filename of the config. This can either be an
113 absolute filename or a filename relative to the
114 root path.
115 """
116 filename = os.path.join(self.root_path, filename)
117 d = type(sys)('config')
118 d.__file__ = filename
119 execfile(filename, d.__dict__)
120 self.from_object(d)
121
122 def from_object(self, obj):
123 """Updates the values from the given object. An object can be of one
124 of the following two types:
125
126 - a string: in this case the object with that name will be imported
127 - an actual object reference: that object is used directly
128
129 Objects are usually either modules or classes.
130
131 Just the uppercase variables in that object are stored in the config
132 after lowercasing. Example usage::
133
134 app.config.from_object('yourapplication.default_config')
135 from yourapplication import default_config
136 app.config.from_object(default_config)
137
138 You should not use this function to load the actual configuration but
139 rather configuration defaults. The actual config should be loaded
140 with :meth:`from_pyfile` and ideally from a location not within the
141 package because the package might be installed system wide.
142
143 :param obj: an import name or object
144 """
145 if isinstance(obj, basestring):
146 obj = import_string(obj)
147 for key in dir(obj):
148 if key.isupper():
149 self[key] = getattr(obj, key)
150
151 def __repr__(self):
152 return '<%s %s>' % (self.__class__.__name__, dict.__repr__(self))
Something went wrong with that request. Please try again.