Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 19 additions & 19 deletions json2xml/dicttoxml.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,15 @@
import os
from collections.abc import Callable, Sequence
from random import randint
from typing import Any, Dict, List, Optional, Tuple, Union
from typing import Any, Dict, Union

from defusedxml.minidom import parseString

DEBUGMODE = os.getenv("DEBUGMODE", False) # pragma: no cover
LOG = logging.getLogger("dicttoxml") # pragma: no cover


ids: List[str] = [] # initialize list of unique ids
ids: list[str] = [] # initialize list of unique ids


def make_id(element: str, start: int = 100000, end: int = 999999) -> str:
Expand Down Expand Up @@ -82,7 +82,7 @@ def get_xml_type(val: ELEMENT) -> str:
return type(val).__name__


def escape_xml(s: Union[str, numbers.Number]) -> str:
def escape_xml(s: str | numbers.Number) -> str:
if isinstance(s, str):
s = str(s) # avoid UnicodeDecodeError
s = s.replace("&", "&")
Expand Down Expand Up @@ -111,7 +111,7 @@ def key_is_valid_xml(key: str) -> bool:
return False


def make_valid_xml_name(key: str, attr: Dict[str, Any]) -> Tuple[str, Dict[str, Any]]:
def make_valid_xml_name(key: str, attr: dict[str, Any]) -> tuple[str, dict[str, Any]]:
"""Tests an XML name and fixes it if invalid"""
if DEBUGMODE: # pragma: no cover
LOG.info(
Expand Down Expand Up @@ -144,7 +144,7 @@ def make_valid_xml_name(key: str, attr: Dict[str, Any]) -> Tuple[str, Dict[str,
return key, attr


def wrap_cdata(s: Union[str, numbers.Number]) -> str:
def wrap_cdata(s: str | numbers.Number) -> str:
"""Wraps a string into CDATA sections"""
s = str(s).replace("]]>", "]]]]><![CDATA[>")
return "<![CDATA[" + s + "]]>"
Expand Down Expand Up @@ -219,8 +219,8 @@ def is_primitive_type(val: Any) -> bool:

def dict2xml_str(
attr_type: bool,
attr: Dict[str, Any],
item: Dict[str, Any],
attr: dict[str, Any],
item: dict[str, Any],
item_func: Callable[[str], str],
cdata: bool,
item_name: str,
Expand Down Expand Up @@ -258,7 +258,7 @@ def dict2xml_str(

def list2xml_str(
attr_type: bool,
attr: Dict[str, Any],
attr: dict[str, Any],
item: Sequence[Any],
item_func: Callable[[str], str],
cdata: bool,
Expand Down Expand Up @@ -287,8 +287,8 @@ def list2xml_str(


def convert_dict(
obj: Dict[str, Any],
ids: List[str],
obj: dict[str, Any],
ids: list[str],
parent: str,
attr_type: bool,
item_func: Callable[[str], str],
Expand All @@ -305,7 +305,7 @@ def convert_dict(
if LOG.getEffectiveLevel() <= logging.DEBUG:
LOG.debug(f' obj="{str(obj)}"')

output: List[str] = []
output: list[str] = []
addline = output.append

for key, val in obj.items():
Expand Down Expand Up @@ -376,7 +376,7 @@ def convert_dict(

def convert_list(
items: Sequence[Any],
ids: List[str],
ids: list[str],
parent: str,
attr_type: bool,
item_func: Callable[[str], str],
Expand All @@ -390,7 +390,7 @@ def convert_list(
if LOG.getEffectiveLevel() <= logging.DEBUG:
LOG.debug(f' items="{str(items)}"')

output: List[str] = []
output: list[str] = []
addline = output.append

item_name = item_func(parent)
Expand Down Expand Up @@ -477,9 +477,9 @@ def convert_list(

def convert_kv(
key: str,
val: Union[str, numbers.Number],
val: str | numbers.Number,
attr_type: bool,
attr: Dict[str, Any] = {},
attr: dict[str, Any] = {},
cdata: bool = False,
) -> str:
"""Converts a number or string into an XML element"""
Expand All @@ -496,7 +496,7 @@ def convert_kv(


def convert_bool(
key: str, val: bool, attr_type: bool, attr: Dict[str, Any] = {}, cdata: bool = False
key: str, val: bool, attr_type: bool, attr: dict[str, Any] = {}, cdata: bool = False
) -> str:
"""Converts a boolean into an XML element"""
if DEBUGMODE: # pragma: no cover
Expand All @@ -512,7 +512,7 @@ def convert_bool(


def convert_none(
key: str, attr_type: bool, attr: Dict[str, Any] = {}, cdata: bool = False
key: str, attr_type: bool, attr: dict[str, Any] = {}, cdata: bool = False
) -> str:
"""Converts a null value into an XML element"""
key, attr = make_valid_xml_name(key, attr)
Expand All @@ -524,10 +524,10 @@ def convert_none(


def dicttoxml(
obj: Dict[str, Any],
obj: dict[str, Any],
root: bool = True,
custom_root: str = "root",
ids: Optional[List[int]] = None,
ids: list[int] | None = None,
attr_type: bool = True,
item_wrap: bool = True,
item_func: Callable[[str], str] = default_item_func,
Expand Down
8 changes: 5 additions & 3 deletions json2xml/json2xml.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
from typing import Any, Dict, Optional
from __future__ import annotations

from typing import Any

from defusedxml.minidom import parseString
from pyexpat import ExpatError
Expand All @@ -14,7 +16,7 @@ class Json2xml:
"""
def __init__(
self,
data: Dict[str, Any],
data: dict[str, Any],
wrapper: str = "all",
root: bool = True,
pretty: bool = True,
Expand All @@ -28,7 +30,7 @@ def __init__(
self.root = root
self.item_wrap = item_wrap

def to_xml(self) -> Optional[Any]:
def to_xml(self) -> Any | None:
"""
Convert to xml using dicttoxml.dicttoxml and then pretty print it.
"""
Expand Down
9 changes: 5 additions & 4 deletions json2xml/utils.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from __future__ import annotations

"""Utils methods to convert XML data to dict from various sources"""
import json
from typing import Dict, Optional

import requests

Expand All @@ -21,7 +22,7 @@ class StringReadError(Exception):
pass


def readfromjson(filename: str) -> Dict[str, str]:
def readfromjson(filename: str) -> dict[str, str]:
"""
Reads a json string and emits json string
"""
Expand All @@ -38,7 +39,7 @@ def readfromjson(filename: str) -> Dict[str, str]:
raise JSONReadError("Invalid JSON File")


def readfromurl(url: str, params: Optional[Dict[str, str]] = None) -> Dict[str, str]:
def readfromurl(url: str, params: dict[str, str] | None = None) -> dict[str, str]:
"""
Loads json from an URL over the internets
"""
Expand All @@ -51,7 +52,7 @@ def readfromurl(url: str, params: Optional[Dict[str, str]] = None) -> Dict[str,
raise URLReadError("URL is not returning correct response")


def readfromstring(jsondata: str) -> Dict[str, str]:
def readfromstring(jsondata: str) -> dict[str, str]:
"""
Loads json from string
"""
Expand Down
Loading