view tests/test-wireproto.py @ 20858:bc56ec9e64df stable

hg: introduce "wirepeersetupfuncs" to setup wire peer by extensions (issue4109) Since changeset 6f72e7d28b35, "reposetup()" of each extensions is invoked only on repositories enabling corresponded extensions. This causes that largefiles specific interactions between the repository enabling largefiles locally and remote (wire) peer fail, because there is no way to know whether largefiles is enabled on the remote repository behind the wire peer, and largefiles specific "wireproto functions" are not given to any wire peers. To avoid this problem, largefiles should be enabled in wider scope than each repositories (e.g. user-wide "${HOME}/.hgrc"). This patch introduces "wirepeersetupfuncs" to setup wire peer by extensions already enabled. Functions registered into "wirepeersetupfuncs" are invoked for all wire peers. This patch uses plain list instead of "util.hooks" for "wirepeersetupfuncs", because the former allows to control order of function invocation by order of extension enabling: it may be useful for workaround of problems with combination of enabled extensions
author FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
date Sat, 29 Mar 2014 01:20:07 +0900
parents 753acee7d6dd
children c69f62906358
line wrap: on
line source

from mercurial import wireproto

class proto(object):
    def __init__(self, args):
        self.args = args
    def getargs(self, spec):
        args = self.args
        args.setdefault('*', {})
        names = spec.split()
        return [args[n] for n in names]

class clientpeer(wireproto.wirepeer):
    def __init__(self, serverrepo):
        self.serverrepo = serverrepo
    def _call(self, cmd, **args):
        return wireproto.dispatch(self.serverrepo, proto(args), cmd)

    @wireproto.batchable
    def greet(self, name):
        f = wireproto.future()
        yield wireproto.todict(name=mangle(name)), f
        yield unmangle(f.value)

class serverrepo(object):
    def greet(self, name):
        return "Hello, " + name

    def filtered(self, name):
        return self

def mangle(s):
    return ''.join(chr(ord(c) + 1) for c in s)
def unmangle(s):
    return ''.join(chr(ord(c) - 1) for c in s)

def greet(repo, proto, name):
    return mangle(repo.greet(unmangle(name)))

wireproto.commands['greet'] = (greet, 'name',)

srv = serverrepo()
clt = clientpeer(srv)

print clt.greet("Foobar")
b = clt.batch()
fs = [b.greet(s) for s in ["Fo, =;o", "Bar"]]
b.submit()
print [f.value for f in fs]