Show More
@@ -1,122 +1,122 | |||
|
1 | 1 | import sys |
|
2 | 2 | |
|
3 | 3 | from mercurial import ( |
|
4 | 4 | error, |
|
5 | 5 | pycompat, |
|
6 | 6 | ui as uimod, |
|
7 | 7 | util, |
|
8 | 8 | wireprototypes, |
|
9 | 9 | wireprotov1peer, |
|
10 | 10 | wireprotov1server, |
|
11 | 11 | ) |
|
12 | 12 | from mercurial.utils import stringutil |
|
13 | 13 | |
|
14 | 14 | stringio = util.stringio |
|
15 | 15 | |
|
16 | 16 | |
|
17 | 17 | class proto: |
|
18 | 18 | def __init__(self, args): |
|
19 | 19 | self.args = args |
|
20 | 20 | self.name = 'dummyproto' |
|
21 | 21 | |
|
22 | 22 | def getargs(self, spec): |
|
23 | 23 | args = self.args |
|
24 | 24 | args.setdefault(b'*', {}) |
|
25 | 25 | names = spec.split() |
|
26 | 26 | return [args[n] for n in names] |
|
27 | 27 | |
|
28 | 28 | def checkperm(self, perm): |
|
29 | 29 | pass |
|
30 | 30 | |
|
31 | 31 | |
|
32 | 32 | wireprototypes.TRANSPORTS['dummyproto'] = { |
|
33 | 33 | 'transport': 'dummy', |
|
34 | 34 | 'version': 1, |
|
35 | 35 | } |
|
36 | 36 | |
|
37 | 37 | |
|
38 | 38 | class clientpeer(wireprotov1peer.wirepeer): |
|
39 | 39 | def __init__(self, serverrepo, ui): |
|
40 | 40 | self.serverrepo = serverrepo |
|
41 | 41 | self.ui = ui |
|
42 | 42 | |
|
43 | 43 | def url(self): |
|
44 | 44 | return b'test' |
|
45 | 45 | |
|
46 | 46 | def local(self): |
|
47 | 47 | return None |
|
48 | 48 | |
|
49 | 49 | def peer(self): |
|
50 | 50 | return self |
|
51 | 51 | |
|
52 | 52 | def canpush(self): |
|
53 | 53 | return True |
|
54 | 54 | |
|
55 | 55 | def close(self): |
|
56 | 56 | pass |
|
57 | 57 | |
|
58 | 58 | def capabilities(self): |
|
59 | 59 | return [b'batch'] |
|
60 | 60 | |
|
61 | 61 | def _call(self, cmd, **args): |
|
62 | 62 | args = pycompat.byteskwargs(args) |
|
63 | 63 | res = wireprotov1server.dispatch(self.serverrepo, proto(args), cmd) |
|
64 | 64 | if isinstance(res, wireprototypes.bytesresponse): |
|
65 | 65 | return res.data |
|
66 | 66 | elif isinstance(res, bytes): |
|
67 | 67 | return res |
|
68 | 68 | else: |
|
69 | raise error.Abort('dummy client does not support response type') | |
|
69 | raise error.Abort(b'dummy client does not support response type') | |
|
70 | 70 | |
|
71 | 71 | def _callstream(self, cmd, **args): |
|
72 | 72 | return stringio(self._call(cmd, **args)) |
|
73 | 73 | |
|
74 | 74 | @wireprotov1peer.batchable |
|
75 | 75 | def greet(self, name): |
|
76 | 76 | return {b'name': mangle(name)}, unmangle |
|
77 | 77 | |
|
78 | 78 | |
|
79 | 79 | class serverrepo: |
|
80 | 80 | def __init__(self, ui): |
|
81 | 81 | self.ui = ui |
|
82 | 82 | |
|
83 | 83 | def greet(self, name): |
|
84 | 84 | return b"Hello, " + name |
|
85 | 85 | |
|
86 | 86 | def filtered(self, name): |
|
87 | 87 | return self |
|
88 | 88 | |
|
89 | 89 | |
|
90 | 90 | def mangle(s): |
|
91 | 91 | return b''.join(pycompat.bytechr(ord(c) + 1) for c in pycompat.bytestr(s)) |
|
92 | 92 | |
|
93 | 93 | |
|
94 | 94 | def unmangle(s): |
|
95 | 95 | return b''.join(pycompat.bytechr(ord(c) - 1) for c in pycompat.bytestr(s)) |
|
96 | 96 | |
|
97 | 97 | |
|
98 | 98 | def greet(repo, proto, name): |
|
99 | 99 | return mangle(repo.greet(unmangle(name))) |
|
100 | 100 | |
|
101 | 101 | |
|
102 | 102 | wireprotov1server.commands[b'greet'] = (greet, b'name') |
|
103 | 103 | |
|
104 | 104 | srv = serverrepo(uimod.ui()) |
|
105 | 105 | clt = clientpeer(srv, uimod.ui()) |
|
106 | 106 | |
|
107 | 107 | |
|
108 | 108 | def printb(data, end=b'\n'): |
|
109 | 109 | out = getattr(sys.stdout, 'buffer', sys.stdout) |
|
110 | 110 | out.write(data + end) |
|
111 | 111 | out.flush() |
|
112 | 112 | |
|
113 | 113 | |
|
114 | 114 | printb(clt.greet(b"Foobar")) |
|
115 | 115 | |
|
116 | 116 | with clt.commandexecutor() as e: |
|
117 | 117 | fgreet1 = e.callcommand(b'greet', {b'name': b'Fo, =;:<o'}) |
|
118 | 118 | fgreet2 = e.callcommand(b'greet', {b'name': b'Bar'}) |
|
119 | 119 | |
|
120 | 120 | printb( |
|
121 | 121 | stringutil.pprint([f.result() for f in (fgreet1, fgreet2)], bprefix=True) |
|
122 | 122 | ) |
General Comments 0
You need to be logged in to leave comments.
Login now