Mercurial > hg
comparison mercurial/utils/stringutil.py @ 40275:f2fbd32c7664
stringutil: make level parameter of pprintgen() 0-origin
I think this makes more sense in that the level is incremented where nesting
goes one more deep.
author | Yuya Nishihara <yuya@tcha.org> |
---|---|
date | Sun, 14 Oct 2018 07:18:19 +0200 |
parents | 0f549da54379 |
children | be57c7019c70 |
comparison
equal
deleted
inserted
replaced
40274:a8b9174517c7 | 40275:f2fbd32c7664 |
---|---|
45 | 45 |
46 def pprint(o, bprefix=False, indent=0): | 46 def pprint(o, bprefix=False, indent=0): |
47 """Pretty print an object.""" | 47 """Pretty print an object.""" |
48 return b''.join(pprintgen(o, bprefix=bprefix, indent=indent)) | 48 return b''.join(pprintgen(o, bprefix=bprefix, indent=indent)) |
49 | 49 |
50 def pprintgen(o, bprefix=False, indent=0, _level=1): | 50 def pprintgen(o, bprefix=False, indent=0, _level=0): |
51 """Pretty print an object to a generator of atoms. | 51 """Pretty print an object to a generator of atoms. |
52 | 52 |
53 ``bprefix`` is a flag influencing whether bytestrings are preferred with | 53 ``bprefix`` is a flag influencing whether bytestrings are preferred with |
54 a ``b''`` prefix. | 54 a ``b''`` prefix. |
55 | 55 |
73 return | 73 return |
74 | 74 |
75 yield '[' | 75 yield '[' |
76 | 76 |
77 if indent: | 77 if indent: |
78 _level += 1 | |
78 yield '\n' | 79 yield '\n' |
79 yield ' ' * (_level * indent) | 80 yield ' ' * (_level * indent) |
80 | 81 |
81 for i, a in enumerate(o): | 82 for i, a in enumerate(o): |
82 for chunk in pprintgen(a, bprefix=bprefix, indent=indent, | 83 for chunk in pprintgen(a, bprefix=bprefix, indent=indent, |
83 _level=_level + 1): | 84 _level=_level): |
84 yield chunk | 85 yield chunk |
85 | 86 |
86 if i + 1 < len(o): | 87 if i + 1 < len(o): |
87 if indent: | 88 if indent: |
88 yield ',\n' | 89 yield ',\n' |
89 yield ' ' * (_level * indent) | 90 yield ' ' * (_level * indent) |
90 else: | 91 else: |
91 yield ', ' | 92 yield ', ' |
92 | 93 |
93 if indent: | 94 if indent: |
94 yield '\n' | 95 _level -= 1 |
95 yield ' ' * ((_level - 1) * indent) | 96 yield '\n' |
97 yield ' ' * (_level * indent) | |
96 | 98 |
97 yield ']' | 99 yield ']' |
98 elif isinstance(o, dict): | 100 elif isinstance(o, dict): |
99 if not o: | 101 if not o: |
100 yield '{}' | 102 yield '{}' |
101 return | 103 return |
102 | 104 |
103 yield '{' | 105 yield '{' |
104 | 106 |
105 if indent: | 107 if indent: |
108 _level += 1 | |
106 yield '\n' | 109 yield '\n' |
107 yield ' ' * (_level * indent) | 110 yield ' ' * (_level * indent) |
108 | 111 |
109 for i, (k, v) in enumerate(sorted(o.items())): | 112 for i, (k, v) in enumerate(sorted(o.items())): |
110 for chunk in pprintgen(k, bprefix=bprefix, indent=indent, | 113 for chunk in pprintgen(k, bprefix=bprefix, indent=indent, |
111 _level=_level + 1): | 114 _level=_level): |
112 yield chunk | 115 yield chunk |
113 | 116 |
114 yield ': ' | 117 yield ': ' |
115 | 118 |
116 for chunk in pprintgen(v, bprefix=bprefix, indent=indent, | 119 for chunk in pprintgen(v, bprefix=bprefix, indent=indent, |
117 _level=_level + 1): | 120 _level=_level): |
118 yield chunk | 121 yield chunk |
119 | 122 |
120 if i + 1 < len(o): | 123 if i + 1 < len(o): |
121 if indent: | 124 if indent: |
122 yield ',\n' | 125 yield ',\n' |
123 yield ' ' * (_level * indent) | 126 yield ' ' * (_level * indent) |
124 else: | 127 else: |
125 yield ', ' | 128 yield ', ' |
126 | 129 |
127 if indent: | 130 if indent: |
128 yield '\n' | 131 _level -= 1 |
129 yield ' ' * ((_level - 1) * indent) | 132 yield '\n' |
133 yield ' ' * (_level * indent) | |
130 | 134 |
131 yield '}' | 135 yield '}' |
132 elif isinstance(o, set): | 136 elif isinstance(o, set): |
133 if not o: | 137 if not o: |
134 yield 'set([])' | 138 yield 'set([])' |
135 return | 139 return |
136 | 140 |
137 yield 'set([' | 141 yield 'set([' |
138 | 142 |
139 if indent: | 143 if indent: |
144 _level += 1 | |
140 yield '\n' | 145 yield '\n' |
141 yield ' ' * (_level * indent) | 146 yield ' ' * (_level * indent) |
142 | 147 |
143 for i, k in enumerate(sorted(o)): | 148 for i, k in enumerate(sorted(o)): |
144 for chunk in pprintgen(k, bprefix=bprefix, indent=indent, | 149 for chunk in pprintgen(k, bprefix=bprefix, indent=indent, |
145 _level=_level + 1): | 150 _level=_level): |
146 yield chunk | 151 yield chunk |
147 | 152 |
148 if i + 1 < len(o): | 153 if i + 1 < len(o): |
149 if indent: | 154 if indent: |
150 yield ',\n' | 155 yield ',\n' |
151 yield ' ' * (_level * indent) | 156 yield ' ' * (_level * indent) |
152 else: | 157 else: |
153 yield ', ' | 158 yield ', ' |
154 | 159 |
155 if indent: | 160 if indent: |
156 yield '\n' | 161 _level -= 1 |
157 yield ' ' * ((_level - 1) * indent) | 162 yield '\n' |
163 yield ' ' * (_level * indent) | |
158 | 164 |
159 yield '])' | 165 yield '])' |
160 elif isinstance(o, tuple): | 166 elif isinstance(o, tuple): |
161 if not o: | 167 if not o: |
162 yield '()' | 168 yield '()' |
163 return | 169 return |
164 | 170 |
165 yield '(' | 171 yield '(' |
166 | 172 |
167 if indent: | 173 if indent: |
174 _level += 1 | |
168 yield '\n' | 175 yield '\n' |
169 yield ' ' * (_level * indent) | 176 yield ' ' * (_level * indent) |
170 | 177 |
171 for i, a in enumerate(o): | 178 for i, a in enumerate(o): |
172 for chunk in pprintgen(a, bprefix=bprefix, indent=indent, | 179 for chunk in pprintgen(a, bprefix=bprefix, indent=indent, |
173 _level=_level + 1): | 180 _level=_level): |
174 yield chunk | 181 yield chunk |
175 | 182 |
176 if i + 1 < len(o): | 183 if i + 1 < len(o): |
177 if indent: | 184 if indent: |
178 yield ',\n' | 185 yield ',\n' |
179 yield ' ' * (_level * indent) | 186 yield ' ' * (_level * indent) |
180 else: | 187 else: |
181 yield ', ' | 188 yield ', ' |
182 | 189 |
183 if indent: | 190 if indent: |
184 yield '\n' | 191 _level -= 1 |
185 yield ' ' * ((_level - 1) * indent) | 192 yield '\n' |
193 yield ' ' * (_level * indent) | |
186 | 194 |
187 yield ')' | 195 yield ')' |
188 elif isinstance(o, types.GeneratorType): | 196 elif isinstance(o, types.GeneratorType): |
189 # Special case of empty generator. | 197 # Special case of empty generator. |
190 try: | 198 try: |
194 return | 202 return |
195 | 203 |
196 yield 'gen[' | 204 yield 'gen[' |
197 | 205 |
198 if indent: | 206 if indent: |
207 _level += 1 | |
199 yield '\n' | 208 yield '\n' |
200 yield ' ' * (_level * indent) | 209 yield ' ' * (_level * indent) |
201 | 210 |
202 last = False | 211 last = False |
203 | 212 |
208 nextitem = next(o) | 217 nextitem = next(o) |
209 except StopIteration: | 218 except StopIteration: |
210 last = True | 219 last = True |
211 | 220 |
212 for chunk in pprintgen(current, bprefix=bprefix, indent=indent, | 221 for chunk in pprintgen(current, bprefix=bprefix, indent=indent, |
213 _level=_level + 1): | 222 _level=_level): |
214 yield chunk | 223 yield chunk |
215 | 224 |
216 if not last: | 225 if not last: |
217 if indent: | 226 if indent: |
218 yield ',\n' | 227 yield ',\n' |
219 yield ' ' * (_level * indent) | 228 yield ' ' * (_level * indent) |
220 else: | 229 else: |
221 yield ', ' | 230 yield ', ' |
222 | 231 |
223 if indent: | 232 if indent: |
224 yield '\n' | 233 _level -= 1 |
225 yield ' ' * ((_level -1) * indent) | 234 yield '\n' |
235 yield ' ' * (_level * indent) | |
226 | 236 |
227 yield ']' | 237 yield ']' |
228 else: | 238 else: |
229 yield pycompat.byterepr(o) | 239 yield pycompat.byterepr(o) |
230 | 240 |