Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

on Python3, use the new unittest.mock from standard library #48

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions pgxnclient/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,15 @@

# This file is part of the PGXN client

from six.moves.urllib.parse import urlencode
from urllib.parse import urlencode

from pgxnclient import network
from pgxnclient.utils import load_json
from pgxnclient.errors import NetworkError, NotFound, ResourceNotFound
from pgxnclient.utils.uri import expand_template


class Api(object):
class Api:
def __init__(self, mirror):
self.mirror = mirror

Expand Down
2 changes: 1 addition & 1 deletion pgxnclient/archive.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ def from_file(filename):
)


class Archive(object):
class Archive:
"""Base class to handle archives."""

def __init__(self, filename):
Expand Down
16 changes: 7 additions & 9 deletions pgxnclient/commands/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,6 @@
import argparse
from subprocess import Popen, PIPE

import six

from pgxnclient.utils import load_json, find_executable

from pgxnclient import __version__
Expand Down Expand Up @@ -148,7 +146,7 @@ def __init__(cls, name, bases, dct):
super(CommandType, cls).__init__(name, bases, dct)


class Command(six.with_metaclass(CommandType, object)):
class Command(metaclass=CommandType):
"""
Base class to implement client commands.

Expand Down Expand Up @@ -254,7 +252,7 @@ def confirm(self, prompt):
return True

while 1:
ans = six.moves.input(_("%s [y/N] ") % prompt)
ans = input(_("%s [y/N] ") % prompt)
if _('no').startswith(ans.lower()):
raise UserAbort(_("operation interrupted on user request"))
elif _('yes').startswith(ans.lower()):
Expand All @@ -273,7 +271,7 @@ def popen(self, cmd, *args, **kwargs):
try:
return Popen(cmd, *args, **kwargs)
except OSError as e:
if not isinstance(cmd, six.string_types):
if not isinstance(cmd, str):
cmd = ' '.join(cmd)
msg = _("%s running command: %s") % (e, cmd)
raise ProcessError(msg)
Expand Down Expand Up @@ -541,7 +539,7 @@ def get_spec(self, **kwargs):
return super(WithSpecUrl, self).get_spec(**kwargs)


class WithPgConfig(object):
class WithPgConfig:
"""
Mixin to implement commands that should query :program:`pg_config`.
"""
Expand Down Expand Up @@ -654,7 +652,7 @@ def run_make(self, cmd, dir, env=None, sudo=None):
[self.get_make(), 'PG_CONFIG=%s' % self.get_pg_config()]
)

if isinstance(cmd, six.string_types):
if isinstance(cmd, str):
cmdline.append(cmd)
else: # a list
cmdline.extend(cmd)
Expand Down Expand Up @@ -713,7 +711,7 @@ def _find_default_make(self):
return 'gmake'


class WithSudo(object):
class WithSudo:
"""
Mixin to implement commands that may invoke sudo.
"""
Expand Down Expand Up @@ -748,7 +746,7 @@ def customize_parser(self, parser, subparsers, **kwargs):
return subp


class WithDatabase(object):
class WithDatabase:
"""
Mixin to implement commands that should communicate to a database.
"""
Expand Down
4 changes: 1 addition & 3 deletions pgxnclient/commands/info.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,6 @@
import textwrap
import xml.sax.saxutils as saxutils

import six

from pgxnclient import SemVer
from pgxnclient.i18n import _, N_
from pgxnclient.utils import emit
Expand Down Expand Up @@ -138,7 +136,7 @@ def clean_excerpt(self, excerpt):

# Convert numerical entities
excerpt = re.sub(
r'\&\#(\d+)\;', lambda c: six.unichr(int(c.group(1))), excerpt
r'\&\#(\d+)\;', lambda c: chr(int(c.group(1))), excerpt
)

# Hilight found terms
Expand Down
4 changes: 1 addition & 3 deletions pgxnclient/commands/install.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,6 @@
import tempfile
from subprocess import PIPE

import six

from pgxnclient import SemVer
from pgxnclient import archive
from pgxnclient import network
Expand Down Expand Up @@ -337,7 +335,7 @@ def load_sql(self, filename=None, data=None):
logger.debug('running sql command: "%s"', tdata)
p = self.popen(cmdline, stdin=PIPE)
# for Python 3: just assume default encoding will do
if isinstance(data, six.text_type):
if isinstance(data, str):
data = data.encode()
p.communicate(data)

Expand Down
6 changes: 3 additions & 3 deletions pgxnclient/network.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@
# This file is part of the PGXN client

import os
from six.moves.urllib.request import build_opener
from six.moves.urllib.error import HTTPError, URLError
from six.moves.urllib.parse import urlsplit
from urllib.request import build_opener
from urllib.error import HTTPError, URLError
from urllib.parse import urlsplit
from itertools import count
from contextlib import closing

Expand Down
4 changes: 2 additions & 2 deletions pgxnclient/spec.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

import os
import re
from six.moves.urllib.parse import unquote_plus
from urllib.parse import unquote_plus
import operator as _op

from pgxnclient.i18n import _
Expand All @@ -19,7 +19,7 @@
from pgxnclient.utils.strings import Term


class Spec(object):
class Spec:
"""A name together with a range of versions."""

# Available release statuses.
Expand Down
9 changes: 2 additions & 7 deletions pgxnclient/utils/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,6 @@

# This file is part of the PGXN client


from __future__ import print_function

__all__ = ['emit', 'load_json', 'load_jsons', 'sha1', 'find_executable']


Expand All @@ -20,8 +17,6 @@
# Import the sha1 object without warnings
from hashlib import sha1

import six


def emit(s=b'', file=None):
"""
Expand All @@ -36,7 +31,7 @@ def emit(s=b'', file=None):

