Show More
@@ -1,59 +1,60 | |||||
1 | from __future__ import absolute_import, print_function |
|
1 | from __future__ import absolute_import, print_function | |
2 |
|
2 | |||
3 | import StringIO |
|
3 | import StringIO | |
4 |
|
4 | from mercurial import ( | ||
5 | from mercurial import wireproto |
|
5 | wireproto, | |
|
6 | ) | |||
6 |
|
7 | |||
7 | class proto(object): |
|
8 | class proto(object): | |
8 | def __init__(self, args): |
|
9 | def __init__(self, args): | |
9 | self.args = args |
|
10 | self.args = args | |
10 | def getargs(self, spec): |
|
11 | def getargs(self, spec): | |
11 | args = self.args |
|
12 | args = self.args | |
12 | args.setdefault('*', {}) |
|
13 | args.setdefault('*', {}) | |
13 | names = spec.split() |
|
14 | names = spec.split() | |
14 | return [args[n] for n in names] |
|
15 | return [args[n] for n in names] | |
15 |
|
16 | |||
16 | class clientpeer(wireproto.wirepeer): |
|
17 | class clientpeer(wireproto.wirepeer): | |
17 | def __init__(self, serverrepo): |
|
18 | def __init__(self, serverrepo): | |
18 | self.serverrepo = serverrepo |
|
19 | self.serverrepo = serverrepo | |
19 |
|
20 | |||
20 | def _capabilities(self): |
|
21 | def _capabilities(self): | |
21 | return ['batch'] |
|
22 | return ['batch'] | |
22 |
|
23 | |||
23 | def _call(self, cmd, **args): |
|
24 | def _call(self, cmd, **args): | |
24 | return wireproto.dispatch(self.serverrepo, proto(args), cmd) |
|
25 | return wireproto.dispatch(self.serverrepo, proto(args), cmd) | |
25 |
|
26 | |||
26 | def _callstream(self, cmd, **args): |
|
27 | def _callstream(self, cmd, **args): | |
27 | return StringIO.StringIO(self._call(cmd, **args)) |
|
28 | return StringIO.StringIO(self._call(cmd, **args)) | |
28 |
|
29 | |||
29 | @wireproto.batchable |
|
30 | @wireproto.batchable | |
30 | def greet(self, name): |
|
31 | def greet(self, name): | |
31 | f = wireproto.future() |
|
32 | f = wireproto.future() | |
32 | yield {'name': mangle(name)}, f |
|
33 | yield {'name': mangle(name)}, f | |
33 | yield unmangle(f.value) |
|
34 | yield unmangle(f.value) | |
34 |
|
35 | |||
35 | class serverrepo(object): |
|
36 | class serverrepo(object): | |
36 | def greet(self, name): |
|
37 | def greet(self, name): | |
37 | return "Hello, " + name |
|
38 | return "Hello, " + name | |
38 |
|
39 | |||
39 | def filtered(self, name): |
|
40 | def filtered(self, name): | |
40 | return self |
|
41 | return self | |
41 |
|
42 | |||
42 | def mangle(s): |
|
43 | def mangle(s): | |
43 | return ''.join(chr(ord(c) + 1) for c in s) |
|
44 | return ''.join(chr(ord(c) + 1) for c in s) | |
44 | def unmangle(s): |
|
45 | def unmangle(s): | |
45 | return ''.join(chr(ord(c) - 1) for c in s) |
|
46 | return ''.join(chr(ord(c) - 1) for c in s) | |
46 |
|
47 | |||
47 | def greet(repo, proto, name): |
|
48 | def greet(repo, proto, name): | |
48 | return mangle(repo.greet(unmangle(name))) |
|
49 | return mangle(repo.greet(unmangle(name))) | |
49 |
|
50 | |||
50 | wireproto.commands['greet'] = (greet, 'name',) |
|
51 | wireproto.commands['greet'] = (greet, 'name',) | |
51 |
|
52 | |||
52 | srv = serverrepo() |
|
53 | srv = serverrepo() | |
53 | clt = clientpeer(srv) |
|
54 | clt = clientpeer(srv) | |
54 |
|
55 | |||
55 | print(clt.greet("Foobar")) |
|
56 | print(clt.greet("Foobar")) | |
56 | b = clt.batch() |
|
57 | b = clt.batch() | |
57 | fs = [b.greet(s) for s in ["Fo, =;:<o", "Bar"]] |
|
58 | fs = [b.greet(s) for s in ["Fo, =;:<o", "Bar"]] | |
58 | b.submit() |
|
59 | b.submit() | |
59 | print([f.value for f in fs]) |
|
60 | print([f.value for f in fs]) |
General Comments 0
You need to be logged in to leave comments.
Login now