summaryrefslogtreecommitdiff
path: root/sqlparse
diff options
context:
space:
mode:
authorAndi Albrecht <albrecht.andi@gmail.com>2015-01-17 12:47:03 +0100
committerAndi Albrecht <albrecht.andi@gmail.com>2015-01-17 12:47:03 +0100
commit71af186659923dfe8721c551d5dbf4db7c4854d9 (patch)
tree15be117fe01d992811a9b84b0269deeca80f73a1 /sqlparse
parent29521dad3cc458f5e551cafdef2c5ce52fc01f78 (diff)
downloadsqlparse-71af186659923dfe8721c551d5dbf4db7c4854d9.tar.gz
Remove six dependency.
We only use very little of six. That isn't worth to add an extra dependency.
Diffstat (limited to 'sqlparse')
-rw-r--r--sqlparse/__init__.py7
-rw-r--r--sqlparse/compat.py39
-rw-r--r--sqlparse/engine/grouping.py3
-rw-r--r--sqlparse/filters.py33
-rw-r--r--sqlparse/lexer.py20
-rw-r--r--sqlparse/sql.py21
-rw-r--r--sqlparse/utils.py3
7 files changed, 81 insertions, 45 deletions
diff --git a/sqlparse/__init__.py b/sqlparse/__init__.py
index 2537d5e..79dd83f 100644
--- a/sqlparse/__init__.py
+++ b/sqlparse/__init__.py
@@ -8,10 +8,8 @@
__version__ = '0.1.14'
-import six
-
-
# Setup namespace
+from sqlparse import compat
from sqlparse import engine
from sqlparse import filters
from sqlparse import formatter
@@ -66,7 +64,8 @@ def split(sql, encoding=None):
"""
stack = engine.FilterStack()
stack.split_statements = True
- return [six.text_type(stmt).strip() for stmt in stack.run(sql, encoding)]
+ return [compat.text_type(stmt).strip()
+ for stmt in stack.run(sql, encoding)]
from sqlparse.engine.filter import StatementFilter
diff --git a/sqlparse/compat.py b/sqlparse/compat.py
new file mode 100644
index 0000000..1849c13
--- /dev/null
+++ b/sqlparse/compat.py
@@ -0,0 +1,39 @@
+"""Python 2/3 compatibility.
+
+This module only exists to avoid a dependency on six
+for very trivial stuff. We only need to take care regarding
+string types and buffers.
+"""
+
+import sys
+
+PY2 = sys.version_info[0] == 2
+PY3 = sys.version_info[0] == 3
+
+if PY3:
+ text_type = str
+ string_types = (str,)
+ from io import StringIO
+
+ def u(s):
+ return s
+
+elif PY2:
+ text_type = unicode
+ string_types = (basestring,)
+ from StringIO import StringIO # flake8: noqa
+
+ def u(s):
+ return unicode(s, 'unicode_escape')
+
+
+# Directly copied from six:
+def with_metaclass(meta, *bases):
+ """Create a base class with a metaclass."""
+ # This requires a bit of explanation: the basic idea is to make a dummy
+ # metaclass for one level of class instantiation that replaces itself with
+ # the actual metaclass.
+ class metaclass(meta):
+ def __new__(cls, name, this_bases, d):
+ return meta(name, bases, d)
+ return type.__new__(metaclass, 'temporary_class', (), {})
diff --git a/sqlparse/engine/grouping.py b/sqlparse/engine/grouping.py
index cebf8dc..6aa9e18 100644
--- a/sqlparse/engine/grouping.py
+++ b/sqlparse/engine/grouping.py
@@ -402,6 +402,5 @@ def group(tlist):
group_if,
group_for,
group_foreach,
- group_begin,
- ]:
+ group_begin]:
func(tlist)
diff --git a/sqlparse/filters.py b/sqlparse/filters.py
index aaeb15e..697f22b 100644
--- a/sqlparse/filters.py
+++ b/sqlparse/filters.py
@@ -4,9 +4,7 @@ import re
from os.path import abspath, join
-import six
-
-from sqlparse import sql, tokens as T
+from sqlparse import compat, sql, tokens as T
from sqlparse.engine import FilterStack
from sqlparse.pipeline import Pipeline
from sqlparse.tokens import (Comment, Comparison, Keyword, Name, Punctuation,
@@ -26,7 +24,7 @@ class _CaseFilter:
if case is None:
case = 'upper'
assert case in ['lower', 'upper', 'capitalize']
- self.convert = getattr(six.text_type, case)
+ self.convert = getattr(compat.text_type, case)
def process(self, stack, stream):
for ttype, value in stream:
@@ -53,19 +51,19 @@ class TruncateStringFilter:
def __init__(self, width, char):
self.width = max(width, 1)
- self.char = six.text_type(char)
+ self.char = compat.text_type(char)
def process(self, stack, stream):
for ttype, value in stream:
if ttype is T.Literal.String.Single:
if value[:2] == '\'\'':
inner = value[2:-2]
- quote = six.text_type('\'\'')
+ quote = compat.text_type('\'\'')
else:
inner = value[1:-1]
- quote = six.text_type('\'')
+ quote = compat.text_type('\'')
if len(inner) > self.width:
- value = six.text_type('').join(
+ value = compat.text_type('').join(
(quote, inner[:self.width], self.char, quote))
yield ttype, value
@@ -160,7 +158,8 @@ class IncludeStatement:
raise
# Put the exception as a comment on the SQL code
- yield Comment, six.text_type('-- IOError: %s\n' % err)
+ yield Comment, compat.text_type(
+ '-- IOError: %s\n' % err)
else:
# Create new FilterStack to parse readed file
@@ -177,7 +176,7 @@ class IncludeStatement:
raise
# Put the exception as a comment on the SQL code
- yield Comment, six.text_type(
+ yield Comment, compat.text_type(
'-- ValueError: %s\n' % err)
stack = FilterStack()
@@ -297,7 +296,7 @@ class ReindentFilter:
raise StopIteration
def _get_offset(self, token):
- raw = ''.join(map(six.text_type, self._flatten_up_to_token(token)))
+ raw = ''.join(map(compat.text_type, self._flatten_up_to_token(token)))
line = raw.splitlines()[-1]
# Now take current offset into account and return relative offset.
full_offset = len(line) - len(self.char * (self.width * self.indent))
@@ -337,7 +336,7 @@ class ReindentFilter:
if prev and prev.is_whitespace() and prev not in added:
tlist.tokens.pop(tlist.token_index(prev))
offset += 1
- uprev = six.text_type(prev)
+ uprev = compat.text_type(prev)
if (prev and (uprev.endswith('\n') or uprev.endswith('\r'))):
nl = tlist.token_next(token)
else:
@@ -458,7 +457,7 @@ class ReindentFilter:
self._process(stmt)
if isinstance(stmt, sql.Statement):
if self._last_stmt is not None:
- if six.text_type(self._last_stmt).endswith('\n'):
+ if compat.text_type(self._last_stmt).endswith('\n'):
nl = '\n'
else:
nl = '\n\n'
@@ -490,7 +489,7 @@ class RightMarginFilter:
and token.__class__ not in self.keep_together):
token.tokens = self._process(stack, token, token.tokens)
else:
- val = six.text_type(token)
+ val = compat.text_type(token)
if len(self.line) + len(val) > self.width:
match = re.search('^ +', self.line)
if match is not None:
@@ -564,7 +563,7 @@ class ColumnsSelect:
class SerializerUnicode:
def process(self, stack, stmt):
- raw = six.text_type(stmt)
+ raw = compat.text_type(stmt)
lines = split_unquoted_newlines(raw)
res = '\n'.join(line.rstrip() for line in lines)
return res
@@ -574,7 +573,7 @@ def Tokens2Unicode(stream):
result = ""
for _, value in stream:
- result += six.text_type(value)
+ result += compat.text_type(value)
return result
@@ -596,7 +595,7 @@ class OutputFilter:
else:
varname = self.varname
- has_nl = len(six.text_type(stmt).strip().splitlines()) > 1
+ has_nl = len(compat.text_type(stmt).strip().splitlines()) > 1
stmt.tokens = self._process(stmt.tokens, varname, has_nl)
return stmt
diff --git a/sqlparse/lexer.py b/sqlparse/lexer.py
index 37eb57b..ac50442 100644
--- a/sqlparse/lexer.py
+++ b/sqlparse/lexer.py
@@ -15,10 +15,9 @@
import re
import sys
-import six
-from six import StringIO
-
+from sqlparse import compat
from sqlparse import tokens
+from sqlparse.compat import StringIO
from sqlparse.keywords import KEYWORDS, KEYWORDS_COMMON
@@ -132,7 +131,7 @@ class LexerMeta(type):
return type.__call__(cls, *args, **kwds)
-class Lexer(six.with_metaclass(LexerMeta)):
+class Lexer(compat.with_metaclass(LexerMeta)):
encoding = 'utf-8'
stripall = False
@@ -174,7 +173,8 @@ class Lexer(six.with_metaclass(LexerMeta)):
# not a real string literal in ANSI SQL:
(r'(""|".*?[^\\]")', tokens.String.Symbol),
(r'(\[[^\]]+\])', tokens.Name),
- (r'((LEFT\s+|RIGHT\s+|FULL\s+)?(INNER\s+|OUTER\s+|STRAIGHT\s+)?|(CROSS\s+|NATURAL\s+)?)?JOIN\b', tokens.Keyword),
+ ((r'((LEFT\s+|RIGHT\s+|FULL\s+)?(INNER\s+|OUTER\s+|STRAIGHT\s+)?'
+ r'|(CROSS\s+|NATURAL\s+)?)?JOIN\b'), tokens.Keyword),
(r'END(\s+IF|\s+LOOP)?\b', tokens.Keyword),
(r'NOT NULL\b', tokens.Keyword),
(r'CREATE(\s+OR\s+REPLACE)?\b', tokens.Keyword.DDL),
@@ -207,8 +207,8 @@ class Lexer(six.with_metaclass(LexerMeta)):
if self.encoding == 'guess':
try:
text = text.decode('utf-8')
- if text.startswith(six.text_type('\ufeff')):
- text = text[len(six.text_type('\ufeff')):]
+ if text.startswith(compat.text_type('\ufeff')):
+ text = text[len(compat.text_type('\ufeff')):]
except UnicodeDecodeError:
text = text.decode('latin1')
else:
@@ -230,13 +230,13 @@ class Lexer(six.with_metaclass(LexerMeta)):
Also preprocess the text, i.e. expand tabs and strip it if
wanted and applies registered filters.
"""
- if isinstance(text, six.string_types):
+ if isinstance(text, compat.string_types):
if self.stripall:
text = text.strip()
elif self.stripnl:
text = text.strip('\n')
- if six.PY2 and isinstance(text, six.text_type):
+ if compat.PY2 and isinstance(text, compat.text_type):
text = StringIO(text.encode('utf-8'))
self.encoding = 'utf-8'
else:
@@ -309,7 +309,7 @@ class Lexer(six.with_metaclass(LexerMeta)):
pos += 1
statestack = ['root']
statetokens = tokendefs['root']
- yield pos, tokens.Text, six.text_type('\n')
+ yield pos, tokens.Text, compat.text_type('\n')
continue
yield pos, tokens.Error, text[pos]
pos += 1
diff --git a/sqlparse/sql.py b/sqlparse/sql.py
index f3a4820..17509eb 100644
--- a/sqlparse/sql.py
+++ b/sqlparse/sql.py
@@ -5,8 +5,7 @@
import re
import sys
-import six
-
+from sqlparse import compat
from sqlparse import tokens as T
@@ -34,7 +33,7 @@ class Token(object):
if sys.version_info[0] == 3:
return self.value
else:
- return six.text_type(self).encode('utf-8')
+ return compat.text_type(self).encode('utf-8')
def __repr__(self):
short = self._get_repr_value()
@@ -53,15 +52,15 @@ class Token(object):
.. deprecated:: 0.1.5
Use ``unicode(token)`` (for Python 3: ``str(token)``) instead.
"""
- return six.text_type(self)
+ return compat.text_type(self)
def _get_repr_name(self):
return str(self.ttype).split('.')[-1]
def _get_repr_value(self):
- raw = six.text_type(self)
+ raw = compat.text_type(self)
if len(raw) > 7:
- raw = raw[:6] + six.text_type('...')
+ raw = raw[:6] + compat.text_type('...')
return re.sub('\s+', ' ', raw)
def flatten(self):
@@ -85,7 +84,7 @@ class Token(object):
return type_matched
if regex:
- if isinstance(values, six.string_types):
+ if isinstance(values, compat.string_types):
values = set([values])
if self.ttype is T.Keyword:
@@ -98,7 +97,7 @@ class Token(object):
return True
return False
- if isinstance(values, six.string_types):
+ if isinstance(values, compat.string_types):
if self.is_keyword:
return values.upper() == self.normalized
return values == self.value
@@ -174,7 +173,7 @@ class TokenList(Token):
if sys.version_info[0] == 3:
return ''.join(x.value for x in self.flatten())
else:
- return ''.join(six.text_type(x) for x in self.flatten())
+ return ''.join(compat.text_type(x) for x in self.flatten())
def _get_repr_name(self):
return self.__class__.__name__
@@ -398,7 +397,7 @@ class TokenList(Token):
alias = next_
if isinstance(alias, Identifier):
return alias.get_name()
- return self._remove_quotes(six.text_type(alias))
+ return self._remove_quotes(compat.text_type(alias))
def get_name(self):
"""Returns the name of this identifier.
@@ -487,7 +486,7 @@ class Identifier(TokenList):
next_ = self.token_next(self.token_index(marker), False)
if next_ is None:
return None
- return six.text_type(next_)
+ return compat.text_type(next_)
def get_ordering(self):
"""Returns the ordering or ``None`` as uppercase string."""
diff --git a/sqlparse/utils.py b/sqlparse/utils.py
index 0bafce1..7595e9d 100644
--- a/sqlparse/utils.py
+++ b/sqlparse/utils.py
@@ -96,6 +96,7 @@ SPLIT_REGEX = re.compile(r"""
LINE_MATCH = re.compile(r'(\r\n|\r|\n)')
+
def split_unquoted_newlines(text):
"""Split a string on all unquoted newlines.
@@ -110,4 +111,4 @@ def split_unquoted_newlines(text):
outputlines.append('')
else:
outputlines[-1] += line
- return outputlines \ No newline at end of file
+ return outputlines