Skip to content

Commit 31e0787

Browse files
committed
Remove six and replace all with py3 (#2)
1 parent 6754da6 commit 31e0787

13 files changed

Lines changed: 86 additions & 163 deletions

File tree

neuxml/utils/__init__.py

Whitespace-only changes.

neuxml/utils/compat.py

Lines changed: 0 additions & 24 deletions
This file was deleted.

neuxml/xmlmap/cerp.py

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,7 @@
2121
import logging
2222
import os
2323

24-
import six
25-
2624
from neuxml import xmlmap
27-
from neuxml.utils.compat import u
2825

2926
logger = logging.getLogger(__name__)
3027

@@ -281,7 +278,7 @@ def from_email_message(cls, message, local_id=None):
281278
payload = message.get_payload(decode=False)
282279

283280
# if not unicode, attempt to convert
284-
if isinstance(payload, six.binary_type):
281+
if isinstance(payload, bytes):
285282
charset = message.get_charset()
286283
# decode according to the specified character set, if any
287284
if charset is not None:
@@ -290,14 +287,14 @@ def from_email_message(cls, message, local_id=None):
290287

291288
# otherwise, just try to convert
292289
else:
293-
payload = u(payload)
290+
payload = str(payload)
294291

295292
# remove any control characters not allowed in XML
296293
control_char_map = dict.fromkeys(range(32))
297294
for i in [9, 10, 13]: # preserve horizontal tab, line feed, carriage return
298295
del control_char_map[i]
299296

300-
payload = u(payload).translate(control_char_map)
297+
payload = str(payload).translate(control_char_map)
301298

302299
result.single_body.body_content.content = payload
303300

neuxml/xmlmap/core.py

Lines changed: 12 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -16,16 +16,10 @@
1616

1717
from __future__ import unicode_literals
1818
import logging
19-
import os
20-
import warnings
21-
import urllib
22-
import time
19+
import io
2320
from lxml import etree
2421
from lxml.builder import ElementMaker
25-
import six
26-
from six.moves.urllib.request import urlopen
2722

28-
from neuxml.utils.compat import u
2923
from neuxml.xmlmap.fields import Field
3024

3125

@@ -193,7 +187,7 @@ def __new__(cls, name, bases, defined_attrs):
193187
# collect self-referential NodeFields so that we can resolve
194188
# them once we've created the new class
195189
node_class = getattr(field, 'node_class', None)
196-
if isinstance(node_class, six.string_types):
190+
if isinstance(node_class, str):
197191
if node_class in ('self', name):
198192
recursive_fields.append(field)
199193
else:
@@ -235,8 +229,7 @@ def create_field(xmlobject):
235229
return create_field
236230

237231

238-
@six.python_2_unicode_compatible
239-
class XmlObject(six.with_metaclass(XmlObjectType, object)):
232+
class XmlObject(object, metaclass=XmlObjectType):
240233

241234
"""
242235
A Python object wrapped around an XML node.
@@ -324,15 +317,15 @@ def __init__(self, node=None, context=None, **kwargs):
324317

325318
# xpath has no notion of a default namespace - omit any namespace with no prefix
326319
self.context = {'namespaces': dict([(prefix, ns) for prefix, ns
327-
in six.iteritems(nsmap) if prefix])}
320+
in nsmap.items() if prefix])}
328321

329322
if context is not None:
330323
self.context.update(context)
331324
if hasattr(self, 'ROOT_NAMESPACES'):
332325
# also include any root namespaces to guarantee that expected prefixes are available
333326
self.context['namespaces'].update(self.ROOT_NAMESPACES)
334327

335-
for field, value in six.iteritems(kwargs):
328+
for field, value in kwargs.items():
336329
# TODO (maybe): handle setting/creating list fields
337330
setattr(self, field, value)
338331

@@ -373,8 +366,8 @@ def xsl_transform(self, filename=None, xsl=None, return_type=None, **params):
373366
return_type = XmlObject
374367

375368
# automatically encode any string params as XSLT string parameters
376-
for key, val in six.iteritems(params):
377-
if isinstance(val, six.string_types):
369+
for key, val in params.items():
370+
if isinstance(val, str):
378371
params[key] = etree.XSLT.strparam(val)
379372

380373
parser = _get_xmlparser()
@@ -407,7 +400,7 @@ def xsl_transform(self, filename=None, xsl=None, return_type=None, **params):
407400
# empty xmlobject which will behave unexpectedly.
408401

409402
# text output does not include a root node, so check separately
410-
if issubclass(return_type, six.string_types):
403+
if issubclass(return_type, str):
411404
if result is None:
412405
logger.warning("XSL transform generated an empty result")
413406
return
@@ -421,14 +414,14 @@ def xsl_transform(self, filename=None, xsl=None, return_type=None, **params):
421414
return return_type(result.getroot())
422415

423416
def __str__(self):
424-
if isinstance(self.node, six.string_types):
417+
if isinstance(self.node, str):
425418
return self.node
426419
return self.node.xpath("normalize-space(.)")
427420

428421
def __string__(self):
429-
if isinstance(self.node, six.string_types):
422+
if isinstance(self.node, str):
430423
return self.node
431-
return u(self).encode('ascii', 'xmlcharrefreplace')
424+
return str(self).encode('ascii', 'xmlcharrefreplace')
432425

433426
def __eq__(self, other):
434427
# consider two xmlobjects equal if they are pointing to the same xml node
@@ -471,7 +464,7 @@ def _serialize(self, node, stream=None, pretty=False, xml_declaration=False):
471464
# actual logic of xml serialization
472465
if stream is None:
473466
string_mode = True
474-
stream = six.BytesIO()
467+
stream = io.BytesIO()
475468
else:
476469
string_mode = False
477470

neuxml/xmlmap/eadmap.py

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,6 @@
1717
from __future__ import unicode_literals
1818
from copy import deepcopy
1919

20-
import six
21-
2220
from neuxml import xmlmap
2321

2422
# xmlmap objects for various sections of an ead
@@ -62,7 +60,6 @@ class Section(_EadBase):
6260
":class:`Note`"
6361

6462

65-
@six.python_2_unicode_compatible
6663
class Heading(_EadBase):
6764
"""Generic xml object for headings used under `controlaccess`"""
6865
source = xmlmap.StringField("@source")
@@ -107,7 +104,6 @@ class ControlledAccessHeadings(Section):
107104
"list of :class:`ControlledAccessHeadings` - recursive mapping to `controlaccess`"
108105

109106

110-
@six.python_2_unicode_compatible
111107
class Container(_EadBase):
112108
"""
113109
Container - :class:`DescriptiveIdentification` subelement for locating materials.
@@ -123,7 +119,6 @@ def __str__(self):
123119
return self.value
124120

125121

126-
@six.python_2_unicode_compatible
127122
class DateField(_EadBase):
128123
"""
129124
DateField - for access to date and unitdate elements value and attributes.
@@ -312,7 +307,6 @@ def hasSeries(self):
312307
return False
313308

314309

315-
@six.python_2_unicode_compatible
316310
class Reference(_EadBase):
317311
"""Internal linking element that may contain text.
318312

neuxml/xmlmap/fields.py

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,7 @@
2121

2222
from lxml import etree
2323
from lxml.builder import ElementMaker
24-
import six
2524

26-
from neuxml.utils.compat import u
2725
from neuxml.xpath import ast, parse, serialize
2826

2927
__all__ = [
@@ -94,7 +92,7 @@ def to_xml(self, value):
9492
if value is None:
9593
return value
9694
else:
97-
return u(value)
95+
return str(value)
9896

9997

10098
class StringMapper(Mapper):
@@ -106,7 +104,7 @@ def __init__(self, normalize=False):
106104
def to_python(self, node):
107105
if node is None:
108106
return None
109-
if isinstance(node, six.string_types):
107+
if isinstance(node, str):
110108
return node
111109
return self.XPATH(node)
112110

@@ -117,7 +115,7 @@ def to_python(self, node):
117115
return None
118116
try:
119117
# xpath functions such as count return a float and must be converted to int
120-
if isinstance(node, six.string_types) or isinstance(node, float):
118+
if isinstance(node, str) or isinstance(node, float):
121119
return int(node)
122120

123121
return int(self.XPATH(node))
@@ -131,7 +129,7 @@ def to_python(self, node):
131129
if node is None:
132130
return None
133131
try:
134-
if isinstance(node, six.string_types):
132+
if isinstance(node, str):
135133
return float(node)
136134

137135
return float(self.XPATH(node))
@@ -155,7 +153,7 @@ def to_python(self, node):
155153
else:
156154
return None
157155

158-
if isinstance(node, six.string_types):
156+
if isinstance(node, str):
159157
value = node
160158
else:
161159
value = self.XPATH(node)
@@ -187,7 +185,7 @@ def __init__(self, format=None, normalize=False):
187185
def to_python(self, node):
188186
if node is None:
189187
return None
190-
if isinstance(node, six.string_types):
188+
if isinstance(node, str):
191189
rep = node
192190
else:
193191
rep = self.XPATH(node)
@@ -209,9 +207,9 @@ def to_python(self, node):
209207
def to_xml(self, dt):
210208
val = None
211209
if self.format is not None:
212-
val = u(dt.strftime(self.format))
210+
val = str(dt.strftime(self.format))
213211
else:
214-
val = u(dt.isoformat())
212+
val = str(dt.isoformat())
215213
return val
216214

217215

@@ -225,7 +223,7 @@ def __init__(self, format=None, normalize=False):
225223
def to_python(self, node):
226224
if node is None:
227225
return None
228-
if isinstance(node, six.string_types):
226+
if isinstance(node, str):
229227
rep = node
230228
elif hasattr(node, 'text'):
231229
rep = node.text
@@ -365,7 +363,7 @@ def _predicate_is_constructible(pred):
365363
if not _predicate_is_constructible(pred.left):
366364
return False
367365
if not isinstance(pred.right,
368-
(six.integer_types, six.string_types, ast.VariableReference)):
366+
(int, str, ast.VariableReference)):
369367
return False
370368

371369
# otherwise, i guess we're ok
@@ -730,7 +728,7 @@ def __ne__(self, other):
730728

731729
def _check_key_type(self, key):
732730
# check argument type for getitem, setitem, delitem
733-
if not isinstance(key, (slice, six.integer_types)):
731+
if not isinstance(key, (slice, int)):
734732
raise TypeError
735733
assert not isinstance(key, slice), "Slice indexing is not supported"
736734

neuxml/xmlmap/mods.py

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,6 @@
2020
(Metadata Object Description Schema).
2121
'''
2222

23-
from __future__ import unicode_literals
24-
import six
25-
2623
from neuxml import xmlmap
2724

2825
MODS_NAMESPACE = 'http://www.loc.gov/mods/v3'
@@ -42,7 +39,6 @@ class Common(xmlmap.XmlObject):
4239
XSD_SCHEMA = MODSv34_SCHEMA
4340
schema_validate = False
4441

45-
@six.python_2_unicode_compatible
4642
class Date(Common):
4743
''':class:`~neuxml.xmlmap.XmlObject` for MODS date element (common fields
4844
for the dates under mods:originInfo).'''
@@ -192,7 +188,7 @@ def __unicode__(self):
192188
# default text display of a name (excluding roles for now)
193189
# TODO: improve logic for converting to plain-text name
194190
# (e.g., for template display, setting as dc:creator, etc)
195-
return ' '.join([unicode(part) for part in self.name_parts])
191+
return ' '.join([str(part) for part in self.name_parts])
196192

197193
class Genre(Common):
198194
ROOT_NAME = 'genre'

neuxml/xpath/ast.py

Lines changed: 1 addition & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -23,22 +23,6 @@
2323
perhaps introspect ASTs returned by the parser.
2424
'''
2525

26-
from __future__ import unicode_literals
27-
import sys
28-
29-
30-
# python2/3 string type logic borrowed from six
31-
# NOTE: not importing six here because setup.py needs to generate
32-
# the parser at install time, when six installation is not yet available
33-
PY2 = sys.version_info[0] == 2
34-
PY3 = sys.version_info[0] == 3
35-
36-
if PY3:
37-
string_types = str
38-
else:
39-
string_types = basestring
40-
41-
4226
__all__ = [
4327
'serialize',
4428
'UnaryExpression',
@@ -64,7 +48,7 @@ def _serialize(xp_ast):
6448
if hasattr(xp_ast, '_serialize'):
6549
for tok in xp_ast._serialize():
6650
yield tok
67-
elif isinstance(xp_ast, string_types):
51+
elif isinstance(xp_ast, str):
6852
# strings in serialized xpath needed to be quoted
6953
# (e.g. for use in paths, comparisons, etc)
7054
# using repr to quote them; for unicode, the leading

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ classifiers = [
2323
"Topic :: Software Development :: Libraries :: Python Modules",
2424
"Topic :: Text Processing :: Markup :: XML",
2525
]
26-
dependencies = ["ply>=3.8", "lxml>=3.4", "six>=1.10", "rdflib>=3.0"]
26+
dependencies = ["ply>=3.8", "lxml>=3.4", "rdflib>=3.0"]
2727

2828
[project.optional-dependencies]
2929
dev = [

0 commit comments

Comments
 (0)