enc = file.encoding or 'ascii'

if isinstance(s, six.text_type):
if isinstance(s, str):
s = s.encode(enc, 'replace')

# OTOH, printing bytes on Py3 to stdout/stderr will barf as well...
Expand All @@ -50,7 +45,7 @@ def emit(s=b'', file=None):

def load_json(f):
data = f.read()
if not isinstance(data, six.text_type):
if not isinstance(data, str):
data = data.decode('utf-8')
return load_jsons(data)

Expand Down
9 changes: 4 additions & 5 deletions pgxnclient/utils/uri.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,7 @@
"""

import re
import six
from six.moves.urllib.parse import quote
from urllib.parse import quote

__all__ = ["expand_template", "TemplateSyntaxError"]

Expand Down Expand Up @@ -120,7 +119,7 @@ def parse_expansion(expansion):
def percent_encode(values):
rv = {}
for k, v in values.items():
if isinstance(v, six.string_types):
if isinstance(v, str):
rv[k] = quote(v)
else:
rv[k] = [quote(s) for s in v]
Expand All @@ -133,13 +132,13 @@ def percent_encode(values):
#


class _operators(object):
class _operators:
@staticmethod
def opt(variables, arg, values):
for k in variables.keys():
v = values.get(k, None)
if v is None or (
not isinstance(v, six.string_types) and len(v) == 0
not isinstance(v, str) and len(v) == 0
):
continue
else:
Expand Down
16 changes: 7 additions & 9 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,12 @@
long_description = f.read()

# External dependencies, depending on the Python version
requires = ['six']
requires = []
setup_requires = ['pytest-runner']
tests_require = ['mock', 'pytest']
tests_require = ['pytest']

if sys.version_info < (2, 7):
raise ValueError("PGXN client requires at least Python 2.7")
if (3,) < sys.version_info < (3, 4):
raise ValueError("PGXN client requires at least Python 3.4")
if sys.version_info < (3, 6):
raise ValueError("PGXN client requires at least Python 3.6")


classifiers = """
Expand All @@ -47,7 +45,7 @@
Intended Audience :: System Administrators
License :: OSI Approved :: BSD License
Operating System :: POSIX
Programming Language :: Python :: 2
Programming Language :: Python
Programming Language :: Python :: 3
Topic :: Database
"""
Expand Down Expand Up @@ -105,7 +103,7 @@ def fix_script_hashbang(self, filename):
},
license='BSD',
# NOTE: keep consistent with docs/install.txt
python_requires='>=2.7,!=3.0.*,!=3.1.*,!=3.2.*,!=3.3.*',
python_requires='>=3.6',
packages=find_packages(exclude=["tests"]),
package_data={'pgxnclient': ['libexec/*']},
entry_points={
Expand All @@ -121,5 +119,5 @@ def fix_script_hashbang(self, filename):
tests_require=tests_require,
version=version,
cmdclass={'build_py': CustomBuildPy},
extras_require={'dev': ['pytest', 'mock', 'black']},
extras_require={'dev': ['pytest', 'black']},
)
8 changes: 0 additions & 8 deletions tests/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,5 @@

import unittest


# fix unittest maintainers stubborness: see Python issue #9424
if unittest.TestCase.assert_ is not unittest.TestCase.assertTrue:
# Vaffanculo, Wolf
unittest.TestCase.assert_ = unittest.TestCase.assertTrue
unittest.TestCase.assertEquals = unittest.TestCase.assertEqual


if __name__ == '__main__':
unittest.main()
16 changes: 8 additions & 8 deletions tests/test_archives.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,13 @@ class TestArchive(unittest.TestCase):
def test_from_file_zip(self):
fn = get_test_filename('foobar-0.42.1.zip')
a = archive.from_file(fn)
self.assert_(isinstance(a, zip.ZipArchive))
self.assertTrue(isinstance(a, zip.ZipArchive))
self.assertEqual(a.filename, fn)

def test_from_file_tar(self):
fn = get_test_filename('foobar-0.42.1.tar.gz')
a = archive.from_file(fn)
self.assert_(isinstance(a, tar.TarArchive))
self.assertTrue(isinstance(a, tar.TarArchive))
self.assertEqual(a.filename, fn)

def test_from_file_unknown(self):
Expand All @@ -30,39 +30,39 @@ class TestZipArchive(unittest.TestCase):
def test_can_open(self):
fn = get_test_filename('foobar-0.42.1.zip')
a = zip.ZipArchive(fn)
self.assert_(a.can_open())
self.assertTrue(a.can_open())
a.open()
a.close()

def test_can_open_noext(self):
fn = get_test_filename('zip.ext')
a = zip.ZipArchive(fn)
self.assert_(a.can_open())
self.assertTrue(a.can_open())
a.open()
a.close()

def test_cannot_open(self):
fn = get_test_filename('foobar-0.42.1.tar.gz')
a = zip.ZipArchive(fn)
self.assert_(not a.can_open())
self.assertTrue(not a.can_open())


class TestTarArchive(unittest.TestCase):
def test_can_open(self):
fn = get_test_filename('foobar-0.42.1.tar.gz')
a = tar.TarArchive(fn)
self.assert_(a.can_open())
self.assertTrue(a.can_open())
a.open()
a.close()

def test_can_open_noext(self):
fn = get_test_filename('tar.ext')
a = tar.TarArchive(fn)
self.assert_(a.can_open())
self.assertTrue(a.can_open())
a.open()
a.close()

def test_cannot_open(self):
fn = get_test_filename('foobar-0.42.1.zip')
a = tar.TarArchive(fn)
self.assert_(not a.can_open())
self.assertTrue(not a.can_open())
Loading