Skip to content

Commit 4314c91

Browse files
authored
Merge pull request #38 from altairwei/command_line_interface
improve CLI for graph_split.py
2 parents da94478 + e5ef2bc commit 4314c91

File tree

7 files changed

+100
-19
lines changed

7 files changed

+100
-19
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,3 +102,5 @@ venv.bak/
102102

103103
# mypy
104104
.mypy_cache/
105+
106+
.vscode/

MANIFEST.in

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
include README.rst
2+
include Makefile
3+
include MANIFEST.in
4+
include LICENSE.txt
5+
include *requirements.txt
6+
include testdata/*.cwl
7+
include testdata/*.yaml
8+
include testdata/*.input
9+
global-exclude *~
10+
global-exclude *.pyc

cwl_utils/graph_split.py

Lines changed: 82 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -7,20 +7,64 @@
77

88
import os
99
import sys
10+
import json
11+
import argparse
1012
from pathlib import Path
1113
from typing import IO, Any, Dict, List, MutableMapping, Set, Text, Union, cast
1214

1315
from ruamel import yaml
1416
from schema_salad.sourceline import SourceLine, add_lc_filename
17+
from cwlformat.formatter import stringify_dict
1518

1619

17-
def main(args: List[str]) -> None:
20+
def main() -> None:
1821
"""Split the packed CWL at the path of the first argument."""
19-
with open(args[0], "r") as source_handle:
20-
run(source_handle)
21-
22-
23-
def run(sourceIO: IO[str]) -> None:
22+
parser = argparse.ArgumentParser(description="Split the packed CWL.")
23+
parser.add_argument("cwlfile")
24+
parser.add_argument(
25+
"-m",
26+
"--mainfile",
27+
default=None,
28+
type=str,
29+
help="Specify the name of the main document.",
30+
)
31+
parser.add_argument(
32+
"-f",
33+
"--output-format",
34+
choices=["json", "yaml"],
35+
type=str,
36+
default="json",
37+
help="Specify the format of the output CWL files.",
38+
)
39+
parser.add_argument(
40+
"-p",
41+
"--pretty",
42+
action="store_true",
43+
default=False,
44+
help="Beautify the output CWL document, only works with yaml format.",
45+
)
46+
parser.add_argument(
47+
"-C",
48+
"--outdir",
49+
type=str,
50+
default=os.getcwd(),
51+
help="Output folder for the unpacked CWL files.",
52+
)
53+
options = parser.parse_args()
54+
55+
with open(options.cwlfile, "r") as source_handle:
56+
run(
57+
source_handle,
58+
options.outdir,
59+
options.output_format,
60+
options.mainfile,
61+
options.pretty,
62+
)
63+
64+
65+
def run(
66+
sourceIO: IO[str], output_dir: str, output_format: str, mainfile: str, pretty: bool
67+
) -> None:
2468
"""Loop over the provided packed CWL document and split it up."""
2569
source = yaml.main.round_trip_load(sourceIO, preserve_quotes=True)
2670
add_lc_filename(source, sourceIO.name)
@@ -47,15 +91,16 @@ def my_represent_none(
4791
for import_name in imports:
4892
rewrite_types(entry, "#{}".format(import_name), False)
4993
if entry_id == "main":
50-
entry_id = "unpacked_{}".format(os.path.basename(sourceIO.name))
51-
with open(entry_id, "w", encoding="utf-8") as result_handle:
52-
yaml.main.round_trip_dump(
53-
entry,
54-
result_handle,
55-
default_flow_style=False,
56-
indent=4,
57-
block_seq_indent=2,
58-
)
94+
if mainfile is None:
95+
entry_id = "unpacked_{}".format(os.path.basename(sourceIO.name))
96+
else:
97+
entry_id = mainfile
98+
99+
output_file = os.path.join(output_dir, entry_id)
100+
if output_format == "json":
101+
json_dump(entry, output_file)
102+
elif output_format == "yaml":
103+
yaml_dump(entry, output_file, pretty)
59104

60105

61106
def rewrite(document: Any, doc_id: str) -> Set[str]:
@@ -177,5 +222,26 @@ def seen_import(entry: MutableMapping[str, Any]) -> bool:
177222
return seen_imports
178223

179224

225+
def json_dump(entry: Any, output_file: str) -> None:
226+
"""Output object as JSON."""
227+
with open(output_file, "w", encoding="utf-8") as result_handle:
228+
json.dump(entry, result_handle, indent=4)
229+
230+
231+
def yaml_dump(entry: Any, output_file: str, pretty: bool) -> None:
232+
"""Output object as YAML."""
233+
with open(output_file, "w", encoding="utf-8") as result_handle:
234+
if pretty:
235+
result_handle.write(stringify_dict(entry))
236+
else:
237+
yaml.main.round_trip_dump(
238+
entry,
239+
result_handle,
240+
default_flow_style=False,
241+
indent=4,
242+
block_seq_indent=2,
243+
)
244+
245+
180246
if __name__ == "__main__":
181-
main(sys.argv[1:])
247+
main()

setup.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,15 +13,17 @@
1313
license="Apache 2.0",
1414
author="Common workflow language working group",
1515
author_email="[email protected]",
16-
packages=["cwl_utils", "cwl_utils.tests"],
17-
package_dir={"cwl_utils.tests": "tests"},
16+
packages=["cwl_utils", "cwl_utils.tests", "cwl_utils.testdata"],
17+
package_dir={"cwl_utils.tests": "tests", "cwl_utils.testdata": "testdata"},
18+
include_package_data=True,
1819
python_requires=">=3.6",
1920
install_requires=[
2021
"ruamel.yaml<=0.16.5,>=0.12.4",
2122
"requests",
2223
"schema-salad >= 7, < 8",
2324
"typing_extensions",
2425
"cwltool",
26+
"cwlformat",
2527
],
2628
setup_requires=[] + pytest_runner,
2729
tests_require=["pytest<7", "cwltool"],

tests/test_graph_split.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,4 +16,4 @@ def test_graph_split(tmp_path: Path):
1616
os.chdir(tmp_path)
1717
sourceIO = StringIO(requests.get(URI).text)
1818
sourceIO.name = URI
19-
run(sourceIO)
19+
run(sourceIO, ".", "yaml", "main.cwl", True)

typeshed/cwlformat/__init__.pyi

Whitespace-only changes.

typeshed/cwlformat/formatter.pyi

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
def stringify_dict(as_dict: dict) -> str: ...

0 commit comments

Comments
 (0)