Skip to content

Commit

Permalink
get rid of six
Browse files Browse the repository at this point in the history
  • Loading branch information
ThomasWaldmann committed Apr 3, 2024
1 parent 3e270a1 commit dc32887
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 37 deletions.
11 changes: 4 additions & 7 deletions emeraldtree/html.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,14 +31,11 @@
# Tools to build element trees from HTML files.
##

import six
from six.moves import html_entities
from six.moves import html_parser
from html.parser import HTMLParser as HTMLParserBase
from html.entities import entitydefs as html_entitydefs

from . import tree

HTMLParserBase = html_parser.HTMLParser


##
# ElementTree builder for HTML source code. This builder converts an
Expand Down Expand Up @@ -148,7 +145,7 @@ def handle_charref(self, char):
# (Internal) Handles entity references.

def handle_entityref(self, name):
entity = html_entities.entitydefs.get(name)
entity = html_entitydefs.get(name)
if entity:
if len(entity) == 1:
entity = ord(entity)
Expand All @@ -165,7 +162,7 @@ def handle_entityref(self, name):
# (Internal) Handles character data.

def handle_data(self, data):
if isinstance(data, six.binary_type):
if isinstance(data, bytes):
# convert to unicode, but only if necessary
data = data.decode(self.encoding, "ignore")
self.__builder.data(data)
Expand Down
5 changes: 2 additions & 3 deletions emeraldtree/tests/test_html.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import six
from six import StringIO
from io import StringIO

from .. import html, tree

Expand All @@ -26,7 +25,7 @@ def test_read_text1():
assert isinstance(elem, tree.Element)
assert len(elem) == 1
assert elem[0] == 'b'
assert isinstance(elem[0], six.text_type)
assert isinstance(elem[0], str)

def test_read_text2():
elem = html.HTML('<a>b<c>d</c>d</a>')
Expand Down
16 changes: 8 additions & 8 deletions emeraldtree/tests/test_tree.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import six
from six import StringIO
from io import StringIO

import pytest

from emeraldtree.tree import *
Expand Down Expand Up @@ -345,19 +345,19 @@ def test_QName___init__():
qname = QName(u'a')
assert qname.uri is None
assert qname.name == u'a'
assert isinstance(qname.name, six.text_type)
assert six.text_type(qname) == u'a'
assert isinstance(qname.name, str)
assert str(qname) == u'a'

qname = QName(u'{b}a')
assert qname.uri == u'b'
assert isinstance(qname.uri, six.text_type)
assert isinstance(qname.uri, str)
assert qname.name == u'a'
assert six.text_type(qname) == u'{b}a'
assert str(qname) == u'{b}a'

qname = QName(u'a', u'b')
assert qname.uri == u'b'
assert qname.name == u'a'
assert six.text_type(qname) == u'{b}a'
assert str(qname) == u'{b}a'

pytest.raises(ValueError, QName, u'{bau')
pytest.raises(ValueError, QName, u'{b}a', u'c')
Expand Down Expand Up @@ -395,7 +395,7 @@ def test_XMLParser_text1():
assert isinstance(elem, Element)
assert len(elem) == 1
assert elem[0] == u'b'
assert isinstance(elem[0], six.text_type)
assert isinstance(elem[0], str)

def test_XMLParser_text2():
elem = XML(u'<a>b<c>d</c>d</a>')
Expand Down
28 changes: 13 additions & 15 deletions emeraldtree/tree.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,6 @@
# ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
# OF THIS SOFTWARE.

import six

__all__ = [
# public symbols
"Comment",
Expand Down Expand Up @@ -129,7 +127,7 @@ class Element(Node):

@property
def text(self):
if len(self) and isinstance(self[0], six.string_types):
if len(self) and isinstance(self[0], str):
return self[0]

##
Expand Down Expand Up @@ -372,7 +370,7 @@ def itertext(self):
if isinstance(e, Element):
for s in e.itertext():
yield s
elif isinstance(e, six.string_types):
elif isinstance(e, str):
yield e

def iter_elements(self):
Expand Down Expand Up @@ -448,7 +446,7 @@ def __init__(self, target, text = None):

PI = ProcessingInstruction

class QName(six.text_type):
class QName(str):
"""
QName wrapper. This can be used to wrap a QName attribute value, in
order to get proper namespace handling on output.
Expand All @@ -461,7 +459,7 @@ class QName(six.text_type):
__slots__ = 'name', 'uri'

def __new__(cls, name, uri=None):
text = name = six.text_type(name)
text = name = str(name)

if name[0] == '{':
if uri is not None:
Expand All @@ -473,12 +471,12 @@ def __new__(cls, name, uri=None):
name = name[i + 1:]

if uri is not None:
uri = six.text_type(uri)
uri = str(uri)
text = '{' + uri + '}' + name

ret = six.text_type.__new__(cls, text)
six.text_type.__setattr__(ret, 'name', name)
six.text_type.__setattr__(ret, 'uri', uri)
ret = str.__new__(cls, text)
str.__setattr__(ret, 'name', name)
str.__setattr__(ret, 'uri', uri)

return ret

Expand Down Expand Up @@ -1198,15 +1196,15 @@ def add_qname(qname):
tag = elem.tag
if isinstance(tag, QName):
add_qname(tag)
elif isinstance(tag, six.string_types):
elif isinstance(tag, str):
add_qname(QName(tag))
elif tag is not None:
self._raise_serialization_error(tag)

for key in six.iterkeys(elem.attrib):
for key in elem.attrib:
if isinstance(key, QName):
add_qname(key)
elif isinstance(key, six.string_types):
elif isinstance(key, str):
add_qname(QName(key))
elif key is not None:
self._raise_serialization_error(key)
Expand Down Expand Up @@ -1292,7 +1290,7 @@ def _attrib_string(self, d, qnames):
if isinstance(v, QName):
v = qnames[v]
else:
v = self._escape_attrib(six.text_type(v))
v = self._escape_attrib(str(v))
# FIXME: handle boolean attributes for HTML
result.append(u' %s="%s"' % (k, v))
return u''.join(result)
Expand Down Expand Up @@ -1322,7 +1320,7 @@ def _serialize_pi(self, write, elem):
write(u"<?%s?>" % text)

def _serialize_cdata(self, write, elem):
write(self._escape_cdata(six.text_type(elem)))
write(self._escape_cdata(str(elem)))

def serialize(self, write, elem, qnames, namespaces={}):
if isinstance(elem, Element):
Expand Down
6 changes: 2 additions & 4 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ license = {text="Python (MIT style)"}
where = ["."]

[build-system]
requires = ["setuptools", "wheel", "setuptools_scm[toml]>=1.0"]
requires = ["setuptools", "wheel", "setuptools_scm[toml]"]
build-backend = "setuptools.build_meta"

[tool.setuptools_scm]
Expand All @@ -42,8 +42,6 @@ legacy_tox_ini = """
envlist = py{39,310,311,312}
[testenv]
deps =
pytest
six
deps = pytest
commands = pytest -rs --pyargs {posargs:emeraldtree}
"""

0 comments on commit dc32887

Please sign in to comment.