Rev 5776: Merge config-stack into config-concrete-stacks in file:///home/vila/src/bzr/experimental/config/

Vincent Ladeuil v.ladeuil+lp at free.fr
Tue May 3 16:27:06 UTC 2011


At file:///home/vila/src/bzr/experimental/config/

------------------------------------------------------------
revno: 5776 [merge]
revision-id: v.ladeuil+lp at free.fr-20110503162706-kkpdfdix35li5y04
parent: v.ladeuil+lp at free.fr-20110503150422-pyd7vs36ah55o5pg
parent: v.ladeuil+lp at free.fr-20110503162611-5s750clmwwyrpy2a
committer: Vincent Ladeuil <v.ladeuil+lp at free.fr>
branch nick: config-concrete-stacks
timestamp: Tue 2011-05-03 18:27:06 +0200
message:
  Merge config-stack into config-concrete-stacks
modified:
  bzrlib/config.py               config.py-20051011043216-070c74f4e9e338e8
  bzrlib/tests/test_config.py    testconfig.py-20051011041908-742d0c15d8d8c8eb
-------------- next part --------------
=== modified file 'bzrlib/config.py'
--- a/bzrlib/config.py	2011-05-03 15:04:22 +0000
+++ b/bzrlib/config.py	2011-05-03 16:27:06 +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:04:22 +0000
+++ b/bzrlib/tests/test_config.py	2011-05-03 16:27:06 +0000
@@ -2230,7 +2230,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 ?
@@ -2238,7 +2238,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')
 
@@ -2251,7 +2251,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 ?
@@ -2259,7 +2259,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