Skip to content

Commit

Permalink
add xml cli options. (#5)
Browse files Browse the repository at this point in the history
  • Loading branch information
wf001 authored Nov 28, 2022
1 parent 191d33e commit fe3b4a8
Show file tree
Hide file tree
Showing 3 changed files with 86 additions and 1 deletion.
6 changes: 6 additions & 0 deletions fconv/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,10 @@
"yaml_explicit_start": "add explicit start marker(See also https://yaml.org/spec/1.2.2/#914-explicit-documents)",
"yaml_explicit_end": "add explicit end marker(See also https://yaml.org/spec/1.2.2/#914-explicit-documents)",
"yaml_indent": "YAML array elements and object members will be pretty-printed with that indent level. ",
# xml input
"xml_process_namespaces": "enable namespace support(See more info at https://github.com/martinblech/xmltodict#namespace-support)",
"xml_process_comments": "treat comments directives as an attribute",
# xml output
"xml_particle_document": "disable to add document root attribute",
"xml_disable_pretty": "disable XML document to be pretty-printed with indent",
}
30 changes: 30 additions & 0 deletions fconv/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,26 @@ def _set_opts() -> Namespace:
)
p.add_argument("--yaml-indent", help=HELP["yaml_indent"], type=int)

# xml intput
p.add_argument(
"--xml-process-namespaces",
help=HELP["xml_process_namespaces"],
action="store_true",
)
p.add_argument(
"--xml-process-comments", help=HELP["xml_process_comments"], action="store_true"
)

# xml output
p.add_argument(
"--xml-particle-document",
help=HELP["xml_particle_document"],
action="store_true",
)
p.add_argument(
"--xml-disable-pretty", help=HELP["xml_disable_pretty"], action="store_true"
)

a = p.parse_args()
return a

Expand Down Expand Up @@ -104,6 +124,16 @@ def parse_args(a):
out_opt["explicit_end"] = True
if a.yaml_indent is not None:
out_opt["indent"] = a.yaml_indent
# xml input
if a.xml_process_namespaces:
out_opt["process_namespaces"] = True
if a.xml_process_comments:
out_opt["process_comments"] = True
# xml output
if a.xml_particle_document:
out_opt["full_document"] = False
if a.xml_disable_pretty:
out_opt["pretty"] = False
return in_opt, out_opt


Expand Down
51 changes: 50 additions & 1 deletion tests/test_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,11 @@
from fconv import __prog__
from fconv.__main__ import main

from .fixtures import JSON_FILE_PATH, TOML_FILE_PATH, YAML_FILE_PATH
# fmt: off
from .fixtures import (JSON_FILE_PATH, TOML_FILE_PATH, XML_FILE_PATH,
YAML_FILE_PATH)

# fmt: on


@pytest.mark.parametrize(
Expand Down Expand Up @@ -212,6 +216,51 @@ def test_cli_json_opt_callable(self, mocker, argv, expect_opt):
[__prog__, "json", "yaml", "-i", JSON_FILE_PATH, "--yaml-indent", "1"],
{"indent": 1},
),
# XML input opt
(
[
__prog__,
"xml",
"yaml",
"-i",
XML_FILE_PATH,
"--xml-process-namespaces",
],
{"process_namespaces": True},
),
(
[
__prog__,
"xml",
"yaml",
"-i",
XML_FILE_PATH,
"--xml-process-comments",
],
{"process_comments": True},
),
(
[
__prog__,
"json",
"xml",
"-i",
JSON_FILE_PATH,
"--xml-particle-document",
],
{"full_document": False},
),
(
[
__prog__,
"json",
"xml",
"-i",
JSON_FILE_PATH,
"--xml-disable-pretty",
],
{"pretty": False},
),
],
)
@pytest.mark.this
Expand Down

0 comments on commit fe3b4a8

Please sign in to comment.