Rev 5790: Add hooks for config stores (but the load one is not in the right place). in file:///home/vila/src/bzr/experimental/config/

Vincent Ladeuil v.ladeuil+lp at free.fr
Wed Jun 8 17:17:21 UTC 2011


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

------------------------------------------------------------
revno: 5790
revision-id: v.ladeuil+lp at free.fr-20110608171721-iwym6ng0rgndxuue
parent: v.ladeuil+lp at free.fr-20110608162936-nglond8qgcsvyth0
committer: Vincent Ladeuil <v.ladeuil+lp at free.fr>
branch nick: config-test-stats
timestamp: Wed 2011-06-08 19:17:21 +0200
message:
  Add hooks for config stores (but the load one is not in the right place).
-------------- next part --------------
=== modified file 'bzrlib/config.py'
--- a/bzrlib/config.py	2011-06-08 16:29:36 +0000
+++ b/bzrlib/config.py	2011-06-08 17:17:21 +0000
@@ -2291,6 +2291,8 @@
         except configobj.ConfigObjError, e:
             self._config_obj = None
             raise errors.ParseConfigError(e.errors, self.external_url())
+        for hook in Store.hooks['load']:
+            hook(self)
 
     def save(self):
         if not self.is_loaded():
@@ -2299,6 +2301,8 @@
         out = StringIO()
         self._config_obj.write(out)
         self.transport.put_bytes(self.file_name, out.getvalue())
+        for hook in Store.hooks['save']:
+            hook(self)
 
     def external_url(self):
         # FIXME: external_url should really accepts an optional relpath
@@ -2338,6 +2342,28 @@
             section = self._config_obj.setdefault(section_name, {})
         return self.mutable_section_class(section_name, section)
 
+class StoreHooks(hooks.Hooks):
+    """A dict mapping hook names and a list of callables for config stores.
+    """
+
+    def __init__(self):
+        """Create the default hooks.
+
+        These are all empty initially, because by default nothing should get
+        notified.
+        """
+        super(StoreHooks, self).__init__('bzrlib.config', 'Store.hooks')
+        self.add_hook('load',
+                      'Invoked when a config store is loaded.'
+                      ' The signature is (store).',
+                      (2, 4))
+        self.add_hook('save',
+                      'Invoked when a config store is saved.'
+                      ' The signature is (store).',
+                      (2, 4))
+# install the default hooks into the Stack class.
+Store.hooks = StoreHooks()
+
 
 # Note that LockableConfigObjStore inherits from ConfigObjStore because we need
 # unlockable stores for use with objects that can already ensure the locking
@@ -2645,7 +2671,6 @@
                       'Invoked when a config option is removed.'
                       ' The signature is (stack, name).',
                       (2, 4))
-
 # install the default hooks into the Stack class.
 Stack.hooks = StackHooks()
 

=== modified file 'bzrlib/hooks.py'
--- a/bzrlib/hooks.py	2011-06-08 16:29:36 +0000
+++ b/bzrlib/hooks.py	2011-06-08 17:17:21 +0000
@@ -73,6 +73,7 @@
     ('bzrlib.bzrdir', 'BzrDir.hooks', 'BzrDirHooks'),
     ('bzrlib.commands', 'Command.hooks', 'CommandHooks'),
     ('bzrlib.config', 'Stack.hooks', 'StackHooks'),
+    ('bzrlib.config', 'Store.hooks', 'StoreHooks'),
     ('bzrlib.info', 'hooks', 'InfoHooks'),
     ('bzrlib.lock', 'Lock.hooks', 'LockHooks'),
     ('bzrlib.merge', 'Merger.hooks', 'MergeHooks'),

=== modified file 'bzrlib/tests/test_config.py'
--- a/bzrlib/tests/test_config.py	2011-06-08 16:29:36 +0000
+++ b/bzrlib/tests/test_config.py	2011-06-08 17:17:21 +0000
@@ -2083,6 +2083,17 @@
         store._load_from_string('foo=bar')
         self.assertRaises(AssertionError, store._load_from_string, 'bar=baz')
 
+    def test_load_hook(self):
+        calls = []
+        def hook(*args):
+            calls.append(args)
+        config.Store.hooks.install_named_hook('load', hook, None)
+        self.assertLength(0, calls)
+        store = self.get_store(self)
+        store._load_from_string('')
+        self.assertLength(1, calls)
+        self.assertEquals((store,), calls[0])
+
 
 class TestMutableStore(TestStore):
 
@@ -2167,6 +2178,19 @@
         self.assertLength(1, sections)
         self.assertSectionContent(('baz', {'foo': 'bar'}), sections[0])
 
+    def test_save_hook(self):
+        calls = []
+        def hook(*args):
+            calls.append(args)
+        config.Store.hooks.install_named_hook('save', hook, None)
+        self.assertLength(0, calls)
+        store = self.get_store(self)
+        section = store.get_mutable_section('baz')
+        section.set('foo', 'bar')
+        store.save()
+        self.assertLength(1, calls)
+        self.assertEquals((store,), calls[0])
+
 
 class TestIniFileStore(TestStore):
 



More information about the bazaar-commits mailing list