Skip to content

Commit

Permalink
fix: we support Python3.7+ now (#101)
Browse files Browse the repository at this point in the history
  • Loading branch information
vinitkumar authored Jan 29, 2022
1 parent fb8e2f0 commit 5cc818a
Show file tree
Hide file tree
Showing 8 changed files with 21 additions and 29 deletions.
15 changes: 7 additions & 8 deletions docs/conf.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
# json2xml documentation build configuration file, created by
# sphinx-quickstart on Fri Jun 9 13:47:02 2017.
Expand Down Expand Up @@ -47,9 +46,9 @@
master_doc = 'index'

# General information about the project.
project = u'json2xml'
copyright = u"2019, Vinit Kumar"
author = u"Vinit Kumar"
project = 'json2xml'
copyright = "2019, Vinit Kumar"
author = "Vinit Kumar"

# The version info for the project you're documenting, acts as replacement
# for |version| and |release|, also used in various other places throughout
Expand Down Expand Up @@ -129,8 +128,8 @@
# [howto, manual, or own class]).
latex_documents = [
(master_doc, 'json2xml.tex',
u'json2xml Documentation',
u'Vinit Kumar', 'manual'),
'json2xml Documentation',
'Vinit Kumar', 'manual'),
]


Expand All @@ -140,7 +139,7 @@
# (source start file, name, description, authors, manual section).
man_pages = [
(master_doc, 'json2xml',
u'json2xml Documentation',
'json2xml Documentation',
[author], 1)
]

