keeps track of the number of braces on a given line and returns the result. It starts at zero and subtracts for closed braces, and adds for open braces.
(line)
| 82 | |
| 83 | |
| 84 | def count_braces(line): |
| 85 | """keeps track of the number of braces on a given line and returns the result. |
| 86 | |
| 87 | It starts at zero and subtracts for closed braces, and adds for open braces. |
| 88 | """ |
| 89 | open_braces = ["[", "(", "{"] |
| 90 | close_braces = ["]", ")", "}"] |
| 91 | closing_prefix_re = re.compile(r"[^\s\]\}\)]\s*[\]\}\)]+,?\s*$") |
| 92 | cnt = 0 |
| 93 | stripline = COMMENT_RE.sub(r"", line) |
| 94 | stripline = QUOTE_RE.sub(r"''", stripline) |
| 95 | for char in stripline: |
| 96 | for brace in open_braces: |
| 97 | if char == brace: |
| 98 | cnt += 1 |
| 99 | for brace in close_braces: |
| 100 | if char == brace: |
| 101 | cnt -= 1 |
| 102 | |
| 103 | after = False |
| 104 | if cnt > 0: |
| 105 | after = True |
| 106 | |
| 107 | # This catches the special case of a closing brace having something |
| 108 | # other than just whitespace ahead of it -- we don't want to |
| 109 | # unindent that until after this line is printed so it stays with |
| 110 | # the previous indentation level. |
| 111 | if cnt < 0 and closing_prefix_re.match(stripline): |
| 112 | after = True |
| 113 | return (cnt, after) |
| 114 | |
| 115 | |
| 116 | def prettyprint_input(lines): |
no test coverage detected