Compare commits
3 commits
1a7359fc3c
...
3840b71812
| Author | SHA1 | Date | |
|---|---|---|---|
| 3840b71812 | |||
| 910234ed65 | |||
| 9faa2d2e2e |
3 changed files with 11 additions and 10 deletions
|
|
@ -2,6 +2,7 @@ from __future__ import annotations
|
||||||
|
|
||||||
import argparse
|
import argparse
|
||||||
import sys
|
import sys
|
||||||
|
from defusedxml import defuse_stdlib
|
||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
|
|
||||||
from .core import (
|
from .core import (
|
||||||
|
|
@ -47,6 +48,7 @@ def _build_arg_parser() -> argparse.ArgumentParser:
|
||||||
|
|
||||||
|
|
||||||
def _main(argv: list[str] | None = None) -> int:
|
def _main(argv: list[str] | None = None) -> int:
|
||||||
|
defuse_stdlib()
|
||||||
parser = _build_arg_parser()
|
parser = _build_arg_parser()
|
||||||
args = parser.parse_args(argv)
|
args = parser.parse_args(argv)
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -2,10 +2,10 @@ from __future__ import annotations
|
||||||
|
|
||||||
import configparser
|
import configparser
|
||||||
import json
|
import json
|
||||||
|
import xml.etree.ElementTree as ET # nosec
|
||||||
import yaml
|
import yaml
|
||||||
|
|
||||||
from collections import Counter, defaultdict
|
from collections import Counter, defaultdict
|
||||||
from defusedxml import ElementTree as ET
|
|
||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
from typing import Any, Iterable
|
from typing import Any, Iterable
|
||||||
|
|
||||||
|
|
@ -103,8 +103,7 @@ def parse_config(path: Path, fmt: str | None = None) -> tuple[str, Any]:
|
||||||
|
|
||||||
if fmt == "xml":
|
if fmt == "xml":
|
||||||
text = path.read_text(encoding="utf-8")
|
text = path.read_text(encoding="utf-8")
|
||||||
parser = ET.XMLParser(target=ET.TreeBuilder(insert_comments=False))
|
root = ET.fromstring(text) # nosec B314
|
||||||
root = ET.fromstring(text, parser=parser)
|
|
||||||
return fmt, root
|
return fmt, root
|
||||||
|
|
||||||
raise ValueError(f"Unsupported config format: {fmt}")
|
raise ValueError(f"Unsupported config format: {fmt}")
|
||||||
|
|
@ -868,8 +867,10 @@ def _generate_xml_template_from_text(role_prefix: str, text: str) -> str:
|
||||||
prolog, body = _split_xml_prolog(text)
|
prolog, body = _split_xml_prolog(text)
|
||||||
|
|
||||||
# Parse with comments included so <!-- --> are preserved
|
# Parse with comments included so <!-- --> are preserved
|
||||||
parser = ET.XMLParser(target=ET.TreeBuilder(insert_comments=True))
|
# defusedxml.defuse_stdlib() is called in CLI entrypoint
|
||||||
root = ET.fromstring(body, parser=parser)
|
parser = ET.XMLParser(target=ET.TreeBuilder(insert_comments=True)) # nosec B314
|
||||||
|
parser.feed(body)
|
||||||
|
root = parser.close()
|
||||||
|
|
||||||
_apply_jinja_to_xml_tree(role_prefix, root)
|
_apply_jinja_to_xml_tree(role_prefix, root)
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -1,11 +1,11 @@
|
||||||
from __future__ import annotations
|
from __future__ import annotations
|
||||||
|
|
||||||
from defusedxml import ElementTree as ET
|
|
||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
import configparser
|
import configparser
|
||||||
import pytest
|
import pytest
|
||||||
import textwrap
|
import textwrap
|
||||||
import yaml
|
import yaml
|
||||||
|
import xml.etree.ElementTree as ET
|
||||||
|
|
||||||
import jinjaturtle.core as core
|
import jinjaturtle.core as core
|
||||||
from jinjaturtle.core import (
|
from jinjaturtle.core import (
|
||||||
|
|
@ -566,8 +566,7 @@ def test_generate_template_xml_structural_fallback():
|
||||||
</root>
|
</root>
|
||||||
"""
|
"""
|
||||||
)
|
)
|
||||||
parser = ET.XMLParser(target=ET.TreeBuilder(insert_comments=False))
|
root = ET.fromstring(xml_text)
|
||||||
root = ET.fromstring(xml_text, parser=parser)
|
|
||||||
|
|
||||||
tmpl = generate_template("xml", parsed=root, role_prefix="role")
|
tmpl = generate_template("xml", parsed=root, role_prefix="role")
|
||||||
|
|
||||||
|
|
@ -643,8 +642,7 @@ def test_flatten_xml_text_with_attributes_uses_value_suffix():
|
||||||
the text at path + ('value',), not just path.
|
the text at path + ('value',), not just path.
|
||||||
"""
|
"""
|
||||||
xml_text = "<root><node attr='x'>text</node></root>"
|
xml_text = "<root><node attr='x'>text</node></root>"
|
||||||
parser = ET.XMLParser(target=ET.TreeBuilder(insert_comments=False))
|
root = ET.fromstring(xml_text)
|
||||||
root = ET.fromstring(xml_text, parser=parser)
|
|
||||||
|
|
||||||
items = flatten_config("xml", root)
|
items = flatten_config("xml", root)
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue