Show More
@@ -1,93 +1,95 | |||||
1 | from __future__ import absolute_import, print_function |
|
1 | from __future__ import absolute_import, print_function | |
2 |
|
2 | |||
3 | from mercurial import ( |
|
3 | from mercurial import ( | |
4 | error, |
|
4 | error, | |
5 | pycompat, |
|
5 | pycompat, | |
|
6 | ui as uimod, | |||
6 | util, |
|
7 | util, | |
7 | wireproto, |
|
8 | wireproto, | |
8 | wireprototypes, |
|
9 | wireprototypes, | |
9 | ) |
|
10 | ) | |
10 | stringio = util.stringio |
|
11 | stringio = util.stringio | |
11 |
|
12 | |||
12 | class proto(object): |
|
13 | class proto(object): | |
13 | def __init__(self, args): |
|
14 | def __init__(self, args): | |
14 | self.args = args |
|
15 | self.args = args | |
15 | def getargs(self, spec): |
|
16 | def getargs(self, spec): | |
16 | args = self.args |
|
17 | args = self.args | |
17 | args.setdefault(b'*', {}) |
|
18 | args.setdefault(b'*', {}) | |
18 | names = spec.split() |
|
19 | names = spec.split() | |
19 | return [args[n] for n in names] |
|
20 | return [args[n] for n in names] | |
20 |
|
21 | |||
21 | def checkperm(self, perm): |
|
22 | def checkperm(self, perm): | |
22 | pass |
|
23 | pass | |
23 |
|
24 | |||
24 | class clientpeer(wireproto.wirepeer): |
|
25 | class clientpeer(wireproto.wirepeer): | |
25 | def __init__(self, serverrepo): |
|
26 | def __init__(self, serverrepo, ui): | |
26 | self.serverrepo = serverrepo |
|
27 | self.serverrepo = serverrepo | |
|
28 | self._ui = ui | |||
27 |
|
29 | |||
28 | @property |
|
30 | @property | |
29 | def ui(self): |
|
31 | def ui(self): | |
30 |
return self. |
|
32 | return self._ui | |
31 |
|
33 | |||
32 | def url(self): |
|
34 | def url(self): | |
33 | return b'test' |
|
35 | return b'test' | |
34 |
|
36 | |||
35 | def local(self): |
|
37 | def local(self): | |
36 | return None |
|
38 | return None | |
37 |
|
39 | |||
38 | def peer(self): |
|
40 | def peer(self): | |
39 | return self |
|
41 | return self | |
40 |
|
42 | |||
41 | def canpush(self): |
|
43 | def canpush(self): | |
42 | return True |
|
44 | return True | |
43 |
|
45 | |||
44 | def close(self): |
|
46 | def close(self): | |
45 | pass |
|
47 | pass | |
46 |
|
48 | |||
47 | def capabilities(self): |
|
49 | def capabilities(self): | |
48 | return [b'batch'] |
|
50 | return [b'batch'] | |
49 |
|
51 | |||
50 | def _call(self, cmd, **args): |
|
52 | def _call(self, cmd, **args): | |
51 | args = pycompat.byteskwargs(args) |
|
53 | args = pycompat.byteskwargs(args) | |
52 | res = wireproto.dispatch(self.serverrepo, proto(args), cmd) |
|
54 | res = wireproto.dispatch(self.serverrepo, proto(args), cmd) | |
53 | if isinstance(res, wireprototypes.bytesresponse): |
|
55 | if isinstance(res, wireprototypes.bytesresponse): | |
54 | return res.data |
|
56 | return res.data | |
55 | elif isinstance(res, bytes): |
|
57 | elif isinstance(res, bytes): | |
56 | return res |
|
58 | return res | |
57 | else: |
|
59 | else: | |
58 | raise error.Abort('dummy client does not support response type') |
|
60 | raise error.Abort('dummy client does not support response type') | |
59 |
|
61 | |||
60 | def _callstream(self, cmd, **args): |
|
62 | def _callstream(self, cmd, **args): | |
61 | return stringio(self._call(cmd, **args)) |
|
63 | return stringio(self._call(cmd, **args)) | |
62 |
|
64 | |||
63 | @wireproto.batchable |
|
65 | @wireproto.batchable | |
64 | def greet(self, name): |
|
66 | def greet(self, name): | |
65 | f = wireproto.future() |
|
67 | f = wireproto.future() | |
66 | yield {b'name': mangle(name)}, f |
|
68 | yield {b'name': mangle(name)}, f | |
67 | yield unmangle(f.value) |
|
69 | yield unmangle(f.value) | |
68 |
|
70 | |||
69 | class serverrepo(object): |
|
71 | class serverrepo(object): | |
70 | def greet(self, name): |
|
72 | def greet(self, name): | |
71 | return b"Hello, " + name |
|
73 | return b"Hello, " + name | |
72 |
|
74 | |||
73 | def filtered(self, name): |
|
75 | def filtered(self, name): | |
74 | return self |
|
76 | return self | |
75 |
|
77 | |||
76 | def mangle(s): |
|
78 | def mangle(s): | |
77 | return b''.join(pycompat.bytechr(ord(c) + 1) for c in pycompat.bytestr(s)) |
|
79 | return b''.join(pycompat.bytechr(ord(c) + 1) for c in pycompat.bytestr(s)) | |
78 | def unmangle(s): |
|
80 | def unmangle(s): | |
79 | return b''.join(pycompat.bytechr(ord(c) - 1) for c in pycompat.bytestr(s)) |
|
81 | return b''.join(pycompat.bytechr(ord(c) - 1) for c in pycompat.bytestr(s)) | |
80 |
|
82 | |||
81 | def greet(repo, proto, name): |
|
83 | def greet(repo, proto, name): | |
82 | return mangle(repo.greet(unmangle(name))) |
|
84 | return mangle(repo.greet(unmangle(name))) | |
83 |
|
85 | |||
84 | wireproto.commands[b'greet'] = (greet, b'name',) |
|
86 | wireproto.commands[b'greet'] = (greet, b'name',) | |
85 |
|
87 | |||
86 | srv = serverrepo() |
|
88 | srv = serverrepo() | |
87 | clt = clientpeer(srv) |
|
89 | clt = clientpeer(srv, uimod.ui()) | |
88 |
|
90 | |||
89 | print(clt.greet(b"Foobar")) |
|
91 | print(clt.greet(b"Foobar")) | |
90 | b = clt.iterbatch() |
|
92 | b = clt.iterbatch() | |
91 | list(map(b.greet, (b'Fo, =;:<o', b'Bar'))) |
|
93 | list(map(b.greet, (b'Fo, =;:<o', b'Bar'))) | |
92 | b.submit() |
|
94 | b.submit() | |
93 | print([r for r in b.results()]) |
|
95 | print([r for r in b.results()]) |
General Comments 0
You need to be logged in to leave comments.
Login now