Skip to content

Commit 2a09057

Browse files
committed
Clean up imports.
Removed wildcard imports, mainly to keep pyflakes quiet. But this change lifted some wrong imports and class usage.
1 parent b11dd97 commit 2a09057

7 files changed

Lines changed: 701 additions & 706 deletions

File tree

sqlparse/engine/filter.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# -*- coding: utf-8 -*-
22

3+
from sqlparse.sql import Statement, Token
34
from sqlparse import tokens as T
4-
from sqlparse.engine.grouping import Statement, Token
55

66

77
class TokenFilter(object):

sqlparse/engine/grouping.py

Lines changed: 33 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22

33
import itertools
44

5+
from sqlparse import sql
56
from sqlparse import tokens as T
6-
from sqlparse.sql import *
77

88

99
def _group_left_right(tlist, ttype, value, cls,
@@ -83,34 +83,35 @@ def _find_matching(i, tl, stt, sva, ett, eva):
8383
token = tlist.token_next_match(idx, start_ttype, start_value)
8484

8585
def group_if(tlist):
86-
_group_matching(tlist, T.Keyword, 'IF', T.Keyword, 'END IF', If, True)
86+
_group_matching(tlist, T.Keyword, 'IF', T.Keyword, 'END IF', sql.If, True)
8787

8888
def group_for(tlist):
89-
_group_matching(tlist, T.Keyword, 'FOR', T.Keyword, 'END LOOP', For, True)
89+
_group_matching(tlist, T.Keyword, 'FOR', T.Keyword, 'END LOOP',
90+
sql.For, True)
9091

9192
def group_as(tlist):
9293
def _right_valid(token):
9394
# Currently limited to DML/DDL. Maybe additional more non SQL reserved
9495
# keywords should appear here (see issue8).
9596
return not token.ttype in (T.DML, T.DDL)
96-
_group_left_right(tlist, T.Keyword, 'AS', Identifier,
97+
_group_left_right(tlist, T.Keyword, 'AS', sql.Identifier,
9798
check_right=_right_valid)
9899

99100
def group_assignment(tlist):
100-
_group_left_right(tlist, T.Assignment, ':=', Assignment,
101+
_group_left_right(tlist, T.Assignment, ':=', sql.Assignment,
101102
include_semicolon=True)
102103

103104
def group_comparsion(tlist):
104105
def _parts_valid(token):
105106
return (token.ttype in (T.String.Symbol, T.Name, T.Number,
106107
T.Number.Integer, T.Literal)
107-
or isinstance(token, (Identifier,)))
108-
_group_left_right(tlist, T.Operator.Comparsion, None, Comparsion,
108+
or isinstance(token, (sql.Identifier,)))
109+
_group_left_right(tlist, T.Operator.Comparsion, None, sql.Comparsion,
109110
check_left=_parts_valid, check_right=_parts_valid)
110111

111112

112113
def group_case(tlist):
113-
_group_matching(tlist, T.Keyword, 'CASE', T.Keyword, 'END', Case,
114+
_group_matching(tlist, T.Keyword, 'CASE', T.Keyword, 'END', sql.Case,
114115
include_semicolon=True, recurse=True)
115116

116117

@@ -131,40 +132,40 @@ def _consume_cycle(tl, i):
131132

132133
# bottom up approach: group subgroups first
133134
[group_identifier(sgroup) for sgroup in tlist.get_sublists()
134-
if not isinstance(sgroup, Identifier)]
135+
if not isinstance(sgroup, sql.Identifier)]
135136

136137
# real processing
137138
idx = 0
138-
token = tlist.token_next_by_instance(idx, Function)
139+
token = tlist.token_next_by_instance(idx, sql.Function)
139140
if token is None:
140141
token = tlist.token_next_by_type(idx, (T.String.Symbol, T.Name))
141142
while token:
142143
identifier_tokens = [token]+list(
143144
_consume_cycle(tlist,
144145
tlist.token_index(token)+1))
145146
if not (len(identifier_tokens) == 1
146-
and isinstance(identifier_tokens[0], Function)):
147-
group = tlist.group_tokens(Identifier, identifier_tokens)
147+
and isinstance(identifier_tokens[0], sql.Function)):
148+
group = tlist.group_tokens(sql.Identifier, identifier_tokens)
148149
idx = tlist.token_index(group)+1
149150
else:
150151
idx += 1
151-
token = tlist.token_next_by_instance(idx, Function)
152+
token = tlist.token_next_by_instance(idx, sql.Function)
152153
if token is None:
153154
token = tlist.token_next_by_type(idx, (T.String.Symbol, T.Name))
154155

155156

156157
def group_identifier_list(tlist):
157158
[group_identifier_list(sgroup) for sgroup in tlist.get_sublists()
158-
if not isinstance(sgroup, (Identifier, IdentifierList))]
159+
if not isinstance(sgroup, (sql.Identifier, sql.IdentifierList))]
159160
idx = 0
160161
# Allowed list items
161-
fend1_funcs = [lambda t: isinstance(t, Identifier),
162+
fend1_funcs = [lambda t: isinstance(t, sql.Identifier),
162163
lambda t: t.is_whitespace(),
163164
lambda t: t.ttype == T.Wildcard,
164165
lambda t: t.match(T.Keyword, 'null'),
165166
lambda t: t.ttype == T.Number.Integer,
166167
lambda t: t.ttype == T.String.Single,
167-
lambda t: isinstance(t, Comparsion),
168+
lambda t: isinstance(t, sql.Comparsion),
168169
]
169170
tcomma = tlist.token_next_match(idx, T.Punctuation, ',')
170171
start = None
@@ -190,7 +191,7 @@ def group_identifier_list(tlist):
190191
if next_ is None or not next_.match(T.Punctuation, ','):
191192
# Reached the end of the list
192193
tokens = tlist.tokens_between(start, after)
193-
group = tlist.group_tokens(IdentifierList, tokens)
194+
group = tlist.group_tokens(sql.IdentifierList, tokens)
194195
start = None
195196
tcomma = tlist.token_next_match(tlist.token_index(group)+1,
196197
T.Punctuation, ',')
@@ -199,11 +200,12 @@ def group_identifier_list(tlist):
199200

200201

201202
def group_parenthesis(tlist):
202-
_group_matching(tlist, T.Punctuation, '(', T.Punctuation, ')', Parenthesis)
203+
_group_matching(tlist, T.Punctuation, '(', T.Punctuation, ')',
204+
sql.Parenthesis)
203205

204206
def group_comments(tlist):
205207
[group_comments(sgroup) for sgroup in tlist.get_sublists()
206-
if not isinstance(sgroup, Comment)]
208+
if not isinstance(sgroup, sql.Comment)]
207209
idx = 0
208210
token = tlist.token_next_by_type(idx, T.Comment)
209211
while token:
@@ -217,13 +219,13 @@ def group_comments(tlist):
217219
eidx = tlist.token_index(end)
218220
grp_tokens = tlist.tokens_between(token,
219221
tlist.token_prev(eidx, False))
220-
group = tlist.group_tokens(Comment, grp_tokens)
222+
group = tlist.group_tokens(sql.Comment, grp_tokens)
221223
idx = tlist.token_index(group)
222224
token = tlist.token_next_by_type(idx, T.Comment)
223225

224226
def group_where(tlist):
225227
[group_where(sgroup) for sgroup in tlist.get_sublists()
226-
if not isinstance(sgroup, Where)]
228+
if not isinstance(sgroup, sql.Where)]
227229
idx = 0
228230
token = tlist.token_next_match(idx, T.Keyword, 'WHERE')
229231
stopwords = ('ORDER', 'GROUP', 'LIMIT', 'UNION')
@@ -234,41 +236,42 @@ def group_where(tlist):
234236
end = tlist._groupable_tokens[-1]
235237
else:
236238
end = tlist.tokens[tlist.token_index(end)-1]
237-
group = tlist.group_tokens(Where, tlist.tokens_between(token, end))
239+
group = tlist.group_tokens(sql.Where,
240+
tlist.tokens_between(token, end))
238241
idx = tlist.token_index(group)
239242
token = tlist.token_next_match(idx, T.Keyword, 'WHERE')
240243

