/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar
5743.3.7 by Vincent Ladeuil
Add some documentation about option and sections.
1
Configuring Bazaar
2
==================
3
6332.1.2 by Vincent Ladeuil
Add a contents directive and fold the initial remarks about config option in the right section.
4
.. contents::
5
   :depth: 2
6
6362.3.1 by Vincent Ladeuil
Config doc refresh, clarifying the sections used in the implemented stacks.
7
As a Bazaar developer there are a few things you need to know about
8
configuration:
9
6362.3.2 by Vincent Ladeuil
Fix as per jelmer's review.
10
* how to add a new option,
11
12
* how add a new stack,
13
14
* how add a new store.
6362.3.1 by Vincent Ladeuil
Config doc refresh, clarifying the sections used in the implemented stacks.
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
----------------
6332.1.3 by Vincent Ladeuil
Better tweaks (the value is not a property of the option).
32
33
You add a new ``Option`` to the ``option_registry``, either inside
6362.3.1 by Vincent Ladeuil
Config doc refresh, clarifying the sections used in the implemented stacks.
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::
6332.1.1 by Martin Pool
Attempted developer documentation of configuration
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
6362.3.1 by Vincent Ladeuil
Config doc refresh, clarifying the sections used in the implemented stacks.
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
------------------------------
6332.1.3 by Vincent Ladeuil
Better tweaks (the value is not a property of the option).
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.
6332.1.1 by Martin Pool
Attempted developer documentation of configuration
75
6362.3.1 by Vincent Ladeuil
Config doc refresh, clarifying the sections used in the implemented stacks.
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
6362.3.2 by Vincent Ladeuil
Fix as per jelmer's review.
90
* replace the custom accessor calls with ``conf.get(option_name)``.
6362.3.1 by Vincent Ladeuil
Config doc refresh, clarifying the sections used in the implemented stacks.
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
132
133
The following stores are used by ``bzr`` in ways that illustrate various
134
uses of sections.
135
136
bazaar.conf
137
===========
138
139
``bzr`` itself defines two sections here:
140
141
* ``DEFAULT`` where global options are defined,
142
143
* ``ALIASES`` where command aliases are defined. This section is *not*
144
  available via ``GlobalStack``, instead, the ``bzr alias`` command uses it
145
  for its own purposes.
146
147
Plugins can define either additional options in the ``DEFAULT`` section or
148
new sections for their own needs (this is not especially encouraged
149
though). The ``bzr-bookmarks`` plugin defines a ``BOOKMARKS`` section there
150
for example.
151
152
pkgimport.conf
153
==============
154
155
The Ubuntu package importer defines a store and two stacks involving
156
``pkgimport.conf``. A no-name section contains the options common to all
157
packages and sections named after their corresponding package can also be
158
defined.
159
160
The ``ImporterStack`` uses ``locations.conf`` and the no-name section in
161
``pkgimport.conf`` for the importer options.
162
163
The ``PackageStack`` uses only ``pkgimport.conf`` and uses the section name
164
after the package followed by the no-name section.
165
166
location.conf
167
=============
168
169
``bzr`` defines sections corresponding to URLs there and includes the
170
relevant sections in ``LocationStack`` and ``BranchStack``. No no-name
171
section is recognized in this file.
172
173
branch.conf
174
===========
175
176
This file defines the option for a given branch and uses only the no-name
177
section.
178
5743.12.10 by Vincent Ladeuil
Add documentation.
179
Option
180
------
181
182
The Option object is used to define its properties:
183
184
* name: a name: a valid python identifier (even if it's not used as an
185
  identifier in python itself). This is also used to register the option.
186
6332.1.2 by Vincent Ladeuil
Add a contents directive and fold the initial remarks about config option in the right section.
187
* from_unicode: a callable accepting a unicode string and returning a
188
  suitable value for the option. If the string cannot be coerced it should
189
  return None.
190
6362.3.1 by Vincent Ladeuil
Config doc refresh, clarifying the sections used in the implemented stacks.
191
* default: the default value that Stack.get() should return if no value can
192
  be found for the option. This can also be a callable as long as it returns
193
  a unicode string.
5743.12.10 by Vincent Ladeuil
Add documentation.
194
6082.2.1 by Vincent Ladeuil
Implement default values from environment for config options
195
* default_from_env: a list of environment variables. The first variable set
196
  will provide a default value overriding 'default' which remains the
6082.2.2 by Vincent Ladeuil
Fix typos.
197
  default value if *no* environment variable is set.
