Skip to content

Commit e006e16

Browse files
committed
Update compat and utils
1 parent 43c14e0 commit e006e16

File tree

2 files changed

+98
-13
lines changed

2 files changed

+98
-13
lines changed

sqlparse/compat.py

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,29 +14,40 @@
1414
PY3 = sys.version_info[0] == 3
1515

1616
if PY3:
17+
def u(s):
18+
return str(s)
19+
20+
21+
range = range
1722
text_type = str
1823
string_types = (str,)
1924
from io import StringIO
2025

21-
def u(s):
22-
return str(s)
2326

2427
elif PY2:
28+
def u(s, encoding=None):
29+
encoding = encoding or 'unicode-escape'
30+
try:
31+
return unicode(s)
32+
except UnicodeDecodeError:
33+
return unicode(s, encoding)
34+
35+
36+
range = xrange
2537
text_type = unicode
2638
string_types = (basestring,)
27-
from StringIO import StringIO # flake8: noqa
28-
29-
def u(s):
30-
return unicode(s)
39+
from StringIO import StringIO
3140

3241

3342
# Directly copied from six:
3443
def with_metaclass(meta, *bases):
3544
"""Create a base class with a metaclass."""
45+
3646
# This requires a bit of explanation: the basic idea is to make a dummy
3747
# metaclass for one level of class instantiation that replaces itself with
3848
# the actual metaclass.
3949
class metaclass(meta):
4050
def __new__(cls, name, this_bases, d):
4151
return meta(name, bases, d)
52+
4253
return type.__new__(metaclass, 'temporary_class', (), {})

sqlparse/utils.py

Lines changed: 81 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,13 @@
1-
'''
2-
Created on 17/05/2012
3-
4-
@author: piranna
5-
'''
6-
1+
import itertools
72
import re
8-
from collections import OrderedDict
3+
from collections import OrderedDict, deque
4+
from contextlib import contextmanager
95

106

117
class Cache(OrderedDict):
128
"""Cache with LRU algorithm using an OrderedDict as basis
139
"""
10+
1411
def __init__(self, maxsize=100):
1512
OrderedDict.__init__(self)
1613

@@ -113,3 +110,80 @@ def split_unquoted_newlines(text):
113110
else:
114111
outputlines[-1] += line
115112
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

Comments
 (0)