|
1 | | -''' |
2 | | -Created on 17/05/2012 |
3 | | -
|
4 | | -@author: piranna |
5 | | -''' |
6 | | - |
| 1 | +import itertools |
7 | 2 | import re |
8 | | -from collections import OrderedDict |
| 3 | +from collections import OrderedDict, deque |
| 4 | +from contextlib import contextmanager |
9 | 5 |
|
10 | 6 |
|
11 | 7 | class Cache(OrderedDict): |
12 | 8 | """Cache with LRU algorithm using an OrderedDict as basis |
13 | 9 | """ |
| 10 | + |
14 | 11 | def __init__(self, maxsize=100): |
15 | 12 | OrderedDict.__init__(self) |
16 | 13 |
|
@@ -113,3 +110,80 @@ def split_unquoted_newlines(text): |
113 | 110 | else: |
114 | 111 | outputlines[-1] += line |
115 | 112 | return outputlines |
| 113 | + |
| 114 | + |
| 115 | +def remove_quotes(val): |
| 116 | + """Helper that removes surrounding quotes from strings.""" |
| 117 | + if val is None: |
| 118 | + return |
| 119 | + if val[0] in ('"', "'") and val[0] == val[-1]: |
| 120 | + val = val[1:-1] |
| 121 | + return val |
| 122 | + |
| 123 | + |
| 124 | +def recurse(*cls): |
| 125 | + def wrap(f): |
| 126 | + def wrapped_f(tlist): |
| 127 | + for sgroup in tlist.get_sublists(): |
| 128 | + if not isinstance(sgroup, cls): |
| 129 | + wrapped_f(sgroup) |
| 130 | + f(tlist) |
| 131 | + |
| 132 | + return wrapped_f |
| 133 | + |
| 134 | + return wrap |
| 135 | + |
| 136 | + |
| 137 | +def imt(token, i=None, m=None, t=None): |
| 138 | + """Aid function to refactor comparisons for Instance, Match and TokenType |
| 139 | + Aid fun |
| 140 | + :param token: |
| 141 | + :param i: Class or Tuple/List of Classes |
| 142 | + :param m: Tuple of TokenType & Value. Can be list of Tuple for multiple |
| 143 | + :param t: TokenType or Tuple/List of TokenTypes |
| 144 | + :return: bool |
| 145 | + """ |
| 146 | + t = (t,) if t and not isinstance(t, (list, tuple)) else t |
| 147 | + m = (m,) if m and not isinstance(m, (list,)) else m |
| 148 | + |
| 149 | + if token is None: |
| 150 | + return False |
| 151 | + elif i is not None and isinstance(token, i): |
| 152 | + return True |
| 153 | + elif m is not None and any((token.match(*x) for x in m)): |
| 154 | + return True |
| 155 | + elif t is not None and token.ttype in t: |
| 156 | + return True |
| 157 | + else: |
| 158 | + return False |
| 159 | + |
| 160 | + |
| 161 | +def find_matching(tlist, token, M1, M2): |
| 162 | + idx = tlist.token_index(token) |
| 163 | + depth = 0 |
| 164 | + for token in tlist[idx:]: |
| 165 | + if token.match(*M1): |
| 166 | + depth += 1 |
| 167 | + elif token.match(*M2): |
| 168 | + depth -= 1 |
| 169 | + if depth == 0: |
| 170 | + return token |
| 171 | + |
| 172 | + |
| 173 | +def consume(iterator, n): |
| 174 | + """Advance the iterator n-steps ahead. If n is none, consume entirely.""" |
| 175 | + deque(itertools.islice(iterator, n), maxlen=0) |
| 176 | + |
| 177 | + |
| 178 | +@contextmanager |
| 179 | +def offset(filter_, n=0): |
| 180 | + filter_.offset += n |
| 181 | + yield |
| 182 | + filter_.offset -= n |
| 183 | + |
| 184 | + |
| 185 | +@contextmanager |
| 186 | +def indent(filter_, n=1): |
| 187 | + filter_.indent += n |
| 188 | + yield |
| 189 | + filter_.indent -= n |
0 commit comments