6082.2.1 by Vincent Ladeuil
Implement default values from environment for config options
198
6056.2.4 by Vincent Ladeuil
Option help is now part of the object itself.
199
* help: a doc string describing the option, the first line should be a
200
  summary and can be followed by a blank line and a more detailed
6332.1.3 by Vincent Ladeuil
Better tweaks (the value is not a property of the option).
201
  explanation. This will be displayed to the user with::
202
203
    bzr help <option name>
6059.1.1 by Vincent Ladeuil
Implement from_unicode to convert config option values from store.
204
6059.1.5 by Vincent Ladeuil
Handle invalid config option values.
205
* invalid: the action to be taken when an invalid value is encountered in a
206
  store (during a Stack.get()).
207
6332.1.3 by Vincent Ladeuil
Better tweaks (the value is not a property of the option).
208
The value of an option is a unicode string or ``None`` if it's not
209
defined. By using ``from_unicode`` you can turn this string into a more
210
appropriate representation (a list of unicode strings for example).
211
5743.3.7 by Vincent Ladeuil
Add some documentation about option and sections.
212
Sections
213
--------
214
215
Options are grouped into sections which share some properties with the well
216
known dict objects:
217
5743.12.10 by Vincent Ladeuil
Add documentation.
218
* the key is the name,
219
* you can get, set and remove an option,
220
* the value is a unicode string.
5743.3.7 by Vincent Ladeuil
Add some documentation about option and sections.
221
5743.3.10 by Vincent Ladeuil
Fix typos mentioned in reviews.
222
MutableSection is needed to set or remove an option, ReadOnlySection should
5743.3.7 by Vincent Ladeuil
Add some documentation about option and sections.
223
be used otherwise.
224
6082.5.24 by Vincent Ladeuil
More documentation about local section options.
225
5743.4.16 by Vincent Ladeuil
Some doc for the stores.
226
Stores
227
------
228
5743.4.25 by Vincent Ladeuil
Address review comments by jelmer and poolie.
229
Options can be persistent in which case they are saved into Stores.
5743.4.16 by Vincent Ladeuil
Some doc for the stores.
230
231
``config.Store`` defines the abstract interface that all stores should
232
implement.
233
5743.4.25 by Vincent Ladeuil
Address review comments by jelmer and poolie.
234
This object doesn't provide direct access to the options, it only provides
235
access to Sections. This is deliberate to ensure that sections can be
236
properly shared by reusing the same underlying objects. Accessing options
237
should be done via the ``Section`` objects.
5743.4.16 by Vincent Ladeuil
Some doc for the stores.
238
239
A ``Store`` can contain one or more sections, each section is uniquely
240
identified by a unicode string.
241
6362.3.1 by Vincent Ladeuil
Config doc refresh, clarifying the sections used in the implemented stacks.
242
``config.IniFileStore`` is an implementation that use ``ConfigObj``.
5743.4.16 by Vincent Ladeuil
Some doc for the stores.
243
244
Depending on the object it is associated with (or not) a ``Store`` also needs
6362.3.1 by Vincent Ladeuil
Config doc refresh, clarifying the sections used in the implemented stacks.
245
to implement a locking mechanism. ``LockableIniFileStore`` implements such a
246
mechanism for ``IniFileStore`` based stores.
5743.5.6 by Vincent Ladeuil
Mention that the test framework ought to support adding new stores.
247
248
Classes are provided for the usual Bazaar configuration files and could be
249
used as examples to define new ones if needed. The associated tests provides a
250
basis for new classes which only need to register themselves in the right
251
places to inherit from the existing basic tests and add their own specific
252
ones.
5743.2.29 by Vincent Ladeuil
Add doc for the section matchers.
253
254
Filtering sections
255
------------------
256
6362.3.1 by Vincent Ladeuil
Config doc refresh, clarifying the sections used in the implemented stacks.
257
For some contexts, only some sections from a given store will apply. The
258
``SectionMatcher`` objects are used to define which sections in a store
259
apply to a given context.
5743.2.29 by Vincent Ladeuil
Add doc for the section matchers.
260
261
The main constraint here is that a ``SectionMatcher`` should delay the loading
262
of the associated store as long as possible. The constructor should collect
263
all data needed for the selection and uses it while processing the sections in
264
``get_sections``.
265
6123.7.1 by Vincent Ladeuil
Provide config.IdMatcher for config files defining secion names as unique ids
266
Only ``ReadOnlySection`` objects are manipulated here but a
267
``SectionMatcher`` can return dedicated ``Section`` objects to provide
268
additional context (the ``LocationSection`` add an ``extra_path`` attribute
6362.3.1 by Vincent Ladeuil
Config doc refresh, clarifying the sections used in the implemented stacks.
269
to implement the section local options for example). If no sections match,
6123.7.1 by Vincent Ladeuil
Provide config.IdMatcher for config files defining secion names as unique ids
270
an empty list is returned.
5743.2.29 by Vincent Ladeuil
Add doc for the section matchers.
271
6362.3.1 by Vincent Ladeuil
Config doc refresh, clarifying the sections used in the implemented stacks.
272
Options local to a section can be defined for special purposes and be
6082.5.24 by Vincent Ladeuil
More documentation about local section options.
273
handled by ``Section.get()``. One such option is ``relpath`` which is
274
defined in ``LocationSection`` as an alternative to the ``appendpath``
275
policy.
276
6082.5.25 by Vincent Ladeuil
Add ``basename`` as a section local option
277
For ``appendpath``, the ``LocationSection`` will carry ``extra_path`` as the
278
relative path between the section name and the location used. ``relpath``
279
will be available as a ``Section`` local option with the same
280
value. ``basename`` will carry the location base name and be available as a
281
local option with the same name. Note that such options can only be expanded
282
inside the section that defines them.
5743.1.24 by Vincent Ladeuil
Some generic doc about stacks.
283
6123.7.1 by Vincent Ladeuil
Provide config.IdMatcher for config files defining secion names as unique ids
284
Specific section matchers can be implemented by overriding ``get_sections``
285
or just ``match``.
286
287
``bzrlib`` provides:
288
289
* ``LocationMatcher(store, location)``: To select all sections that match
290
  ``location``.
