|
5 | 5 | import numbers |
6 | 6 | from collections.abc import Callable, Sequence |
7 | 7 | from random import SystemRandom |
8 | | -from typing import Any, Dict, Union |
| 8 | +from typing import Any, Dict, Union, TypeAlias |
9 | 9 |
|
10 | 10 | from defusedxml.minidom import parseString |
11 | 11 |
|
|
14 | 14 | # Set up logging |
15 | 15 | LOG = logging.getLogger("dicttoxml") |
16 | 16 |
|
| 17 | +XML_TYPES = {"str", "unicode", "int", "long", "float", "bool", "number", "null"} |
| 18 | +XMLValue: TypeAlias = Union[ |
| 19 | + str, int, float, bool, numbers.Number, |
| 20 | + Sequence[str], datetime.datetime, datetime.date, |
| 21 | + None, dict[str, Any] |
| 22 | +] |
| 23 | + |
17 | 24 | _RANDOM = SystemRandom() |
18 | 25 |
|
19 | 26 | def make_id(element: str, start: int = 100000, end: int = 999999) -> str: |
@@ -77,24 +84,18 @@ def get_xml_type(val: ELEMENT) -> str: |
77 | 84 | Returns: |
78 | 85 | str: The XML type. |
79 | 86 | """ |
80 | | - if val is not None: |
81 | | - if type(val).__name__ in ("str", "unicode"): |
82 | | - return "str" |
83 | | - if type(val).__name__ in ("int", "long"): |
84 | | - return "int" |
85 | | - if type(val).__name__ == "float": |
86 | | - return "float" |
87 | | - if type(val).__name__ == "bool": |
88 | | - return "bool" |
89 | | - if isinstance(val, numbers.Number): |
90 | | - return "number" |
91 | | - if isinstance(val, dict): |
92 | | - return "dict" |
93 | | - if isinstance(val, Sequence): |
94 | | - return "list" |
95 | | - else: |
| 87 | + if val is None: |
96 | 88 | return "null" |
97 | | - return type(val).__name__ |
| 89 | + type_name = type(val).__name__ |
| 90 | + if type_name in XML_TYPES: |
| 91 | + return type_name |
| 92 | + if isinstance(val, numbers.Number): |
| 93 | + return "number" |
| 94 | + if isinstance(val, dict): |
| 95 | + return "dict" |
| 96 | + if isinstance(val, Sequence): |
| 97 | + return "list" |
| 98 | + return type_name |
98 | 99 |
|
99 | 100 |
|
100 | 101 | def escape_xml(s: str | numbers.Number) -> str: |
|
0 commit comments