From 61c81b05f50a073f62629bdcf8eb4317d7a2084c Mon Sep 17 00:00:00 2001 From: Georgy Frolov Date: Thu, 12 Mar 2020 15:01:19 +0300 Subject: removed py2-related stuff --- pgcli/config.py | 7 +----- pgcli/encodingutils.py | 28 ----------------------- pgcli/key_bindings.py | 2 -- pgcli/main.py | 19 ++++++--------- pgcli/packages/parseutils/ctes.py | 2 -- pgcli/packages/parseutils/meta.py | 1 - pgcli/packages/parseutils/tables.py | 2 -- pgcli/packages/parseutils/utils.py | 1 - pgcli/packages/prioritization.py | 2 -- pgcli/packages/prompt_utils.py | 3 --- pgcli/packages/sqlcompletion.py | 12 +--------- pgcli/pgbuffer.py | 1 - pgcli/pgcompleter.py | 1 - pgcli/pgexecute.py | 10 +++----- pgcli/pgstyle.py | 2 -- pgcli/pgtoolbar.py | 2 -- release.py | 1 - tests/conftest.py | 2 -- tests/features/db_utils.py | 4 ---- tests/features/environment.py | 3 --- tests/features/fixture_utils.py | 4 ---- tests/features/steps/auto_vertical.py | 3 --- tests/features/steps/basic_commands.py | 2 -- tests/features/steps/crud_database.py | 3 --- tests/features/steps/crud_table.py | 2 -- tests/features/steps/expanded.py | 2 -- tests/features/steps/iocommands.py | 2 -- tests/features/steps/named_queries.py | 2 -- tests/features/steps/specials.py | 2 -- tests/features/steps/wrappers.py | 3 --- tests/metadata.py | 5 +--- tests/test_fuzzy_completion.py | 1 - tests/test_main.py | 2 -- tests/test_naive_completion.py | 1 - tests/test_pgexecute.py | 3 --- tests/test_prompt_utils.py | 3 --- tests/test_smart_completion_multiple_schemata.py | 2 -- tests/test_smart_completion_public_schema_only.py | 2 -- 38 files changed, 13 insertions(+), 136 deletions(-) delete mode 100644 pgcli/encodingutils.py diff --git a/pgcli/config.py b/pgcli/config.py index a2c0b0b2..0fc42dde 100644 --- a/pgcli/config.py +++ b/pgcli/config.py @@ -26,12 +26,7 @@ def load_config(usr_cfg, def_cfg=None): def ensure_dir_exists(path): parent_dir = expanduser(dirname(path)) - try: - os.makedirs(parent_dir) - except OSError as exc: - # ignore existing destination (py2 has no exist_ok arg to makedirs) - if exc.errno != errno.EEXIST: - raise + os.makedirs(parent_dir, exist_ok=True) def write_default_config(source, destination, overwrite=False): diff --git a/pgcli/encodingutils.py b/pgcli/encodingutils.py deleted file mode 100644 index 279da3a8..00000000 --- a/pgcli/encodingutils.py +++ /dev/null @@ -1,28 +0,0 @@ -import sys - -PY2 = sys.version_info[0] == 2 -PY3 = sys.version_info[0] == 3 - -text_type = unicode if PY2 else str - - -def unicode2utf8(arg): - """ - Only in Python 2. Psycopg2 expects the args as bytes not unicode. - In Python 3 the args are expected as unicode. - """ - - if PY2 and isinstance(arg, unicode): - return arg.encode("utf-8") - return arg - - -def utf8tounicode(arg): - """ - Only in Python 2. Psycopg2 returns the error message as utf-8. - In Python 3 the errors are returned as unicode. - """ - - if PY2 and isinstance(arg, str): - return arg.decode("utf-8") - return arg diff --git a/pgcli/key_bindings.py b/pgcli/key_bindings.py index db783855..23174b6b 100644 --- a/pgcli/key_bindings.py +++ b/pgcli/key_bindings.py @@ -1,5 +1,3 @@ -from __future__ import unicode_literals - import logging from prompt_toolkit.enums import EditingMode from prompt_toolkit.key_binding import KeyBindings diff --git a/pgcli/main.py b/pgcli/main.py index fb3b3546..08c6c491 100644 --- a/pgcli/main.py +++ b/pgcli/main.py @@ -1,6 +1,3 @@ -from __future__ import print_function -from __future__ import unicode_literals - import warnings from pgspecial.namedqueries import NamedQueries @@ -63,8 +60,6 @@ from .config import ( get_config, ) from .key_bindings import pgcli_bindings -from .encodingutils import utf8tounicode -from .encodingutils import text_type from .packages.prompt_utils import confirm_destructive_query from .__init__ import __version__ @@ -538,7 +533,7 @@ class PGCli(object): # fails. Don't prompt if the -w flag is supplied if self.never_passwd_prompt: return False - error_msg = utf8tounicode(exc.args[0]) + error_msg = exc.args[0] if "no password supplied" in error_msg: return True if "password authentication failed" in error_msg: @@ -1365,7 +1360,7 @@ def is_select(status): def exception_formatter(e): - return click.style(utf8tounicode(str(e)), fg="red") + return click.style(str(e), fg="red") def format_output(title, cur, headers, status, settings): @@ -1381,7 +1376,7 @@ def format_output(title, cur, headers, status, settings): return settings.missingval if not isinstance(val, list): return val - return "{" + ",".join(text_type(format_array(e)) for e in val) + "}" + return "{" + ",".join(str(format_array(e)) for e in val) + "}" def format_arrays(data, headers, **_): data = list(data) @@ -1411,7 +1406,7 @@ def format_output(title, cur, headers, status, settings): output.append(title) if cur: - headers = [case_function(utf8tounicode(x)) for x in headers] + headers = [case_function(x) for x in headers] if max_width is not None: cur = list(cur) column_types = None @@ -1429,10 +1424,10 @@ def format_output(title, cur, headers, status, settings): ): column_types.append(int) else: - column_types.append(text_type) + column_types.append(str) formatted = formatter.format_output(cur, headers, **output_kwargs) - if isinstance(formatted, (text_type)): + if isinstance(formatted, str): formatted = iter(formatted.splitlines()) first_line = next(formatted) formatted = itertools.chain([first_line], formatted) @@ -1440,7 +1435,7 @@ def format_output(title, cur, headers, status, settings): formatted = formatter.format_output( cur, headers, format_name="vertical", column_types=None, **output_kwargs ) - if isinstance(formatted, (text_type)): + if isinstance(formatted, str): formatted = iter(formatted.splitlines()) output = itertools.chain(output, formatted) diff --git a/pgcli/packages/parseutils/ctes.py b/pgcli/packages/parseutils/ctes.py index 4b8786dc..75e4e40f 100644 --- a/pgcli/packages/parseutils/ctes.py +++ b/pgcli/packages/parseutils/ctes.py @@ -1,5 +1,3 @@ -from __future__ import unicode_literals - from sqlparse import parse from sqlparse.tokens import Keyword, CTE, DML from sqlparse.sql import Identifier, IdentifierList, Parenthesis diff --git a/pgcli/packages/parseutils/meta.py b/pgcli/packages/parseutils/meta.py index a892b880..108c01a3 100644 --- a/pgcli/packages/parseutils/meta.py +++ b/pgcli/packages/parseutils/meta.py @@ -1,4 +1,3 @@ -from __future__ import unicode_literals from collections import namedtuple _ColumnMetadata = namedtuple( diff --git a/pgcli/packages/parseutils/tables.py b/pgcli/packages/parseutils/tables.py index ac4c1b9d..55b65b00 100644 --- a/pgcli/packages/parseutils/tables.py +++ b/pgcli/packages/parseutils/tables.py @@ -1,5 +1,3 @@ -from __future__ import print_function, unicode_literals - import sqlparse from collections import namedtuple from sqlparse.sql import IdentifierList, Identifier, Function diff --git a/pgcli/packages/parseutils/utils.py b/pgcli/packages/parseutils/utils.py index 1d6c45b8..594cabfd 100644 --- a/pgcli/packages/parseutils/utils.py +++ b/pgcli/packages/parseutils/utils.py @@ -1,4 +1,3 @@ -from __future__ import print_function import re import sqlparse from sqlparse.sql import Identifier diff --git a/pgcli/packages/prioritization.py b/pgcli/packages/prioritization.py index b692b75a..e92dcbb6 100644 --- a/pgcli/packages/prioritization.py +++ b/pgcli/packages/prioritization.py @@ -1,5 +1,3 @@ -from __future__ import unicode_literals - import re import sqlparse from sqlparse.tokens import Name diff --git a/pgcli/packages/prompt_utils.py b/pgcli/packages/prompt_utils.py index 63b5e059..3c584908 100644 --- a/pgcli/packages/prompt_utils.py +++ b/pgcli/packages/prompt_utils.py @@ -1,6 +1,3 @@ -# -*- coding: utf-8 -*- -from __future__ import unicode_literals - import sys import click from .parseutils import is_destructive diff --git a/pgcli/packages/sqlcompletion.py b/pgcli/packages/sqlcompletion.py index 2828b175..93736e34 100644 --- a/pgcli/packages/sqlcompletion.py +++ b/pgcli/packages/sqlcompletion.py @@ -1,5 +1,3 @@ -from __future__ import print_function, unicode_literals - import sys import re import sqlparse @@ -10,14 +8,6 @@ from .parseutils.tables import extract_tables from .parseutils.ctes import isolate_query_ctes from pgspecial.main import parse_special_command -PY2 = sys.version_info[0] == 2 -PY3 = sys.version_info[0] == 3 - -if PY3: - string_types = str -else: - string_types = basestring - Special = namedtuple("Special", []) Database = namedtuple("Database", []) @@ -301,7 +291,7 @@ def suggest_special(text): def suggest_based_on_last_token(token, stmt): - if isinstance(token, string_types): + if isinstance(token, str): token_v = token.lower() elif isinstance(token, Comparison): # If 'token' is a Comparison type such as diff --git a/pgcli/pgbuffer.py b/pgcli/pgbuffer.py index 8e842c97..706ed25f 100644 --- a/pgcli/pgbuffer.py +++ b/pgcli/pgbuffer.py @@ -1,4 +1,3 @@ -from __future__ import unicode_literals import logging from prompt_toolkit.enums import DEFAULT_BUFFER diff --git a/pgcli/pgcompleter.py b/pgcli/pgcompleter.py index 14ae4fa2..b2a57834 100644 --- a/pgcli/pgcompleter.py +++ b/pgcli/pgcompleter.py @@ -1,4 +1,3 @@ -from __future__ import print_function, unicode_literals import logging import re from itertools import count, repeat, chain diff --git a/pgcli/pgexecute.py b/pgcli/pgexecute.py index a33afe6f..776c9628 100644 --- a/pgcli/pgexecute.py +++ b/pgcli/pgexecute.py @@ -9,7 +9,6 @@ import pgspecial as special import select from psycopg2.extensions import POLL_OK, POLL_READ, POLL_WRITE, make_dsn from .packages.parseutils.meta import FunctionMetadata, ForeignKey -from .encodingutils import unicode2utf8, PY2, utf8tounicode _logger = logging.getLogger(__name__) @@ -250,7 +249,7 @@ class PGExecute(object): new_params["dsn"], password=new_params.pop("password") ) - conn_params.update({k: unicode2utf8(v) for k, v in new_params.items() if v}) + conn_params.update({k: v for k, v in new_params.items() if v}) conn = psycopg2.connect(**conn_params) cursor = conn.cursor() @@ -333,10 +332,7 @@ class PGExecute(object): See http://initd.org/psycopg/docs/connection.html#connection.encoding """ - if PY2: - return json_data.decode(self.conn.encoding) - else: - return json_data + return json_data def failed_transaction(self): status = self.conn.get_transaction_status() @@ -450,7 +446,7 @@ class PGExecute(object): # conn.notices persist between queies, we use pop to clear out the list title = "" while len(self.conn.notices) > 0: - title = utf8tounicode(self.conn.notices.pop()) + title + title = self.conn.notices.pop() + title # cur.description will be None for operations that do not return # rows. diff --git a/pgcli/pgstyle.py b/pgcli/pgstyle.py index 0355599b..a2f29737 100644 --- a/pgcli/pgstyle.py +++ b/pgcli/pgstyle.py @@ -1,5 +1,3 @@ -from __future__ import unicode_literals - import logging import pygments.styles diff --git a/pgcli/pgtoolbar.py b/pgcli/pgtoolbar.py index fc3bb3b9..f4e7f141 100644 --- a/pgcli/pgtoolbar.py +++ b/pgcli/pgtoolbar.py @@ -1,5 +1,3 @@ -from __future__ import unicode_literals - from prompt_toolkit.key_binding.vi_state import InputMode from prompt_toolkit.application import get_app diff --git a/release.py b/release.py index 0444983e..e83d2392 100644 --- a/release.py +++ b/release.py @@ -1,7 +1,6 @@ #!/usr/bin/env python """A script to publish a release of pgcli to PyPI.""" -from __future__ import print_function import io from optparse import OptionParser import re diff --git a/tests/conftest.py b/tests/conftest.py index 315e3de8..2a715b11 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -1,5 +1,3 @@ -from __future__ import print_function - import os import pytest from utils import ( diff --git a/tests/features/db_utils.py b/tests/features/db_utils.py index 7f8a2e7d..f57bc3b9 100644 --- a/tests/features/db_utils.py +++ b/tests/features/db_utils.py @@ -1,7 +1,3 @@ -# -*- coding: utf-8 -*- -from __future__ import unicode_literals -from __future__ import print_function - from psycopg2 import connect from psycopg2.extensions import AsIs diff --git a/tests/features/environment.py b/tests/features/environment.py index 349e9dd6..4e7e601e 100644 --- a/tests/features/environment.py +++ b/tests/features/environment.py @@ -1,6 +1,3 @@ -# -*- coding: utf-8 -*- -from __future__ import unicode_literals, print_function - import copy import os import sys diff --git a/tests/features/fixture_utils.py b/tests/features/fixture_utils.py index 25204544..16f123a6 100644 --- a/tests/features/fixture_utils.py +++ b/tests/features/fixture_utils.py @@ -1,7 +1,3 @@ -# -*- coding: utf-8 -*- -from __future__ import unicode_literals -from __future__ import print_function - import os import codecs diff --git a/tests/features/steps/auto_vertical.py b/tests/features/steps/auto_vertical.py index 2bb89870..1643ea5e 100644 --- a/tests/features/steps/auto_vertical.py +++ b/tests/features/steps/auto_vertical.py @@ -1,6 +1,3 @@ -# -*- coding: utf-8 -from __future__ import unicode_literals, print_function - from textwrap import dedent from behave import then, when import wrappers diff --git a/tests/features/steps/basic_commands.py b/tests/features/steps/basic_commands.py index bda163f6..0c42ae0e 100644 --- a/tests/features/steps/basic_commands.py +++ b/tests/features/steps/basic_commands.py @@ -1,10 +1,8 @@ -# -*- coding: utf-8 """ Steps for behavioral style tests are defined in this module. Each step is defined by the string decorating it. This string is used to call the step in "*.feature" file. """ -from __future__ import unicode_literals, print_function import pexpect import subprocess diff --git a/tests/features/steps/crud_database.py b/tests/features/steps/crud_database.py index 9eab4f45..3fd8b7a1 100644 --- a/tests/features/steps/crud_database.py +++ b/tests/features/steps/crud_database.py @@ -1,11 +1,8 @@ -# -*- coding: utf-8 -*- """ Steps for behavioral style tests are defined in this module. Each step is defined by the string decorating it. This string is used to call the step in "*.feature" file. """ -from __future__ import unicode_literals, print_function - import pexpect from behave import when, then diff --git a/tests/features/steps/crud_table.py b/tests/features/steps/crud_table.py index 6d848aba..0375883a 100644 --- a/tests/features/steps/crud_table.py +++ b/tests/features/steps/crud_table.py @@ -1,10 +1,8 @@ -# -*- coding: utf-8 """ Steps for behavioral style tests are defined in this module. Each step is defined by the string decorating it. This string is used to call the step in "*.feature" file. """ -from __future__ import unicode_literals, print_function from behave import when, then from textwrap import dedent diff --git a/tests/features/steps/expanded.py b/tests/features/steps/expanded.py index f79f913b..f34fcf04 100644 --- a/tests/features/steps/expanded.py +++ b/tests/features/steps/expanded.py @@ -1,11 +1,9 @@ -# -*- coding: utf-8 """Steps for behavioral style tests are defined in this module. Each step is defined by the string decorating it. This string is used to call the step in "*.feature" file. """ -from __future__ import unicode_literals, print_function from behave import when, then from textwrap import dedent diff --git a/tests/features/steps/iocommands.py b/tests/features/steps/iocommands.py index 416eac59..613aeb29 100644 --- a/tests/features/steps/iocommands.py +++ b/tests/features/steps/iocommands.py @@ -1,5 +1,3 @@ -# -*- coding: utf-8 -from __future__ import unicode_literals, print_function import os import os.path diff --git a/tests/features/steps/named_queries.py b/tests/features/steps/named_queries.py index 289ec639..3f52859b 100644 --- a/tests/features/steps/named_queries.py +++ b/tests/features/steps/named_queries.py @@ -1,10 +1,8 @@ -# -*- coding: utf-8 """ Steps for behavioral style tests are defined in this module. Each step is defined by the string decorating it. This string is used to call the step in "*.feature" file. """ -from __future__ import unicode_literals, print_function from behave import when, then import wrappers diff --git a/tests/features/steps/specials.py b/tests/features/steps/specials.py index 2c77a3b1..813292c4 100644 --- a/tests/features/steps/specials.py +++ b/tests/features/steps/specials.py @@ -1,10 +1,8 @@ -# -*- coding: utf-8 """ Steps for behavioral style tests are defined in this module. Each step is defined by the string decorating it. This string is used to call the step in "*.feature" file. """ -from __future__ import unicode_literals, print_function from behave import when, then import wrappers diff --git a/tests/features/steps/wrappers.py b/tests/features/steps/wrappers.py index 9a2db982..e0f5a20e 100644 --- a/tests/features/steps/wrappers.py +++ b/tests/features/steps/wrappers.py @@ -1,6 +1,3 @@ -# -*- coding: utf-8 -from __future__ import unicode_literals - import re import pexpect from pgcli.main import COLOR_CODE_REGEX diff --git a/tests/metadata.py b/tests/metadata.py index 5cf9456d..2f89ea28 100644 --- a/tests/metadata.py +++ b/tests/metadata.py @@ -1,12 +1,9 @@ -from __future__ import unicode_literals - from functools import partial from itertools import product from pgcli.packages.parseutils.meta import FunctionMetadata, ForeignKey from prompt_toolkit.completion import Completion from prompt_toolkit.document import Document from mock import Mock -from six import iteritems import pytest parametrize = pytest.mark.parametrize @@ -78,7 +75,7 @@ class MetaData(object): def specials(self, pos=0): return [ Completion(text=k, start_position=pos, display_meta=v.description) - for k, v in iteritems(self.completer.pgspecial.commands) + for k, v in self.completer.pgspecial.commands.items() ] def columns(self, tbl, parent="public", typ="tables", pos=0): diff --git a/tests/test_fuzzy_completion.py b/tests/test_fuzzy_completion.py index 30f9de2c..8f8f2cd0 100644 --- a/tests/test_fuzzy_completion.py +++ b/tests/test_fuzzy_completion.py @@ -1,4 +1,3 @@ -from __future__ import unicode_literals import pytest diff --git a/tests/test_main.py b/tests/test_main.py index c55944b6..044181b1 100644 --- a/tests/test_main.py +++ b/tests/test_main.py @@ -1,5 +1,3 @@ -# coding=utf-8 -from __future__ import unicode_literals, print_function import os import platform import mock diff --git a/tests/test_naive_completion.py b/tests/test_naive_completion.py index cf2824e4..7c7ed8e7 100644 --- a/tests/test_naive_completion.py +++ b/tests/test_naive_completion.py @@ -1,4 +1,3 @@ -from __future__ import unicode_literals import pytest from prompt_toolkit.completion import Completion from prompt_toolkit.document import Document diff --git a/tests/test_pgexecute.py b/tests/test_pgexecute.py index 46bd735c..9273be94 100644 --- a/tests/test_pgexecute.py +++ b/tests/test_pgexecute.py @@ -1,6 +1,3 @@ -# coding=UTF-8 -from __future__ import print_function - from textwrap import dedent import psycopg2 diff --git a/tests/test_prompt_utils.py b/tests/test_prompt_utils.py index 4986dc57..c1f8a169 100644 --- a/tests/test_prompt_utils.py +++ b/tests/test_prompt_utils.py @@ -1,6 +1,3 @@ -# -*- coding: utf-8 -*- - - import click from pgcli.packages.prompt_utils import confirm_destructive_query diff --git a/tests/test_smart_completion_multiple_schemata.py b/tests/test_smart_completion_multiple_schemata.py index 9fff2a17..805b727c 100644 --- a/tests/test_smart_completion_multiple_schemata.py +++ b/tests/test_smart_completion_multiple_schemata.py @@ -1,5 +1,3 @@ -from __future__ import unicode_literals, print_function - import itertools from metadata import ( MetaData, diff --git a/tests/test_smart_completion_public_schema_only.py b/tests/test_smart_completion_public_schema_only.py index bb371543..e046978b 100644 --- a/tests/test_smart_completion_public_schema_only.py +++ b/tests/test_smart_completion_public_schema_only.py @@ -1,5 +1,3 @@ -from __future__ import unicode_literals, print_function - from metadata import ( MetaData, alias, -- cgit v1.2.3