D2983: wireproto: port protocol handler to zope.interface

indygreg (Gregory Szorc) phabricator at mercurial-scm.org
Fri Mar 30 21:16:18 UTC 2018


indygreg created this revision.
Herald added a subscriber: mercurial-devel.
Herald added a reviewer: hg-reviewers.

REVISION SUMMARY
  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.

REPOSITORY
  rHG Mercurial

REVISION DETAIL
  https://phab.mercurial-scm.org/D2983

AFFECTED FILES
  mercurial/wireprotoserver.py
  mercurial/wireprototypes.py
  tests/test-check-interfaces.py

CHANGE DETAILS

diff --git a/tests/test-check-interfaces.py b/tests/test-check-interfaces.py
--- a/tests/test-check-interfaces.py
+++ b/tests/test-check-interfaces.py
@@ -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()
diff --git a/mercurial/wireprototypes.py b/mercurial/wireprototypes.py
--- a/mercurial/wireprototypes.py
+++ b/mercurial/wireprototypes.py
@@ -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,39 +97,33 @@
     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
     handlers and the wire protocol transport layer. Protocol handlers provide
     methods to read command arguments, redirect stdio for the duration of
     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,21 +136,18 @@
         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.
 
         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
diff --git a/mercurial/wireprotoserver.py b/mercurial/wireprotoserver.py
--- a/mercurial/wireprotoserver.py
+++ b/mercurial/wireprotoserver.py
@@ -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):
+ at 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):
+ at 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):
+ at 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



To: indygreg, #hg-reviewers
Cc: mercurial-devel


More information about the Mercurial-devel mailing list