comparison mercurial/dagparser.py @ 43077:687b865b95ad

formatting: byteify all mercurial/ and hgext/ string literals Done with python3.7 contrib/byteify-strings.py -i $(hg files 'set:mercurial/**.py - mercurial/thirdparty/** + hgext/**.py - hgext/fsmonitor/pywatchman/** - mercurial/__init__.py') black -l 80 -t py33 -S $(hg files 'set:**.py - mercurial/thirdparty/** - "contrib/python-zstandard/**" - hgext/fsmonitor/pywatchman/**') # skip-blame mass-reformatting only Differential Revision: https://phab.mercurial-scm.org/D6972
author Augie Fackler <augie@google.com>
date Sun, 06 Oct 2019 09:48:39 -0400
parents 2372284d9457
children 8ff1ecfadcd1
comparison
equal deleted inserted replaced
43076:2372284d9457 43077:687b865b95ad
183 return labels[ref] 183 return labels[ref]
184 184
185 chiter = pycompat.iterbytestr(desc) 185 chiter = pycompat.iterbytestr(desc)
186 186
187 def nextch(): 187 def nextch():
188 return next(chiter, '\0') 188 return next(chiter, b'\0')
189 189
190 def nextrun(c, allow): 190 def nextrun(c, allow):
191 s = '' 191 s = b''
192 while c in allow: 192 while c in allow:
193 s += c 193 s += c
194 c = nextch() 194 c = nextch()
195 return c, s 195 return c, s
196 196
197 def nextdelimited(c, limit, escape): 197 def nextdelimited(c, limit, escape):
198 s = '' 198 s = b''
199 while c != limit: 199 while c != limit:
200 if c == escape: 200 if c == escape:
201 c = nextch() 201 c = nextch()
202 s += c 202 s += c
203 c = nextch() 203 c = nextch()
204 return nextch(), s 204 return nextch(), s
205 205
206 def nextstring(c): 206 def nextstring(c):
207 if c == '"': 207 if c == b'"':
208 return nextdelimited(nextch(), '"', '\\') 208 return nextdelimited(nextch(), b'"', b'\\')
209 else: 209 else:
210 return nextrun(c, wordchars) 210 return nextrun(c, wordchars)
211 211
212 c = nextch() 212 c = nextch()
213 while c != '\0': 213 while c != b'\0':
214 while c in pycompat.bytestr(string.whitespace): 214 while c in pycompat.bytestr(string.whitespace):
215 c = nextch() 215 c = nextch()
216 if c == '.': 216 if c == b'.':
217 yield 'n', (r, [p1]) 217 yield b'n', (r, [p1])
218 p1 = r 218 p1 = r
219 r += 1 219 r += 1
220 c = nextch() 220 c = nextch()
221 elif c == '+': 221 elif c == b'+':
222 c, digs = nextrun(nextch(), pycompat.bytestr(string.digits)) 222 c, digs = nextrun(nextch(), pycompat.bytestr(string.digits))
223 n = int(digs) 223 n = int(digs)
224 for i in pycompat.xrange(0, n): 224 for i in pycompat.xrange(0, n):
225 yield 'n', (r, [p1]) 225 yield b'n', (r, [p1])
226 p1 = r 226 p1 = r
227 r += 1 227 r += 1
228 elif c in '*/': 228 elif c in b'*/':
229 if c == '*': 229 if c == b'*':
230 c = nextch() 230 c = nextch()
231 c, pref = nextstring(c) 231 c, pref = nextstring(c)
232 prefs = [pref] 232 prefs = [pref]
233 while c == '/': 233 while c == b'/':
234 c, pref = nextstring(nextch()) 234 c, pref = nextstring(nextch())
235 prefs.append(pref) 235 prefs.append(pref)
236 ps = [resolve(ref) for ref in prefs] 236 ps = [resolve(ref) for ref in prefs]
237 yield 'n', (r, ps) 237 yield b'n', (r, ps)
238 p1 = r 238 p1 = r
239 r += 1 239 r += 1
240 elif c == '<': 240 elif c == b'<':
241 c, ref = nextstring(nextch()) 241 c, ref = nextstring(nextch())
242 p1 = resolve(ref) 242 p1 = resolve(ref)
243 elif c == ':': 243 elif c == b':':
244 c, name = nextstring(nextch()) 244 c, name = nextstring(nextch())
245 labels[name] = p1 245 labels[name] = p1
246 yield 'l', (p1, name) 246 yield b'l', (p1, name)
247 elif c == '@': 247 elif c == b'@':
248 c, text = nextstring(nextch()) 248 c, text = nextstring(nextch())
249 yield 'a', text 249 yield b'a', text
250 elif c == '!': 250 elif c == b'!':
251 c = nextch() 251 c = nextch()
252 if c == '!': 252 if c == b'!':
253 cmd = '' 253 cmd = b''
254 c = nextch() 254 c = nextch()
255 while c not in '\n\r\0': 255 while c not in b'\n\r\0':
256 cmd += c 256 cmd += c
257 c = nextch() 257 c = nextch()
258 yield 'C', cmd 258 yield b'C', cmd
259 else: 259 else:
260 c, cmd = nextstring(c) 260 c, cmd = nextstring(c)
261 yield 'c', cmd 261 yield b'c', cmd
262 elif c == '#': 262 elif c == b'#':
263 while c not in '\n\r\0': 263 while c not in b'\n\r\0':
264 c = nextch() 264 c = nextch()
265 elif c == '$': 265 elif c == b'$':
266 p1 = -1 266 p1 = -1
267 c = nextch() 267 c = nextch()
268 elif c == '\0': 268 elif c == b'\0':
269 return # in case it was preceded by whitespace 269 return # in case it was preceded by whitespace
270 else: 270 else:
271 s = '' 271 s = b''
272 i = 0 272 i = 0
273 while c != '\0' and i < 10: 273 while c != b'\0' and i < 10:
274 s += c 274 s += c
275 i += 1 275 i += 1
276 c = nextch() 276 c = nextch()
277 raise error.Abort( 277 raise error.Abort(
278 _('invalid character in dag description: ' '%s...') % s 278 _(b'invalid character in dag description: ' b'%s...') % s
279 ) 279 )
280 280
281 281
282 def dagtextlines( 282 def dagtextlines(
283 events, 283 events,
290 maxlinewidth=70, 290 maxlinewidth=70,
291 ): 291 ):
292 '''generates single lines for dagtext()''' 292 '''generates single lines for dagtext()'''
293 293
294 def wrapstring(text): 294 def wrapstring(text):
295 if re.match("^[0-9a-z]*$", text): 295 if re.match(b"^[0-9a-z]*$", text):
296 return text 296 return text
297 return '"' + text.replace('\\', '\\\\').replace('"', '\"') + '"' 297 return b'"' + text.replace(b'\\', b'\\\\').replace(b'"', b'\"') + b'"'
298 298
299 def gen(): 299 def gen():
300 labels = {} 300 labels = {}
301 run = 0 301 run = 0
302 wantr = 0 302 wantr = 0
303 needroot = False 303 needroot = False
304 for kind, data in events: 304 for kind, data in events:
305 if kind == 'n': 305 if kind == b'n':
306 r, ps = data 306 r, ps = data
307 307
308 # sanity check 308 # sanity check
309 if r != wantr: 309 if r != wantr:
310 raise error.Abort(_("expected id %i, got %i") % (wantr, r)) 310 raise error.Abort(_(b"expected id %i, got %i") % (wantr, r))
311 if not ps: 311 if not ps:
312 ps = [-1] 312 ps = [-1]
313 else: 313 else:
314 for p in ps: 314 for p in ps:
315 if p >= r: 315 if p >= r:
316 raise error.Abort( 316 raise error.Abort(
317 _( 317 _(
318 "parent id %i is larger than " 318 b"parent id %i is larger than "
319 "current id %i" 319 b"current id %i"
320 ) 320 )
321 % (p, r) 321 % (p, r)
322 ) 322 )
323 wantr += 1 323 wantr += 1
324 324
325 # new root? 325 # new root?
326 p1 = r - 1 326 p1 = r - 1
327 if len(ps) == 1 and ps[0] == -1: 327 if len(ps) == 1 and ps[0] == -1:
328 if needroot: 328 if needroot:
329 if run: 329 if run:
330 yield '+%d' % run 330 yield b'+%d' % run
331 run = 0 331 run = 0
332 if wrapnonlinear: 332 if wrapnonlinear:
333 yield '\n' 333 yield b'\n'
334 yield '$' 334 yield b'$'
335 p1 = -1 335 p1 = -1
336 else: 336 else:
337 needroot = True 337 needroot = True
338 if len(ps) == 1 and ps[0] == p1: 338 if len(ps) == 1 and ps[0] == p1:
339 if usedots: 339 if usedots:
340 yield "." 340 yield b"."
341 else: 341 else:
342 run += 1 342 run += 1
343 else: 343 else:
344 if run: 344 if run:
345 yield '+%d' % run 345 yield b'+%d' % run
346 run = 0 346 run = 0
347 if wrapnonlinear: 347 if wrapnonlinear:
348 yield '\n' 348 yield b'\n'
349 prefs = [] 349 prefs = []
350 for p in ps: 350 for p in ps:
351 if p == p1: 351 if p == p1:
352 prefs.append('') 352 prefs.append(b'')
353 elif p in labels: 353 elif p in labels:
354 prefs.append(labels[p]) 354 prefs.append(labels[p])
355 else: 355 else:
356 prefs.append('%d' % (r - p)) 356 prefs.append(b'%d' % (r - p))
357 yield '*' + '/'.join(prefs) 357 yield b'*' + b'/'.join(prefs)
358 else: 358 else:
359 if run: 359 if run:
360 yield '+%d' % run 360 yield b'+%d' % run
361 run = 0 361 run = 0
362 if kind == 'l': 362 if kind == b'l':
363 rid, name = data 363 rid, name = data
364 labels[rid] = name 364 labels[rid] = name
365 yield ':' + name 365 yield b':' + name
366 if wraplabels: 366 if wraplabels:
367 yield '\n' 367 yield b'\n'
368 elif kind == 'c': 368 elif kind == b'c':
369 yield '!' + wrapstring(data) 369 yield b'!' + wrapstring(data)
370 if wrapcommands: 370 if wrapcommands:
371 yield '\n' 371 yield b'\n'
372 elif kind == 'C': 372 elif kind == b'C':
373 yield '!!' + data 373 yield b'!!' + data
374 yield '\n' 374 yield b'\n'
375 elif kind == 'a': 375 elif kind == b'a':
376 if wrapannotations: 376 if wrapannotations:
377 yield '\n' 377 yield b'\n'
378 yield '@' + wrapstring(data) 378 yield b'@' + wrapstring(data)
379 elif kind == '#': 379 elif kind == b'#':
380 yield '#' + data 380 yield b'#' + data
381 yield '\n' 381 yield b'\n'
382 else: 382 else:
383 raise error.Abort( 383 raise error.Abort(
384 _("invalid event type in dag: " "('%s', '%s')") 384 _(b"invalid event type in dag: " b"('%s', '%s')")
385 % ( 385 % (
386 stringutil.escapestr(kind), 386 stringutil.escapestr(kind),
387 stringutil.escapestr(data), 387 stringutil.escapestr(data),
388 ) 388 )
389 ) 389 )
390 if run: 390 if run:
391 yield '+%d' % run 391 yield b'+%d' % run
392 392
393 line = '' 393 line = b''
394 for part in gen(): 394 for part in gen():
395 if part == '\n': 395 if part == b'\n':
396 if line: 396 if line:
397 yield line 397 yield line
398 line = '' 398 line = b''
399 else: 399 else:
400 if len(line) + len(part) >= maxlinewidth: 400 if len(line) + len(part) >= maxlinewidth:
401 yield line 401 yield line
402 line = '' 402 line = b''
403 elif addspaces and line and part != '.': 403 elif addspaces and line and part != b'.':
404 line += ' ' 404 line += b' '
405 line += part 405 line += part
406 if line: 406 if line:
407 yield line 407 yield line
408 408
409 409
492 492
493 >>> dagtext(parsedag(b'+1 :f +1 :p2 *f */p2')) 493 >>> dagtext(parsedag(b'+1 :f +1 :p2 *f */p2'))
494 '+1 :f +1 :p2 *f */p2' 494 '+1 :f +1 :p2 *f */p2'
495 495
496 ''' 496 '''
497 return "\n".join( 497 return b"\n".join(
498 dagtextlines( 498 dagtextlines(
499 dag, 499 dag,
500 addspaces, 500 addspaces,
501 wraplabels, 501 wraplabels,
502 wrapannotations, 502 wrapannotations,