Mercurial > hgrepos > Python > libs > ConfigMix
comparison doc/introduction.rst @ 115:a5339d39af5c
Begin the documentation of variables and its expansion
| author | Franz Glasner <hg@dom66.de> |
|---|---|
| date | Sun, 25 Mar 2018 16:45:15 +0200 |
| parents | aa0c61e79660 |
| children | c5b638f9c607 |
comparison
equal
deleted
inserted
replaced
| 114:aa0c61e79660 | 115:a5339d39af5c |
|---|---|
| 9 | 9 |
| 10 - :ref:`YAML files <yaml-files>` | 10 - :ref:`YAML files <yaml-files>` |
| 11 - :ref:`INI files <ini-files>` | 11 - :ref:`INI files <ini-files>` |
| 12 - :ref:`executable Python scripts <executable-python-scripts>` | 12 - :ref:`executable Python scripts <executable-python-scripts>` |
| 13 | 13 |
| 14 .. todo:: Describe evaluation syntax | |
| 15 | |
| 16 .. todo:: Describe available filter functions (and syntax) | |
| 17 | |
| 18 .. todo:: Describe late evaluation of interpolation strings | |
| 19 | |
| 20 | 14 |
| 21 .. _yaml-files: | 15 .. _yaml-files: |
| 22 | 16 |
| 23 YAML Files | 17 YAML Files |
| 24 ---------- | 18 ---------- |
| 66 | 60 |
| 67 .. note:: All strings are returned as Unicode text strings. | 61 .. note:: All strings are returned as Unicode text strings. |
| 68 | 62 |
| 69 .. note:: Contrary to the behaviour of the standard Python :mod:`configparser` | 63 .. note:: Contrary to the behaviour of the standard Python :mod:`configparser` |
| 70 module the INI file reader is *case-sensitive*. | 64 module the INI file reader is *case-sensitive*. |
| 71 | |
| 72 .. todo:: Document the build of tree-ish configuration settings out of | |
| 73 INI files. | |
| 74 | 65 |
| 75 The example INI style configuration below yields an equivalent | 66 The example INI style configuration below yields an equivalent |
| 76 configuration to the YAML configuration above: | 67 configuration to the YAML configuration above: |
| 77 | 68 |
| 78 .. literalinclude:: ../tests/data/conf10.ini | 69 .. literalinclude:: ../tests/data/conf10.ini |
| 147 ``.yml`` or ``.yaml`` | 138 ``.yml`` or ``.yaml`` |
| 148 for YAML configuration files | 139 for YAML configuration files |
| 149 | 140 |
| 150 ``.ini`` | 141 ``.ini`` |
| 151 for INI configuration files | 142 for INI configuration files |
| 143 | |
| 144 | |
| 145 .. _variable-expansion: | |
| 146 | |
| 147 Variable Names and Expansion | |
| 148 ---------------------------- | |
| 149 | |
| 150 Variables are fetched with the API method :py:meth:`.Configuration.getvar` | |
| 151 or :py:meth:`.Configuration.getvar_s`. | |
| 152 | |
| 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 | |
| 164 {{[namespace:]variable[|filter[|filter...]]}} | |
| 165 | |
| 166 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 | |
| 168 introduced by a pipe symbol ``|``. | |
| 169 | |
| 170 Variables are expanded at request at runtime -- exactly when calling | |
| 171 :py:meth:`.Configuration.getvar_s`. | |
| 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 | |
| 212 | |
| 213 Filter functions | |
| 214 ~~~~~~~~~~~~~~~~ | |
| 215 | |
| 216 Interpolated values can be processed through a series of filter functions:: | |
| 217 | |
| 218 {{my.variable|filter1|filter2}} | |
| 219 | |
| 220 Available filter functions are: | |
| 221 | |
| 222 ``urlquote`` | |
| 223 | |
| 224 ``saslprep`` | |
| 225 | |
| 226 ``normpath`` | |
| 227 | |
| 228 ``abspath`` | |
| 229 | |
| 230 ``posixpath`` | |
| 231 | |
| 232 ``lower`` | |
| 233 | |
| 234 ``upper`` | |
| 235 | |
| 236 | |
| 237 Examples | |
| 238 ~~~~~~~~ | |
| 239 | |
| 240 :: | |
| 241 | |
| 242 {{OS:cwd|posixpath}} | |
| 243 | |
| 244 yields the current working directory as POSIX path: on Windows all backslashes | |
| 245 are replaced by forward slashes. | |
| 246 | |
| 247 :: | |
| 248 | |
| 249 {{ENV:PATH}} | |
| 250 | |
| 251 gives the current search path from the process environment. | |
| 252 | |
| 253 | |
| 254 :: | |
| 255 | |
| 256 {{PY:version}} | |
| 257 | |
| 258 returns the current running Python version (e.g. ``3.6.4``). | |
| 259 | |
| 260 :: | |
| 261 | |
| 262 {{PY::implementation|upper}} | |
| 263 | |
| 264 yields something like ``CPYTHON`` when using the standard Python interpreter | |
| 265 written in C. |
