From 5cc818ae37f0d025fb400ffe68a315ddcdc6c9f6 Mon Sep 17 00:00:00 2001 From: Vinit Kumar Date: Sat, 29 Jan 2022 15:05:16 +0530 Subject: [PATCH] fix: we support Python3.7+ now (#101) --- docs/conf.py | 15 +++++++-------- json2xml/__init__.py | 2 -- json2xml/dicttoxml.py | 26 +++++++++++++------------- json2xml/json2xml.py | 1 - json2xml/utils.py | 2 +- setup.py | 1 - tests/__init__.py | 2 -- tests/test_json2xml.py | 1 - 8 files changed, 21 insertions(+), 29 deletions(-) diff --git a/docs/conf.py b/docs/conf.py index 55b0a03..a971300 100755 --- a/docs/conf.py +++ b/docs/conf.py @@ -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. @@ -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 @@ -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'), ] @@ -140,7 +139,7 @@ # (source start file, name, description, authors, manual section). man_pages = [ (master_doc, 'json2xml', - u'json2xml Documentation', + 'json2xml Documentation', [author], 1) ] @@ -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.', diff --git a/json2xml/__init__.py b/json2xml/__init__.py index 5ceb6aa..fb1a494 100644 --- a/json2xml/__init__.py +++ b/json2xml/__init__.py @@ -1,5 +1,3 @@ -# -*- coding: utf-8 -*- - """Top-level package for json2xml.""" __author__ = """Vinit Kumar""" diff --git a/json2xml/dicttoxml.py b/json2xml/dicttoxml.py index 140a013..44ce8df 100755 --- a/json2xml/dicttoxml.py +++ b/json2xml/dicttoxml.py @@ -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): @@ -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 = '<%s>foo' % (key, key) + test_xml = f'<{key}>foo' try: parseString(test_xml) return True @@ -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) @@ -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): @@ -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) @@ -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)) @@ -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) @@ -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" % ( + return "<{}{}>{}".format( key, attrstring, wrap_cdata(val) if cdata else escape_xml(val), @@ -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" % (key, attrstring, str(val).lower(), key) + return f"<{key}{attrstring}>{str(val).lower()}" def convert_none(key, val, attr_type, attr={}, cdata=False): @@ -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>" % (key, attrstring, key) + return f"<{key}{attrstring}>" def dicttoxml( @@ -446,7 +446,7 @@ def dicttoxml( output = [] if root: output.append('') - output.append('<%s>%s' % ( + output.append('<{}>{}'.format( custom_root, convert(obj, ids, attr_type, item_func, cdata, item_wrap, parent=custom_root), custom_root diff --git a/json2xml/json2xml.py b/json2xml/json2xml.py index 2574d7a..d7d92b5 100644 --- a/json2xml/json2xml.py +++ b/json2xml/json2xml.py @@ -1,4 +1,3 @@ -# -*- coding: utf-8 -*- from typing import Optional, Any from defusedxml.minidom import parseString from json2xml import dicttoxml diff --git a/json2xml/utils.py b/json2xml/utils.py index 480143b..a0fd6a4 100644 --- a/json2xml/utils.py +++ b/json2xml/utils.py @@ -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") diff --git a/setup.py b/setup.py index ba1d227..c4ccde3 100644 --- a/setup.py +++ b/setup.py @@ -1,5 +1,4 @@ #!/usr/bin/env python -# -*- coding: utf-8 -*- """The setup script.""" diff --git a/tests/__init__.py b/tests/__init__.py index c01515b..30c8951 100644 --- a/tests/__init__.py +++ b/tests/__init__.py @@ -1,3 +1 @@ -# -*- coding: utf-8 -*- - """Unit test package for json2xml.""" diff --git a/tests/test_json2xml.py b/tests/test_json2xml.py index 5b65b68..334d82a 100644 --- a/tests/test_json2xml.py +++ b/tests/test_json2xml.py @@ -1,5 +1,4 @@ #!/usr/bin/env python -# -*- coding: utf-8 -*- """Tests for `json2xml` package."""