Rev 5756: Better explain lazy loading and make sure the mutable section respect the design. in file:///home/vila/src/bzr/experimental/config/
Vincent Ladeuil
v.ladeuil+lp at free.fr
Wed Apr 6 09:11:48 UTC 2011
At file:///home/vila/src/bzr/experimental/config/
------------------------------------------------------------
revno: 5756
revision-id: v.ladeuil+lp at free.fr-20110406091148-30rdyrxq0c3odhrz
parent: v.ladeuil+lp at free.fr-20110406082436-1d0css49ccsb6vc2
committer: Vincent Ladeuil <v.ladeuil+lp at free.fr>
branch nick: config-stack
timestamp: Wed 2011-04-06 11:11:48 +0200
message:
Better explain lazy loading and make sure the mutable section respect the design.
-------------- next part --------------
=== modified file 'bzrlib/config.py'
--- a/bzrlib/config.py 2011-04-06 08:24:36 +0000
+++ b/bzrlib/config.py 2011-04-06 09:11:48 +0000
@@ -2183,19 +2183,27 @@
class ConfigStack(object):
"""A stack of configurations where an option can be defined"""
- def __init__(self, sections, mutable_section=None):
+ def __init__(self, sections, get_mutable_section=None):
"""Creates a stack of sections with an optional store for changes.
:param sections: A list of ReadOnlySection or callables that returns an
iterable of ReadOnlySection.
- :param mutable_section: A MutableSection where changes are recorded.
+ :param get_mutable_section: A callable that returns a MutableSection
+ where changes are recorded.
"""
self.sections = sections
- self.mutable_section = mutable_section
+ self.get_mutable_section = get_mutable_section
def get(self, name):
- """Return the value from the first definition found in the sections"""
+ """Return the value from the first definition found in the sections.
+
+ This is where we guarantee that sections coming from Store lazily load
+ them: the loading is delayed until we need to either check that an
+ option exists or get its value, which in turn may require to discover
+ in which sections it can be defined. Both of these (section and option
+ existence) require loading the store (even partially).
+ """
for section_or_callable in self.sections:
# Each section can expand to multiple ones when a callable is used
if callable(section_or_callable):
@@ -2210,7 +2218,13 @@
return None
def set(self, name, value):
- self.mutable_section.set(name, value)
+ """Set a new value for the option.
+
+ This where we guarantee that the mutable section is lazily loaded: this
+ means we won't load the correspoding store before setting a value.
+ """
+ section = self.get_mutable_section()
+ section.set(name, value)
class cmd_config(commands.Command):
=== modified file 'bzrlib/tests/test_config.py'
--- a/bzrlib/tests/test_config.py 2011-04-06 07:59:42 +0000
+++ b/bzrlib/tests/test_config.py 2011-04-06 09:11:48 +0000
@@ -2069,7 +2069,7 @@
store = config.ConfigObjStore.from_string(
'foo=bar', self.get_transport(), 'test.conf')
conf = config.ConfigStack(
- [store.get_sections], store.get_mutable_section(None))
+ [store.get_sections], store.get_mutable_section)
self.assertEquals('bar', conf.get('foo'))
conf.set('foo', 'baz')
# Did we get it back ?
@@ -2079,7 +2079,7 @@
store = config.ConfigObjStore.from_string(
'', self.get_transport(), 'test.conf')
conf = config.ConfigStack(
- [store.get_sections], store.get_mutable_section(None))
+ [store.get_sections], store.get_mutable_section)
conf.set('foo', 'baz')
self.assertEquals, 'baz', conf.get('foo')
More information about the bazaar-commits
mailing list