view tests/test-wireproto.py @ 26064:1b1ab6ff58c4

ui: capture push location on path instances Currently, we treat "default" and "default-push" as separate paths, even though they are the same logical entity but with different paths for different operations. Because they are the same entity and because we will eventually be implementing an official mechanism for declaring push URLs for paths, we establish a "pushloc" attribute on path instances. We populate this attribute on the "default" path with the "default-push" value, if present. This will enable consumers stop referencing "default-push" which will make their code simpler.
author Gregory Szorc <gregory.szorc@gmail.com>
date Fri, 07 Aug 2015 21:53:34 -0700
parents cbbdd085c991
children 5defcb7d6e5f
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 _capabilities(self):
        return ['batch']

    def _call(self, cmd, **args):
        return wireproto.dispatch(self.serverrepo, proto(args), cmd)

    @wireproto.batchable
    def greet(self, name):
        f = wireproto.future()
        yield {'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]