summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorVictor Uriarte <victor.m.uriarte@intel.com>2016-06-10 20:16:53 -0700
committerVictor Uriarte <victor.m.uriarte@intel.com>2016-06-11 04:34:12 -0700
commitff0ee839987c43c420f1a5d167e3c3c0e873411c (patch)
treef4be82d608dc19f86426031b80951491c2a8862d
parentdeb38ec69a428452c3b519d4ab02956af8eebd0d (diff)
downloadsqlparse-ff0ee839987c43c420f1a5d167e3c3c0e873411c.tar.gz
Apply new-style str format
-rw-r--r--docs/source/conf.py8
-rw-r--r--examples/column_defs_lowlevel.py4
-rw-r--r--examples/extract_table_names.py15
-rw-r--r--setup.py7
-rw-r--r--sqlparse/filters/output.py2
-rw-r--r--sqlparse/filters/right_margin.py2
-rw-r--r--sqlparse/formatter.py40
-rw-r--r--tests/test_grouping.py2
-rw-r--r--tests/test_regressions.py16
-rw-r--r--tests/test_tokenize.py2
10 files changed, 49 insertions, 49 deletions
diff --git a/docs/source/conf.py b/docs/source/conf.py
index 5f7d34f..70bd69a 100644
--- a/docs/source/conf.py
+++ b/docs/source/conf.py
@@ -42,8 +42,8 @@ source_suffix = '.rst'
master_doc = 'index'
# General information about the project.
-project = u'python-sqlparse'
-copyright = u'%s, Andi Albrecht' % datetime.date.today().strftime('%Y')
+project = 'python-sqlparse'
+copyright = '{:%Y}, Andi Albrecht'.format(datetime.date.today())
# The version info for the project you're documenting, acts as replacement for
# |version| and |release|, also used in various other places throughout the
@@ -177,8 +177,8 @@ htmlhelp_basename = 'python-sqlparsedoc'
# Grouping the document tree into LaTeX files. List of tuples
# (source start file, target name, title, author, documentclass [howto/manual]).
latex_documents = [
- ('index', 'python-sqlparse.tex', ur'python-sqlparse Documentation',
- ur'Andi Albrecht', 'manual'),
+ ('index', 'python-sqlparse.tex', 'python-sqlparse Documentation',
+ 'Andi Albrecht', 'manual'),
]
# The name of an image file (relative to this directory) to place at the top of
diff --git a/examples/column_defs_lowlevel.py b/examples/column_defs_lowlevel.py
index 7cce753..5acbdec 100644
--- a/examples/column_defs_lowlevel.py
+++ b/examples/column_defs_lowlevel.py
@@ -49,5 +49,5 @@ def extract_definitions(token_list):
columns = extract_definitions(par)
for column in columns:
- print('NAME: %-12s DEFINITION: %s' % (column[0],
- ''.join(str(t) for t in column[1:])))
+ print('NAME: {name:10} DEFINITION: {definition}'.format(
+ name=column[0], definition=''.join(str(t) for t in column[1:])))
diff --git a/examples/extract_table_names.py b/examples/extract_table_names.py
index b43ee5f..c1bcf8b 100644
--- a/examples/extract_table_names.py
+++ b/examples/extract_table_names.py
@@ -12,11 +12,6 @@
# See:
# http://groups.google.com/group/sqlparse/browse_thread/thread/b0bd9a022e9d4895
-sql = """
-select K.a,K.b from (select H.b from (select G.c from (select F.d from
-(select E.e from A, B, C, D, E), F), G), H), I, J, K order by 1,2;
-"""
-
import sqlparse
from sqlparse.sql import IdentifierList, Identifier
from sqlparse.tokens import Keyword, DML
@@ -59,10 +54,16 @@ def extract_table_identifiers(token_stream):
yield item.value
-def extract_tables():
+def extract_tables(sql):
stream = extract_from_part(sqlparse.parse(sql)[0])
return list(extract_table_identifiers(stream))
if __name__ == '__main__':
- print('Tables: %s' % ', '.join(extract_tables()))
+ sql = """
+ select K.a,K.b from (select H.b from (select G.c from (select F.d from
+ (select E.e from A, B, C, D, E), F), G), H), I, J, K order by 1,2;
+ """
+
+ tables = ', '.join(extract_tables(sql))
+ print('Tables: {0}'.format(tables))
diff --git a/setup.py b/setup.py
index ffdbdb9..45d560f 100644
--- a/setup.py
+++ b/setup.py
@@ -21,14 +21,13 @@ except ImportError:
def get_version():
"""Parse __init__.py for version number instead of importing the file."""
VERSIONFILE = 'sqlparse/__init__.py'
- verstrline = open(VERSIONFILE, "rt").read()
VSRE = r'^__version__ = [\'"]([^\'"]*)[\'"]'
+ with open(VERSIONFILE) as f:
+ verstrline = f.read()
mo = re.search(VSRE, verstrline, re.M)
if mo:
return mo.group(1)
- else:
- raise RuntimeError('Unable to find version string in %s.'
- % (VERSIONFILE,))
+ raise RuntimeError('Unable to find version in {fn}'.format(fn=VERSIONFILE))
LONG_DESCRIPTION = """
diff --git a/sqlparse/filters/output.py b/sqlparse/filters/output.py
index d4528e9..bbc5076 100644
--- a/sqlparse/filters/output.py
+++ b/sqlparse/filters/output.py
@@ -22,7 +22,7 @@ class OutputFilter(object):
def process(self, stmt):
self.count += 1
if self.count > 1:
- varname = '%s%d' % (self.varname, self.count)
+ varname = '{f.varname}{f.count}'.format(f=self)
else:
varname = self.varname
diff --git a/sqlparse/filters/right_margin.py b/sqlparse/filters/right_margin.py
index 4e10dc0..b3f905d 100644
--- a/sqlparse/filters/right_margin.py
+++ b/sqlparse/filters/right_margin.py
@@ -38,7 +38,7 @@ class RightMarginFilter(object):
indent = match.group()
else:
indent = ''
- yield sql.Token(T.Whitespace, '\n%s' % indent)
+ yield sql.Token(T.Whitespace, '\n{0}'.format(indent))
self.line = indent
self.line += val
yield token
diff --git a/sqlparse/formatter.py b/sqlparse/formatter.py
index 069109b..8f10557 100644
--- a/sqlparse/formatter.py
+++ b/sqlparse/formatter.py
@@ -15,61 +15,65 @@ def validate_options(options):
"""Validates options."""
kwcase = options.get('keyword_case')
if kwcase not in [None, 'upper', 'lower', 'capitalize']:
- raise SQLParseError('Invalid value for keyword_case: %r' % kwcase)
+ raise SQLParseError('Invalid value for keyword_case: '
+ '{0!r}'.format(kwcase))
idcase = options.get('identifier_case')
if idcase not in [None, 'upper', 'lower', 'capitalize']:
- raise SQLParseError('Invalid value for identifier_case: %r' % idcase)
+ raise SQLParseError('Invalid value for identifier_case: '
+ '{0!r}'.format(idcase))
ofrmt = options.get('output_format')
if ofrmt not in [None, 'sql', 'python', 'php']:
- raise SQLParseError('Unknown output format: %r' % ofrmt)
+ raise SQLParseError('Unknown output format: '
+ '{0!r}'.format(ofrmt))
strip_comments = options.get('strip_comments', False)
if strip_comments not in [True, False]:
- raise SQLParseError('Invalid value for strip_comments: %r'
- % strip_comments)
+ raise SQLParseError('Invalid value for strip_comments: '
+ '{0!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: %r'
- % space_around_operators)
+ raise SQLParseError('Invalid value for use_space_around_operators: '
+ '{0!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: %r'
- % strip_ws)
+ raise SQLParseError('Invalid value for strip_whitespace: '
+ '{0!r}'.format(strip_ws))
truncate_strings = options.get('truncate_strings')
if truncate_strings is not None:
try:
truncate_strings = int(truncate_strings)
except (ValueError, TypeError):
- raise SQLParseError('Invalid value for truncate_strings: %r'
- % truncate_strings)
+ raise SQLParseError('Invalid value for truncate_strings: '
+ '{0!r}'.format(truncate_strings))
if truncate_strings <= 1:
- raise SQLParseError('Invalid value for truncate_strings: %r'
- % truncate_strings)
+ raise SQLParseError('Invalid value for truncate_strings: '
+ '{0!r}'.format(truncate_strings))
options['truncate_strings'] = truncate_strings
options['truncate_char'] = options.get('truncate_char', '[...]')
reindent = options.get('reindent', False)
if reindent not in [True, False]:
- raise SQLParseError('Invalid value for reindent: %r'
- % reindent)
+ raise SQLParseError('Invalid value for reindent: '
+ '{0!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: %r'
- % reindent)
+ raise SQLParseError('Invalid value for reindent_aligned: '
+ '{0!r}'.format(reindent))
elif reindent_aligned:
options['strip_whitespace'] = True
indent_tabs = options.get('indent_tabs', False)
if indent_tabs not in [True, False]:
- raise SQLParseError('Invalid value for indent_tabs: %r' % indent_tabs)
+ raise SQLParseError('Invalid value for indent_tabs: '
+ '{0!r}'.format(indent_tabs))
elif indent_tabs:
options['indent_char'] = '\t'
else:
diff --git a/tests/test_grouping.py b/tests/test_grouping.py
index fdcd4a7..7ea1c75 100644
--- a/tests/test_grouping.py
+++ b/tests/test_grouping.py
@@ -373,7 +373,7 @@ def test_comparison_with_functions(): # issue230
@pytest.mark.parametrize('start', ['FOR', 'FOREACH'])
def test_forloops(start):
- p = sqlparse.parse('%s foo in bar LOOP foobar END LOOP' % start)[0]
+ p = sqlparse.parse('{0} foo in bar LOOP foobar END LOOP'.format(start))[0]
assert (len(p.tokens)) == 1
assert isinstance(p.tokens[0], sql.For)
diff --git a/tests/test_regressions.py b/tests/test_regressions.py
index 13ca04b..d38b8f1 100644
--- a/tests/test_regressions.py
+++ b/tests/test_regressions.py
@@ -171,10 +171,11 @@ def test_comment_encoding_when_reindent():
def test_parse_sql_with_binary():
# See https://github.com/andialbrecht/sqlparse/pull/88
+ # digest = '‚|ËêŠplL4¡h‘øN{'
digest = '\x82|\xcb\x0e\xea\x8aplL4\xa1h\x91\xf8N{'
- sql = 'select * from foo where bar = \'%s\'' % digest
+ sql = "select * from foo where bar = '{0}'".format(digest)
formatted = sqlparse.format(sql, reindent=True)
- tformatted = 'select *\nfrom foo\nwhere bar = \'%s\'' % digest
+ tformatted = "select *\nfrom foo\nwhere bar = '{0}'".format(digest)
if sys.version_info < (3,):
tformatted = tformatted.decode('unicode-escape')
assert formatted == tformatted
@@ -193,10 +194,8 @@ def test_dont_alias_keywords():
def test_format_accepts_encoding(): # issue20
sql = load_file('test_cp1251.sql', 'cp1251')
formatted = sqlparse.format(sql, reindent=True, encoding='cp1251')
- if sys.version_info < (3,):
- tformatted = u'insert into foo\nvalues (1); -- Песня про надежду\n'
- else:
- tformatted = 'insert into foo\nvalues (1); -- Песня про надежду\n'
+ tformatted = u'insert into foo\nvalues (1); -- Песня про надежду\n'
+
assert formatted == tformatted
@@ -278,10 +277,7 @@ def test_issue186_get_type():
def test_issue212_py2unicode():
- if sys.version_info < (3,):
- t1 = sql.Token(T.String, u"schöner ")
- else:
- t1 = sql.Token(T.String, "schöner ")
+ t1 = sql.Token(T.String, u"schöner ")
t2 = sql.Token(T.String, u"bug")
l = sql.TokenList([t1, t2])
assert str(l) == 'schöner bug'
diff --git a/tests/test_tokenize.py b/tests/test_tokenize.py
index 2e931ba..adfd1ea 100644
--- a/tests/test_tokenize.py
+++ b/tests/test_tokenize.py
@@ -151,7 +151,7 @@ class TestStream(unittest.TestCase):
'CROSS JOIN', 'STRAIGHT JOIN',
'INNER JOIN', 'LEFT INNER JOIN'])
def test_parse_join(expr):
- p = sqlparse.parse('%s foo' % expr)[0]
+ p = sqlparse.parse('{0} foo'.format(expr))[0]
assert len(p.tokens) == 3
assert p.tokens[0].ttype is T.Keyword