comparison doc/introduction.rst @ 117:c5b638f9c607

- More on getting variable values - Reorga of the variable documentation
author Franz Glasner <hg@dom66.de>
date Thu, 29 Mar 2018 08:58:34 +0200
parents a5339d39af5c
children 21d92ff8cf31
comparison
equal deleted inserted replaced
116:c37fa787c022 117:c5b638f9c607
140 140
141 ``.ini`` 141 ``.ini``
142 for INI configuration files 142 for INI configuration files
143 143
144 144
145 .. _getting-values:
146
147 Getting configuration variables
148 -------------------------------
149
150 Get a -- possibly expanded -- configuration variable's value with::
151
152 value1 = config.getvar_s("key1")
153
154 Get a raw configuration variable's value with::
155
156 value1_raw = config.getvar("key1")
157
158 Because the configuration is not only a plain list of but a tree of
159 key-value pairs you will want to fetch them by separating the individual
160 level keys with a point ``.``.
161
162 Looking at the example in chapter :ref:`yaml-files` -- when calling
163 ``config.getvar_s("tree1.tree2.key4")`` you will get the value
164 ``get this as `tree1.tree2.key4'``.
165
166 This is true for both methods :py:meth:`.Configuration.getvar` and
167 :py:meth:`.Configuration.getvar_s`.
168
169 Both methods also perform :ref:`variable-expansion` and handle
170 :ref:`variable-namespaces`. Filtering is not supported. So -- the
171 variable name arguments of :py:meth:`.Configuration.getvar` and
172 :py:meth:`.Configuration.getvar_s` are of the form
173 ``[namespace:]variable``.
174
175
176 .. _variable-namespaces:
177
178 Variable Namespaces
179 -------------------
180
181 Currently there are 4 namespaces:
182
183 1. The unnamed namespace (which is also default).
184
185 All the configuration variables are part of this namespace.
186
187 2. The namespace ``OS``
188
189 Available functions:
190
191 ``cwd``
192 Contains the current working directory of the process
193
194 3. The namespace ``ENV``
195
196 This namespace contains all the environment variables as they are
197 available from :py:data:`os.environ`.
198
199 4. The namespace ``PY``
200
201 Contains selected values from the running Python:
202
203 ``version``
204 The return value of :py:func:`platform.python_version`
205
206 ``version_maj_min``
207 Just the major and minor version of the running Python
208 (``.`` separated)
209
210 ``version_maj``
211 Just the major version of the running Python
212
213 ``implementation``
214 The return value of :py:func:`platform.python_implementation`
215
216
217 Examples
218 ~~~~~~~~
219
220 ::
221
222 config.getvar("OS:cwd")
223
224 yields the current working directory as :py:func:`os.getcwd` does.
225
226
145 .. _variable-expansion: 227 .. _variable-expansion:
146 228
147 Variable Names and Expansion 229 Variable Expansion
148 ---------------------------- 230 ------------------
149 231
150 Variables are fetched with the API method :py:meth:`.Configuration.getvar` 232 Configuration variable values that are read with
151 or :py:meth:`.Configuration.getvar_s`. 233 :py:meth:`.Configuration.getvar_s` are subject to variable
152 234 expansion. The general syntactic pattern for this is::
153 Variable tree levels are separated with a point ``.`` when trying to
154 fetch them.
155
156 Looking at the example in chapter :ref:`yaml-files` -- when calling
157 ``myconfig.getvar_s("tree1.tree2.key4")`` you will get the value
158 ``get this as `tree1.tree2.key4'``.
159
160 Additionally -- configuration variable values that are read with
161 :py:meth:`.Configuration.getvar_s` are subject to
162 variable expansion. The general syntactic pattern for this is::
163 235
164 {{[namespace:]variable[|filter[|filter...]]}} 236 {{[namespace:]variable[|filter[|filter...]]}}
165 237
166 I.e.: between double curly braces an optional `namespace` name followed by 238 I.e.: between double curly braces an optional `namespace` name followed by
167 a colon ``:``, the `variable` and then zero or more filters, each one 239 a colon ``:``, the `variable` and then zero or more filters, each one
168 introduced by a pipe symbol ``|``. 240 introduced by a pipe symbol ``|``.
169 241
170 Variables are expanded at request at runtime -- exactly when calling 242 Variables are expanded lately at runtime -- exactly when calling
171 :py:meth:`.Configuration.getvar_s`. 243 :py:meth:`.Configuration.getvar_s` or :py:meth:`.Configuration.getvar`.
172
173
174 Variable Namespaces
175 ~~~~~~~~~~~~~~~~~~~
176
177 There are 4 namespaces currently:
178
179 1. The unnamed namespace (which is also default).
180
181 All the configuration variables are part of this namespace.
182
183 2. The namespace ``OS``
184
185 Available functions:
186
187 ``cwd``
188 Contains the current working directory of the process
189
190 3. The namespace ``ENV``
191
192 This namespace contains all the environment variables as they are
193 available from :py:data:`os.environ`.
194
195 4. The namespace ``PY``
196
197 Contains selected values from the running Python:
198
199 ``version``
200 The return value of :py:func:`platform.python_version`
201
202 ``version_maj_min``
203 Just the major and minor version of the running Python
204 (``.`` separated)
205
206 ``version_maj``
207 Just the major version of the running Python
208
209 ``implementation``
210 The return value of :py:func:`platform.python_implementation`
211 244
212 245
213 Filter functions 246 Filter functions
214 ~~~~~~~~~~~~~~~~ 247 ~~~~~~~~~~~~~~~~
215 248
239 272
240 :: 273 ::
241 274
242 {{OS:cwd|posixpath}} 275 {{OS:cwd|posixpath}}
243 276
244 yields the current working directory as POSIX path: on Windows all backslashes 277 expands to the current working directory as POSIX path: on Windows all
245 are replaced by forward slashes. 278 backslashes are replaced by forward slashes.
246 279
247 :: 280 ::
248 281
249 {{ENV:PATH}} 282 {{ENV:PATH}}
250 283
251 gives the current search path from the process environment. 284 expands to the current search path from the process environment.
252
253 285
254 :: 286 ::
255 287
256 {{PY:version}} 288 {{PY:version}}
257 289
258 returns the current running Python version (e.g. ``3.6.4``). 290 expands to the current running Python version (e.g. ``3.6.4``).
259 291
260 :: 292 ::
261 293
262 {{PY::implementation|upper}} 294 {{PY::implementation|upper}}
263 295
264 yields something like ``CPYTHON`` when using the standard Python interpreter 296 expands to something like ``CPYTHON`` when using the standard Python
265 written in C. 297 interpreter written in C.