291
6123.7.2 by Vincent Ladeuil
Rename IdMatcher to NameMatcher.
292
* ``NameMatcher(store, unique_id)``: To select a single section matching
6123.7.1 by Vincent Ladeuil
Provide config.IdMatcher for config files defining secion names as unique ids
293
  ``unique_id``.
294
5743.1.24 by Vincent Ladeuil
Some generic doc about stacks.
295
Stacks
296
------
297
6332.1.3 by Vincent Ladeuil
Better tweaks (the value is not a property of the option).
298
An option can take different values depending on the context it is
299
used. This can involve configuration files, options from the command line,
5743.1.24 by Vincent Ladeuil
Some generic doc about stacks.
300
default values in bzrlib and then some.
301
302
Such a context is implemented by creating a list of ``Section`` stacked upon
303
each other. A ``Stack`` can then be asked for an option value and returns the
304
first definition found.
305
306
This provides a great flexibility to decide priorities between sections when
307
the stack is defined without to worry about them in the code itself.
308
309
A stack also defines a mutable section (which can be None) to handle
310
modifications.
311
312
Many sections (or even stores) are aimed at providing default values for an
313
option but these sections shouldn't be modified lightly as modifying an option
314
used for different contexts will indeed be seen by all these contexts.
315
5743.1.35 by Vincent Ladeuil
Address some review comments from jelmer and poolie.
316
Default values in configuration files are defined by users. Developers
5743.1.24 by Vincent Ladeuil
Some generic doc about stacks.
317
shouldn't have to modify them, as such, no mechanism nor heuristics are used
5743.1.35 by Vincent Ladeuil
Address some review comments from jelmer and poolie.
318
to find which section (or sections) should be modified.
5743.1.24 by Vincent Ladeuil
Some generic doc about stacks.
319
5743.1.35 by Vincent Ladeuil
Address some review comments from jelmer and poolie.
320
A ``Stack`` defines a mutable section when there is no ambiguity.  If there
321
is one, then the *user* should be able to decide and in this case a new
322
``Stack`` can be created cheaply.
5743.1.24 by Vincent Ladeuil
Some generic doc about stacks.
323
5743.6.5 by Vincent Ladeuil
Complement the stacks doc.
324
Different stacks can be created for different purposes, the existing
5743.6.16 by Vincent Ladeuil
Fix typo.
325
``GlobalStack``, ``LocationStack`` and ``BranchStack`` can be used as basis
5743.6.5 by Vincent Ladeuil
Complement the stacks doc.
326
or examples. These classes are the only ones that should be used in code,
327
``Stores`` can be used to build them but shouldn't be used otherwise, ditto
328
for sections. Again, the associated tests could and should be used against the
329
created stacks.