241244
def group_aliased(tlist):
242245
[group_aliased(sgroup) for sgroup in tlist.get_sublists()
243-
if not isinstance(sgroup, Identifier)]
246+
if not isinstance(sgroup, sql.Identifier)]
244247
idx = 0
245-
token = tlist.token_next_by_instance(idx, Identifier)
248+
token = tlist.token_next_by_instance(idx, sql.Identifier)
246249
while token:
247250
next_ = tlist.token_next(tlist.token_index(token))
248-
if next_ is not None and isinstance(next_, Identifier):
251+
if next_ is not None and isinstance(next_, sql.Identifier):
249252
grp = tlist.tokens_between(token, next_)[1:]
250253
token.tokens.extend(grp)
251254
for t in grp:
252255
tlist.tokens.remove(t)
253256
idx = tlist.token_index(token)+1
254-
token = tlist.token_next_by_instance(idx, Identifier)
257+
token = tlist.token_next_by_instance(idx, sql.Identifier)
255258

256259

257260
def group_typecasts(tlist):
258-
_group_left_right(tlist, T.Punctuation, '::', Identifier)
261+
_group_left_right(tlist, T.Punctuation, '::', sql.Identifier)
259262

260263

261264
def group_functions(tlist):
262265
[group_functions(sgroup) for sgroup in tlist.get_sublists()
263-
if not isinstance(sgroup, Function)]
266+
if not isinstance(sgroup, sql.Function)]
264267
idx = 0
265268
token = tlist.token_next_by_type(idx, T.Name)
266269
while token:
267270
next_ = tlist.token_next(token)
268-
if not isinstance(next_, Parenthesis):
271+
if not isinstance(next_, sql.Parenthesis):
269272
idx = tlist.token_index(token)+1
270273
else:
271-
func = tlist.group_tokens(Function,
274+
func = tlist.group_tokens(sql.Function,
272275
tlist.tokens_between(token, next_))
273276
idx = tlist.token_index(func)+1
274277
token = tlist.token_next_by_type(idx, T.Name)

sqlparse/filters.py

Lines changed: 43 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
import re
44

5-
from sqlparse.engine import grouping
65
from sqlparse import tokens as T
76
from sqlparse import sql
87

@@ -53,10 +52,9 @@ class IdentifierCaseFilter(_CaseFilter):
5352
class StripCommentsFilter(Filter):
5453

5554
def _process(self, tlist):
56-
idx = 0
5755
clss = set([x.__class__ for x in tlist.tokens])
58-
while grouping.Comment in clss:
59-
token = tlist.token_next_by_instance(0, grouping.Comment)
56+
while sql.Comment in clss:
57+
token = tlist.token_next_by_instance(0, sql.Comment)
6058
tidx = tlist.token_index(token)
6159
prev = tlist.token_prev(tidx, False)
6260
next_ = tlist.token_next(tidx, False)
@@ -66,7 +64,7 @@ def _process(self, tlist):
6664
and not prev.is_whitespace() and not next_.is_whitespace()
6765
and not (prev.match(T.Punctuation, '(')
6866
or next_.match(T.Punctuation, ')'))):
69-
tlist.tokens[tidx] = grouping.Token(T.Whitespace, ' ')
67+
tlist.tokens[tidx] = sql.Token(T.Whitespace, ' ')
7068
else:
7169
tlist.tokens.pop(tidx)
7270
clss = set([x.__class__ for x in tlist.tokens])
@@ -130,7 +128,7 @@ def _get_offset(self, token):
130128
def nl(self):
131129
# TODO: newline character should be configurable
132130
ws = '\n'+(self.char*((self.indent*self.width)+self.offset))
133-
return grouping.Token(T.Whitespace, ws)
131+
return sql.Token(T.Whitespace, ws)
134132

