Patchwork [4,of,6] bundle2: introduce a `parthandler` decorator

login
register
mail settings
Submitter Pierre-Yves David
Date April 1, 2014, 7:35 p.m.
Message ID <8b74b0f5ff191eb97967.1396380925@marginatus.alto.octopoid.net>
Download mbox | patch
Permalink /patch/4181/
State Accepted
Commit ec7fc110faee7302b8af69c5fc584c9bdf057f0f
Headers show

Comments

Pierre-Yves David - April 1, 2014, 7:35 p.m.
# HG changeset patch
# User Pierre-Yves David <pierre-yves.david@fb.com>
# Date 1395701460 25200
#      Mon Mar 24 15:51:00 2014 -0700
# Node ID 8b74b0f5ff191eb9796706e089b6c14a30c05d40
# Parent  a624e622d98db1b9575db11b73bdd803da02ef6b
bundle2: introduce a `parthandler` decorator

Simple syntax sugar to register an handler for a new part type.

Patch

diff --git a/mercurial/bundle2.py b/mercurial/bundle2.py
--- a/mercurial/bundle2.py
+++ b/mercurial/bundle2.py
@@ -146,12 +146,27 @@  def _makefpartparamsizes(nbparams):
     The number parameters is variable so we need to build that format
     dynamically.
     """
     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
 
     Currently it:
diff --git a/tests/test-bundle2.t b/tests/test-bundle2.t
--- a/tests/test-bundle2.t
+++ b/tests/test-bundle2.t
@@ -18,18 +18,17 @@  Create an extension to test bundle2 API
   > ELEPHANTSSONG = """Patali Dirapata, Cromda Cromda Ripalo, Pata Pata, Ko Ko Ko
   > Bokoro Dipoulito, Rondi Rondi Pepino, Pata Pata, Ko Ko Ko
   > 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'),],
   >          '[OUTPUTFILE]')
   > def cmdbundle2(ui, repo, path=None, **opts):