D3257: wireproto: move value encoding functions to wireprototypes (API)
This revision was automatically updated to reflect the committed changes. Closed by commit rHG5e71dea79aae: wireproto: move value encoding functions to wireprototypes (API) (authored by indygreg, committed by ). REPOSITORY rHG Mercurial CHANGES SINCE LAST UPDATE https://phab.mercurial-scm.org/D3257?vs=8023=8098 REVISION DETAIL https://phab.mercurial-scm.org/D3257 AFFECTED FILES hgext/infinitepush/__init__.py mercurial/wireproto.py mercurial/wireprototypes.py CHANGE DETAILS diff --git a/mercurial/wireprototypes.py b/mercurial/wireprototypes.py --- a/mercurial/wireprototypes.py +++ b/mercurial/wireprototypes.py @@ -5,6 +5,10 @@ from __future__ import absolute_import +from .node import ( +bin, +hex, +) from .thirdparty.zope import ( interface as zi, ) @@ -102,6 +106,34 @@ def __init__(self, v): self.value = v +# list of nodes encoding / decoding +def decodelist(l, sep=' '): +if l: +return [bin(v) for v in l.split(sep)] +return [] + +def encodelist(l, sep=' '): +try: +return sep.join(map(hex, l)) +except TypeError: +raise + +# batched call argument encoding + +def escapebatcharg(plain): +return (plain +.replace(':', ':c') +.replace(',', ':o') +.replace(';', ':s') +.replace('=', ':e')) + +def unescapebatcharg(escaped): +return (escaped +.replace(':e', '=') +.replace(':s', ';') +.replace(':o', ',') +.replace(':c', ':')) + class baseprotocolhandler(zi.Interface): """Abstract base class for wire protocol handlers. diff --git a/mercurial/wireproto.py b/mercurial/wireproto.py --- a/mercurial/wireproto.py +++ b/mercurial/wireproto.py @@ -115,37 +115,11 @@ batchable = peer.batchable future = peer.future -# list of nodes encoding / decoding - -def decodelist(l, sep=' '): -if l: -return [bin(v) for v in l.split(sep)] -return [] - -def encodelist(l, sep=' '): -try: -return sep.join(map(hex, l)) -except TypeError: -raise - -# batched call argument encoding - -def escapearg(plain): -return (plain -.replace(':', ':c') -.replace(',', ':o') -.replace(';', ':s') -.replace('=', ':e')) - -def unescapearg(escaped): -return (escaped -.replace(':e', '=') -.replace(':s', ';') -.replace(':o', ',') -.replace(':c', ':')) def encodebatchcmds(req): """Return a ``cmds`` argument value for the ``batch`` command.""" +escapearg = wireprototypes.escapebatcharg + cmds = [] for op, argsdict in req: # Old servers didn't properly unescape argument names. So prevent @@ -227,14 +201,14 @@ yield {}, f d = f.value try: -yield decodelist(d[:-1]) +yield wireprototypes.decodelist(d[:-1]) except ValueError: self._abort(error.ResponseError(_("unexpected response:"), d)) @batchable def known(self, nodes): f = future() -yield {'nodes': encodelist(nodes)}, f +yield {'nodes': wireprototypes.encodelist(nodes)}, f d = f.value try: yield [bool(int(b)) for b in d] @@ -251,7 +225,7 @@ for branchpart in d.splitlines(): branchname, branchheads = branchpart.split(' ', 1) branchname = encoding.tolocal(urlreq.unquote(branchname)) -branchheads = decodelist(branchheads) +branchheads = wireprototypes.decodelist(branchheads) branchmap[branchname] = branchheads yield branchmap except TypeError: @@ -306,7 +280,7 @@ raise error.ProgrammingError( 'Unexpectedly None keytype for key %s' % key) elif keytype == 'nodes': -value = encodelist(value) +value = wireprototypes.encodelist(value) elif keytype == 'csv': value = ','.join(value) elif keytype == 'scsv': @@ -338,10 +312,10 @@ ''' if heads != ['force'] and self.capable('unbundlehash'): -heads = encodelist(['hashed', -hashlib.sha1(''.join(sorted(heads))).digest()]) +heads = wireprototypes.encodelist( +['hashed', hashlib.sha1(''.join(sorted(heads))).digest()]) else: -heads = encodelist(heads) +heads = wireprototypes.encodelist(heads) if util.safehasattr(cg, 'deltaheader'): # this a bundle10, do the old style call sequence @@ -368,35 +342,37 @@ # Begin of ipeerlegacycommands interface. def branches(self, nodes): -n = encodelist(nodes) +n = wireprototypes.encodelist(nodes) d = self._call("branches", nodes=n) try: -br = [tuple(decodelist(b)) for b in d.splitlines()] +
D3257: wireproto: move value encoding functions to wireprototypes (API)
indygreg created this revision. Herald added a subscriber: mercurial-devel. Herald added a reviewer: hg-reviewers. REVISION SUMMARY These functions should live in the same place. I plan to separate client from server code in upcoming commits. wireprototypes is where we are putting shared code like this. REPOSITORY rHG Mercurial REVISION DETAIL https://phab.mercurial-scm.org/D3257 AFFECTED FILES hgext/infinitepush/__init__.py mercurial/wireproto.py mercurial/wireprototypes.py CHANGE DETAILS diff --git a/mercurial/wireprototypes.py b/mercurial/wireprototypes.py --- a/mercurial/wireprototypes.py +++ b/mercurial/wireprototypes.py @@ -5,6 +5,10 @@ from __future__ import absolute_import +from .node import ( +bin, +hex, +) from .thirdparty.zope import ( interface as zi, ) @@ -102,6 +106,34 @@ def __init__(self, v): self.value = v +# list of nodes encoding / decoding +def decodelist(l, sep=' '): +if l: +return [bin(v) for v in l.split(sep)] +return [] + +def encodelist(l, sep=' '): +try: +return sep.join(map(hex, l)) +except TypeError: +raise + +# batched call argument encoding + +def escapebatcharg(plain): +return (plain +.replace(':', ':c') +.replace(',', ':o') +.replace(';', ':s') +.replace('=', ':e')) + +def unescapebatcharg(escaped): +return (escaped +.replace(':e', '=') +.replace(':s', ';') +.replace(':o', ',') +.replace(':c', ':')) + class baseprotocolhandler(zi.Interface): """Abstract base class for wire protocol handlers. diff --git a/mercurial/wireproto.py b/mercurial/wireproto.py --- a/mercurial/wireproto.py +++ b/mercurial/wireproto.py @@ -115,37 +115,11 @@ batchable = peer.batchable future = peer.future -# list of nodes encoding / decoding - -def decodelist(l, sep=' '): -if l: -return [bin(v) for v in l.split(sep)] -return [] - -def encodelist(l, sep=' '): -try: -return sep.join(map(hex, l)) -except TypeError: -raise - -# batched call argument encoding - -def escapearg(plain): -return (plain -.replace(':', ':c') -.replace(',', ':o') -.replace(';', ':s') -.replace('=', ':e')) - -def unescapearg(escaped): -return (escaped -.replace(':e', '=') -.replace(':s', ';') -.replace(':o', ',') -.replace(':c', ':')) def encodebatchcmds(req): """Return a ``cmds`` argument value for the ``batch`` command.""" +escapearg = wireprototypes.escapebatcharg + cmds = [] for op, argsdict in req: # Old servers didn't properly unescape argument names. So prevent @@ -227,14 +201,14 @@ yield {}, f d = f.value try: -yield decodelist(d[:-1]) +yield wireprototypes.decodelist(d[:-1]) except ValueError: self._abort(error.ResponseError(_("unexpected response:"), d)) @batchable def known(self, nodes): f = future() -yield {'nodes': encodelist(nodes)}, f +yield {'nodes': wireprototypes.encodelist(nodes)}, f d = f.value try: yield [bool(int(b)) for b in d] @@ -251,7 +225,7 @@ for branchpart in d.splitlines(): branchname, branchheads = branchpart.split(' ', 1) branchname = encoding.tolocal(urlreq.unquote(branchname)) -branchheads = decodelist(branchheads) +branchheads = wireprototypes.decodelist(branchheads) branchmap[branchname] = branchheads yield branchmap except TypeError: @@ -306,7 +280,7 @@ raise error.ProgrammingError( 'Unexpectedly None keytype for key %s' % key) elif keytype == 'nodes': -value = encodelist(value) +value = wireprototypes.encodelist(value) elif keytype == 'csv': value = ','.join(value) elif keytype == 'scsv': @@ -338,10 +312,10 @@ ''' if heads != ['force'] and self.capable('unbundlehash'): -heads = encodelist(['hashed', -hashlib.sha1(''.join(sorted(heads))).digest()]) +heads = wireprototypes.encodelist( +['hashed', hashlib.sha1(''.join(sorted(heads))).digest()]) else: -heads = encodelist(heads) +heads = wireprototypes.encodelist(heads) if util.safehasattr(cg, 'deltaheader'): # this a bundle10, do the old style call sequence @@ -368,35 +342,37 @@ # Begin of ipeerlegacycommands interface. def branches(self, nodes): -n = encodelist(nodes) +n = wireprototypes.encodelist(nodes) d = self._call("branches", nodes=n) try: -br = [tuple(decodelist(b)) for b in