135133
def _split_kwds(self, tlist):
136134
split_words = ('FROM', 'JOIN$', 'AND', 'OR',
@@ -209,7 +207,6 @@ def _process_identifierlist(self, tlist):
209207
self._process_default(tlist)
210208

211209
def _process_case(self, tlist):
212-
cases = tlist.get_cases()
213210
is_first = True
214211
num_offset = None
215212
case = tlist.tokens[0]
@@ -245,17 +242,17 @@ def _process_default(self, tlist, stmts=True, kwds=True):
245242
[self._process(sgroup) for sgroup in tlist.get_sublists()]
246243

247244
def process(self, stack, stmt):
248-
if isinstance(stmt, grouping.Statement):
245+
if isinstance(stmt, sql.Statement):
249246
self._curr_stmt = stmt
250247
self._process(stmt)
251-
if isinstance(stmt, grouping.Statement):
248+
if isinstance(stmt, sql.Statement):
252249
if self._last_stmt is not None:
253250
if self._last_stmt.to_unicode().endswith('\n'):
254251
nl = '\n'
255252
else:
256253
nl = '\n\n'
257254
stmt.tokens.insert(0,
258-
grouping.Token(T.Whitespace, nl))
255+
sql.Token(T.Whitespace, nl))
259256
if self._last_stmt != stmt:
260257
self._last_stmt = stmt
261258

