tiramisu/doc/config.txt

149 lines
4.6 KiB
Text
Raw Normal View History

.. default-role:: literal
2012-11-20 17:14:58 +01:00
===============================
Configuration handling basics
===============================
2012-10-05 16:00:07 +02:00
2013-05-21 18:42:56 +02:00
Tiramisu is made of almost three main objects :
- :class:`tiramisu.config.Config` witch is the whole configuration entry point
2013-05-21 18:42:56 +02:00
- :class:`tiramisu.option.Option` stands for the option types
- :class:`tiramisu.option.OptionDescription` is the shema, the option's structure
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.
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.
``config.optgroup.optname``.
2013-05-17 12:11:14 +02:00
Let's make the protocol of accessing a config's attribute explicit
(because explicit is better than implicit):
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.
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.
If you know the path:
::
>>> config.gc.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
::
cfg.name = value
2013-05-21 18:42:56 +02:00
And if you wanna come back to a default value, do it the pythonic way::
del(cfg.name)
2013-05-17 12:11:14 +02:00
.. module:: tiramisu.config
The handling of options is split into two parts: the description of
which options are available, what their possible values and defaults are
and how they are organized into a tree. A specific choice of options is
bundled into a configuration object which has a reference to its option
description (and therefore makes sure that the configuration values
adhere to the option description).
Configuration's interesting methods
------------------------------------------
2013-05-17 12:11:14 +02:00
A `Config` object is informed by an `option.OptionDescription`
instance. The attributes of the ``Config`` objects are the names of the
children of the ``OptionDescription``.
Here are the (useful) methods on ``Config``:
With this `config.Config()` configuration management entry point,
it is possible to
- `iter` on config, notice that there is an iteration order wich is
the order of the :ref:`optdescr` specification entries,
- compare two configs (equality),
2013-05-21 18:42:56 +02:00
- export the whole config into a `dict` with `config.SubConfig.make_dict()`,
2013-05-17 12:11:14 +02:00
`option.Option()` objects in a config are iterable in the pythonic
way, that is something like `[(name, value) for name, value in config]`.
To iter on groups in the same manner, use the
`config.Config.iter_groups()` method wich yields generators too.
.. currentmodule:: tiramisu.config
2013-05-17 12:11:14 +02:00
.. autoclass:: Config
.. automethod:: __init__
.. rubric:: Methods
.. autosummary::
~Config.find
~Config.find_first
~Config.iter_groups
~Config.__iter__
.. automethod:: find
.. automethod:: find_first
.. automethod:: iter_groups
.. automethod:: __iter__
2013-05-21 18:42:56 +02:00
or a SubConfig, or a MetaConfig :
.. autoclass:: SubConfig
:members:
:special-members: