diff options
| author | Victor Uriarte <victor.m.uriarte@intel.com> | 2016-05-30 10:49:24 -0700 |
|---|---|---|
| committer | Victor Uriarte <victor.m.uriarte@intel.com> | 2016-06-04 15:24:19 -0700 |
| commit | 658f13aacfd3b94398aaaf14f567564a7c1ec1de (patch) | |
| tree | a0f3854edee83d93a428fd0de03ff3a1ab961c36 /sqlparse/sql.py | |
| parent | 149519305864a6455638a5fa0984a99c3e54f1f3 (diff) | |
| download | sqlparse-658f13aacfd3b94398aaaf14f567564a7c1ec1de.tar.gz | |
Clean-up code style sql.py
other items inside slots are already defined in parent class
Diffstat (limited to 'sqlparse/sql.py')
| -rw-r--r-- | sqlparse/sql.py | 66 |
1 files changed, 31 insertions, 35 deletions
diff --git a/sqlparse/sql.py b/sqlparse/sql.py index 5f353b5..447de7e 100644 --- a/sqlparse/sql.py +++ b/sqlparse/sql.py @@ -11,7 +11,7 @@ from __future__ import print_function import re from sqlparse import tokens as T -from sqlparse.compat import u, string_types, unicode_compatible +from sqlparse.compat import string_types, text_type, unicode_compatible from sqlparse.utils import imt, remove_quotes @@ -27,7 +27,7 @@ class Token(object): __slots__ = ('value', 'ttype', 'parent', 'normalized', 'is_keyword') def __init__(self, ttype, value): - value = u(value) + value = text_type(value) self.value = value self.ttype = ttype self.parent = None @@ -46,7 +46,7 @@ class Token(object): return str(self.ttype).split('.')[-1] def _get_repr_value(self): - raw = self.value + raw = text_type(self) if len(raw) > 7: raw = raw[:6] + '...' return re.sub(r'\s+', ' ', raw) @@ -104,7 +104,7 @@ class Token(object): def is_whitespace(self): """Return ``True`` if this token is a whitespace token.""" - return self.ttype and self.ttype in T.Whitespace + return self.ttype in T.Whitespace def within(self, group_cls): """Returns ``True`` if this token is within *group_cls*. @@ -141,17 +141,17 @@ class TokenList(Token): list of child-tokens. """ - __slots__ = ('value', 'ttype', 'tokens') + __slots__ = 'tokens' def __init__(self, tokens=None): self.tokens = tokens or [] - super(TokenList, self).__init__(None, self.__str__()) + super(TokenList, self).__init__(None, text_type(self)) def __str__(self): return ''.join(token.value for token in self.flatten()) def _get_repr_name(self): - return self.__class__.__name__ + return type(self).__name__ def _pprint_tree(self, max_depth=None, depth=0, f=None): """Pretty-print the object tree.""" @@ -180,7 +180,7 @@ class TokenList(Token): This method is recursively called for all child tokens. """ for token in self.tokens: - if isinstance(token, TokenList): + if token.is_group(): for item in token.flatten(): yield item else: @@ -198,7 +198,7 @@ class TokenList(Token): def get_sublists(self): for token in self.tokens: - if isinstance(token, TokenList): + if token.is_group(): yield token @property @@ -217,7 +217,7 @@ class TokenList(Token): funcs = (funcs,) if reverse: - iterable = iter(reversed(self.tokens[end:start - 1])) + iterable = reversed(self.tokens[end:start - 1]) else: iterable = self.tokens[start:end] @@ -327,7 +327,7 @@ class TokenList(Token): left.parent = self tokens = tokens[1:] left.tokens.extend(tokens) - left.value = left.__str__() + left.value = str(left) else: left = grp_cls(tokens) @@ -415,8 +415,6 @@ class TokenList(Token): class Statement(TokenList): """Represents a SQL statement.""" - __slots__ = ('value', 'ttype', 'tokens') - def get_type(self): """Returns the type of a statement. @@ -440,16 +438,14 @@ class Statement(TokenList): # The WITH keyword should be followed by either an Identifier or # an IdentifierList containing the CTE definitions; the actual # DML keyword (e.g. SELECT, INSERT) will follow next. - idents = self.token_next( - self.token_index(first_token), skip_ws=True) - if isinstance(idents, (Identifier, IdentifierList)): - dml_keyword = self.token_next( - self.token_index(idents), skip_ws=True) + token = self.token_next(first_token, skip_ws=True) + if isinstance(token, (Identifier, IdentifierList)): + dml_keyword = self.token_next(token, skip_ws=True) + if dml_keyword.ttype == T.Keyword.DML: return dml_keyword.normalized - # Hmm, probably invalid syntax, so return unknown. - return 'UNKNOWN' + # Hmm, probably invalid syntax, so return unknown. return 'UNKNOWN' @@ -505,8 +501,8 @@ class IdentifierList(TokenList): class Parenthesis(TokenList): """Tokens between parenthesis.""" - M_OPEN = (T.Punctuation, '(') - M_CLOSE = (T.Punctuation, ')') + M_OPEN = T.Punctuation, '(' + M_CLOSE = T.Punctuation, ')' @property def _groupable_tokens(self): @@ -515,8 +511,8 @@ class Parenthesis(TokenList): class SquareBrackets(TokenList): """Tokens between square brackets""" - M_OPEN = (T.Punctuation, '[') - M_CLOSE = (T.Punctuation, ']') + M_OPEN = T.Punctuation, '[' + M_CLOSE = T.Punctuation, ']' @property def _groupable_tokens(self): @@ -529,14 +525,14 @@ class Assignment(TokenList): class If(TokenList): """An 'if' clause with possible 'else if' or 'else' parts.""" - M_OPEN = (T.Keyword, 'IF') - M_CLOSE = (T.Keyword, 'END IF') + M_OPEN = T.Keyword, 'IF' + M_CLOSE = T.Keyword, 'END IF' class For(TokenList): """A 'FOR' loop.""" - M_OPEN = (T.Keyword, ('FOR', 'FOREACH')) - M_CLOSE = (T.Keyword, 'END LOOP') + M_OPEN = T.Keyword, ('FOR', 'FOREACH') + M_CLOSE = T.Keyword, 'END LOOP' class Comparison(TokenList): @@ -560,15 +556,15 @@ class Comment(TokenList): class Where(TokenList): """A WHERE clause.""" - M_OPEN = (T.Keyword, 'WHERE') - M_CLOSE = (T.Keyword, - ('ORDER', 'GROUP', 'LIMIT', 'UNION', 'EXCEPT', 'HAVING')) + M_OPEN = T.Keyword, 'WHERE' + M_CLOSE = T.Keyword, ('ORDER', 'GROUP', 'LIMIT', 'UNION', 'EXCEPT', + 'HAVING') class Case(TokenList): """A CASE statement with one or more WHEN and possibly an ELSE part.""" - M_OPEN = (T.Keyword, 'CASE') - M_CLOSE = (T.Keyword, 'END') + M_OPEN = T.Keyword, 'CASE' + M_CLOSE = T.Keyword, 'END' def get_cases(self): """Returns a list of 2-tuples (condition, value). @@ -631,5 +627,5 @@ class Function(TokenList): class Begin(TokenList): """A BEGIN/END block.""" - M_OPEN = (T.Keyword, 'BEGIN') - M_CLOSE = (T.Keyword, 'END') + M_OPEN = T.Keyword, 'BEGIN' + M_CLOSE = T.Keyword, 'END' |
