~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to doc/developers/configuration.txt

  • Committer: Vincent Ladeuil
  • Date: 2012-03-13 17:25:29 UTC
  • mfrom: (6499 +trunk)
  • mto: This revision was merged to the branch mainline in revision 6501.
  • Revision ID: v.ladeuil+lp@free.fr-20120313172529-i0suyjnepsor25i7
Merge trunk

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
Configuring Bazaar
2
2
==================
3
3
 
4
 
A configuration option has:
5
 
 
6
 
* a name: a valid python identifier (even if it's not used as an
7
 
  identifier in python itself)
8
 
 
9
 
* a value: a unicode string
 
4
.. contents::
 
5
   :depth: 2
 
6
 
 
7
As a Bazaar developer there are a few things you need to know about
 
8
configuration:
 
9
 
 
10
* how to add a new option,
 
11
 
 
12
* how add a new stack,
 
13
 
 
14
* how add a new store.
 
15
 
 
16
The first sections in this document summarize the steps needed when adding a
 
17
new configuration item, the rest of the document gives more internal details
 
18
on how this is implemented.
 
19
 
 
20
Get an option value
 
21
-------------------
 
22
 
 
23
Options values are obtained with ``stack.get(option_name)`` where ``stack``
 
24
is one of the daughter classes of ``config.Stack``, see their docstrings for
 
25
a description of which sections are used from which stores.
 
26
 
 
27
The value returned is of the type declared for that ``Option`` and if
 
28
nothing is specifically declared you will get the default for that option.
 
29
 
 
30
Add a new option
 
31
----------------
 
32
 
 
33
You add a new ``Option`` to the ``option_registry``, either inside
 
34
``bzrlib/config.py`` or during initialization of your plugin (use
 
35
``register_lazy`` in this case). New plugins should have systematic
 
36
hierarchical names so that related values are grouped together::
 
37
 
 
38
  option_registry.register(
 
39
      Option('dirstate.fdatasync', default=True,
 
40
            from_unicode=bool_from_store,
 
41
            help="Flush dirstate changes onto physical disk? ...."))
 
42
 
 
43
You then need to decide which stack is appropriate to implement the Option
 
44
policy:
 
45
 
 
46
* which config files (aka ``Store``) needs to be queried, which sections are
 
47
  relevant and in what order,
 
48
 
 
49
* which section will receive the modifications (if relevant).
 
50
 
 
51
The docstrings for the existing stacks cover most of the known use cases.
 
52
 
 
53
Modify an option value or delete an option
 
54
------------------------------------------
 
55
 
 
56
Just reading an option is what is needed most of the time, modifying option
 
57
values or removing options is usually something that is not automated but
 
58
left to the user (with ``bzr config``).
 
59
 
 
60
Nevertheless, if you need to save a modified option value, use
 
61
``.set(option_name, value)`` and ``.remove(option_name)`` to delete the
 
62
option. Both methods are provided by the ``Stack`` object.
 
63
 
 
64
But before doing that, you must be sure that the stack you're using have a
 
65
writable section (this is true for ``GlobalStack`` which uses the
 
66
``DEFAULT`` section in ``bazaar.conf`` and for ``BranchStack``which uses the
 
67
no-name section in ``branch.conf``).
 
68
 
 
69
Old and new configuration code
 
70
------------------------------
 
71
 
 
72
There is (as of late 2011) some older and some newer configuration code. The
 
73
old code has specific methods for various checks or uses classes like
 
74
``GlobalConfig``.  Don't add to to it; try to remove it.
 
75
 
 
76
If you encounter an option using the old code you may want to migrate
 
77
it. This generally involves:
 
78
 
 
79
* registering the option,
 
80
 
 
81
* replace the old config by a stack:
 
82
 
 
83
  * ``GlobalConfig`` became ``GlobalStack``,
 
84
 
 
85
  * ``LocationConfig`` became ``LocationStack``,
 
