Skip to content

Commit

Permalink
feat: Remove dependency on six (#1134)
Browse files Browse the repository at this point in the history
* feat: Remove six

* chore: Use context managers

* chore: Remove unnecessary ValueError suppression
  • Loading branch information
kesara authored Jul 1, 2024
1 parent a768f39 commit 14cf536
Show file tree
Hide file tree
Showing 10 changed files with 60 additions and 103 deletions.
1 change: 0 additions & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,4 @@ pycountry>=22.3.5
pyyaml>=5.3.1
requests>=2.5.0
setuptools>=24.2.0
six>=1.4.1
wcwidth>=0.2.5
1 change: 0 additions & 1 deletion setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,6 @@ install_requires = platformdirs>=3.6.0
pyyaml>=5.3.1
requests>=2.5.0
setuptools>=24.2.0
six>=1.4.1
wcwidth>=0.2.5

[options.package_data]
Expand Down
80 changes: 37 additions & 43 deletions xml2rfc/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
import re
import requests
import shutil
import six
import time
import xml2rfc.log
import xml2rfc.utils
Expand Down Expand Up @@ -72,7 +71,7 @@ def __init__(self, cache_path=None, library_dirs=None, source=None,

# Get directory of source
if self.source:
if isinstance(self.source, six.string_types):
if isinstance(self.source, str):
self.source_dir = os.path.abspath(os.path.dirname(self.source))
else:
self.source_dir = os.path.abspath(os.path.dirname(self.source.name))
Expand Down Expand Up @@ -458,7 +457,7 @@ def get(self, key, default=None):
if value == default:
return value
else:
return six.text_type(value)
return str(value)

class XmlRfcParser:

Expand Down Expand Up @@ -543,48 +542,43 @@ def parse(self, remove_comments=True, remove_pis=False, quiet=False, strip_cdata
text = text.replace(b'<rfc ', b'<rfc xmlns:%s="%s" ' % (b'xi', self.nsmap[b'xi']), 1)

# Get an iterating parser object
file = six.BytesIO(text)
file.name = self.source
context = lxml.etree.iterparse(file,
dtd_validation=False,
load_dtd=True,
attribute_defaults=True,
no_network=self.no_network,
remove_comments=remove_comments,
remove_pis=remove_pis,
remove_blank_text=True,
resolve_entities=False,
strip_cdata=strip_cdata,
events=("start",),
tag="rfc",
)
# resolver without knowledge of rfc_number:
caching_resolver = CachingResolver(cache_path=self.cache_path,
library_dirs=self.library_dirs,
templates_path=self.templates_path,
source=self.source,
no_network=self.no_network,
network_locs=self.network_locs,
verbose=self.verbose,
quiet=self.quiet,
options=self.options,
)
context.resolvers.add(caching_resolver)

# Get hold of the rfc number (if any) in the rfc element, so we can
# later resolve the "&rfc.number;" entity.
self.rfc_number = None
self.format_version = None
try:
with io.BytesIO(text) as file:
file.name = self.source
context = lxml.etree.iterparse(file,
dtd_validation=False,
load_dtd=True,
attribute_defaults=True,
no_network=self.no_network,
remove_comments=remove_comments,
remove_pis=remove_pis,
remove_blank_text=True,
resolve_entities=False,
strip_cdata=strip_cdata,
events=("start",),
tag="rfc",
)
# resolver without knowledge of rfc_number:
caching_resolver = CachingResolver(cache_path=self.cache_path,
library_dirs=self.library_dirs,
templates_path=self.templates_path,
source=self.source,
no_network=self.no_network,
network_locs=self.network_locs,
verbose=self.verbose,
quiet=self.quiet,
options=self.options,
)
context.resolvers.add(caching_resolver)

# Get hold of the rfc number (if any) in the rfc element, so we can
# later resolve the "&rfc.number;" entity.
self.rfc_number = None
self.format_version = None
for action, element in context:
if element.tag == "rfc":
self.rfc_number = element.attrib.get("number", None)
self.format_version = element.attrib.get("version", None)
break
except ValueError as e:
if e.message=="I/O operation on closed file":
pass

if self.format_version == "3":
self.default_dtd_path = None

Expand Down Expand Up @@ -621,9 +615,9 @@ def parse(self, remove_comments=True, remove_pis=False, quiet=False, strip_cdata
parser.set_element_class_lookup(element_lookup)

# Parse the XML file into a tree and create an rfc instance
file = six.BytesIO(text)
file.name = self.source
tree = lxml.etree.parse(file, parser)
with io.BytesIO(text) as file:
file.name = self.source
tree = lxml.etree.parse(file, parser)
xmlrfc = XmlRfc(tree, self.default_dtd_path, nsmap=self.nsmap)
xmlrfc.source = self.source

Expand Down
14 changes: 3 additions & 11 deletions xml2rfc/run.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
import lxml.etree
import os
import pycountry
import six
import sys

# If this script is renamed to 'xml2rfc.py' on a Windows system, the import
Expand Down Expand Up @@ -68,10 +67,7 @@ def print_country_help(options, parser):
ids = list(country_ids.values())
ids.sort()
print('Known country codes and country names for use with <country>:\n')
if six.PY3:
print(('\n'.join([ ' '+' - '.join(v) for v in ids])))
else:
print(('\n'.join([ ' '+' - '.join(v) for v in ids])).encode('utf-8'))
print(('\n'.join([ ' '+' - '.join(v) for v in ids])))
sys.exit()


Expand Down Expand Up @@ -767,12 +763,8 @@ def main():
xmlrfc.tree = prep.prep()
if xmlrfc.tree:
info = extract_anchor_info(xmlrfc.tree)
if six.PY2:
with open(filename, 'w') as fp:
json.dump(info, fp, indent=2, ensure_ascii=False, encoding='utf-8')
else:
with io.open(filename, 'w', encoding='utf-8') as fp:
json.dump(info, fp, indent=2, ensure_ascii=False)
with io.open(filename, 'w', encoding='utf-8') as fp:
json.dump(info, fp, indent=2, ensure_ascii=False)
if not options.quiet:
xml2rfc.log.write('Created file', filename)

Expand Down
9 changes: 4 additions & 5 deletions xml2rfc/util/unicode.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
# Unicode operations

import re
import six
import unicodedata

try:
Expand Down Expand Up @@ -117,8 +116,8 @@ def name(e):
try:
names = []
for c in e.text:
if isinstance(c, six.binary_type):
c = six.text_type(c, encoding='latin1')
if isinstance(c, bytes):
c = c.decode('latin1')
names.append(unicodedata.name(c, 'U+%04x'%ord(c)))
return ', '.join(names)
except ValueError as exc:
Expand Down Expand Up @@ -180,7 +179,7 @@ def ascii(e):
def isascii(u):
if u is None:
return True
if isinstance(u, six.text_type):
if isinstance(u, str):
t = u+''
for ch in [ '\u00a0', '\u200B', '\u2011', '\u2028', '\u2060', ]:
if ch in t:
Expand Down Expand Up @@ -438,5 +437,5 @@ def downcode(str, replacements=None, use_charrefs=True):
u'\u2010': '-',
}

controlchars = dict( (six.text_type(chr(i)), ' ') for i in range(0, 32) if not i in [ 9, 10, 13 ] )
controlchars = dict( (str(chr(i)), ' ') for i in range(0, 32) if not i in [ 9, 10, 13 ] )

6 changes: 1 addition & 5 deletions xml2rfc/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,13 @@

import base64
import re
import six
import sys
import textwrap

from collections import OrderedDict
from lxml.etree import _Comment, _ProcessingInstruction

if six.PY2:
from urllib import quote
else:
from urllib.request import quote
from urllib.request import quote

try:
from xml2rfc import debug
Expand Down
12 changes: 3 additions & 9 deletions xml2rfc/writers/html.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@
import lxml
import os
import re
#import sys
import six
import unicodedata
import xml2rfc

Expand All @@ -15,12 +13,8 @@
from lxml.html import html_parser
from lxml.html.builder import ElementMaker

if six.PY2:
from urllib import urlopen
from urlparse import urlparse, urljoin
elif six.PY3:
from urllib.request import urlopen
from urllib.parse import urlparse, urljoin
from urllib.request import urlopen
from urllib.parse import urlparse, urljoin

try:
from xml2rfc import debug
Expand Down Expand Up @@ -221,7 +215,7 @@ def get_bidi_alignment(address):
line = address[field]
if line:
for ch in line:
if isinstance(ch, six.text_type):
if isinstance(ch, str):
dir = unicodedata.bidirectional(ch)
if dir in ['R', 'AL']:
return 'right'
Expand Down
3 changes: 1 addition & 2 deletions xml2rfc/writers/paginated_txt.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

# Python libs
import calendar
import six

try:
import debug
Expand Down Expand Up @@ -100,7 +99,7 @@ def write_with_break_hint(self, writer, type, *args, **kwargs):

def needLines(self, count):
"""Deal with the PI directive needLines"""
if isinstance(count, six.string_types):
if isinstance(count, str):
if count.isdigit():
count = int(count)
else:
Expand Down
14 changes: 3 additions & 11 deletions xml2rfc/writers/preptool.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
import datetime
import os
import re
import six
import sys
import traceback as tb
import unicodedata
Expand All @@ -21,12 +20,8 @@
except ImportError:
pass

if six.PY2:
from urlparse import urlsplit, urlunsplit, urljoin, urlparse
from urllib import urlopen
elif six.PY3:
from urllib.parse import urlsplit, urlunsplit, urljoin, urlparse
from urllib.request import urlopen
from urllib.parse import urlsplit, urlunsplit, urljoin, urlparse
from urllib.request import urlopen

from lxml import etree

Expand Down Expand Up @@ -1851,10 +1846,7 @@ def element_artwork(self, e, p):
if scheme in ['http', 'https']:
with closing(urlopen(src)) as f:
data = f.read()
if six.PY2:
mediatype = f.info().gettype()
else:
mediatype = f.info().get_content_type()
mediatype = f.info().get_content_type()
src = build_dataurl(mediatype, data)
e.set('src', src)
elif scheme == 'file':
Expand Down
23 changes: 8 additions & 15 deletions xml2rfc/writers/text.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
import inspect
import re
import sys
import six
import textwrap

from codecs import open
Expand Down Expand Up @@ -54,7 +53,7 @@
# We don't use namedtuple for Line, because the resulting objects would be immutable:
class Line(object):
def __init__(self, text, elem):
assert isinstance(text, six.text_type)
assert isinstance(text, str)
self.text = text
self.elem = elem
self.page = None
Expand Down Expand Up @@ -177,22 +176,22 @@ def align(lines, how, width):
return lines

def mklines(arg, e):
if isinstance(arg, six.text_type):
if isinstance(arg, str):
# \u2028 and \u2029 are eliminated here, through splitlines()
lines = [ Line(t, e) for t in arg.splitlines() ]
else:
lines = arg
return lines

def mktextblock(arg):
if isinstance(arg, six.text_type):
if isinstance(arg, str):
text = arg
else:
text = '\u2028'.join([ l.text for l in arg ])
return text

def mktext(arg):
if isinstance(arg, six.text_type):
if isinstance(arg, str):
text = arg
else:
text = '\n'.join([ l.text for l in arg ])
Expand Down Expand Up @@ -313,15 +312,9 @@ def process(self):
tag = l.elem.tag if l.elem!=None else '-'
page = l.elem.page if l.elem!=None else '-'
if l.block:
if six.PY2:
sys.stderr.write(("%3d %10s %3d-%3d [%4s] %s\n" % (i, tag, l.block.beg, l.block.end, page, l.text)).encode('utf8'))
else:
sys.stderr.write(("%3d %10s %3d-%3d [%4s] %s\n" % (i, tag, l.block.beg, l.block.end, page, l.text)))
sys.stderr.write(("%3d %10s %3d-%3d [%4s] %s\n" % (i, tag, l.block.beg, l.block.end, page, l.text)))
else:
if six.PY2:
sys.stderr.write(("%3d %10s [%4s] %s\n" % (i, tag, page, l.text)).encode('utf8'))
else:
sys.stderr.write(("%3d %10s [%4s] %s\n" % (i, tag, page, l.text)))
sys.stderr.write(("%3d %10s [%4s] %s\n" % (i, tag, page, l.text)))
for i, l in enumerate(lines):
length = len(l.text)
if length > MAX_WIDTH:
Expand Down Expand Up @@ -525,7 +518,7 @@ def tjoin(self, text, e, width, **kwargs):
Render element e, then format and join it to text using the
appropriate settings in joiners.
'''
assert isinstance(text, six.text_type)
assert isinstance(text, str)
joiners = kwargs['joiners']
j = joiners[e.tag] if e.tag in joiners else joiners[None]
width -= j.indent + j.hang
Expand Down Expand Up @@ -3792,7 +3785,7 @@ def rreplace(s, old, new, max):
text = fill(text, width=width, **kwargs)
lines = mklines(text, e)
else:
if isinstance(text, six.binary_type):
if isinstance(text, bytes):
text = text.decode('utf-8')
lines = [ Line(text, e) ]
return lines
Expand Down

0 comments on commit 14cf536

Please sign in to comment.