Debian Codegraph @

docs/source/configuration.rst

raw
1 .. _configuration:
2
3 Configuring Pecan Applications
4 ==============================
5
6 Pecan is very easy to configure. As long as you follow certain conventions,
7 using, setting and dealing with configuration should be very intuitive.
8
9 Pecan configuration files are pure Python. Each "section" of the
10 configuration is a dictionary assigned to a variable name in the
11 configuration module.
12
13 Default Values
14 ---------------
15
16 Below is the complete list of default values the framework uses::
17
18
19 server = {
20 'port' : '8080',
21 'host' : '0.0.0.0'
22 }
23
24 app = {
25 'root' : None,
26 'modules' : [],
27 'static_root' : 'public',
28 'template_path' : ''
29 }
30
31
32
33 .. _application_configuration:
34
35 Application Configuration
36 -------------------------
37
38 The ``app`` configuration values are used by Pecan to wrap your
39 application into a valid `WSGI app
40 <http://www.wsgi.org/en/latest/what.html>`_. The ``app`` configuration
41 is specific to your application, and includes values like the root
42 controller class location.
43
44 A typical application configuration might look like this::
45
46 app = {
47 'root' : 'project.controllers.root.RootController',
48 'modules' : ['project'],
49 'static_root' : '%(confdir)s/public',
50 'template_path' : '%(confdir)s/project/templates',
51 'debug' : True
52 }
53
54 Let's look at each value and what it means:
55
56 **modules**
57 A list of modules where pecan will search for applications.
58 Generally this should contain a single item, the name of your
59 project's python package. At least one of the listed modules must
60 contain an ``app.setup_app`` function which is called to create the
61 WSGI app. In other words, this package should be where your
62 ``app.py`` file is located, and this file should contain a
63 ``setup_app`` function.
64
65 **root**
66 The root controller of your application. Remember to provide a
67 string representing a Python path to some callable (e.g.,
68 ``"yourapp.controllers.root.RootController"``).
69
70 **static_root**
71 The directory where your static files can be found (relative to
72 the project root). Pecan comes with middleware that can
73 be used to serve static files (like CSS and Javascript files) during
74 development.
75
76 **template_path**
77 Points to the directory where your template files live (relative to
78 the project root).
79
80 **debug**
81 Enables the ability to display tracebacks in the browser and interactively
82 debug during development.
83
84 .. warning::
85
86 ``app`` is a reserved variable name for that section of the
87 configuration, so make sure you don't override it.
88
89 .. warning::
90
91 Make sure **debug** is *always* set to ``False`` in production environments.
92
93 .. seealso::
94
95 * :ref:`app_template`
96
97
98 .. _server_configuration:
99
100 Server Configuration
101 --------------------
102
103 Pecan provides some sane defaults. Change these to alter the host and port your
104 WSGI app is served on.
105
106 ::
107
108 server = {
109 'port' : '8080',
110 'host' : '0.0.0.0'
111 }
112
113 Additional Configuration
114 ------------------------
115
116 Your application may need access to other configuration values at
117 runtime (like third-party API credentials). Put these settings in
118 their own blocks in your configuration file.
119
120 ::
121
122 twitter = {
123 'api_key' : 'FOO',
124 'api_secret' : 'SECRET'
125 }
126
127 .. _accessibility:
128
129 Accessing Configuration at Runtime
130 ----------------------------------
131
132 You can access any configuration value at runtime via :py:mod:`pecan.conf`.
133 This includes custom, application, and server-specific values.
134
135 For example, if you needed to specify a global administrator, you could
136 do so like this within the configuration file.
137
138 ::
139
140 administrator = 'foo_bar_user'
141
142 And it would be accessible in :py:mod:`pecan.conf` as::
143
144 >>> from pecan import conf
145 >>> conf.administrator
146 'foo_bar_user'
147
148
149 Dictionary Conversion
150 ---------------------
151
152 In certain situations you might want to deal with keys and values, but in strict
153 dictionary form. The :class:`~pecan.configuration.Config` object has a helper
154 method for this purpose that will return a dictionary representation of the
155 configuration, including nested values.
156
157 Below is a representation of how you can access the
158 :meth:`~pecan.configuration.Config.to_dict` method and what it returns as
159 a result (shortened for brevity):
160
161 ::
162
163 >>> from pecan import conf
164 >>> conf
165 Config({'app': Config({'errors': {}, 'template_path': '', 'static_root': 'public', [...]
166 >>> conf.to_dict()
167 {'app': {'errors': {}, 'template_path': '', 'static_root': 'public', [...]
168
169
170 Prefixing Dictionary Keys
171 -------------------------
172
173 :func:`~pecan.configuration.Config.to_dict` allows you to pass an optional
174 string argument if you need to prefix the keys in the returned dictionary.
175
176 ::
177
178 >>> from pecan import conf
179 >>> conf
180 Config({'app': Config({'errors': {}, 'template_path': '', 'static_root': 'public', [...]
181 >>> conf.to_dict('prefixed_')
182 {'prefixed_app': {'prefixed_errors': {}, 'prefixed_template_path': '', 'prefixed_static_root': 'prefixed_public', [...]
183
184
185 Dotted Keys, Non-Python Idenfitiers, and Native Dictionaries
186 ------------------------------------------------------------
187
188 Sometimes you want to specify a configuration option that includes dotted keys
189 or is not a valid Python idenfitier, such as ``()``. These situations are
190 especially common when configuring Python logging. By passing a special key,
191 ``__force_dict__``, individual configuration blocks can be treated as native
192 dictionaries.
193
194 ::
195
196 logging = {
197 'root': {'level': 'INFO', 'handlers': ['console']},
198 'loggers': {
199 'sqlalchemy.engine': {'level': 'INFO', 'handlers': ['console']},
200 '__force_dict__': True
201 },
202 'formatters': {
203 'custom': {
204 '()': 'my.package.customFormatter'
205 }
206 }
207 }
208
209 from myapp import conf
210 assert isinstance(conf.logging.loggers, dict)
211 assert isinstance(conf.logging.loggers['sqlalchemy.engine'], dict)