86
 
 
87
  * ``BranchConfig`` became ``BranchStack`` (or in this case,
 
88
    ``get_config()`` became ``get_config_stack()``.
 
89
 
 
90
* replace the custom accessor calls with ``conf.get(option_name)``.
 
91
 
 
92
The new config code provides some help for commonly encountered use cases
 
93
that can allow further simplifications like:
 
94
 
 
95
* providing a default value when the option is not defined in any way by the
 
96
  user,
 
97
 
 
98
* convert the unicode string provided by the user into a suitable
 
99
  representation (integer, list, etc).
 
100
 
 
101
Adding a new stack
 
102
------------------
 
103
 
 
104
Stacks capture the various places an option can be declared by the user with
 
105
associated levels of generality and query them in the appropriate order
 
106
returning the first definition found. For example, the
 
107
``append_revisions_only`` option may be declared for all branches of a user
 
108
in ``bazaar.conf``, or for a hierarchy of branches in ``locations.conf`` or
 
109
in a single branch in ``branch.conf``.
 
110
 
 
111
Defining a new stack means you need a new way to expose these levels to the
 
112
user that is not covered by the existing stacks.
 
113
 
 
114
This is achieved by declaring:
 
115
 
 
116
* which stores can provide a value for the option,
 
117
 
 
118
* which sections apply to the stack instance, some filtering for a given
 
119
  context can be defined,
 
120
 
 
121
* which (store, section) should receive the modifications.
 
122
 
 
123
Mapping different sections to different stacks is a powerful way to organize
 
124
the options and provide various levels of configuration to the user. This is
 
125
achieved with ``Store`` and ``SectionMatcher`` objects.
 
126
 
 
127
 
 
128
Adding a new store
 
129
------------------
 
130
 
 
131
The following stores are used by ``bzr`` in ways that illustrate various
 
132
uses of sections.
 
133
 
 
134
bazaar.conf
 
135
===========
 
136
 
 
137
``bzr`` itself defines two sections here:
 
138
 
 
139
* ``DEFAULT`` where global options are defined,
 
140
 
 
141
* ``ALIASES`` where command aliases are defined. This section is *not*
 
142
  available via ``GlobalStack``, instead, the ``bzr alias`` command uses it
 
143
  for its own purposes.
 
144
 
 
145
Plugins can define either additional options in the ``DEFAULT`` section or
 
146
new sections for their own needs (this is not especially encouraged
 
147
though). The ``bzr-bookmarks`` plugin defines a ``BOOKMARKS`` section there
 
148
for example.
 
149
 
 
150
pkgimport.conf
 
151
==============
 
152
 
 
153
The Ubuntu package importer defines a store and two stacks involving
 
154
``pkgimport.conf``. A no-name section contains the options common to all
 
155
packages and sections named after their corresponding package can also be
 
156
defined.
 
157
 
 
158
The ``ImporterStack`` uses ``locations.conf`` and the no-name section in
 
159
``pkgimport.conf`` for the importer options.
 
160
 
 
161
The ``PackageStack`` uses only ``pkgimport.conf`` and uses the section name
 
162
after the package followed by the no-name section.
 
163
 
 
164
location.conf
 
165
=============
 
166
 
 
167
``bzr`` defines sections corresponding to URLs there and includes the
 
168
relevant sections in ``LocationStack`` and ``BranchStack``. No no-name
 
169
section is recognized in this file.
 
170
 
 
171
branch.conf
 
172
===========
 
173
 
 
174
This file defines the option for a given branch and uses only the no-name
 
175
section.
10
176
 
11
177
Option
12
178
------
16
182
* name: a name: a valid python identifier (even if it's not used as an
17
183
  identifier in python itself). This is also used to register the option.
18
184
 
19
 
* default: the default value that Stack.get() should return if no
20
 
  value can be found for the option.
21
 
 
22
 
Since plugins should be able to lazily register options, the associated help
23
 
is not part of the object but provided at registration time.
 
185
* from_unicode: a callable accepting a unicode string and returning a
 
186
  suitable value for the option. If the string cannot be coerced it should
 
187
  return None.
 
188
 
 
189
* override_from_env: a list of environment variables. The first variable set
 
190
  will be used as the option value overriding any other definition of the
 
191
  option.
 
192
 
 
193
* default: the default value that Stack.get() should return if no value can
 
194
  be found for the option. This can also be a callable as long as it returns
 
195
  a unicode string.
 
196
 
 
197
* default_from_env: a list of environment variables. The first variable set
 
198
  will provide a default value overriding 'default' which remains the
 
199
  default value if *no* environment variable is set.
 
200
 
 
201
* help: a doc string describing the option, the first line should be a
 
202
  summary and can be followed by a blank line and a more detailed
 
203
  explanation. This will be displayed to the user with::
 
204
 
 
205
    bzr help <option name>
 
206
 
 
207
* invalid: the action to be taken when an invalid value is encountered in a
 
208
  store (during a Stack.get()).
 
209
 
 
210
The value of an option is a unicode string or ``None`` if it's not
 
211
defined. By using ``from_unicode`` you can turn this string into a more
 
212
appropriate representation.
 
213
 
 
214
If you need a list value, you should use ``ListOption`` instead.
 
215
 
 
216
For options that take their values from a ``Registry`` object,
 
217
``RegistryOption`` can be used. This will automatically take care of
 
218
looking up the specified values in the dictionary and documenting the
 
219
possible values in help.
24
220
 
25
221
Sections
26
222
--------
35
231
MutableSection is needed to set or remove an option, ReadOnlySection should
36
232
be used otherwise.
37
233
 
 
234
 
38
235
Stores
39
236
------
40
237
 
51
248
A ``Store`` can contain one or more sections, each section is uniquely
52
249
identified by a unicode string.
53
250
 
54
 
``config.ConfigObjStore`` is an implementation that use ``ConfigObj``.
 
251
``config.IniFileStore`` is an implementation that use ``ConfigObj``.
55
252
 
56
253
Depending on the object it is associated with (or not) a ``Store`` also needs
57
 
to implement a locking mechanism. ``LockableConfigObjStore`` implements such a
58
 
mechanism for ``ConfigObj`` based stores.
 
254
to implement a locking mechanism. ``LockableIniFileStore`` implements such a
 
255
mechanism for ``IniFileStore`` based stores.
59
256
 
60
257
Classes are provided for the usual Bazaar configuration files and could be
61
258
used as examples to define new ones if needed. The associated tests provides a
63
260
places to inherit from the existing basic tests and add their own specific
64
261
ones.
65
262
 
 
263
A ``Store`` defines how option values are stored, this includes:
 
264
 
 
265
* defining the sections where the options are grouped,
 
266
 
 
267
* defining how the values are quoted/unquoted for storage purposes. Stacks
 
268
  use the unquoted values internally (default value handling and option
 
269
  expansion are simpler this way) and ``bzr config`` quote them when they
 
270
  need to be displayed.
 
271
 
 
272
 
66
273
Filtering sections
67
274
------------------
68
275
 
69
 
For some contexts, only some sections from a given store will apply. Defining
70
 
which is what the ``SectionMatcher`` are about.
 
276
For some contexts, only some sections from a given store will apply. The
 
277
``SectionMatcher`` objects are used to define which sections in a store
 
278
apply to a given context.
71
279
 
72
280
The main constraint here is that a ``SectionMatcher`` should delay the loading
73
281
of the associated store as long as possible. The constructor should collect
74
282
all data needed for the selection and uses it while processing the sections in
75
283
``get_sections``.
76
284
 
77
 
Only ``ReadOnlySection`` objects are manipulated here but a ``SectionMatcher``
78
 
can return dedicated ``Section`` to provide additional context (the
79
 
``LocationSection`` add an ``extra_path`` attribute to implement the
80
 
``appendpath`` policy for example).
81
 
 
82
 
.. FIXME: Replace the appendpath example if/when it's deprecated ;)
 
285
Only ``ReadOnlySection`` objects are manipulated here but a
 
286
``SectionMatcher`` can return dedicated ``Section`` objects to provide
 
287
additional context (the ``LocationSection`` add an ``extra_path`` attribute
 
288
to implement the section local options for example). If no sections match,
 
289
an empty list is returned.
 
290
 
 
291
Options local to a section can be defined for special purposes and be
 
292
handled by ``Section.get()``. One such option is ``relpath`` which is
 
293
defined in ``LocationSection`` as an alternative to the ``appendpath``
 
294
policy.
 
295
 
 
296
For ``appendpath``, the ``LocationSection`` will carry ``extra_path`` as the
 
297
relative path between the section name and the location used. ``relpath``
 
298
will be available as a ``Section`` local option with the same
 
299
value. ``basename`` will carry the location base name and be available as a
 
300
local option with the same name. Note that such options can only be expanded
 
301
inside the section that defines them.
 
302
 
 
303
Specific section matchers can be implemented by overriding ``get_sections``
 
304
or just ``match``.
 
305
 
 
306
``bzrlib`` provides:
 
307
 
 
308
* ``NameMatcher(store, unique_id)``: To select a single section matching
 
309
  ``unique_id``.
 
310
 
 
311
* ``LocationMatcher(store, location)``: To select all sections that match
 
312
  ``location`` sorted by decreasing number of path components.
 
313
 
 
314
* ``StartingPathMatcher(store, location)``: To select all sections that
 
315
  match ``location`` in the order they appear in the ``store``.
83
316
 
84
317
Stacks
85
318
------
86
319
 
87
 
An option can take different values depending on the context it is used. Such
88
 
a context can involve configuration files, options from the command line,
 
320
An option can take different values depending on the context it is
 
321
used. This can involve configuration files, options from the command line,
89
322
default values in bzrlib and then some.
90
323
 
91
324
Such a context is implemented by creating a list of ``Section`` stacked upon
116
349
``Stores`` can be used to build them but shouldn't be used otherwise, ditto
117
350
for sections. Again, the associated tests could and should be used against the
118
351
created stacks.
 
352
 
 
353
Also note that ``MemoryStack`` can be used without any disk resources which
 
354
allows for simpler and faster tests. A common pattern is to accept a
 
355
``config`` parameter related to a given feature and test it with predefined
 
356
configurations without involving the whole
 
357
stack. ``bzrlib.tests.test_commit``, ``bzrlib.tests.test_gpg`` and
 
358
``bzrlib.tests.test_smtp_connection`` are good examples.
 
359