changeset 20890:ec7fc110faee

bundle2: introduce a `parthandler` decorator Simple syntax sugar to register an handler for a new part type.
author Pierre-Yves David <pierre-yves.david@fb.com>
date Mon, 24 Mar 2014 15:51:00 -0700
parents deed5edb72de
children 1c6cd23fc221
files mercurial/bundle2.py tests/test-bundle2.t
diffstat 2 files changed, 17 insertions(+), 3 deletions(-) [+]
line wrap: on
line diff
--- a/mercurial/bundle2.py	Mon Mar 24 12:25:33 2014 -0700
+++ b/mercurial/bundle2.py	Mon Mar 24 15:51:00 2014 -0700
@@ -148,8 +148,23 @@
     """
     return '>'+('BB'*nbparams)
 
+parthandlermapping = {}
 
-parthandlermapping = {}
+def parthandler(parttype):
+    """decorator that register a function as a bundle2 part handler
+
+    eg::
+
+        @parthandler('myparttype')
+        def myparttypehandler(...):
+            '''process a part of type "my part".'''
+            ...
+    """
+    def _decorator(func):
+        assert parttype not in parthandlermapping
+        parthandlermapping[parttype] = func
+        return func
+    return _decorator
 
 def processbundle(repo, stream):
     """This function process a bundle, apply effect to/from a repo
--- a/tests/test-bundle2.t	Mon Mar 24 12:25:33 2014 -0700
+++ b/tests/test-bundle2.t	Mon Mar 24 15:51:00 2014 -0700
@@ -20,14 +20,13 @@
   > Emana Karassoli, Loucra Loucra Ponponto, Pata Pata, Ko Ko Ko."""
   > assert len(ELEPHANTSSONG) == 178 # future test say 178 bytes, trust it.
   > 
+  > @bundle2.parthandler('test:song')
   > def songhandler(repo, part):
   >     """handle a "test:song" bundle2 part, printing the lyrics on stdin"""
   >     repo.ui.write('The choir start singing:\n')
   >     for line in part.data.split('\n'):
   >         repo.ui.write('    %s\n' % line)
   > 
-  > bundle2.parthandlermapping['test:song'] = songhandler
-  > 
   > @command('bundle2',
   >          [('', 'param', [], 'stream level parameter'),
   >           ('', 'parts', False, 'include some arbitrary parts to the bundle'),],