|
| 1 | +""" |
| 2 | +Format a pretty string of a `SoupSieve` object for easy debugging. |
| 3 | +
|
| 4 | +This won't necessarily support all types and such, and definitely |
| 5 | +not support custom outputs. |
| 6 | +
|
| 7 | +It is mainly geared towards our types as the `SelectorList` |
| 8 | +object is a beast to look at without some indentation and newlines. |
| 9 | +The format and various output types is fairly known (though it |
| 10 | +hasn't been tested extensively to make sure we aren't missing corners). |
| 11 | +
|
| 12 | +Example: |
| 13 | +
|
| 14 | +``` |
| 15 | +>>> import soupsieve as sv |
| 16 | +>>> sv.compile('this > that.class[name=value]').selectors.pretty() |
| 17 | +SelectorList( |
| 18 | + selectors=( |
| 19 | + Selector( |
| 20 | + tag=SelectorTag( |
| 21 | + name='that', |
| 22 | + prefix=None), |
| 23 | + ids=(), |
| 24 | + classes=( |
| 25 | + 'class', |
| 26 | + ), |
| 27 | + attributes=( |
| 28 | + SelectorAttribute( |
| 29 | + attribute='name', |
| 30 | + prefix='', |
| 31 | + pattern=re.compile( |
| 32 | + '^value$'), |
| 33 | + xml_type_pattern=None), |
| 34 | + ), |
| 35 | + nth=(), |
| 36 | + selectors=(), |
| 37 | + relation=SelectorList( |
| 38 | + selectors=( |
| 39 | + Selector( |
| 40 | + tag=SelectorTag( |
| 41 | + name='this', |
| 42 | + prefix=None), |
| 43 | + ids=(), |
| 44 | + classes=(), |
| 45 | + attributes=(), |
| 46 | + nth=(), |
| 47 | + selectors=(), |
| 48 | + relation=SelectorList( |
| 49 | + selectors=(), |
| 50 | + is_not=False, |
| 51 | + is_html=False), |
| 52 | + rel_type='>', |
| 53 | + contains=(), |
| 54 | + lang=(), |
| 55 | + flags=0), |
| 56 | + ), |
| 57 | + is_not=False, |
| 58 | + is_html=False), |
| 59 | + rel_type=None, |
| 60 | + contains=(), |
| 61 | + lang=(), |
| 62 | + flags=0), |
| 63 | + ), |
| 64 | + is_not=False, |
| 65 | + is_html=False) |
| 66 | +``` |
| 67 | +""" |
| 68 | +import re |
| 69 | + |
| 70 | +RE_CLASS = re.compile(r'(?i)[a-z_][_a-z\d\.]+\(') |
| 71 | +RE_PARAM = re.compile(r'(?i)[_a-z][_a-z\d]+=') |
| 72 | +RE_EMPTY = re.compile(r'\(\)|\[\]|\{\}') |
| 73 | +RE_LSTRT = re.compile(r'\[') |
| 74 | +RE_DSTRT = re.compile(r'\{') |
| 75 | +RE_TSTRT = re.compile(r'\(') |
| 76 | +RE_LEND = re.compile(r'\]') |
| 77 | +RE_DEND = re.compile(r'\}') |
| 78 | +RE_TEND = re.compile(r'\)') |
| 79 | +RE_INT = re.compile(r'\d+') |
| 80 | +RE_KWORD = re.compile(r'(?i)[_a-z][_a-z\d]+') |
| 81 | +RE_DQSTR = re.compile(r'"(?:\\.|[^"\\])*"') |
| 82 | +RE_SQSTR = re.compile(r"'(?:\\.|[^'\\])*'") |
| 83 | +RE_SEP = re.compile(r'\s*(,)\s*') |
| 84 | +RE_DSEP = re.compile(r'\s*(:)\s*') |
| 85 | + |
| 86 | +TOKENS = { |
| 87 | + 'class': RE_CLASS, |
| 88 | + 'param': RE_PARAM, |
| 89 | + 'empty': RE_EMPTY, |
| 90 | + 'lstrt': RE_LSTRT, |
| 91 | + 'dstrt': RE_DSTRT, |
| 92 | + 'tstrt': RE_TSTRT, |
| 93 | + 'lend': RE_LEND, |
| 94 | + 'dend': RE_DEND, |
| 95 | + 'tend': RE_TEND, |
| 96 | + 'sqstr': RE_SQSTR, |
| 97 | + 'sep': RE_SEP, |
| 98 | + 'dsep': RE_DSEP, |
| 99 | + 'int': RE_INT, |
| 100 | + 'kword': RE_KWORD, |
| 101 | + 'dqstr': RE_DQSTR |
| 102 | +} |
| 103 | + |
| 104 | + |
| 105 | +def pretty(obj): # pragma: no cover |
| 106 | + """Make the object output string pretty.""" |
| 107 | + |
| 108 | + sel = str(obj) |
| 109 | + index = 0 |
| 110 | + end = len(sel) - 1 |
| 111 | + indent = 0 |
| 112 | + output = [] |
| 113 | + |
| 114 | + while index <= end: |
| 115 | + m = None |
| 116 | + for k, v in TOKENS.items(): |
| 117 | + m = v.match(sel, index) |
| 118 | + |
| 119 | + if m: |
| 120 | + name = k |
| 121 | + index = m.end(0) |
| 122 | + if name in ('class', 'lstrt', 'dstrt', 'tstrt'): |
| 123 | + indent += 4 |
| 124 | + output.append('{}\n{}'.format(m.group(0), " " * indent)) |
| 125 | + elif name in ('param', 'int', 'kword', 'sqstr', 'dqstr', 'empty'): |
| 126 | + output.append(m.group(0)) |
| 127 | + elif name in ('lend', 'dend', 'tend'): |
| 128 | + indent -= 4 |
| 129 | + output.append(m.group(0)) |
| 130 | + elif name in ('sep',): |
| 131 | + output.append('{}\n{}'.format(m.group(1), " " * indent)) |
| 132 | + elif name in ('dsep',): |
| 133 | + output.append('{} '.format(m.group(1))) |
| 134 | + break |
| 135 | + |
| 136 | + return ''.join(output) |
0 commit comments