Rev 2248: Add install_hook to the BranchHooks class as the official means for installing a hook. in file:///home/robertc/source/baz/in-review/branch-hook/

Robert Collins robertc at robertcollins.net
Tue Jan 30 11:52:33 GMT 2007


------------------------------------------------------------
revno: 2248
revision-id: robertc at robertcollins.net-20070130115230-2052dzn5xo9h6o07
parent: robertc at robertcollins.net-20070130104104-wsikhrgretspg86m
committer: Robert Collins <robertc at robertcollins.net>
branch nick: branch-hook
timestamp: Tue 2007-01-30 22:52:30 +1100
message:
  Add install_hook to the BranchHooks class as the official means for installing a hook.
modified:
  NEWS                           NEWS-20050323055033-4e00b5db738777ff
  bzrlib/branch.py               branch.py-20050309040759-e4baf4e0d046576e
  bzrlib/errors.py               errors.py-20050309040759-20512168c4e14fbd
  bzrlib/tests/test_branch.py    test_branch.py-20060116013032-97819aa07b8ab3b5
  bzrlib/tests/test_errors.py    test_errors.py-20060210110251-41aba2deddf936a8
=== modified file 'NEWS'
--- a/NEWS	2007-01-29 16:58:49 +0000
+++ b/NEWS	2007-01-30 11:52:30 +0000
@@ -28,8 +28,7 @@
     * New Branch hooks facility, with one initial hook 'set_rh' which triggers
       whenever the revision history is set. This allows triggering on e.g.
       push, pull, commit, and so on. Developed for use with the branchrss
-      plugin. See bzrlib/tests/branch_implementations/test_hooks for more
-      details. (Robert Collins)
+      plugin. See bzrlib.branch.BranchHooks for more details. (Robert Collins)
 
   BUGFIXES:
 

=== modified file 'bzrlib/branch.py'
--- a/bzrlib/branch.py	2007-01-30 10:41:04 +0000
+++ b/bzrlib/branch.py	2007-01-30 11:52:30 +0000
@@ -631,30 +631,6 @@
         return checkout.create_workingtree(revision_id)
 
 
-class BranchHooks(dict):
-    """A dictionary mapping hook name to a list of callables for branch hooks.
-    
-    e.g. ['set_rh'] Is the list of items to be called when the
-    set_revision_history function is invoked.
-    """
-
-    def __init__(self):
-        """Create the default hooks.
-
-        These are all empty initially, because by default nothing should get
-        notified.
-        """
-        dict.__init__(self)
-        # invoked whenever the revision history has been set
-        # with set_revision_history. The api signature is
-        # (branch, revision_history), and the branch will
-        # be write-locked.
-        self['set_rh'] = []
-
-# install the default hooks into the class.
-Branch.hooks = BranchHooks()
-
-
 class BranchFormat(object):
     """An encapsulation of the initialization and open routines for a format.
 
@@ -742,6 +718,45 @@
         return self.get_format_string().rstrip()
 
 
+class BranchHooks(dict):
+    """A dictionary mapping hook name to a list of callables for branch hooks.
+    
+    e.g. ['set_rh'] Is the list of items to be called when the
+    set_revision_history function is invoked.
+    """
+
+    def __init__(self):
+        """Create the default hooks.
+
+        These are all empty initially, because by default nothing should get
+        notified.
+        """
+        dict.__init__(self)
+        # invoked whenever the revision history has been set
+        # with set_revision_history. The api signature is
+        # (branch, revision_history), and the branch will
+        # be write-locked. Introduced in 0.15.
+        self['set_rh'] = []
+
+    def install_hook(self, hook_name, a_callable):
+        """Install a_callable in to the hook hook_name.
+
+        :param hook_name: A hook name. See the __init__ method of BranchHooks
+            for the complete list of hooks.
+        :param a_callable: The callable to be invoked when the hook triggers.
+            The exact signature will depend on the hook - see the __init__ 
+            method of BranchHooks for details on each hook.
+        """
+        try:
+            self[hook_name].append(a_callable)
+        except KeyError:
+            raise errors.UnknownHook('branch', hook_name)
+
+
+# install the default hooks into the Branch class.
+Branch.hooks = BranchHooks()
+
+
 class BzrBranchFormat4(BranchFormat):
     """Bzr branch format 4.
 

=== modified file 'bzrlib/errors.py'
--- a/bzrlib/errors.py	2007-01-19 19:23:53 +0000
+++ b/bzrlib/errors.py	2007-01-30 11:52:30 +0000
@@ -405,6 +405,16 @@
         self.args = [base] + list(args)
 
 
+class UnknownHook(BzrError):
+
+    _fmt = "The %(type)s hook '%(hook)s' is unknown in this version of bzrlib."
+
+    def __init__(self, hook_type, hook_name):
+        BzrError.__init__(self)
+        self.type = hook_type
+        self.hook = hook_name
+
+
 class UnsupportedProtocol(PathError):
 
     _fmt = 'Unsupported protocol for url "%(path)s"%(extra)s'

=== modified file 'bzrlib/tests/test_branch.py'
--- a/bzrlib/tests/test_branch.py	2007-01-30 10:41:04 +0000
+++ b/bzrlib/tests/test_branch.py	2007-01-30 11:52:30 +0000
@@ -32,6 +32,7 @@
                            BzrDir, BzrDirFormat)
 from bzrlib.errors import (NotBranchError,
                            UnknownFormatError,
+                           UnknownHook,
                            UnsupportedFormatError,
                            )
 
@@ -175,3 +176,14 @@
         """The installed hooks object should be a BranchHooks."""
         # the installed hooks are saved in self._preserved_hooks.
         self.assertIsInstance(self._preserved_hooks, bzrlib.branch.BranchHooks)
+
+    def test_install_hook_raises_unknown_hook(self):
+        """install_hook should raise UnknownHook if a hook is unknown."""
+        hooks = bzrlib.branch.BranchHooks()
+        self.assertRaises(UnknownHook, hooks.install_hook, 'silly', None)
+
+    def test_install_hook_appends_known_hook(self):
+        """install_hook should append the callable for known hooks."""
+        hooks = bzrlib.branch.BranchHooks()
+        hooks.install_hook('set_rh', None)
+        self.assertEqual(hooks['set_rh'], [None])

=== modified file 'bzrlib/tests/test_errors.py'
--- a/bzrlib/tests/test_errors.py	2006-12-21 20:07:34 +0000
+++ b/bzrlib/tests/test_errors.py	2007-01-30 11:52:30 +0000
@@ -94,6 +94,16 @@
             "the current request that is open.",
             str(error))
 
+    def test_unknown_hook(self):
+        error = errors.UnknownHook("branch", "foo")
+        self.assertEqualDiff("The branch hook 'foo' is unknown in this version"
+            " of bzrlib.",
+            str(error))
+        error = errors.UnknownHook("tree", "bar")
+        self.assertEqualDiff("The tree hook 'bar' is unknown in this version"
+            " of bzrlib.",
+            str(error))
+
     def test_up_to_date(self):
         error = errors.UpToDateFormat(bzrdir.BzrDirFormat4())
         self.assertEqualDiff("The branch format Bazaar-NG branch, "



More information about the bazaar-commits mailing list