2012-07-13 09:40:48 +02:00
|
|
|
.. default-role:: literal
|
|
|
|
|
2012-11-20 17:14:58 +01:00
|
|
|
===============================
|
|
|
|
Configuration handling basics
|
|
|
|
===============================
|
2012-10-05 16:00:07 +02:00
|
|
|
|
2012-07-13 09:40:48 +02:00
|
|
|
`Config` and `Option` objects
|
2012-11-20 17:14:58 +01:00
|
|
|
=========================================
|
2012-07-13 09:40:48 +02:00
|
|
|
|
|
|
|
|
2012-11-20 17:14:58 +01:00
|
|
|
Take a look at `test_config.test_base_config()` or
|
|
|
|
`test_config.test_base_config_and_groups()`.
|
2012-07-13 09:40:48 +02:00
|
|
|
|
|
|
|
Accessing the configuration `Option`'s
|
|
|
|
-----------------------------------------
|
|
|
|
|
|
|
|
The `Config` object attribute access notation stands for the value of the
|
|
|
|
configuration's `Option`. That is, the `Config`'s object attribute is the name
|
|
|
|
of the `Option`, and the value is the value accessed by the `__getattr__`
|
2012-10-05 16:00:07 +02:00
|
|
|
attribute access mechanism.
|
2012-07-13 09:40:48 +02:00
|
|
|
|
|
|
|
If the attribute of the `Config` called by `__getattr__` has not been set before
|
|
|
|
(by the classic `__setattr__` mechanism), the default value of the `Option`
|
|
|
|
object is returned, and if no `Option` has been declared in the
|
|
|
|
`OptionDescription` (that is the schema of the configuration), an
|
|
|
|
`AttributeError` is raised.
|
|
|
|
|
|
|
|
::
|
|
|
|
|
|
|
|
>>> gcdummy = BoolOption('dummy', 'dummy', default=False)
|
|
|
|
>>> gcdummy._name
|
|
|
|
'dummy'
|
|
|
|
>>> gcdummy.getdefault()
|
|
|
|
False
|
|
|
|
>>> descr = OptionDescription('tiramisu', '', [gcdummy])
|
|
|
|
>>> cfg = Config(descr)
|
|
|
|
>>> cfg.dummy
|
|
|
|
False
|
|
|
|
>>> cfg.dummy = True
|
|
|
|
>>> cfg.dummy
|
|
|
|
True
|
|
|
|
>>> cfg.idontexist
|
|
|
|
AttributeError: 'OptionDescription' object has no attribute 'idontexist'
|
|
|
|
|
2012-10-05 16:00:07 +02:00
|
|
|
The configuration `Option` objects (in this case the `BoolOption`), are
|
|
|
|
organized into a tree into nested `OptionDescription` objects. Every
|
|
|
|
option has a name, as does every option group. The parts of the full
|
|
|
|
name of the option are separated by dots: e.g.
|
2012-07-13 09:40:48 +02:00
|
|
|
``config.optgroup.optname``.
|
|
|
|
|
|
|
|
**Can you repeat it, what is the protocol of accessing a config's attribute ?**
|
|
|
|
|
|
|
|
1. If the option has not been declared, an `AttributeError` is raised,
|
|
|
|
|
|
|
|
2. If an option is declared, but neither a value nor a default value has
|
|
|
|
been set, the returned value is `None`,
|
|
|
|
|
|
|
|
3. If an option is declared and a default value has been set, but no value
|
|
|
|
has been set, the returned value is the default value of the option,
|
|
|
|
|
|
|
|
4. If an option is declared, and a value has been set, the returned value is
|
|
|
|
the value of the option.
|
|
|
|
|
2012-08-13 11:12:08 +02:00
|
|
|
What if a value has been set and `None` is to be returned again ? Don't
|
2012-11-20 17:14:58 +01:00
|
|
|
worry, an option value can be "reseted" with the help of the `option.Option.reset()`
|
2012-11-14 11:30:11 +01:00
|
|
|
method.
|
2012-08-13 11:12:08 +02:00
|
|
|
|
2012-10-05 16:00:07 +02:00
|
|
|
If you do not want to use the pythonic way, that is the attribute access
|
|
|
|
way to obtain the value of the configuration option, you can also search
|
|
|
|
for it recursively in the whole config namespaces with the ``get()``
|
2012-07-13 09:40:48 +02:00
|
|
|
method :
|
|
|
|
|
|
|
|
::
|
|
|
|
|
|
|
|
>>> config.get('bool')
|
|
|
|
True
|
|
|
|
|
2012-10-05 16:00:07 +02:00
|
|
|
To find the right option, `get()` searches recursively into the whole
|
|
|
|
tree. For example, to find an option which is in the `gc` namespace
|
2012-07-13 09:40:48 +02:00
|
|
|
there are two possibilites.
|
|
|
|
|
|
|
|
If you know the path:
|
|
|
|
|
|
|
|
::
|
|
|
|
|
|
|
|
>>> config.gc.dummy
|
|
|
|
False
|
|
|
|
|
|
|
|
If you don't remember the path:
|
|
|
|
|
|
|
|
::
|
|
|
|
|
|
|
|
>>> config.get('dummy')
|
|
|
|
False
|
|
|
|
|
|
|
|
Setting the values of the options
|
|
|
|
----------------------------------------
|
|
|
|
|
|
|
|
An important part of the setting of the configuration consists of setting the
|
|
|
|
values of the configuration options. There are different ways of setting values,
|
2012-10-05 16:00:07 +02:00
|
|
|
the first one is of course the `__setattr__` method
|
2012-07-13 09:40:48 +02:00
|
|
|
|
|
|
|
::
|
|
|
|
|
|
|
|
cfg.name = value
|
|
|
|
|
2012-10-05 16:00:07 +02:00
|
|
|
wich has the same effect that the "global" `set()` method : it expects that
|
2012-11-20 17:14:58 +01:00
|
|
|
the value owner is the default `glossary#valueowner`
|
2012-07-13 09:40:48 +02:00
|
|
|
|
|
|
|
::
|
|
|
|
|
|
|
|
cfg.set(name=value)
|