Skip to content

Commit c903624

Browse files
committed
type fixes and simplifications
1 parent 9940bae commit c903624

File tree

1 file changed

+19
-22
lines changed

1 file changed

+19
-22
lines changed

cwl_utils/graph_split.py

+19-22
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,8 @@
1818
from pathlib import Path
1919
from typing import (
2020
IO,
21-
TYPE_CHECKING,
2221
Any,
2322
MutableMapping,
24-
Optional,
2523
Set,
2624
TextIO,
2725
Union,
@@ -35,9 +33,6 @@
3533

3634
from cwl_utils.loghandler import _logger as _cwlutilslogger
3735

38-
if TYPE_CHECKING:
39-
from _typeshed import StrPath
40-
4136
_logger = logging.getLogger("cwl-graph-split") # pylint: disable=invalid-name
4237
defaultStreamHandler = logging.StreamHandler() # pylint: disable=invalid-name
4338
_logger.addHandler(defaultStreamHandler)
@@ -93,7 +88,7 @@ def run(args: list[str]) -> int:
9388
with open(options.cwlfile) as source_handle:
9489
graph_split(
9590
source_handle,
96-
options.outdir,
91+
Path(options.outdir),
9792
options.output_format,
9893
options.mainfile,
9994
options.pretty,
@@ -103,7 +98,7 @@ def run(args: list[str]) -> int:
10398

10499
def graph_split(
105100
sourceIO: IO[str],
106-
output_dir: "StrPath",
101+
output_dir: Path,
107102
output_format: str,
108103
mainfile: str,
109104
pretty: bool,
@@ -121,11 +116,11 @@ def graph_split(
121116
version = source.pop("cwlVersion")
122117

123118
# Check outdir parent exists
124-
if not Path(output_dir).parent.is_dir():
119+
if not output_dir.parent.is_dir():
125120
raise NotADirectoryError(f"Parent directory of {output_dir} does not exist")
126121
# If output_dir is not a directory, create it
127-
if not Path(output_dir).is_dir():
128-
os.mkdir(output_dir)
122+
if not output_dir.is_dir():
123+
output_dir.mkdir()
129124

130125
def my_represent_none(
131126
self: Any, data: Any
@@ -138,7 +133,7 @@ def my_represent_none(
138133
for entry in source["$graph"]:
139134
entry_id = entry.pop("id").lstrip("#")
140135
entry["cwlVersion"] = version
141-
imports = rewrite(entry, entry_id, Path(output_dir))
136+
imports = rewrite(entry, entry_id, output_dir)
142137
if imports:
143138
for import_name in imports:
144139
rewrite_types(entry, f"#{import_name}", False)
@@ -148,15 +143,15 @@ def my_represent_none(
148143
else:
149144
entry_id = mainfile
150145

151-
output_file = Path(output_dir) / (re.sub(".cwl$", "", entry_id) + ".cwl")
146+
output_file = output_dir / (re.sub(".cwl$", "", entry_id) + ".cwl")
152147
if output_format == "json":
153148
json_dump(entry, output_file)
154149
elif output_format == "yaml":
155150
yaml_dump(entry, output_file, pretty)
156151

157152

158153
def rewrite(
159-
document: Any, doc_id: str, output_dir: Path, pretty: Optional[bool] = False
154+
document: Any, doc_id: str, output_dir: Path, pretty: bool = False
160155
) -> Set[str]:
161156
"""Rewrite the given element from the CWL $graph."""
162157
imports = set()
@@ -245,7 +240,7 @@ def rewrite_types(field: Any, entry_file: str, sameself: bool) -> None:
245240

246241

247242
def rewrite_schemadef(
248-
document: MutableMapping[str, Any], output_dir: Path, pretty: Optional[bool] = False
243+
document: MutableMapping[str, Any], output_dir: Path, pretty: bool = False
249244
) -> Set[str]:
250245
"""Dump the schemadefs to their own file."""
251246
for entry in document["types"]:
@@ -256,7 +251,7 @@ def rewrite_schemadef(
256251
for field in entry["fields"]:
257252
field["name"] = field["name"].split("/")[2]
258253
rewrite_types(field, entry_file, True)
259-
with open(output_dir / entry_file, "a", encoding="utf-8") as entry_handle:
254+
with (output_dir / entry_file).open("a", encoding="utf-8") as entry_handle:
260255
yaml_dump(entry, entry_handle, pretty)
261256
entry["$import"] = entry_file
262257
del entry["name"]
@@ -278,15 +273,15 @@ def seen_import(entry: MutableMapping[str, Any]) -> bool:
278273
return seen_imports
279274

280275

281-
def json_dump(entry: Any, output_file: str) -> None:
276+
def json_dump(entry: Any, output_file: Path) -> None:
282277
"""Output object as JSON."""
283-
with open(output_file, "w", encoding="utf-8") as result_handle:
278+
with output_file.open("w", encoding="utf-8") as result_handle:
284279
json.dump(entry, result_handle, indent=4)
285280

286281

287282
def yaml_dump(
288283
entry: Any,
289-
output_file_or_handle: Optional[Union[str, Path, TextIOWrapper, TextIO]],
284+
output_file_or_handle: Union[str, Path, TextIOWrapper, TextIO],
290285
pretty: bool,
291286
) -> None:
292287
"""Output object as YAML."""
@@ -299,13 +294,15 @@ def yaml_dump(
299294
with open(output_file_or_handle, "w", encoding="utf-8") as result_handle:
300295
if pretty:
301296
result_handle.write(stringify_dict(entry))
302-
else:
303-
yaml.dump(entry, result_handle)
297+
return
298+
yaml.dump(entry, result_handle)
299+
return
304300
elif isinstance(output_file_or_handle, (TextIOWrapper, TextIO)):
305301
if pretty:
306302
output_file_or_handle.write(stringify_dict(entry))
307-
else:
308-
yaml.dump(entry, output_file_or_handle)
303+
return
304+
yaml.dump(entry, output_file_or_handle)
305+
return
309306
else:
310307
raise ValueError(
311308
f"output_file_or_handle must be a string or a file handle but got {type(output_file_or_handle)}"

0 commit comments

Comments
 (0)