|
24 | 24 | import logging |
25 | 25 | import threading |
26 | 26 | import typing |
27 | | -import xml.etree.ElementTree # nosec # for typing only |
28 | | - |
29 | | -# External Dependencies |
30 | | -import defusedxml.ElementTree # type: ignore |
31 | 27 |
|
32 | 28 | # Exec-Helpers Implementation |
33 | 29 | from exec_helpers import exceptions |
|
42 | 38 | import ruamel.yaml as ruamel_yaml # type: ignore |
43 | 39 | except ImportError: |
44 | 40 | ruamel_yaml = None # pylint: disable=invalid-name |
45 | | - |
| 41 | +try: |
| 42 | + import defusedxml.ElementTree # type: ignore |
| 43 | +except ImportError: |
| 44 | + defusedxml = None # pylint: disable=invalid-name |
46 | 45 | try: |
47 | 46 | # noinspection PyPackageRequirements |
48 | 47 | import lxml.etree # type: ignore # nosec |
49 | 48 | except ImportError: |
50 | 49 | lxml = None # pylint: disable=invalid-name |
51 | 50 |
|
| 51 | +if typing.TYPE_CHECKING: |
| 52 | + import xml.etree.ElementTree # nosec # for typing only |
| 53 | + |
52 | 54 | LOGGER: logging.Logger = logging.getLogger(__name__) |
53 | 55 |
|
54 | 56 |
|
@@ -551,12 +553,15 @@ def stdout_yaml(self) -> typing.Any: |
551 | 553 | return self.__deserialize(fmt="yaml") |
552 | 554 |
|
553 | 555 | @property |
554 | | - def stdout_xml(self) -> xml.etree.ElementTree.Element: |
| 556 | + def stdout_xml(self) -> "xml.etree.ElementTree.Element": |
555 | 557 | """XML from stdout. |
556 | 558 |
|
557 | 559 | :rtype: xml.etree.ElementTree.Element |
558 | 560 | :raises DeserializeValueError: STDOUT can not be deserialized as XML |
| 561 | + :raises AttributeError: defusedxml is not installed |
559 | 562 | """ |
| 563 | + if defusedxml is None: |
| 564 | + raise AttributeError("defusedxml is not installed -> attribute is not functional by security reasons.") |
560 | 565 | with self.stdout_lock: |
561 | 566 | return self.__deserialize(fmt="xml") # type: ignore |
562 | 567 |
|
@@ -591,11 +596,12 @@ def __dir__(self) -> typing.List[str]: |
591 | 596 | "stdout_lines", |
592 | 597 | "stderr_lines", |
593 | 598 | "stdout_json", |
594 | | - "stdout_xml", |
595 | 599 | "lock", |
596 | 600 | ] |
597 | 601 | if yaml is not None or ruamel_yaml is not None: |
598 | 602 | content.append("stdout_yaml") |
| 603 | + if defusedxml is not None: |
| 604 | + content.append("stdout_xml") |
599 | 605 | if lxml is not None: |
600 | 606 | content.append("stdout_lxml") |
601 | 607 | return content |
|
0 commit comments