Rev 5759: Allows empty sections and empty section callables. in file:///home/vila/src/bzr/experimental/config/
Vincent Ladeuil
v.ladeuil+lp at free.fr
Wed Apr 6 09:38:41 UTC 2011
At file:///home/vila/src/bzr/experimental/config/
------------------------------------------------------------
revno: 5759
revision-id: v.ladeuil+lp at free.fr-20110406093841-h1gqat7d5ozp8hfc
parent: v.ladeuil+lp at free.fr-20110406091927-xc722o12neu8n7zp
committer: Vincent Ladeuil <v.ladeuil+lp at free.fr>
branch nick: config-stack
timestamp: Wed 2011-04-06 11:38:41 +0200
message:
Allows empty sections and empty section callables.
-------------- next part --------------
=== modified file 'bzrlib/config.py'
--- a/bzrlib/config.py 2011-04-06 09:19:27 +0000
+++ b/bzrlib/config.py 2011-04-06 09:38:41 +0000
@@ -2182,7 +2182,7 @@
class ConfigStack(object):
"""A stack of configurations where an option can be defined"""
- def __init__(self, sections, get_mutable_section=None):
+ def __init__(self, sections=None, 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
@@ -2191,18 +2191,23 @@
:param get_mutable_section: A callable that returns a MutableSection
where changes are recorded.
"""
+ if sections is None:
+ sections = []
self.sections = sections
self.get_mutable_section = get_mutable_section
def get(self, name):
- """Return the value from the first definition found in the sections.
+ """Return the *first* option value 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
+ This is where we guarantee that sections coming from Store are loaded
+ lazily: 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).
"""
+ # Ensuring lazy loading is achieved by delaying section matching until
+ # it can be avoided anymore by using callables to describe (possibly
+ # empty) section lists.
for section_or_callable in self.sections:
# Each section can expand to multiple ones when a callable is used
if callable(section_or_callable):
=== modified file 'bzrlib/tests/test_config.py'
--- a/bzrlib/tests/test_config.py 2011-04-06 09:19:27 +0000
+++ b/bzrlib/tests/test_config.py 2011-04-06 09:38:41 +0000
@@ -2059,6 +2059,14 @@
conf_stack = config.ConfigStack([conf1, conf2])
self.assertEquals('baz', conf_stack.get('foo'))
+ def test_get_for_empty_stack(self):
+ conf_stack = config.ConfigStack()
+ self.assertEquals(None, conf_stack.get('foo'))
+
+ def test_get_for_empty_section_callable(self):
+ conf_stack = config.ConfigStack([lambda : []])
+ self.assertEquals(None, conf_stack.get('foo'))
+
class TestConfigStackSet(tests.TestCaseWithTransport):
More information about the bazaar-commits
mailing list