wireproto: port protocol handler to zope.interface
authorGregory Szorc <gregory.szorc@gmail.com>
Fri, 23 Mar 2018 16:24:53 -0700
changeset 37296 78103e4138b1
parent 37295 45b39c69fae0
child 37297 97eedbd5a56c
wireproto: port protocol handler to zope.interface zope.interface is superior to the abc module. Let's port to it. As part of this, we add tests for interface conformance for classes implementing the interface. Differential Revision: https://phab.mercurial-scm.org/D2983
mercurial/wireprotoserver.py
mercurial/wireprototypes.py
tests/test-check-interfaces.py
--- a/mercurial/wireprotoserver.py	Wed Mar 28 10:40:41 2018 -0700
+++ b/mercurial/wireprotoserver.py	Fri Mar 23 16:24:53 2018 -0700
@@ -12,6 +12,9 @@
 import threading
 
 from .i18n import _
+from .thirdparty.zope import (
+    interface as zi,
+)
 from . import (
     encoding,
     error,
@@ -58,7 +61,8 @@
 
     return ''.join(chunks)
 
-class httpv1protocolhandler(wireprototypes.baseprotocolhandler):
+@zi.implementer(wireprototypes.baseprotocolhandler)
+class httpv1protocolhandler(object):
     def __init__(self, req, ui, checkperm):
         self._req = req
         self._ui = ui
@@ -574,7 +578,8 @@
     },
 }
 
-class httpv2protocolhandler(wireprototypes.baseprotocolhandler):
+@zi.implementer(wireprototypes.baseprotocolhandler)
+class httpv2protocolhandler(object):
     def __init__(self, req, ui, args=None):
         self._req = req
         self._ui = ui
@@ -737,7 +742,8 @@
     fout.write(b'\n')
     fout.flush()
 
-class sshv1protocolhandler(wireprototypes.baseprotocolhandler):
+@zi.implementer(wireprototypes.baseprotocolhandler)
+class sshv1protocolhandler(object):
     """Handler for requests services via version 1 of SSH protocol."""
     def __init__(self, ui, fin, fout):
         self._ui = ui
--- a/mercurial/wireprototypes.py	Wed Mar 28 10:40:41 2018 -0700
+++ b/mercurial/wireprototypes.py	Fri Mar 23 16:24:53 2018 -0700
@@ -5,7 +5,9 @@
 
 from __future__ import absolute_import
 
-import abc
+from .thirdparty.zope import (
+    interface as zi,
+)
 
 # Names of the SSH protocol implementations.
 SSHV1 = 'ssh-v1'
@@ -95,7 +97,7 @@
     def __init__(self, gen=None):
         self.gen = gen
 
-class baseprotocolhandler(object):
+class baseprotocolhandler(zi.Interface):
     """Abstract base class for wire protocol handlers.
 
     A wire protocol handler serves as an interface between protocol command
@@ -104,30 +106,24 @@
     the request, handle response types, etc.
     """
 
-    __metaclass__ = abc.ABCMeta
-
-    @abc.abstractproperty
-    def name(self):
+    name = zi.Attribute(
         """The name of the protocol implementation.
 
         Used for uniquely identifying the transport type.
-        """
+        """)
 
-    @abc.abstractmethod
-    def getargs(self, args):
+    def getargs(args):
         """return the value for arguments in <args>
 
         returns a list of values (same order as <args>)"""
 
-    @abc.abstractmethod
-    def forwardpayload(self, fp):
+    def forwardpayload(fp):
         """Read the raw payload and forward to a file.
 
         The payload is read in full before the function returns.
         """
 
-    @abc.abstractmethod
-    def mayberedirectstdio(self):
+    def mayberedirectstdio():
         """Context manager to possibly redirect stdio.
 
         The context manager yields a file-object like object that receives
@@ -140,12 +136,10 @@
         won't be captured.
         """
 
-    @abc.abstractmethod
-    def client(self):
+    def client():
         """Returns a string representation of this client (as bytes)."""
 
-    @abc.abstractmethod
-    def addcapabilities(self, repo, caps):
+    def addcapabilities(repo, caps):
         """Adds advertised capabilities specific to this protocol.
 
         Receives the list of capabilities collected so far.
@@ -153,8 +147,7 @@
         Returns a list of capabilities. The passed in argument can be returned.
         """
 
-    @abc.abstractmethod
-    def checkperm(self, perm):
+    def checkperm(perm):
         """Validate that the client has permissions to perform a request.
 
         The argument is the permission required to proceed. If the client
--- a/tests/test-check-interfaces.py	Wed Mar 28 10:40:41 2018 -0700
+++ b/tests/test-check-interfaces.py	Fri Mar 23 16:24:53 2018 -0700
@@ -19,6 +19,8 @@
     statichttprepo,
     ui as uimod,
     unionrepo,
+    wireprotoserver,
+    wireprototypes,
 )
 
 rootdir = os.path.normpath(os.path.join(os.path.dirname(__file__), '..'))
@@ -122,4 +124,23 @@
     repo = localrepo.localrepository(ui, rootdir)
     checkzobject(repo)
 
+    ziverify.verifyClass(wireprototypes.baseprotocolhandler,
+                         wireprotoserver.sshv1protocolhandler)
+    ziverify.verifyClass(wireprototypes.baseprotocolhandler,
+                         wireprotoserver.sshv2protocolhandler)
+    ziverify.verifyClass(wireprototypes.baseprotocolhandler,
+                         wireprotoserver.httpv1protocolhandler)
+    ziverify.verifyClass(wireprototypes.baseprotocolhandler,
+                         wireprotoserver.httpv2protocolhandler)
+
+    sshv1 = wireprotoserver.sshv1protocolhandler(None, None, None)
+    checkzobject(sshv1)
+    sshv2 = wireprotoserver.sshv2protocolhandler(None, None, None)
+    checkzobject(sshv2)
+
+    httpv1 = wireprotoserver.httpv1protocolhandler(None, None, None)
+    checkzobject(httpv1)
+    httpv2 = wireprotoserver.httpv2protocolhandler(None, None)
+    checkzobject(httpv2)
+
 main()