Skip to content

Commit 8924b9c

Browse files
altairweimr-c
authored andcommitted
add --pretty option to graph_split.py
1 parent 70ba5f4 commit 8924b9c

File tree

2 files changed

+32
-8
lines changed

2 files changed

+32
-8
lines changed

cwl_utils/graph_split.py

Lines changed: 31 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,13 @@ def main() -> None:
3636
default="json",
3737
help="Specify the format of the output CWL files.",
3838
)
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+
)
3946
parser.add_argument(
4047
"-C",
4148
"--outdir",
@@ -46,10 +53,18 @@ def main() -> None:
4653
options = parser.parse_args()
4754

4855
with open(options.cwlfile, "r") as source_handle:
49-
run(source_handle, options.outdir, options.output_format, options.mainfile)
50-
51-
52-
def run(sourceIO: IO[str], output_dir: str, output_format: str, mainfile: str) -> None:
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:
5368
"""Loop over the provided packed CWL document and split it up."""
5469
source = yaml.main.round_trip_load(sourceIO, preserve_quotes=True)
5570
add_lc_filename(source, sourceIO.name)
@@ -85,7 +100,7 @@ def my_represent_none(
85100
if output_format == "json":
86101
json_dump(entry, output_file)
87102
elif output_format == "yaml":
88-
yaml_dump(entry, output_file)
103+
yaml_dump(entry, output_file, pretty)
89104

90105

91106
def rewrite(document: Any, doc_id: str) -> Set[str]:
@@ -212,9 +227,18 @@ def json_dump(entry: Any, output_file: str) -> None:
212227
json.dump(entry, result_handle, indent=4)
213228

214229

215-
def yaml_dump(entry: Any, output_file: str) -> None:
230+
def yaml_dump(entry: Any, output_file: str, pretty: bool) -> None:
216231
with open(output_file, "w", encoding="utf-8") as result_handle:
217-
result_handle.write(stringify_dict(entry))
232+
if pretty:
233+
result_handle.write(stringify_dict(entry))
234+
else:
235+
yaml.main.round_trip_dump(
236+
entry,
237+
result_handle,
238+
default_flow_style=False,
239+
indent=4,
240+
block_seq_indent=2,
241+
)
218242

219243

220244
if __name__ == "__main__":

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, ".", "yaml", "main.cwl")
19+
run(sourceIO, ".", "yaml", "main.cwl", True)

0 commit comments

Comments
 (0)