Mercurial > python-hglib
annotate hglib/util.py @ 180:ff6efc1ab9e4
protocol: allow hglib user to get call backs for prompts, output and errors
setcbout(cbout), setcberr(cberr) and setcbprompt(cbprompt) are used to
set the call back function used by the hgclient class. cb stands for
call back.
cbout is a function that will be called with the stdout data of the
command as it runs. cbout is called with output as it is made available,
which can be as partial lines or multiple lines.
cberr is a function that will be called with the stderr data of the
command as it runs. cberr is called with output as it is made available,
which can be as partial lines or multiple lines.
Command that make remote connects can prompt for username and password
for HTTP/HTTPS connections.
cbprompt is called when hgclient need a response to a prompt from the
server. It receives the max number of bytes to return and the contents
of stdout received so far. The last text sent to either cbout or cberr
will contain the prompt text itself.
author | Barry A. Scott <barry@barrys-emacs.org> |
---|---|
date | Fri, 28 Oct 2016 11:33:20 +0100 |
parents | 16496e0f3c09 |
children | 0f81ed8e147b |
rev | line source |
---|---|
152
0808bb03add5
util: don't try to use itertools.izip under Python 3 (issue4520)
Brett Cannon <brett@python.org>
parents:
150
diff
changeset
|
1 import os, subprocess, sys |
148
c1b966866ed7
hglib: make all imports absolute (issue4520)
Brett Cannon <brett@python.org>
parents:
146
diff
changeset
|
2 from hglib import error |
146
8d7bf729a4db
hglib: use io.BytesIO when available (issue4520)
Brett Cannon <brett@python.org>
parents:
145
diff
changeset
|
3 try: |
8d7bf729a4db
hglib: use io.BytesIO when available (issue4520)
Brett Cannon <brett@python.org>
parents:
145
diff
changeset
|
4 from io import BytesIO |
8d7bf729a4db
hglib: use io.BytesIO when available (issue4520)
Brett Cannon <brett@python.org>
parents:
145
diff
changeset
|
5 except ImportError: |
8d7bf729a4db
hglib: use io.BytesIO when available (issue4520)
Brett Cannon <brett@python.org>
parents:
145
diff
changeset
|
6 from cStringIO import StringIO as BytesIO |
141
ea80bd2775f6
hglib: introduce util.b() (issue4520)
Brett Cannon <brett@python.org>
parents:
134
diff
changeset
|
7 |
ea80bd2775f6
hglib: introduce util.b() (issue4520)
Brett Cannon <brett@python.org>
parents:
134
diff
changeset
|
8 if sys.version_info[0] > 2: |
152
0808bb03add5
util: don't try to use itertools.izip under Python 3 (issue4520)
Brett Cannon <brett@python.org>
parents:
150
diff
changeset
|
9 izip = zip |
153
ef8eb78fc88d
hglib: don't try to use long under Python 3 (issue4520)
Brett Cannon <brett@python.org>
parents:
152
diff
changeset
|
10 integertypes = (int,) |
152
0808bb03add5
util: don't try to use itertools.izip under Python 3 (issue4520)
Brett Cannon <brett@python.org>
parents:
150
diff
changeset
|
11 |
141
ea80bd2775f6
hglib: introduce util.b() (issue4520)
Brett Cannon <brett@python.org>
parents:
134
diff
changeset
|
12 def b(s): |
ea80bd2775f6
hglib: introduce util.b() (issue4520)
Brett Cannon <brett@python.org>
parents:
134
diff
changeset
|
13 """Encode the string as bytes.""" |
ea80bd2775f6
hglib: introduce util.b() (issue4520)
Brett Cannon <brett@python.org>
parents:
134
diff
changeset
|
14 return s.encode('latin-1') |
ea80bd2775f6
hglib: introduce util.b() (issue4520)
Brett Cannon <brett@python.org>
parents:
134
diff
changeset
|
15 else: |
152
0808bb03add5
util: don't try to use itertools.izip under Python 3 (issue4520)
Brett Cannon <brett@python.org>
parents:
150
diff
changeset
|
16 from itertools import izip |
153
ef8eb78fc88d
hglib: don't try to use long under Python 3 (issue4520)
Brett Cannon <brett@python.org>
parents:
152
diff
changeset
|
17 integertypes = (long, int) |
158
2104fc9aa513
util: make cmdbuilder work with bytes (issue4520)
Brett Cannon <brett@python.org>
parents:
153
diff
changeset
|
18 bytes = str # Defined in Python 2.6/2.7, but to the same value. |
152
0808bb03add5
util: don't try to use itertools.izip under Python 3 (issue4520)
Brett Cannon <brett@python.org>
parents:
150
diff
changeset
|
19 |
141
ea80bd2775f6
hglib: introduce util.b() (issue4520)
Brett Cannon <brett@python.org>
parents:
134
diff
changeset
|
20 def b(s): |
ea80bd2775f6
hglib: introduce util.b() (issue4520)
Brett Cannon <brett@python.org>
parents:
134
diff
changeset
|
21 """Encode the string as bytes.""" |
ea80bd2775f6
hglib: introduce util.b() (issue4520)
Brett Cannon <brett@python.org>
parents:
134
diff
changeset
|
22 return s |
0 | 23 |
150
b94e1263836c
util: introduce strtobytes() (issue4520)
Brett Cannon <brett@python.org>
parents:
149
diff
changeset
|
24 def strtobytes(s): |
b94e1263836c
util: introduce strtobytes() (issue4520)
Brett Cannon <brett@python.org>
parents:
149
diff
changeset
|
25 """Return the bytes of the string representation of an object.""" |
b94e1263836c
util: introduce strtobytes() (issue4520)
Brett Cannon <brett@python.org>
parents:
149
diff
changeset
|
26 return str(s).encode('latin-1') |
b94e1263836c
util: introduce strtobytes() (issue4520)
Brett Cannon <brett@python.org>
parents:
149
diff
changeset
|
27 |
0 | 28 def grouper(n, iterable): |
29 ''' list(grouper(2, range(4))) -> [(0, 1), (2, 3)] ''' | |
30 args = [iter(iterable)] * n | |
152
0808bb03add5
util: don't try to use itertools.izip under Python 3 (issue4520)
Brett Cannon <brett@python.org>
parents:
150
diff
changeset
|
31 return izip(*args) |
3
d7903b923217
util: add cmdbuilder, a helper function to generate the command to run
Idan Kamara <idankk86@gmail.com>
parents:
0
diff
changeset
|
32 |
8
3ac38d500d68
move hgclient._eatlines to util
Idan Kamara <idankk86@gmail.com>
parents:
3
diff
changeset
|
33 def eatlines(s, n): |
9
5882a698ad5c
util: rewrite eatlines (faster and simpler version)
Idan Kamara <idankk86@gmail.com>
parents:
8
diff
changeset
|
34 """ |
159
16496e0f3c09
util: update doctests to work with bytes (issue4520)
Brett Cannon <brett@python.org>
parents:
158
diff
changeset
|
35 >>> eatlines(b("1\\n2"), 1) == b('2') |
16496e0f3c09
util: update doctests to work with bytes (issue4520)
Brett Cannon <brett@python.org>
parents:
158
diff
changeset
|
36 True |
16496e0f3c09
util: update doctests to work with bytes (issue4520)
Brett Cannon <brett@python.org>
parents:
158
diff
changeset
|
37 >>> eatlines(b("1\\n2"), 2) == b('') |
16496e0f3c09
util: update doctests to work with bytes (issue4520)
Brett Cannon <brett@python.org>
parents:
158
diff
changeset
|
38 True |
16496e0f3c09
util: update doctests to work with bytes (issue4520)
Brett Cannon <brett@python.org>
parents:
158
diff
changeset
|
39 >>> eatlines(b("1\\n2"), 3) == b('') |
16496e0f3c09
util: update doctests to work with bytes (issue4520)
Brett Cannon <brett@python.org>
parents:
158
diff
changeset
|
40 True |
16496e0f3c09
util: update doctests to work with bytes (issue4520)
Brett Cannon <brett@python.org>
parents:
158
diff
changeset
|
41 >>> eatlines(b("1\\n2\\n3"), 1) == b('2\\n3') |
16496e0f3c09
util: update doctests to work with bytes (issue4520)
Brett Cannon <brett@python.org>
parents:
158
diff
changeset
|
42 True |
9
5882a698ad5c
util: rewrite eatlines (faster and simpler version)
Idan Kamara <idankk86@gmail.com>
parents:
8
diff
changeset
|
43 """ |
145
f3c430afa598
hglib: abstract out use of cStringIO.StringIO (issue4520)
Brett Cannon <brett@python.org>
parents:
142
diff
changeset
|
44 cs = BytesIO(s) |
8
3ac38d500d68
move hgclient._eatlines to util
Idan Kamara <idankk86@gmail.com>
parents:
3
diff
changeset
|
45 |
9
5882a698ad5c
util: rewrite eatlines (faster and simpler version)
Idan Kamara <idankk86@gmail.com>
parents:
8
diff
changeset
|
46 for line in cs: |
5882a698ad5c
util: rewrite eatlines (faster and simpler version)
Idan Kamara <idankk86@gmail.com>
parents:
8
diff
changeset
|
47 n -= 1 |
5882a698ad5c
util: rewrite eatlines (faster and simpler version)
Idan Kamara <idankk86@gmail.com>
parents:
8
diff
changeset
|
48 if n == 0: |
5882a698ad5c
util: rewrite eatlines (faster and simpler version)
Idan Kamara <idankk86@gmail.com>
parents:
8
diff
changeset
|
49 return cs.read() |
142
fe74d5599539
hglib: wrap all application string literals in util.b() (issue4520)
Brett Cannon <brett@python.org>
parents:
141
diff
changeset
|
50 return b('') |
8
3ac38d500d68
move hgclient._eatlines to util
Idan Kamara <idankk86@gmail.com>
parents:
3
diff
changeset
|
51 |
19 | 52 def skiplines(s, prefix): |
53 """ | |
54 Skip lines starting with prefix in s | |
55 | |
159
16496e0f3c09
util: update doctests to work with bytes (issue4520)
Brett Cannon <brett@python.org>
parents:
158
diff
changeset
|
56 >>> skiplines(b('a\\nb\\na\\n'), b('a')) == b('b\\na\\n') |
16496e0f3c09
util: update doctests to work with bytes (issue4520)
Brett Cannon <brett@python.org>
parents:
158
diff
changeset
|
57 True |
16496e0f3c09
util: update doctests to work with bytes (issue4520)
Brett Cannon <brett@python.org>
parents:
158
diff
changeset
|
58 >>> skiplines(b('a\\na\\n'), b('a')) == b('') |
16496e0f3c09
util: update doctests to work with bytes (issue4520)
Brett Cannon <brett@python.org>
parents:
158
diff
changeset
|
59 True |
16496e0f3c09
util: update doctests to work with bytes (issue4520)
Brett Cannon <brett@python.org>
parents:
158
diff
changeset
|
60 >>> skiplines(b(''), b('a')) == b('') |
16496e0f3c09
util: update doctests to work with bytes (issue4520)
Brett Cannon <brett@python.org>
parents:
158
diff
changeset
|
61 True |
16496e0f3c09
util: update doctests to work with bytes (issue4520)
Brett Cannon <brett@python.org>
parents:
158
diff
changeset
|
62 >>> skiplines(b('a\\nb'), b('b')) == b('a\\nb') |
16496e0f3c09
util: update doctests to work with bytes (issue4520)
Brett Cannon <brett@python.org>
parents:
158
diff
changeset
|
63 True |
19 | 64 """ |
145
f3c430afa598
hglib: abstract out use of cStringIO.StringIO (issue4520)
Brett Cannon <brett@python.org>
parents:
142
diff
changeset
|
65 cs = BytesIO(s) |
19 | 66 |
67 for line in cs: | |
68 if not line.startswith(prefix): | |
69 return line + cs.read() | |
70 | |
142
fe74d5599539
hglib: wrap all application string literals in util.b() (issue4520)
Brett Cannon <brett@python.org>
parents:
141
diff
changeset
|
71 return b('') |
19 | 72 |
158
2104fc9aa513
util: make cmdbuilder work with bytes (issue4520)
Brett Cannon <brett@python.org>
parents:
153
diff
changeset
|
73 def _cmdval(val): |
2104fc9aa513
util: make cmdbuilder work with bytes (issue4520)
Brett Cannon <brett@python.org>
parents:
153
diff
changeset
|
74 if isinstance(val, bytes): |
2104fc9aa513
util: make cmdbuilder work with bytes (issue4520)
Brett Cannon <brett@python.org>
parents:
153
diff
changeset
|
75 return val |
2104fc9aa513
util: make cmdbuilder work with bytes (issue4520)
Brett Cannon <brett@python.org>
parents:
153
diff
changeset
|
76 else: |
2104fc9aa513
util: make cmdbuilder work with bytes (issue4520)
Brett Cannon <brett@python.org>
parents:
153
diff
changeset
|
77 return strtobytes(val) |
2104fc9aa513
util: make cmdbuilder work with bytes (issue4520)
Brett Cannon <brett@python.org>
parents:
153
diff
changeset
|
78 |
3
d7903b923217
util: add cmdbuilder, a helper function to generate the command to run
Idan Kamara <idankk86@gmail.com>
parents:
0
diff
changeset
|
79 def cmdbuilder(name, *args, **kwargs): |
d7903b923217
util: add cmdbuilder, a helper function to generate the command to run
Idan Kamara <idankk86@gmail.com>
parents:
0
diff
changeset
|
80 """ |
d7903b923217
util: add cmdbuilder, a helper function to generate the command to run
Idan Kamara <idankk86@gmail.com>
parents:
0
diff
changeset
|
81 A helper for building the command arguments |
d7903b923217
util: add cmdbuilder, a helper function to generate the command to run
Idan Kamara <idankk86@gmail.com>
parents:
0
diff
changeset
|
82 |
d7903b923217
util: add cmdbuilder, a helper function to generate the command to run
Idan Kamara <idankk86@gmail.com>
parents:
0
diff
changeset
|
83 args are the positional arguments |
d7903b923217
util: add cmdbuilder, a helper function to generate the command to run
Idan Kamara <idankk86@gmail.com>
parents:
0
diff
changeset
|
84 |
d7903b923217
util: add cmdbuilder, a helper function to generate the command to run
Idan Kamara <idankk86@gmail.com>
parents:
0
diff
changeset
|
85 kwargs are the options |
d7903b923217
util: add cmdbuilder, a helper function to generate the command to run
Idan Kamara <idankk86@gmail.com>
parents:
0
diff
changeset
|
86 keys that are single lettered are prepended with '-', others with '--', |
d7903b923217
util: add cmdbuilder, a helper function to generate the command to run
Idan Kamara <idankk86@gmail.com>
parents:
0
diff
changeset
|
87 underscores are replaced with dashes |
d7903b923217
util: add cmdbuilder, a helper function to generate the command to run
Idan Kamara <idankk86@gmail.com>
parents:
0
diff
changeset
|
88 |
d7903b923217
util: add cmdbuilder, a helper function to generate the command to run
Idan Kamara <idankk86@gmail.com>
parents:
0
diff
changeset
|
89 keys with False boolean values are ignored, lists add the key multiple times |
d7903b923217
util: add cmdbuilder, a helper function to generate the command to run
Idan Kamara <idankk86@gmail.com>
parents:
0
diff
changeset
|
90 |
d7903b923217
util: add cmdbuilder, a helper function to generate the command to run
Idan Kamara <idankk86@gmail.com>
parents:
0
diff
changeset
|
91 None arguments are skipped |
d7903b923217
util: add cmdbuilder, a helper function to generate the command to run
Idan Kamara <idankk86@gmail.com>
parents:
0
diff
changeset
|
92 |
158
2104fc9aa513
util: make cmdbuilder work with bytes (issue4520)
Brett Cannon <brett@python.org>
parents:
153
diff
changeset
|
93 >>> cmdbuilder(b('cmd'), a=True, b=False, c=None) == [b('cmd'), b('-a')] |
2104fc9aa513
util: make cmdbuilder work with bytes (issue4520)
Brett Cannon <brett@python.org>
parents:
153
diff
changeset
|
94 True |
2104fc9aa513
util: make cmdbuilder work with bytes (issue4520)
Brett Cannon <brett@python.org>
parents:
153
diff
changeset
|
95 >>> cmdbuilder(b('cmd'), long=True) == [b('cmd'), b('--long')] |
2104fc9aa513
util: make cmdbuilder work with bytes (issue4520)
Brett Cannon <brett@python.org>
parents:
153
diff
changeset
|
96 True |
2104fc9aa513
util: make cmdbuilder work with bytes (issue4520)
Brett Cannon <brett@python.org>
parents:
153
diff
changeset
|
97 >>> cmdbuilder(b('cmd'), str=b('s')) == [b('cmd'), b('--str'), b('s')] |
2104fc9aa513
util: make cmdbuilder work with bytes (issue4520)
Brett Cannon <brett@python.org>
parents:
153
diff
changeset
|
98 True |
2104fc9aa513
util: make cmdbuilder work with bytes (issue4520)
Brett Cannon <brett@python.org>
parents:
153
diff
changeset
|
99 >>> cmdbuilder(b('cmd'), d_ash=True) == [b('cmd'), b('--d-ash')] |
2104fc9aa513
util: make cmdbuilder work with bytes (issue4520)
Brett Cannon <brett@python.org>
parents:
153
diff
changeset
|
100 True |
2104fc9aa513
util: make cmdbuilder work with bytes (issue4520)
Brett Cannon <brett@python.org>
parents:
153
diff
changeset
|
101 >>> cmdbuilder(b('cmd'), _=True) == [b('cmd'), b('-')] |
2104fc9aa513
util: make cmdbuilder work with bytes (issue4520)
Brett Cannon <brett@python.org>
parents:
153
diff
changeset
|
102 True |
2104fc9aa513
util: make cmdbuilder work with bytes (issue4520)
Brett Cannon <brett@python.org>
parents:
153
diff
changeset
|
103 >>> expect = [b('cmd'), b('--list'), b('1'), b('--list'), b('2')] |
2104fc9aa513
util: make cmdbuilder work with bytes (issue4520)
Brett Cannon <brett@python.org>
parents:
153
diff
changeset
|
104 >>> cmdbuilder(b('cmd'), list=[1, 2]) == expect |
2104fc9aa513
util: make cmdbuilder work with bytes (issue4520)
Brett Cannon <brett@python.org>
parents:
153
diff
changeset
|
105 True |
2104fc9aa513
util: make cmdbuilder work with bytes (issue4520)
Brett Cannon <brett@python.org>
parents:
153
diff
changeset
|
106 >>> cmdbuilder(b('cmd'), None) == [b('cmd')] |
2104fc9aa513
util: make cmdbuilder work with bytes (issue4520)
Brett Cannon <brett@python.org>
parents:
153
diff
changeset
|
107 True |
3
d7903b923217
util: add cmdbuilder, a helper function to generate the command to run
Idan Kamara <idankk86@gmail.com>
parents:
0
diff
changeset
|
108 """ |
d7903b923217
util: add cmdbuilder, a helper function to generate the command to run
Idan Kamara <idankk86@gmail.com>
parents:
0
diff
changeset
|
109 cmd = [name] |
d7903b923217
util: add cmdbuilder, a helper function to generate the command to run
Idan Kamara <idankk86@gmail.com>
parents:
0
diff
changeset
|
110 for arg, val in kwargs.items(): |
d7903b923217
util: add cmdbuilder, a helper function to generate the command to run
Idan Kamara <idankk86@gmail.com>
parents:
0
diff
changeset
|
111 if val is None: |
d7903b923217
util: add cmdbuilder, a helper function to generate the command to run
Idan Kamara <idankk86@gmail.com>
parents:
0
diff
changeset
|
112 continue |
d7903b923217
util: add cmdbuilder, a helper function to generate the command to run
Idan Kamara <idankk86@gmail.com>
parents:
0
diff
changeset
|
113 |
158
2104fc9aa513
util: make cmdbuilder work with bytes (issue4520)
Brett Cannon <brett@python.org>
parents:
153
diff
changeset
|
114 arg = arg.encode('latin-1').replace(b('_'), b('-')) |
142
fe74d5599539
hglib: wrap all application string literals in util.b() (issue4520)
Brett Cannon <brett@python.org>
parents:
141
diff
changeset
|
115 if arg != b('-'): |
86
f98d6e234cd9
util: eliminate py2.6 if/else expression
Matt Mackall <mpm@selenic.com>
parents:
77
diff
changeset
|
116 if len(arg) == 1: |
142
fe74d5599539
hglib: wrap all application string literals in util.b() (issue4520)
Brett Cannon <brett@python.org>
parents:
141
diff
changeset
|
117 arg = b('-') + arg |
86
f98d6e234cd9
util: eliminate py2.6 if/else expression
Matt Mackall <mpm@selenic.com>
parents:
77
diff
changeset
|
118 else: |
142
fe74d5599539
hglib: wrap all application string literals in util.b() (issue4520)
Brett Cannon <brett@python.org>
parents:
141
diff
changeset
|
119 arg = b('--') + arg |
3
d7903b923217
util: add cmdbuilder, a helper function to generate the command to run
Idan Kamara <idankk86@gmail.com>
parents:
0
diff
changeset
|
120 if isinstance(val, bool): |
d7903b923217
util: add cmdbuilder, a helper function to generate the command to run
Idan Kamara <idankk86@gmail.com>
parents:
0
diff
changeset
|
121 if val: |
d7903b923217
util: add cmdbuilder, a helper function to generate the command to run
Idan Kamara <idankk86@gmail.com>
parents:
0
diff
changeset
|
122 cmd.append(arg) |
d7903b923217
util: add cmdbuilder, a helper function to generate the command to run
Idan Kamara <idankk86@gmail.com>
parents:
0
diff
changeset
|
123 elif isinstance(val, list): |
d7903b923217
util: add cmdbuilder, a helper function to generate the command to run
Idan Kamara <idankk86@gmail.com>
parents:
0
diff
changeset
|
124 for v in val: |
d7903b923217
util: add cmdbuilder, a helper function to generate the command to run
Idan Kamara <idankk86@gmail.com>
parents:
0
diff
changeset
|
125 cmd.append(arg) |
158
2104fc9aa513
util: make cmdbuilder work with bytes (issue4520)
Brett Cannon <brett@python.org>
parents:
153
diff
changeset
|
126 cmd.append(_cmdval(v)) |
3
d7903b923217
util: add cmdbuilder, a helper function to generate the command to run
Idan Kamara <idankk86@gmail.com>
parents:
0
diff
changeset
|
127 else: |
d7903b923217
util: add cmdbuilder, a helper function to generate the command to run
Idan Kamara <idankk86@gmail.com>
parents:
0
diff
changeset
|
128 cmd.append(arg) |
158
2104fc9aa513
util: make cmdbuilder work with bytes (issue4520)
Brett Cannon <brett@python.org>
parents:
153
diff
changeset
|
129 cmd.append(_cmdval(val)) |
3
d7903b923217
util: add cmdbuilder, a helper function to generate the command to run
Idan Kamara <idankk86@gmail.com>
parents:
0
diff
changeset
|
130 |
d7903b923217
util: add cmdbuilder, a helper function to generate the command to run
Idan Kamara <idankk86@gmail.com>
parents:
0
diff
changeset
|
131 for a in args: |
d7903b923217
util: add cmdbuilder, a helper function to generate the command to run
Idan Kamara <idankk86@gmail.com>
parents:
0
diff
changeset
|
132 if a is not None: |
d7903b923217
util: add cmdbuilder, a helper function to generate the command to run
Idan Kamara <idankk86@gmail.com>
parents:
0
diff
changeset
|
133 cmd.append(a) |
d7903b923217
util: add cmdbuilder, a helper function to generate the command to run
Idan Kamara <idankk86@gmail.com>
parents:
0
diff
changeset
|
134 |
d7903b923217
util: add cmdbuilder, a helper function to generate the command to run
Idan Kamara <idankk86@gmail.com>
parents:
0
diff
changeset
|
135 return cmd |
49
3d7e0325ba1c
util: introduce a generic error handler that is aware of return codes
Idan Kamara <idankk86@gmail.com>
parents:
19
diff
changeset
|
136 |
3d7e0325ba1c
util: introduce a generic error handler that is aware of return codes
Idan Kamara <idankk86@gmail.com>
parents:
19
diff
changeset
|
137 class reterrorhandler(object): |
134 | 138 """This class is meant to be used with rawcommand() error handler |
139 argument. It remembers the return value the command returned if | |
140 it's one of allowed values, which is only 1 if none are given. | |
141 Otherwise it raises a CommandError. | |
49
3d7e0325ba1c
util: introduce a generic error handler that is aware of return codes
Idan Kamara <idankk86@gmail.com>
parents:
19
diff
changeset
|
142 |
3d7e0325ba1c
util: introduce a generic error handler that is aware of return codes
Idan Kamara <idankk86@gmail.com>
parents:
19
diff
changeset
|
143 >>> e = reterrorhandler('') |
3d7e0325ba1c
util: introduce a generic error handler that is aware of return codes
Idan Kamara <idankk86@gmail.com>
parents:
19
diff
changeset
|
144 >>> bool(e) |
3d7e0325ba1c
util: introduce a generic error handler that is aware of return codes
Idan Kamara <idankk86@gmail.com>
parents:
19
diff
changeset
|
145 True |
3d7e0325ba1c
util: introduce a generic error handler that is aware of return codes
Idan Kamara <idankk86@gmail.com>
parents:
19
diff
changeset
|
146 >>> e(1, 'a', '') |
3d7e0325ba1c
util: introduce a generic error handler that is aware of return codes
Idan Kamara <idankk86@gmail.com>
parents:
19
diff
changeset
|
147 'a' |
3d7e0325ba1c
util: introduce a generic error handler that is aware of return codes
Idan Kamara <idankk86@gmail.com>
parents:
19
diff
changeset
|
148 >>> bool(e) |
3d7e0325ba1c
util: introduce a generic error handler that is aware of return codes
Idan Kamara <idankk86@gmail.com>
parents:
19
diff
changeset
|
149 False |
134 | 150 |
49
3d7e0325ba1c
util: introduce a generic error handler that is aware of return codes
Idan Kamara <idankk86@gmail.com>
parents:
19
diff
changeset
|
151 """ |
3d7e0325ba1c
util: introduce a generic error handler that is aware of return codes
Idan Kamara <idankk86@gmail.com>
parents:
19
diff
changeset
|
152 def __init__(self, args, allowed=None): |
3d7e0325ba1c
util: introduce a generic error handler that is aware of return codes
Idan Kamara <idankk86@gmail.com>
parents:
19
diff
changeset
|
153 self.args = args |
3d7e0325ba1c
util: introduce a generic error handler that is aware of return codes
Idan Kamara <idankk86@gmail.com>
parents:
19
diff
changeset
|
154 self.ret = 0 |
3d7e0325ba1c
util: introduce a generic error handler that is aware of return codes
Idan Kamara <idankk86@gmail.com>
parents:
19
diff
changeset
|
155 if allowed is None: |
3d7e0325ba1c
util: introduce a generic error handler that is aware of return codes
Idan Kamara <idankk86@gmail.com>
parents:
19
diff
changeset
|
156 self.allowed = [1] |
3d7e0325ba1c
util: introduce a generic error handler that is aware of return codes
Idan Kamara <idankk86@gmail.com>
parents:
19
diff
changeset
|
157 else: |
3d7e0325ba1c
util: introduce a generic error handler that is aware of return codes
Idan Kamara <idankk86@gmail.com>
parents:
19
diff
changeset
|
158 self.allowed = allowed |
3d7e0325ba1c
util: introduce a generic error handler that is aware of return codes
Idan Kamara <idankk86@gmail.com>
parents:
19
diff
changeset
|
159 |
3d7e0325ba1c
util: introduce a generic error handler that is aware of return codes
Idan Kamara <idankk86@gmail.com>
parents:
19
diff
changeset
|
160 def __call__(self, ret, out, err): |
3d7e0325ba1c
util: introduce a generic error handler that is aware of return codes
Idan Kamara <idankk86@gmail.com>
parents:
19
diff
changeset
|
161 self.ret = ret |
3d7e0325ba1c
util: introduce a generic error handler that is aware of return codes
Idan Kamara <idankk86@gmail.com>
parents:
19
diff
changeset
|
162 if ret not in self.allowed: |
3d7e0325ba1c
util: introduce a generic error handler that is aware of return codes
Idan Kamara <idankk86@gmail.com>
parents:
19
diff
changeset
|
163 raise error.CommandError(self.args, ret, out, err) |
3d7e0325ba1c
util: introduce a generic error handler that is aware of return codes
Idan Kamara <idankk86@gmail.com>
parents:
19
diff
changeset
|
164 return out |
3d7e0325ba1c
util: introduce a generic error handler that is aware of return codes
Idan Kamara <idankk86@gmail.com>
parents:
19
diff
changeset
|
165 |
3d7e0325ba1c
util: introduce a generic error handler that is aware of return codes
Idan Kamara <idankk86@gmail.com>
parents:
19
diff
changeset
|
166 def __nonzero__(self): |
3d7e0325ba1c
util: introduce a generic error handler that is aware of return codes
Idan Kamara <idankk86@gmail.com>
parents:
19
diff
changeset
|
167 """ Returns True if the return code was 0, False otherwise """ |
3d7e0325ba1c
util: introduce a generic error handler that is aware of return codes
Idan Kamara <idankk86@gmail.com>
parents:
19
diff
changeset
|
168 return self.ret == 0 |
72 | 169 |
149
958307b30af3
hglib: add a __bool__ method where __nonzero__ is defined (issue4520)
Brett Cannon <brett@python.org>
parents:
148
diff
changeset
|
170 def __bool__(self): |
958307b30af3
hglib: add a __bool__ method where __nonzero__ is defined (issue4520)
Brett Cannon <brett@python.org>
parents:
148
diff
changeset
|
171 return self.__nonzero__() |
958307b30af3
hglib: add a __bool__ method where __nonzero__ is defined (issue4520)
Brett Cannon <brett@python.org>
parents:
148
diff
changeset
|
172 |
77
4282391dd693
util: add propertycache decorator
Idan Kamara <idankk86@gmail.com>
parents:
74
diff
changeset
|
173 class propertycache(object): |
4282391dd693
util: add propertycache decorator
Idan Kamara <idankk86@gmail.com>
parents:
74
diff
changeset
|
174 """ |
4282391dd693
util: add propertycache decorator
Idan Kamara <idankk86@gmail.com>
parents:
74
diff
changeset
|
175 Decorator that remembers the return value of a function call. |
4282391dd693
util: add propertycache decorator
Idan Kamara <idankk86@gmail.com>
parents:
74
diff
changeset
|
176 |
159
16496e0f3c09
util: update doctests to work with bytes (issue4520)
Brett Cannon <brett@python.org>
parents:
158
diff
changeset
|
177 >>> execcount = 0 |
77
4282391dd693
util: add propertycache decorator
Idan Kamara <idankk86@gmail.com>
parents:
74
diff
changeset
|
178 >>> class obj(object): |
4282391dd693
util: add propertycache decorator
Idan Kamara <idankk86@gmail.com>
parents:
74
diff
changeset
|
179 ... def func(self): |
159
16496e0f3c09
util: update doctests to work with bytes (issue4520)
Brett Cannon <brett@python.org>
parents:
158
diff
changeset
|
180 ... global execcount |
16496e0f3c09
util: update doctests to work with bytes (issue4520)
Brett Cannon <brett@python.org>
parents:
158
diff
changeset
|
181 ... execcount += 1 |
77
4282391dd693
util: add propertycache decorator
Idan Kamara <idankk86@gmail.com>
parents:
74
diff
changeset
|
182 ... return [] |
4282391dd693
util: add propertycache decorator
Idan Kamara <idankk86@gmail.com>
parents:
74
diff
changeset
|
183 ... func = propertycache(func) |
4282391dd693
util: add propertycache decorator
Idan Kamara <idankk86@gmail.com>
parents:
74
diff
changeset
|
184 >>> o = obj() |
4282391dd693
util: add propertycache decorator
Idan Kamara <idankk86@gmail.com>
parents:
74
diff
changeset
|
185 >>> o.func |
4282391dd693
util: add propertycache decorator
Idan Kamara <idankk86@gmail.com>
parents:
74
diff
changeset
|
186 [] |
159
16496e0f3c09
util: update doctests to work with bytes (issue4520)
Brett Cannon <brett@python.org>
parents:
158
diff
changeset
|
187 >>> execcount |
16496e0f3c09
util: update doctests to work with bytes (issue4520)
Brett Cannon <brett@python.org>
parents:
158
diff
changeset
|
188 1 |
77
4282391dd693
util: add propertycache decorator
Idan Kamara <idankk86@gmail.com>
parents:
74
diff
changeset
|
189 >>> o.func |
4282391dd693
util: add propertycache decorator
Idan Kamara <idankk86@gmail.com>
parents:
74
diff
changeset
|
190 [] |
159
16496e0f3c09
util: update doctests to work with bytes (issue4520)
Brett Cannon <brett@python.org>
parents:
158
diff
changeset
|
191 >>> execcount |
16496e0f3c09
util: update doctests to work with bytes (issue4520)
Brett Cannon <brett@python.org>
parents:
158
diff
changeset
|
192 1 |
77
4282391dd693
util: add propertycache decorator
Idan Kamara <idankk86@gmail.com>
parents:
74
diff
changeset
|
193 """ |
4282391dd693
util: add propertycache decorator
Idan Kamara <idankk86@gmail.com>
parents:
74
diff
changeset
|
194 def __init__(self, func): |
4282391dd693
util: add propertycache decorator
Idan Kamara <idankk86@gmail.com>
parents:
74
diff
changeset
|
195 self.func = func |
4282391dd693
util: add propertycache decorator
Idan Kamara <idankk86@gmail.com>
parents:
74
diff
changeset
|
196 self.name = func.__name__ |
4282391dd693
util: add propertycache decorator
Idan Kamara <idankk86@gmail.com>
parents:
74
diff
changeset
|
197 def __get__(self, obj, type=None): |
4282391dd693
util: add propertycache decorator
Idan Kamara <idankk86@gmail.com>
parents:
74
diff
changeset
|
198 result = self.func(obj) |
4282391dd693
util: add propertycache decorator
Idan Kamara <idankk86@gmail.com>
parents:
74
diff
changeset
|
199 setattr(obj, self.name, result) |
4282391dd693
util: add propertycache decorator
Idan Kamara <idankk86@gmail.com>
parents:
74
diff
changeset
|
200 return result |
4282391dd693
util: add propertycache decorator
Idan Kamara <idankk86@gmail.com>
parents:
74
diff
changeset
|
201 |
72 | 202 close_fds = os.name == 'posix' |
203 | |
74
a5dd7b5d0be1
util, popen: hide subprocess window
Idan Kamara <idankk86@gmail.com>
parents:
73
diff
changeset
|
204 startupinfo = None |
a5dd7b5d0be1
util, popen: hide subprocess window
Idan Kamara <idankk86@gmail.com>
parents:
73
diff
changeset
|
205 if os.name == 'nt': |
a5dd7b5d0be1
util, popen: hide subprocess window
Idan Kamara <idankk86@gmail.com>
parents:
73
diff
changeset
|
206 startupinfo = subprocess.STARTUPINFO() |
a5dd7b5d0be1
util, popen: hide subprocess window
Idan Kamara <idankk86@gmail.com>
parents:
73
diff
changeset
|
207 startupinfo.dwFlags |= subprocess.STARTF_USESHOWWINDOW |
a5dd7b5d0be1
util, popen: hide subprocess window
Idan Kamara <idankk86@gmail.com>
parents:
73
diff
changeset
|
208 |
72 | 209 def popen(args, env={}): |
210 environ = None | |
211 if env: | |
212 environ = dict(os.environ) | |
213 environ.update(env) | |
214 | |
215 return subprocess.Popen(args, stdin=subprocess.PIPE, stdout=subprocess.PIPE, | |
73
77ae99e032f6
util, popen: redirect stderr as well (for hglib.init in case of error)
Idan Kamara <idankk86@gmail.com>
parents:
72
diff
changeset
|
216 stderr=subprocess.PIPE, close_fds=close_fds, |
74
a5dd7b5d0be1
util, popen: hide subprocess window
Idan Kamara <idankk86@gmail.com>
parents:
73
diff
changeset
|
217 startupinfo=startupinfo, env=environ) |