changeset 26395:4e7b0bf9f0b1

unbundle20: allow registering handlers for stream level parameters As a comment in the code have been asking for, it is now possible to register piece of code that handle parameters for the stream. I've been wondering is such function should be class methods or not. I eventually went for externally decorated methods to stick with the culture of extensibility from an extensions that apply to bundle2.
author Pierre-Yves David <pierre-yves.david@fb.com>
date Wed, 23 Sep 2015 14:00:16 -0700
parents e75da738add5
children d90c30801cdf
files mercurial/bundle2.py
diffstat 1 files changed, 18 insertions(+), 6 deletions(-) [+]
line wrap: on
line diff
--- a/mercurial/bundle2.py	Wed Sep 23 11:55:27 2015 -0700
+++ b/mercurial/bundle2.py	Wed Sep 23 14:00:16 2015 -0700
@@ -661,13 +661,15 @@
             raise ValueError('empty parameter name')
         if name[0] not in string.letters:
             raise ValueError('non letter first character: %r' % name)
-        # Some logic will be later added here to try to process the option for
-        # a dict of known parameter.
-        if name[0].islower():
-            indebug(self.ui, "ignoring unknown parameter %r" % name)
+        try:
+            handler = b2streamparamsmap[name.lower()]
+        except KeyError:
+            if name[0].islower():
+                indebug(self.ui, "ignoring unknown parameter %r" % name)
+            else:
+                raise error.BundleUnknownFeatureError(params=(name,))
         else:
-            raise error.BundleUnknownFeatureError(params=(name,))
-
+            handler(self, name, value)
 
     def iterparts(self):
         """yield all parts contained in the stream"""
@@ -700,6 +702,16 @@
 
 formatmap = {'20': unbundle20}
 
+b2streamparamsmap = {}
+
+def b2streamparamhandler(name):
+    """register a handler for a stream level parameter"""
+    def decorator(func):
+        assert name not in formatmap
+        b2streamparamsmap[name] = func
+        return func
+    return decorator
+
 class bundlepart(object):
     """A bundle2 part contains application level payload