diff options
| author | Andi Albrecht <albrecht.andi@gmail.com> | 2020-09-13 08:54:12 +0200 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2020-09-13 08:54:12 +0200 |
| commit | 6cb3821bf3b048c7b4182c3d87432c433a5f1a57 (patch) | |
| tree | 2a3849c872dd7cbcda0d92323811394049ca03a5 /sqlparse | |
| parent | db4a9f49286b13a4e9deb810645dd5fc1400b804 (diff) | |
| parent | 686bf6d3c5b2598952a752e708e5d0419fa64781 (diff) | |
| download | sqlparse-6cb3821bf3b048c7b4182c3d87432c433a5f1a57.tar.gz | |
Merge branch 'master' into w503
Diffstat (limited to 'sqlparse')
| -rw-r--r-- | sqlparse/__init__.py | 6 | ||||
| -rw-r--r-- | sqlparse/__main__.py | 1 | ||||
| -rwxr-xr-x | sqlparse/cli.py | 40 | ||||
| -rw-r--r-- | sqlparse/compat.py | 45 | ||||
| -rw-r--r-- | sqlparse/engine/__init__.py | 1 | ||||
| -rw-r--r-- | sqlparse/engine/filter_stack.py | 3 | ||||
| -rw-r--r-- | sqlparse/engine/grouping.py | 1 | ||||
| -rw-r--r-- | sqlparse/engine/statement_splitter.py | 3 | ||||
| -rw-r--r-- | sqlparse/exceptions.py | 1 | ||||
| -rw-r--r-- | sqlparse/filters/__init__.py | 1 | ||||
| -rw-r--r-- | sqlparse/filters/aligned_indent.py | 11 | ||||
| -rw-r--r-- | sqlparse/filters/others.py | 9 | ||||
| -rw-r--r-- | sqlparse/filters/output.py | 8 | ||||
| -rw-r--r-- | sqlparse/filters/reindent.py | 10 | ||||
| -rw-r--r-- | sqlparse/filters/right_margin.py | 8 | ||||
| -rw-r--r-- | sqlparse/filters/tokens.py | 8 | ||||
| -rw-r--r-- | sqlparse/formatter.py | 27 | ||||
| -rw-r--r-- | sqlparse/keywords.py | 1 | ||||
| -rw-r--r-- | sqlparse/lexer.py | 12 | ||||
| -rw-r--r-- | sqlparse/sql.py | 34 | ||||
| -rw-r--r-- | sqlparse/tokens.py | 1 | ||||
| -rw-r--r-- | sqlparse/utils.py | 4 |
22 files changed, 76 insertions, 159 deletions
diff --git a/sqlparse/__init__.py b/sqlparse/__init__.py index b475061..5dcd982 100644 --- a/sqlparse/__init__.py +++ b/sqlparse/__init__.py @@ -1,4 +1,3 @@ -# -*- coding: utf-8 -*- # # Copyright (C) 2009-2018 the sqlparse authors and contributors # <see AUTHORS file> @@ -16,7 +15,6 @@ from sqlparse import tokens from sqlparse import filters from sqlparse import formatter -from sqlparse.compat import text_type __version__ = '0.3.2.dev0' __all__ = ['engine', 'filters', 'formatter', 'sql', 'tokens', 'cli'] @@ -58,7 +56,7 @@ def format(sql, encoding=None, **options): options = formatter.validate_options(options) stack = formatter.build_filter_stack(stack, options) stack.postprocess.append(filters.SerializerUnicode()) - return u''.join(stack.run(sql, encoding)) + return ''.join(stack.run(sql, encoding)) def split(sql, encoding=None): @@ -69,4 +67,4 @@ def split(sql, encoding=None): :returns: A list of strings. """ stack = engine.FilterStack() - return [text_type(stmt).strip() for stmt in stack.run(sql, encoding)] + return [str(stmt).strip() for stmt in stack.run(sql, encoding)] diff --git a/sqlparse/__main__.py b/sqlparse/__main__.py index 867d75d..3e6d325 100644 --- a/sqlparse/__main__.py +++ b/sqlparse/__main__.py @@ -1,5 +1,4 @@ #!/usr/bin/env python -# -*- coding: utf-8 -*- # # Copyright (C) 2009-2018 the sqlparse authors and contributors # <see AUTHORS file> diff --git a/sqlparse/cli.py b/sqlparse/cli.py index 25555a5..957d220 100755 --- a/sqlparse/cli.py +++ b/sqlparse/cli.py @@ -1,5 +1,4 @@ #!/usr/bin/env python -# -*- coding: utf-8 -*- # # Copyright (C) 2009-2018 the sqlparse authors and contributors # <see AUTHORS file> @@ -23,10 +22,8 @@ Why does this file exist, and why not put this in __main__? import argparse import sys from io import TextIOWrapper -from codecs import open, getreader import sqlparse -from sqlparse.compat import PY2 from sqlparse.exceptions import SQLParseError @@ -62,16 +59,16 @@ def create_parser(): metavar='CHOICE', dest='keyword_case', choices=_CASE_CHOICES, - help='change case of keywords, CHOICE is one of {0}'.format( - ', '.join('"{0}"'.format(x) for x in _CASE_CHOICES))) + help='change case of keywords, CHOICE is one of {}'.format( + ', '.join('"{}"'.format(x) for x in _CASE_CHOICES))) group.add_argument( '-i', '--identifiers', metavar='CHOICE', dest='identifier_case', choices=_CASE_CHOICES, - help='change case of identifiers, CHOICE is one of {0}'.format( - ', '.join('"{0}"'.format(x) for x in _CASE_CHOICES))) + help='change case of identifiers, CHOICE is one of {}'.format( + ', '.join('"{}"'.format(x) for x in _CASE_CHOICES))) group.add_argument( '-l', '--language', @@ -153,7 +150,7 @@ def create_parser(): def _error(msg): """Print msg and optionally exit with return code exit_.""" - sys.stderr.write(u'[ERROR] {0}\n'.format(msg)) + sys.stderr.write('[ERROR] {}\n'.format(msg)) return 1 @@ -162,29 +159,26 @@ def main(args=None): args = parser.parse_args(args) if args.filename == '-': # read from stdin - if PY2: - data = getreader(args.encoding)(sys.stdin).read() - else: - wrapper = TextIOWrapper(sys.stdin.buffer, encoding=args.encoding) - try: - data = wrapper.read() - finally: - wrapper.detach() + wrapper = TextIOWrapper(sys.stdin.buffer, encoding=args.encoding) + try: + data = wrapper.read() + finally: + wrapper.detach() else: try: - with open(args.filename, 'r', args.encoding) as f: + with open(args.filename, encoding=args.encoding) as f: data = ''.join(f.readlines()) - except IOError as e: + except OSError as e: return _error( - u'Failed to read {0}: {1}'.format(args.filename, e)) + 'Failed to read {}: {}'.format(args.filename, e)) close_stream = False if args.outfile: try: - stream = open(args.outfile, 'w', args.encoding) + stream = open(args.outfile, 'w', encoding=args.encoding) close_stream = True - except IOError as e: - return _error(u'Failed to open {0}: {1}'.format(args.outfile, e)) + except OSError as e: + return _error('Failed to open {}: {}'.format(args.outfile, e)) else: stream = sys.stdout @@ -192,7 +186,7 @@ def main(args=None): try: formatter_opts = sqlparse.formatter.validate_options(formatter_opts) except SQLParseError as e: - return _error(u'Invalid options: {0}'.format(e)) + return _error('Invalid options: {}'.format(e)) s = sqlparse.format(data, **formatter_opts) stream.write(s) diff --git a/sqlparse/compat.py b/sqlparse/compat.py deleted file mode 100644 index d2214be..0000000 --- a/sqlparse/compat.py +++ /dev/null @@ -1,45 +0,0 @@ -# -*- coding: utf-8 -*- -# -# Copyright (C) 2009-2018 the sqlparse authors and contributors -# <see AUTHORS file> -# -# This module is part of python-sqlparse and is released under -# the BSD License: https://opensource.org/licenses/BSD-3-Clause - -"""Python 2/3 compatibility. - -This module only exists to avoid a dependency on six -for very trivial stuff. We only need to take care of -string types, buffers and metaclasses. - -Parts of the code is copied directly from six: -https://bitbucket.org/gutworth/six -""" - -import sys -from io import TextIOBase - -PY2 = sys.version_info[0] == 2 -PY3 = sys.version_info[0] == 3 - - -if PY3: - def unicode_compatible(cls): - return cls - - text_type = str - string_types = (str,) - from io import StringIO - file_types = (StringIO, TextIOBase) - - -elif PY2: - def unicode_compatible(cls): - cls.__unicode__ = cls.__str__ - cls.__str__ = lambda x: x.__unicode__().encode('utf-8') - return cls - - text_type = unicode - string_types = (str, unicode,) - from StringIO import StringIO - file_types = (file, StringIO, TextIOBase) diff --git a/sqlparse/engine/__init__.py b/sqlparse/engine/__init__.py index 0b3f3eb..67ecc6e 100644 --- a/sqlparse/engine/__init__.py +++ b/sqlparse/engine/__init__.py @@ -1,4 +1,3 @@ -# -*- coding: utf-8 -*- # # Copyright (C) 2009-2018 the sqlparse authors and contributors # <see AUTHORS file> diff --git a/sqlparse/engine/filter_stack.py b/sqlparse/engine/filter_stack.py index fc77fd6..295c2c1 100644 --- a/sqlparse/engine/filter_stack.py +++ b/sqlparse/engine/filter_stack.py @@ -1,4 +1,3 @@ -# -*- coding: utf-8 -*- # # Copyright (C) 2009-2018 the sqlparse authors and contributors # <see AUTHORS file> @@ -13,7 +12,7 @@ from sqlparse.engine import grouping from sqlparse.engine.statement_splitter import StatementSplitter -class FilterStack(object): +class FilterStack: def __init__(self): self.preprocess = [] self.stmtprocess = [] diff --git a/sqlparse/engine/grouping.py b/sqlparse/engine/grouping.py index e7a4211..e895ce0 100644 --- a/sqlparse/engine/grouping.py +++ b/sqlparse/engine/grouping.py @@ -1,4 +1,3 @@ -# -*- coding: utf-8 -*- # # Copyright (C) 2009-2018 the sqlparse authors and contributors # <see AUTHORS file> diff --git a/sqlparse/engine/statement_splitter.py b/sqlparse/engine/statement_splitter.py index 1e9af3c..fb22c65 100644 --- a/sqlparse/engine/statement_splitter.py +++ b/sqlparse/engine/statement_splitter.py @@ -1,4 +1,3 @@ -# -*- coding: utf-8 -*- # # Copyright (C) 2009-2018 the sqlparse authors and contributors # <see AUTHORS file> @@ -9,7 +8,7 @@ from sqlparse import sql, tokens as T -class StatementSplitter(object): +class StatementSplitter: """Filter that split stream at individual statements""" def __init__(self): diff --git a/sqlparse/exceptions.py b/sqlparse/exceptions.py index 01e60f7..687c20c 100644 --- a/sqlparse/exceptions.py +++ b/sqlparse/exceptions.py @@ -1,4 +1,3 @@ -# -*- coding: utf-8 -*- # # Copyright (C) 2009-2018 the sqlparse authors and contributors # <see AUTHORS file> diff --git a/sqlparse/filters/__init__.py b/sqlparse/filters/__init__.py index c60d84d..ced03ea 100644 --- a/sqlparse/filters/__init__.py +++ b/sqlparse/filters/__init__.py @@ -1,4 +1,3 @@ -# -*- coding: utf-8 -*- # # Copyright (C) 2009-2018 the sqlparse authors and contributors # <see AUTHORS file> diff --git a/sqlparse/filters/aligned_indent.py b/sqlparse/filters/aligned_indent.py index 85b11e5..445d022 100644 --- a/sqlparse/filters/aligned_indent.py +++ b/sqlparse/filters/aligned_indent.py @@ -1,4 +1,3 @@ -# -*- coding: utf-8 -*- # # Copyright (C) 2009-2018 the sqlparse authors and contributors # <see AUTHORS file> @@ -7,11 +6,10 @@ # the BSD License: https://opensource.org/licenses/BSD-3-Clause from sqlparse import sql, tokens as T -from sqlparse.compat import text_type from sqlparse.utils import offset, indent -class AlignedIndentFilter(object): +class AlignedIndentFilter: join_words = (r'((LEFT\s+|RIGHT\s+|FULL\s+)?' r'(INNER\s+|OUTER\s+|STRAIGHT\s+)?|' r'(CROSS\s+|NATURAL\s+)?)?JOIN\b') @@ -73,7 +71,7 @@ class AlignedIndentFilter(object): end_token = tlist.token_next_by(m=(T.Keyword, 'END'))[1] cases.append((None, [end_token])) - condition_width = [len(' '.join(map(text_type, cond))) if cond else 0 + condition_width = [len(' '.join(map(str, cond))) if cond else 0 for cond, _ in cases] max_cond_width = max(condition_width) @@ -82,8 +80,7 @@ class AlignedIndentFilter(object): stmt = cond[0] if cond else value[0] if i > 0: - tlist.insert_before(stmt, self.nl( - offset_ - len(text_type(stmt)))) + tlist.insert_before(stmt, self.nl(offset_ - len(str(stmt)))) if cond: ws = sql.Token(T.Whitespace, self.char * ( max_cond_width - condition_width[i])) @@ -110,7 +107,7 @@ class AlignedIndentFilter(object): ): token_indent = token.value.split()[0] else: - token_indent = text_type(token) + token_indent = str(token) tlist.insert_before(token, self.nl(token_indent)) tidx += 1 tidx, token = self._next_token(tlist, tidx) diff --git a/sqlparse/filters/others.py b/sqlparse/filters/others.py index 52b8617..ff04b0e 100644 --- a/sqlparse/filters/others.py +++ b/sqlparse/filters/others.py @@ -1,4 +1,3 @@ -# -*- coding: utf-8 -*- # # Copyright (C) 2009-2018 the sqlparse authors and contributors # <see AUTHORS file> @@ -10,7 +9,7 @@ from sqlparse import sql, tokens as T from sqlparse.utils import split_unquoted_newlines -class StripCommentsFilter(object): +class StripCommentsFilter: @staticmethod def _process(tlist): def get_next_comment(): @@ -45,7 +44,7 @@ class StripCommentsFilter(object): return stmt -class StripWhitespaceFilter(object): +class StripWhitespaceFilter: def _stripws(self, tlist): func_name = '_stripws_{cls}'.format(cls=type(tlist).__name__) func = getattr(self, func_name.lower(), self._stripws_default) @@ -90,7 +89,7 @@ class StripWhitespaceFilter(object): return stmt -class SpacesAroundOperatorsFilter(object): +class SpacesAroundOperatorsFilter: @staticmethod def _process(tlist): @@ -118,7 +117,7 @@ class SpacesAroundOperatorsFilter(object): # --------------------------- # postprocess -class SerializerUnicode(object): +class SerializerUnicode: @staticmethod def process(stmt): lines = split_unquoted_newlines(stmt) diff --git a/sqlparse/filters/output.py b/sqlparse/filters/output.py index 3fbc46d..f34b29f 100644 --- a/sqlparse/filters/output.py +++ b/sqlparse/filters/output.py @@ -1,4 +1,3 @@ -# -*- coding: utf-8 -*- # # Copyright (C) 2009-2018 the sqlparse authors and contributors # <see AUTHORS file> @@ -7,10 +6,9 @@ # the BSD License: https://opensource.org/licenses/BSD-3-Clause from sqlparse import sql, tokens as T -from sqlparse.compat import text_type -class OutputFilter(object): +class OutputFilter: varname_prefix = '' def __init__(self, varname='sql'): @@ -23,11 +21,11 @@ class OutputFilter(object): def process(self, stmt): self.count += 1 if self.count > 1: - varname = u'{f.varname}{f.count}'.format(f=self) + varname = '{f.varname}{f.count}'.format(f=self) else: varname = self.varname - has_nl = len(text_type(stmt).strip().splitlines()) > 1 + has_nl = len(str(stmt).strip().splitlines()) > 1 stmt.tokens = self._process(stmt.tokens, varname, has_nl) return stmt diff --git a/sqlparse/filters/reindent.py b/sqlparse/filters/reindent.py index acec8ca..2b0f106 100644 --- a/sqlparse/filters/reindent.py +++ b/sqlparse/filters/reindent.py @@ -1,4 +1,3 @@ -# -*- coding: utf-8 -*- # # Copyright (C) 2009-2018 the sqlparse authors and contributors # <see AUTHORS file> @@ -7,11 +6,10 @@ # the BSD License: https://opensource.org/licenses/BSD-3-Clause from sqlparse import sql, tokens as T -from sqlparse.compat import text_type from sqlparse.utils import offset, indent -class ReindentFilter(object): +class ReindentFilter: def __init__(self, width=2, char=' ', wrap_after=0, n='\n', comma_first=False, indent_after_first=False, indent_columns=False): @@ -42,7 +40,7 @@ class ReindentFilter(object): return self.offset + self.indent * self.width def _get_offset(self, token): - raw = u''.join(map(text_type, self._flatten_up_to_token(token))) + raw = ''.join(map(str, self._flatten_up_to_token(token))) line = (raw or '\n').splitlines()[-1] # Now take current offset into account and return relative offset. return len(line) - len(self.char * self.leading_ws) @@ -71,7 +69,7 @@ class ReindentFilter(object): tidx, token = self._next_token(tlist) while token: pidx, prev_ = tlist.token_prev(tidx, skip_ws=False) - uprev = text_type(prev_) + uprev = str(prev_) if prev_ and prev_.is_whitespace: del tlist.tokens[pidx] @@ -234,7 +232,7 @@ class ReindentFilter(object): self._process(stmt) if self._last_stmt is not None: - nl = '\n' if text_type(self._last_stmt).endswith('\n') else '\n\n' + nl = '\n' if str(self._last_stmt).endswith('\n') else '\n\n' stmt.tokens.insert(0, sql.Token(T.Whitespace, nl)) self._last_stmt = stmt diff --git a/sqlparse/filters/right_margin.py b/sqlparse/filters/right_margin.py index 1658138..e263718 100644 --- a/sqlparse/filters/right_margin.py +++ b/sqlparse/filters/right_margin.py @@ -1,4 +1,3 @@ -# -*- coding: utf-8 -*- # # Copyright (C) 2009-2018 the sqlparse authors and contributors # <see AUTHORS file> @@ -9,11 +8,10 @@ import re from sqlparse import sql, tokens as T -from sqlparse.compat import text_type # FIXME: Doesn't work -class RightMarginFilter(object): +class RightMarginFilter: keep_together = ( # sql.TypeCast, sql.Identifier, sql.Alias, ) @@ -32,14 +30,14 @@ class RightMarginFilter(object): elif token.is_group and type(token) not in self.keep_together: token.tokens = self._process(token, token.tokens) else: - val = text_type(token) + val = str(token) if len(self.line) + len(val) > self.width: match = re.search(r'^ +', self.line) if match is not None: indent = match.group() else: indent = '' - yield sql.Token(T.Whitespace, '\n{0}'.format(indent)) + yield sql.Token(T.Whitespace, '\n{}'.format(indent)) self.line = indent self.line += val yield token diff --git a/sqlparse/filters/tokens.py b/sqlparse/filters/tokens.py index 93182b1..a40f4b0 100644 --- a/sqlparse/filters/tokens.py +++ b/sqlparse/filters/tokens.py @@ -1,4 +1,3 @@ -# -*- coding: utf-8 -*- # # Copyright (C) 2009-2018 the sqlparse authors and contributors # <see AUTHORS file> @@ -7,15 +6,14 @@ # the BSD License: https://opensource.org/licenses/BSD-3-Clause from sqlparse import tokens as T -from sqlparse.compat import text_type -class _CaseFilter(object): +class _CaseFilter: ttype = None def __init__(self, case=None): case = case or 'upper' - self.convert = getattr(text_type, case) + self.convert = getattr(str, case) def process(self, stream): for ttype, value in stream: @@ -38,7 +36,7 @@ class IdentifierCaseFilter(_CaseFilter): yield ttype, value -class TruncateStringFilter(object): +class TruncateStringFilter: def __init__(self, width, char): self.width = width self.char = char diff --git a/sqlparse/formatter.py b/sqlparse/formatter.py index 8962759..8016f87 100644 --- a/sqlparse/formatter.py +++ b/sqlparse/formatter.py @@ -1,4 +1,3 @@ -# -*- coding: utf-8 -*- # # Copyright (C) 2009-2018 the sqlparse authors and contributors # <see AUTHORS file> @@ -17,32 +16,32 @@ def validate_options(options): kwcase = options.get('keyword_case') if kwcase not in [None, 'upper', 'lower', 'capitalize']: raise SQLParseError('Invalid value for keyword_case: ' - '{0!r}'.format(kwcase)) + '{!r}'.format(kwcase)) idcase = options.get('identifier_case') if idcase not in [None, 'upper', 'lower', 'capitalize']: raise SQLParseError('Invalid value for identifier_case: ' - '{0!r}'.format(idcase)) + '{!r}'.format(idcase)) ofrmt = options.get('output_format') if ofrmt not in [None, 'sql', 'python', 'php']: raise SQLParseError('Unknown output format: ' - '{0!r}'.format(ofrmt)) + '{!r}'.format(ofrmt)) strip_comments = options.get('strip_comments', False) if strip_comments not in [True, False]: raise SQLParseError('Invalid value for strip_comments: ' - '{0!r}'.format(strip_comments)) + '{!r}'.format(strip_comments)) space_around_operators = options.get('use_space_around_operators', False) if space_around_operators not in [True, False]: raise SQLParseError('Invalid value for use_space_around_operators: ' - '{0!r}'.format(space_around_operators)) + '{!r}'.format(space_around_operators)) strip_ws = options.get('strip_whitespace', False) if strip_ws not in [True, False]: raise SQLParseError('Invalid value for strip_whitespace: ' - '{0!r}'.format(strip_ws)) + '{!r}'.format(strip_ws)) truncate_strings = options.get('truncate_strings') if truncate_strings is not None: @@ -50,17 +49,17 @@ def validate_options(options): truncate_strings = int(truncate_strings) except (ValueError, TypeError): raise SQLParseError('Invalid value for truncate_strings: ' - '{0!r}'.format(truncate_strings)) + '{!r}'.format(truncate_strings)) if truncate_strings <= 1: raise SQLParseError('Invalid value for truncate_strings: ' - '{0!r}'.format(truncate_strings)) + '{!r}'.format(truncate_strings)) options['truncate_strings'] = truncate_strings options['truncate_char'] = options.get('truncate_char', '[...]') indent_columns = options.get('indent_columns', False) if indent_columns not in [True, False]: raise SQLParseError('Invalid value for indent_columns: ' - '{0!r}'.format(indent_columns)) + '{!r}'.format(indent_columns)) elif indent_columns: options['reindent'] = True # enforce reindent options['indent_columns'] = indent_columns @@ -68,27 +67,27 @@ def validate_options(options): reindent = options.get('reindent', False) if reindent not in [True, False]: raise SQLParseError('Invalid value for reindent: ' - '{0!r}'.format(reindent)) + '{!r}'.format(reindent)) elif reindent: options['strip_whitespace'] = True reindent_aligned = options.get('reindent_aligned', False) if reindent_aligned not in [True, False]: raise SQLParseError('Invalid value for reindent_aligned: ' - '{0!r}'.format(reindent)) + '{!r}'.format(reindent)) elif reindent_aligned: options['strip_whitespace'] = True indent_after_first = options.get('indent_after_first', False) if indent_after_first not in [True, False]: raise SQLParseError('Invalid value for indent_after_first: ' - '{0!r}'.format(indent_after_first)) + '{!r}'.format(indent_after_first)) options['indent_after_first'] = indent_after_first indent_tabs = options.get('indent_tabs', False) if indent_tabs not in [True, False]: raise SQLParseError('Invalid value for indent_tabs: ' - '{0!r}'.format(indent_tabs)) + '{!r}'.format(indent_tabs)) elif indent_tabs: options['indent_char'] = '\t' else: diff --git a/sqlparse/keywords.py b/sqlparse/keywords.py index 9c37e50..3123531 100644 --- a/sqlparse/keywords.py +++ b/sqlparse/keywords.py @@ -1,4 +1,3 @@ -# -*- coding: utf-8 -*- # # Copyright (C) 2009-2018 the sqlparse authors and contributors # <see AUTHORS file> diff --git a/sqlparse/lexer.py b/sqlparse/lexer.py index fd007a4..836d53d 100644 --- a/sqlparse/lexer.py +++ b/sqlparse/lexer.py @@ -1,4 +1,3 @@ -# -*- coding: utf-8 -*- # # Copyright (C) 2009-2018 the sqlparse authors and contributors # <see AUTHORS file> @@ -13,13 +12,14 @@ # It's separated from the rest of pygments to increase performance # and to allow some customizations. +from io import TextIOBase + from sqlparse import tokens from sqlparse.keywords import SQL_REGEX -from sqlparse.compat import text_type, file_types from sqlparse.utils import consume -class Lexer(object): +class Lexer: """Lexer Empty class. Leaving for backwards-compatibility """ @@ -38,10 +38,10 @@ class Lexer(object): ``stack`` is the initial stack (default: ``['root']``) """ - if isinstance(text, file_types): + if isinstance(text, TextIOBase): text = text.read() - if isinstance(text, text_type): + if isinstance(text, str): pass elif isinstance(text, bytes): if encoding: @@ -52,7 +52,7 @@ class Lexer(object): except UnicodeDecodeError: text = text.decode('unicode-escape') else: - raise TypeError(u"Expected text or file-like object, got {!r}". + raise TypeError("Expected text or file-like object, got {!r}". format(type(text))) iterable = enumerate(text) diff --git a/sqlparse/sql.py b/sqlparse/sql.py index a942bcd..a9e11da 100644 --- a/sqlparse/sql.py +++ b/sqlparse/sql.py @@ -1,4 +1,3 @@ -# -*- coding: utf-8 -*- # # Copyright (C) 2009-2018 the sqlparse authors and contributors # <see AUTHORS file> @@ -7,12 +6,10 @@ # the BSD License: https://opensource.org/licenses/BSD-3-Clause """This module contains classes representing syntactical elements of SQL.""" -from __future__ import print_function import re from sqlparse import tokens as T -from sqlparse.compat import string_types, text_type, unicode_compatible from sqlparse.utils import imt, remove_quotes @@ -39,8 +36,7 @@ class NameAliasMixin: return self._get_first_name(reverse=True) -@unicode_compatible -class Token(object): +class Token: """Base class for all other classes in this module. It represents a single token and has two instance attributes: @@ -52,7 +48,7 @@ class Token(object): 'is_group', 'is_whitespace') def __init__(self, ttype, value): - value = text_type(value) + value = str(value) self.value = value self.ttype = ttype self.parent = None @@ -72,15 +68,15 @@ class Token(object): cls = self._get_repr_name() value = self._get_repr_value() - q = u'"' if value.startswith("'") and value.endswith("'") else u"'" - return u"<{cls} {q}{value}{q} at 0x{id:2X}>".format( + q = '"' if value.startswith("'") and value.endswith("'") else "'" + return "<{cls} {q}{value}{q} at 0x{id:2X}>".format( id=id(self), **locals()) def _get_repr_name(self): return str(self.ttype).split('.')[-1] def _get_repr_value(self): - raw = text_type(self) + raw = str(self) if len(raw) > 7: raw = raw[:6] + '...' return re.sub(r'\s+', ' ', raw) @@ -105,7 +101,7 @@ class Token(object): if not type_matched or values is None: return type_matched - if isinstance(values, string_types): + if isinstance(values, str): values = (values,) if regex: @@ -150,7 +146,6 @@ class Token(object): return False -@unicode_compatible class TokenList(Token): """A group of tokens. @@ -163,11 +158,11 @@ class TokenList(Token): def __init__(self, tokens=None): self.tokens = tokens or [] [setattr(token, 'parent', self) for token in self.tokens] - super(TokenList, self).__init__(None, text_type(self)) + super().__init__(None, str(self)) self.is_group = True def __str__(self): - return u''.join(token.value for token in self.flatten()) + return ''.join(token.value for token in self.flatten()) # weird bug # def __len__(self): @@ -190,14 +185,14 @@ class TokenList(Token): value = token._get_repr_value() last = idx == (token_count - 1) - pre = u'`- ' if last else u'|- ' + pre = '`- ' if last else '|- ' - q = u'"' if value.startswith("'") and value.endswith("'") else u"'" - print(u"{_pre}{pre}{idx} {cls} {q}{value}{q}" + q = '"' if value.startswith("'") and value.endswith("'") else "'" + print("{_pre}{pre}{idx} {cls} {q}{value}{q}" .format(**locals()), file=f) if token.is_group and (max_depth is None or depth < max_depth): - parent_pre = u' ' if last else u'| ' + parent_pre = ' ' if last else '| ' token._pprint_tree(max_depth, depth + 1, f, _pre + parent_pre) def get_token_at_offset(self, offset): @@ -216,8 +211,7 @@ class TokenList(Token): """ for token in self.tokens: if token.is_group: - for item in token.flatten(): - yield item + yield from token.flatten() else: yield token @@ -328,7 +322,7 @@ class TokenList(Token): grp = start grp.tokens.extend(subtokens) del self.tokens[start_idx + 1:end_idx] - grp.value = text_type(start) + grp.value = str(start) else: subtokens = self.tokens[start_idx:end_idx] grp = grp_cls(subtokens) diff --git a/sqlparse/tokens.py b/sqlparse/tokens.py index eefc0b4..193d100 100644 --- a/sqlparse/tokens.py +++ b/sqlparse/tokens.py @@ -1,4 +1,3 @@ -# -*- coding: utf-8 -*- # # Copyright (C) 2009-2018 the sqlparse authors and contributors # <see AUTHORS file> diff --git a/sqlparse/utils.py b/sqlparse/utils.py index 3283274..265378b 100644 --- a/sqlparse/utils.py +++ b/sqlparse/utils.py @@ -1,4 +1,3 @@ -# -*- coding: utf-8 -*- # # Copyright (C) 2009-2018 the sqlparse authors and contributors # <see AUTHORS file> @@ -10,7 +9,6 @@ import itertools import re from collections import deque from contextlib import contextmanager -from sqlparse.compat import text_type # This regular expression replaces the home-cooked parser that was here before. # It is much faster, but requires an extra post-processing step to get the @@ -40,7 +38,7 @@ def split_unquoted_newlines(stmt): Unlike str.splitlines(), this will ignore CR/LF/CR+LF if the requisite character is inside of a string.""" - text = text_type(stmt) + text = str(stmt) lines = SPLIT_REGEX.split(text) outputlines = [''] for line in lines: |
