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