Rev 5780: Change the way Stacks are built: requires a Store and a mutable section name instead of a callable returning the mutable section. in file:///home/vila/src/bzr/experimental/config/
Vincent Ladeuil
v.ladeuil+lp at free.fr
Tue May 3 16:26:11 UTC 2011
At file:///home/vila/src/bzr/experimental/config/
------------------------------------------------------------
revno: 5780
revision-id: v.ladeuil+lp at free.fr-20110503162611-5s750clmwwyrpy2a
parent: v.ladeuil+lp at free.fr-20110503150359-a3j06zh4k3wzezc2
committer: Vincent Ladeuil <v.ladeuil+lp at free.fr>
branch nick: config-stack
timestamp: Tue 2011-05-03 18:26:11 +0200
message:
Change the way Stacks are built: requires a Store and a mutable section name instead of a callable returning the mutable section.
-------------- next part --------------
=== modified file 'bzrlib/config.py'
--- a/bzrlib/config.py 2011-05-03 15:03:59 +0000
+++ b/bzrlib/config.py 2011-05-03 16:26:11 +0000
@@ -2447,19 +2447,23 @@
class Stack(object):
"""A stack of configurations where an option can be defined"""
- def __init__(self, sections_def, mutable_section_def=None):
+ def __init__(self, sections_def, store=None, mutable_section_name=None):
"""Creates a stack of sections with an optional store for changes.
:param sections_def: A list of Section or callables that returns an
iterable of Section. This defines the Sections for the Stack and
can be called repeatedly if needed.
- :param mutable_section_def: A callable that returns a MutableSection
- where changes are recorded. This defines the MutableSection and can
- be called repeatedly.
+ :param store: The optional Store where modifications will be
+ recorded. If none is specified, no modifications can be done.
+
+ :param mutable_section_name: The name of the MutableSection where
+ changes are recorded. This requires the ``store`` parameter to be
+ specified.
"""
self.sections_def = sections_def
- self.mutable_section_def = mutable_section_def
+ self.store = store
+ self.mutable_section_name = mutable_section_name
def get(self, name):
"""Return the *first* option value found in the sections.
@@ -2488,24 +2492,25 @@
# No definition was found
return None
- def set(self, name, value):
- """Set a new value for the option.
+ def _get_mutable_section(self):
+ """Get the MutableSection for the Stack.
This is where we guarantee that the mutable section is lazily loaded:
- this means we won't load the corresponding store before setting a value.
+ this means we won't load the corresponding store before setting a value
+ or deleting an option. In practice the store will often be loaded but
+ this allows catching some programming errors.
"""
- section = self.mutable_section_def()
+ section = self.store.get_mutable_section(self.mutable_section_name)
+ return section
+
+ def set(self, name, value):
+ """Set a new value for the option."""
+ section = self._get_mutable_section()
section.set(name, value)
def remove(self, name):
- """Remove an existing option.
-
- This is where we guarantee that the mutable section is lazily loaded:
- this means we won't load the correspoding store before trying to delete
- an option. In practice the store will often be loaded but this allows
- catching some programming errors.
- """
- section = self.mutable_section_def()
+ """Remove an existing option."""
+ section = self._get_mutable_section()
section.remove(name)
def __repr__(self):
=== modified file 'bzrlib/tests/test_config.py'
--- a/bzrlib/tests/test_config.py 2011-05-03 15:03:59 +0000
+++ b/bzrlib/tests/test_config.py 2011-05-03 16:26:11 +0000
@@ -2223,7 +2223,7 @@
def test_simple_set(self):
store = config.IniFileStore(self.get_transport(), 'test.conf')
store._load_from_string('foo=bar')
- conf = config.Stack([store.get_sections], store.get_mutable_section)
+ conf = config.Stack([store.get_sections], store)
self.assertEquals('bar', conf.get('foo'))
conf.set('foo', 'baz')
# Did we get it back ?
@@ -2231,7 +2231,7 @@
def test_set_creates_a_new_section(self):
store = config.IniFileStore(self.get_transport(), 'test.conf')
- conf = config.Stack([store.get_sections], store.get_mutable_section)
+ conf = config.Stack([store.get_sections], store)
conf.set('foo', 'baz')
self.assertEquals, 'baz', conf.get('foo')
@@ -2244,7 +2244,7 @@
def test_remove_existing(self):
store = config.IniFileStore(self.get_transport(), 'test.conf')
store._load_from_string('foo=bar')
- conf = config.Stack([store.get_sections], store.get_mutable_section)
+ conf = config.Stack([store.get_sections], store)
self.assertEquals('bar', conf.get('foo'))
conf.remove('foo')
# Did we get it back ?
@@ -2252,7 +2252,7 @@
def test_remove_unknown(self):
store = config.IniFileStore(self.get_transport(), 'test.conf')
- conf = config.Stack([store.get_sections], store.get_mutable_section)
+ conf = config.Stack([store.get_sections], store)
self.assertRaises(KeyError, conf.remove, 'I_do_not_exist')
More information about the bazaar-commits
mailing list