Skip to content

Drop python2 support #7

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

Open
wants to merge 1 commit 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
36 changes: 13 additions & 23 deletions anymarkup_core/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
import re
import traceback

import six
try:
import configobj
except ImportError:
Expand Down Expand Up @@ -95,7 +94,7 @@ def parse(inp, format=None, encoding='utf-8', force_types=True, interpolate=True
if hasattr(inp, 'read'):
proper_inp = inp.read()
# if proper_inp is unicode, encode it
if isinstance(proper_inp, six.text_type):
if isinstance(proper_inp, str):
proper_inp = proper_inp.encode(encoding)

# try to guess markup type
Expand All @@ -105,7 +104,7 @@ def parse(inp, format=None, encoding='utf-8', force_types=True, interpolate=True
fmt = _get_format(format, fname, proper_inp)

# make it look like file-like bytes-yielding object
proper_inp = six.BytesIO(proper_inp)
proper_inp = io.BytesIO(proper_inp)

try:
res = _do_parse(proper_inp, fmt, encoding, force_types, interpolate)
Expand Down Expand Up @@ -232,22 +231,17 @@ def _do_parse(inp, fmt, encoding, force_types, interpolate):
cfg = configobj.ConfigObj(inp, encoding=encoding, interpolation=interpolate)
res = cfg.dict()
elif fmt == 'json':
if six.PY3:
# python 3 json only reads from unicode objects
inp = io.TextIOWrapper(inp, encoding=encoding)
res = json.load(inp)
else:
res = json.load(inp, encoding=encoding)
# python 3 json only reads from unicode objects
inp = io.TextIOWrapper(inp, encoding=encoding)
res = json.load(inp)
elif fmt == 'json5':
if six.PY3:
inp = io.TextIOWrapper(inp, encoding=encoding)
inp = io.TextIOWrapper(inp, encoding=encoding)
res = json5.load(inp, encoding=encoding)
elif fmt == 'toml':
if not _is_utf8(encoding):
raise AnyMarkupError('toml is always utf-8 encoded according to specification')
if six.PY3:
# python 3 toml prefers unicode objects
inp = io.TextIOWrapper(inp, encoding=encoding)
# python 3 toml prefers unicode objects
inp = io.TextIOWrapper(inp, encoding=encoding)
res = toml.load(inp)
elif fmt == 'xml':
res = xmltodict.parse(inp, encoding=encoding)
Expand Down Expand Up @@ -336,9 +330,9 @@ def _ensure_proper_types(struct, encoding, force_types):
res = []
for i in struct:
res.append(_ensure_proper_types(i, encoding, force_types))
elif isinstance(struct, six.binary_type):
elif isinstance(struct, bytes):
res = struct.decode(encoding)
elif isinstance(struct, (six.text_type, type(None), type(True), six.integer_types, float)):
elif isinstance(struct, (str, type(None), type(True), int, float)):
res = struct
elif isinstance(struct, datetime.datetime):
# toml can parse datetime natively
Expand All @@ -347,11 +341,11 @@ def _ensure_proper_types(struct, encoding, force_types):
raise AnyMarkupError('internal error - unexpected type {0} in parsed markup'.
format(type(struct)))

if force_types and isinstance(res, six.text_type):
if force_types and isinstance(res, str):
res = _recognize_basic_types(res)
elif not (force_types or
isinstance(res, (dict, collections.OrderedDict, list, six.text_type))):
res = six.text_type(res)
isinstance(res, (dict, collections.OrderedDict, list, str))):
res = str(res)

return res

Expand All @@ -361,8 +355,6 @@ def _recognize_basic_types(s):
to a proper type and return it.
"""
tps = [int, float]
if not six.PY3: # compat for older versions of six that don't have PY2
tps.append(long)
for tp in tps:
try:
return tp(s)
Expand Down Expand Up @@ -498,5 +490,3 @@ def represent_str(dumper, data):
yaml.SafeLoader.add_constructor(u'tag:yaml.org,2002:omap', construct_ordereddict)
yaml.SafeDumper.add_representer(collections.OrderedDict, represent_ordereddict)
yaml.SafeDumper.add_representer(str, represent_str)
if six.PY2:
yaml.SafeDumper.add_representer(unicode, represent_str)
1 change: 0 additions & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
@@ -1 +0,0 @@
six
7 changes: 3 additions & 4 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,8 @@
'License :: OSI Approved :: BSD License',
'Operating System :: POSIX :: Linux',
'Programming Language :: Python',
'Programming Language :: Python :: 2.7',
'Programming Language :: Python :: 3.3',
'Programming Language :: Python :: 3.4',
'Programming Language :: Python :: 3.5',
'Programming Language :: Python :: 3.8',
'Programming Language :: Python :: 3.9',
'Programming Language :: Python :: 3.10',
]
)
5 changes: 2 additions & 3 deletions test/test_parse.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
import os

import pytest
import six
import toml

from anymarkup_core import *
Expand All @@ -23,8 +22,8 @@ def assert_unicode(self, struct):
elif isinstance(struct, list):
for i in struct:
self.assert_unicode(i)
elif isinstance(struct, (six.string_types, type(None), type(True), \
six.integer_types, float, datetime)):
elif isinstance(struct, (str, type(None), type(True), \
int, float, datetime)):
pass
else:
raise AssertionError('Unexpected type {0} in parsed structure'.format(type(struct)))
Expand Down
3 changes: 1 addition & 2 deletions test/test_serialize.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import os

import pytest
import six

from anymarkup_core import *

Expand All @@ -21,7 +20,7 @@ class TestSerialize(object):
fixtures = os.path.join(os.path.dirname(__file__), 'fixtures')

def _read_decode(self, file):
if isinstance(file, six.string_types):
if isinstance(file, str):
file = open(file, 'rb')
else:
file.seek(0)
Expand Down