comparison mercurial/smartset.py @ 38576:a3130208db1c

stringutil: move _formatsetrepr() from smartset I'll add a matcher subclass wrapping a boolean function, which will use buildrepr() to provide debugging information in a similar way to smartset.filteredset.
author Yuya Nishihara <yuya@tcha.org>
date Sun, 10 Jun 2018 17:19:31 +0900
parents 152f4822d210
children e7aa113b14f7
comparison
equal deleted inserted replaced
38575:152f4822d210 38576:a3130208db1c
11 encoding, 11 encoding,
12 error, 12 error,
13 pycompat, 13 pycompat,
14 util, 14 util,
15 ) 15 )
16 16 from .utils import (
17 def _formatsetrepr(r): 17 stringutil,
18 """Format an optional printable representation of a set 18 )
19
20 ======== =================================
21 type(r) example
22 ======== =================================
23 tuple ('<not %r>', other)
24 bytes '<branch closed>'
25 callable lambda: '<branch %r>' % sorted(b)
26 object other
27 ======== =================================
28 """
29 if r is None:
30 return ''
31 elif isinstance(r, tuple):
32 return r[0] % pycompat.rapply(pycompat.maybebytestr, r[1:])
33 elif isinstance(r, bytes):
34 return r
35 elif callable(r):
36 return r()
37 else:
38 return pycompat.byterepr(r)
39 19
40 def _typename(o): 20 def _typename(o):
41 return pycompat.sysbytes(type(o).__name__).lstrip('_') 21 return pycompat.sysbytes(type(o).__name__).lstrip('_')
42 22
43 class abstractsmartset(object): 23 class abstractsmartset(object):
390 return s 370 return s
391 371
392 @encoding.strmethod 372 @encoding.strmethod
393 def __repr__(self): 373 def __repr__(self):
394 d = {None: '', False: '-', True: '+'}[self._ascending] 374 d = {None: '', False: '-', True: '+'}[self._ascending]
395 s = _formatsetrepr(self._datarepr) 375 s = stringutil.buildrepr(self._datarepr)
396 if not s: 376 if not s:
397 l = self._list 377 l = self._list
398 # if _list has been built from a set, it might have a different 378 # if _list has been built from a set, it might have a different
399 # order from one python implementation to another. 379 # order from one python implementation to another.
400 # We fallback to the sorted version for a stable output. 380 # We fallback to the sorted version for a stable output.
512 return x 492 return x
513 493
514 @encoding.strmethod 494 @encoding.strmethod
515 def __repr__(self): 495 def __repr__(self):
516 xs = [pycompat.byterepr(self._subset)] 496 xs = [pycompat.byterepr(self._subset)]
517 s = _formatsetrepr(self._condrepr) 497 s = stringutil.buildrepr(self._condrepr)
518 if s: 498 if s:
519 xs.append(s) 499 xs.append(s)
520 return '<%s %s>' % (_typename(self), ', '.join(xs)) 500 return '<%s %s>' % (_typename(self), ', '.join(xs))
521 501
522 def _iterordered(ascending, iter1, iter2): 502 def _iterordered(ascending, iter1, iter2):