Expand All @@ -152,7 +151,7 @@
# dir menu entry, description, category)
texinfo_documents = [
(master_doc, 'json2xml',
u'json2xml Documentation',
'json2xml Documentation',
author,
'json2xml',
'One line description of project.',
Expand Down
2 changes: 0 additions & 2 deletions json2xml/__init__.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
# -*- coding: utf-8 -*-

"""Top-level package for json2xml."""

__author__ = """Vinit Kumar"""
Expand Down
26 changes: 13 additions & 13 deletions json2xml/dicttoxml.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@

def make_id(element, start=100000, end=999999):
"""Returns a random integer"""
return "%s_%s" % (element, randint(start, end))
return f"{element}_{randint(start, end)}"


def get_unique_id(element):
Expand Down Expand Up @@ -78,14 +78,14 @@ def escape_xml(s: str) -> str:

def make_attrstring(attr):
"""Returns an attribute string in the form key="val" """
attrstring = " ".join(['%s="%s"' % (k, v) for k, v in attr.items()])
return "%s%s" % (" " if attrstring != "" else "", attrstring)
attrstring = " ".join([f'{k}="{v}"' for k, v in attr.items()])
return "{}{}".format(" " if attrstring != "" else "", attrstring)


def key_is_valid_xml(key):
"""Checks that a key is a valid XML name"""
LOG.info('Inside key_is_valid_xml(). Testing "%s"' % (str(key)))
test_xml = '<?xml version="1.0" encoding="UTF-8" ?><%s>foo</%s>' % (key, key)
test_xml = f'<?xml version="1.0" encoding="UTF-8" ?><{key}>foo</{key}>'
try:
parseString(test_xml)
return True
Expand Down Expand Up @@ -137,7 +137,7 @@ def convert(obj, ids, attr_type, item_func, cdata, item_wrap, parent="root"):
based on their data type"""

LOG.info(
'Inside convert(). obj type is: "%s", obj="%s"' % (type(obj).__name__, str(obj))
f'Inside convert(). obj type is: "{type(obj).__name__}", obj="{str(obj)}"'
)

item_name = item_func(parent)
Expand All @@ -160,7 +160,7 @@ def convert(obj, ids, attr_type, item_func, cdata, item_wrap, parent="root"):
if isinstance(obj, collections.abc.Iterable):
return convert_list(obj, ids, parent, attr_type, item_func, cdata, item_wrap)

raise TypeError("Unsupported data type: %s (%s)" % (obj, type(obj).__name__))
raise TypeError(f"Unsupported data type: {obj} ({type(obj).__name__})")


def convert_dict(obj, ids, parent, attr_type, item_func, cdata, item_wrap):
Expand Down Expand Up @@ -225,7 +225,7 @@ def convert_dict(obj, ids, parent, attr_type, item_func, cdata, item_wrap):

else:
raise TypeError(
"Unsupported data type: %s (%s)" % (val, type(val).__name__)
f"Unsupported data type: {val} ({type(val).__name__})"
)

return "".join(output)
Expand All @@ -247,7 +247,7 @@ def convert_list(items, ids, parent, attr_type, item_func, cdata, item_wrap):
'Looping inside convert_list(): item="%s", item_name="%s", type="%s"'
% (str(item), item_name, type(item).__name__)
)
attr = {} if not ids else {"id": "%s_%s" % (this_id, i + 1)}
attr = {} if not ids else {"id": f"{this_id}_{i + 1}"}
if isinstance(item, numbers.Number) or isinstance(item, str):
if item_wrap:
addline(convert_kv(key=item_name, val=item, attr_type=attr_type, attr=attr, cdata=cdata))
Expand Down Expand Up @@ -359,7 +359,7 @@ def convert_list(items, ids, parent, attr_type, item_func, cdata, item_wrap):

else:
raise TypeError(
"Unsupported data type: %s (%s)" % (item, type(item).__name__)
f"Unsupported data type: {item} ({type(item).__name__})"
)
return "".join(output)

Expand All @@ -376,7 +376,7 @@ def convert_kv(key, val, attr_type, attr={}, cdata: bool = False):
if attr_type:
attr["type"] = get_xml_type(val)
attrstring = make_attrstring(attr)
return "<%s%s>%s</%s>" % (
return "<{}{}>{}</{}>".format(
key,
attrstring,
wrap_cdata(val) if cdata else escape_xml(val),
Expand All @@ -396,7 +396,7 @@ def convert_bool(key, val, attr_type, attr={}, cdata=False):
if attr_type:
attr["type"] = get_xml_type(val)
attrstring = make_attrstring(attr)
return "<%s%s>%s</%s>" % (key, attrstring, str(val).lower(), key)
return f"<{key}{attrstring}>{str(val).lower()}</{key}>"


def convert_none(key, val, attr_type, attr={}, cdata=False):
Expand All @@ -408,7 +408,7 @@ def convert_none(key, val, attr_type, attr={}, cdata=False):
if attr_type:
attr["type"] = get_xml_type(val)
attrstring = make_attrstring(attr)
return "<%s%s></%s>" % (key, attrstring, key)
return f"<{key}{attrstring}></{key}>"


def dicttoxml(
Expand Down Expand Up @@ -446,7 +446,7 @@ def dicttoxml(
output = []
if root:
output.append('<?xml version="1.0" encoding="UTF-8" ?>')
output.append('<%s>%s</%s>' % (
output.append('<{}>{}</{}>'.format(
custom_root,
convert(obj, ids, attr_type, item_func, cdata, item_wrap, parent=custom_root),
custom_root
Expand Down
1 change: 0 additions & 1 deletion json2xml/json2xml.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
# -*- coding: utf-8 -*-
from typing import Optional, Any
from defusedxml.minidom import parseString
from json2xml import dicttoxml
Expand Down
2 changes: 1 addition & 1 deletion json2xml/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ def readfromjson(filename: str) -> Dict[str, str]:
except ValueError as exp:
print(exp)
raise JSONReadError
except IOError as exp:
except OSError as exp:
print(exp)
raise JSONReadError("Invalid JSON File")

Expand Down
1 change: 0 additions & 1 deletion setup.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*-

"""The setup script."""

Expand Down
2 changes: 0 additions & 2 deletions tests/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1 @@
# -*- coding: utf-8 -*-

"""Unit test package for json2xml."""
1 change: 0 additions & 1 deletion tests/test_json2xml.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*-

"""Tests for `json2xml` package."""

Expand Down

0 comments on commit 5cc818a

Please sign in to comment.