Rev 5793: We don't need (nor want) to tie the config hooks to a particular class. Especially when we want to use the same hooks on both implementations. in file:///home/vila/src/bzr/experimental/config/

Vincent Ladeuil v.ladeuil+lp at free.fr
Fri Jun 10 13:14:34 UTC 2011


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

------------------------------------------------------------
revno: 5793
revision-id: v.ladeuil+lp at free.fr-20110610131434-5mlt2npfpvzmn65l
parent: v.ladeuil+lp at free.fr-20110608194923-pg1lny08jhxztqtm
committer: Vincent Ladeuil <v.ladeuil+lp at free.fr>
branch nick: config-hooks
timestamp: Fri 2011-06-10 15:14:34 +0200
message:
  We don't need (nor want) to tie the config hooks to a particular class. Especially when we want to use the same hooks on both implementations.
-------------- next part --------------
=== modified file 'bzrlib/config.py'
--- a/bzrlib/config.py	2011-06-08 19:25:41 +0000
+++ b/bzrlib/config.py	2011-06-10 13:14:34 +0000
@@ -555,6 +555,41 @@
         return command_line
 
 
+class _ConfigHooks(hooks.Hooks):
+    """A dict mapping hook names and a list of callables for configs.
+    """
+
+    def __init__(self):
+        """Create the default hooks.
+
+        These are all empty initially, because by default nothing should get
+        notified.
+        """
+        super(_ConfigHooks, self).__init__('bzrlib.config', 'ConfigHooks')
+        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))
+        # The hooks for config options
+        self.add_hook('get',
+                      'Invoked when a config option is read.'
+                      ' The signature is (stack, name, value).',
+                      (2, 4))
+        self.add_hook('set',
+                      'Invoked when a config option is set.'
+                      ' The signature is (stack, name, value).',
+                      (2, 4))
+        self.add_hook('remove',
+                      'Invoked when a config option is removed.'
+                      ' The signature is (stack, name).',
+                      (2, 4))
+ConfigHooks = _ConfigHooks()
+
+
 class IniBasedConfig(Config):
     """A configuration policy that draws from ini files."""
 
@@ -2272,7 +2307,7 @@
             return
         content = self.transport.get_bytes(self.file_name)
         self._load_from_string(content)
-        for hook in Store.hooks['load']:
+        for hook in ConfigHooks['load']:
             hook(self)
 
     def _load_from_string(self, str_or_unicode):
@@ -2301,7 +2336,7 @@
         out = StringIO()
         self._config_obj.write(out)
         self.transport.put_bytes(self.file_name, out.getvalue())
-        for hook in Store.hooks['save']:
+        for hook in ConfigHooks['save']:
             hook(self)
 
     def external_url(self):
@@ -2342,28 +2377,6 @@
             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
@@ -2614,7 +2627,7 @@
                 opt = None
             if opt is not None:
                 value = opt.get_default()
-        for hook in Stack.hooks['get']:
+        for hook in ConfigHooks['get']:
             hook(self, name, value)
         return value
 
@@ -2633,14 +2646,14 @@
         """Set a new value for the option."""
         section = self._get_mutable_section()
         section.set(name, value)
-        for hook in Stack.hooks['set']:
+        for hook in ConfigHooks['set']:
             hook(self, name, value)
 
     def remove(self, name):
         """Remove an existing option."""
         section = self._get_mutable_section()
         section.remove(name)
-        for hook in Stack.hooks['remove']:
+        for hook in ConfigHooks['remove']:
             hook(self, name)
 
     def __repr__(self):
@@ -2648,33 +2661,6 @@
         return "<config.%s(%s)>" % (self.__class__.__name__, id(self))
 
 
-class StackHooks(hooks.Hooks):
-    """A dict mapping hook names and a list of callables for config stacks.
-    """
-
-    def __init__(self):
-        """Create the default hooks.
-
-        These are all empty initially, because by default nothing should get
-        notified.
-        """
-        super(StackHooks, self).__init__('bzrlib.config', 'Stack.hooks')
-        self.add_hook('get',
-                      'Invoked when a config option is read.'
-                      ' The signature is (stack, name, value).',
-                      (2, 4))
-        self.add_hook('set',
-                      'Invoked when a config option is set.'
-                      ' The signature is (stack, name, value).',
-                      (2, 4))
-        self.add_hook('remove',
-                      '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()
-
-
 class _CompatibleStack(Stack):
     """Place holder for compatibility with previous design.
 

=== modified file 'bzrlib/hooks.py'
--- a/bzrlib/hooks.py	2011-06-08 17:17:21 +0000
+++ b/bzrlib/hooks.py	2011-06-10 13:14:34 +0000
@@ -72,8 +72,7 @@
     ('bzrlib.branch', 'Branch.hooks', 'BranchHooks'),
     ('bzrlib.bzrdir', 'BzrDir.hooks', 'BzrDirHooks'),
     ('bzrlib.commands', 'Command.hooks', 'CommandHooks'),
-    ('bzrlib.config', 'Stack.hooks', 'StackHooks'),
-    ('bzrlib.config', 'Store.hooks', 'StoreHooks'),
+    ('bzrlib.config', 'ConfigHooks', '_ConfigHooks'),
     ('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 19:25:41 +0000
+++ b/bzrlib/tests/test_config.py	2011-06-10 13:14:34 +0000
@@ -2177,7 +2177,7 @@
         calls = []
         def hook(*args):
             calls.append(args)
-        config.Store.hooks.install_named_hook('load', hook, None)
+        config.ConfigHooks.install_named_hook('load', hook, None)
         self.assertLength(0, calls)
         store = self.get_store(self)
         store.load()
@@ -2188,7 +2188,7 @@
         calls = []
         def hook(*args):
             calls.append(args)
-        config.Store.hooks.install_named_hook('save', hook, None)
+        config.ConfigHooks.install_named_hook('save', hook, None)
         self.assertLength(0, calls)
         store = self.get_store(self)
         section = store.get_mutable_section('baz')
@@ -2557,7 +2557,7 @@
         calls = []
         def hook(*args):
             calls.append(args)
-        config.Stack.hooks.install_named_hook('get', hook, None)
+        config.ConfigHooks.install_named_hook('get', hook, None)
         self.assertLength(0, calls)
         conf = self.get_stack(self)
         conf.store._load_from_string('foo=bar')
@@ -2586,7 +2586,7 @@
         calls = []
         def hook(*args):
             calls.append(args)
-        config.Stack.hooks.install_named_hook('set', hook, None)
+        config.ConfigHooks.install_named_hook('set', hook, None)
         self.assertLength(0, calls)
         conf = self.get_stack(self)
         conf.set('foo', 'bar')
@@ -2612,7 +2612,7 @@
         calls = []
         def hook(*args):
             calls.append(args)
-        config.Stack.hooks.install_named_hook('remove', hook, None)
+        config.ConfigHooks.install_named_hook('remove', hook, None)
         self.assertLength(0, calls)
         conf = self.get_stack(self)
         conf.store._load_from_string('foo=bar')



More information about the bazaar-commits mailing list