@@ -264,7 +261,7 @@ def process(self, stack, stmt):
264261
class RightMarginFilter(Filter):
265262

266263
keep_together = (
267-
# grouping.TypeCast, grouping.Identifier, grouping.Alias,
264+
# sql.TypeCast, sql.Identifier, sql.Alias,
268265
)
269266

270267
def __init__(self, width=79):
@@ -289,7 +286,7 @@ def _process(self, stack, group, stream):
289286
indent = match.group()
290287
else:
291288
indent = ''
292-
yield grouping.Token(T.Whitespace, '\n%s' % indent)
289+
yield sql.Token(T.Whitespace, '\n%s' % indent)
293290
self.line = indent
294291
self.line += val
295292
yield token
@@ -321,35 +318,35 @@ def __init__(self, varname='sql'):
321318

322319
def _process(self, stream, varname, count, has_nl):
323320
if count > 1:
324-
yield grouping.Token(T.Whitespace, '\n')
325-
yield grouping.Token(T.Name, varname)
326-
yield grouping.Token(T.Whitespace, ' ')
327-
yield grouping.Token(T.Operator, '=')
328-
yield grouping.Token(T.Whitespace, ' ')
321+
yield sql.Token(T.Whitespace, '\n')
322+
yield sql.Token(T.Name, varname)
323+
yield sql.Token(T.Whitespace, ' ')
324+
yield sql.Token(T.Operator, '=')
325+
yield sql.Token(T.Whitespace, ' ')
329326
if has_nl:
330-
yield grouping.Token(T.Operator, '(')
331-
yield grouping.Token(T.Text, "'")
327+
yield sql.Token(T.Operator, '(')
328+
yield sql.Token(T.Text, "'")
332329
cnt = 0
333330
for token in stream:
334331
cnt += 1
335332
if token.is_whitespace() and '\n' in token.value:
336333
if cnt == 1:
337334
continue
338335
after_lb = token.value.split('\n', 1)[1]
339-
yield grouping.Token(T.Text, " '")
340-
yield grouping.Token(T.Whitespace, '\n')
336+
yield sql.Token(T.Text, " '")
337+
yield sql.Token(T.Whitespace, '\n')
341338
for i in range(len(varname)+4):
342-
yield grouping.Token(T.Whitespace, ' ')
343-
yield grouping.Token(T.Text, "'")
339+
yield sql.Token(T.Whitespace, ' ')
340+
yield sql.Token(T.Text, "'")
344341
if after_lb: # it's the indendation
345-
yield grouping.Token(T.Whitespace, after_lb)
342+
yield sql.Token(T.Whitespace, after_lb)
346343
continue
347344
elif token.value and "'" in token.value:
348345
token.value = token.value.replace("'", "\\'")
349-
yield grouping.Token(T.Text, token.value or '')
350-
yield grouping.Token(T.Text, "'")
346+
yield sql.Token(T.Text, token.value or '')
347+
yield sql.Token(T.Text, "'")
351348
if has_nl:
352-
yield grouping.Token(T.Operator, ')')
349+
yield sql.Token(T.Operator, ')')
353350

354351
def process(self, stack, stmt):
355352
self.cnt += 1
@@ -370,36 +367,32 @@ def __init__(self, varname='sql'):
370367

371368
def _process(self, stream, varname):
372369
if self.count > 1:
373-
yield grouping.Token(T.Whitespace, '\n')
374-
yield grouping.Token(T.Name, varname)
375-
yield grouping.Token(T.Whitespace, ' ')
376-
yield grouping.Token(T.Operator, '=')
377-
yield grouping.Token(T.Whitespace, ' ')
378-
yield grouping.Token(T.Text, '"')
379-
cnt = 0
370+
yield sql.Token(T.Whitespace, '\n')
371+
yield sql.Token(T.Name, varname)
372+
yield sql.Token(T.Whitespace, ' ')
373+
yield sql.Token(T.Operator, '=')
374+
yield sql.Token(T.Whitespace, ' ')
375+
yield sql.Token(T.Text, '"')
380376
for token in stream:
381377
if token.is_whitespace() and '\n' in token.value:
382-
# cnt += 1
383-
# if cnt == 1:
384-
# continue
385378
after_lb = token.value.split('\n', 1)[1]
386-
yield grouping.Token(T.Text, ' "')
387-
yield grouping.Token(T.Operator, ';')
388-
yield grouping.Token(T.Whitespace, '\n')
389-
yield grouping.Token(T.Name, varname)
390-
yield grouping.Token(T.Whitespace, ' ')
391-
yield grouping.Token(T.Punctuation, '.')
392-
yield grouping.Token(T.Operator, '=')
393-
yield grouping.Token(T.Whitespace, ' ')
394-
yield grouping.Token(T.Text, '"')
379+
yield sql.Token(T.Text, ' "')
380+
yield sql.Token(T.Operator, ';')
381+
yield sql.Token(T.Whitespace, '\n')
382+
yield sql.Token(T.Name, varname)
383+
yield sql.Token(T.Whitespace, ' ')
384+
yield sql.Token(T.Punctuation, '.')
385+
yield sql.Token(T.Operator, '=')
386+
yield sql.Token(T.Whitespace, ' ')
387+
yield sql.Token(T.Text, '"')
395388
if after_lb:
396-
yield grouping.Token(T.Text, after_lb)
389+
yield sql.Token(T.Text, after_lb)
397390
continue
398391
elif '"' in token.value:
399392
token.value = token.value.replace('"', '\\"')
400-
yield grouping.Token(T.Text, token.value)
401-
yield grouping.Token(T.Text, '"')
402-
yield grouping.Token(T.Punctuation, ';')
393+
yield sql.Token(T.Text, token.value)
394+
yield sql.Token(T.Text, '"')
395+
yield sql.Token(T.Punctuation, ';')
403396

404397
def process(self, stack, stmt):
405398
self.count += 1

0 commit comments

Comments
 (0)