Skip to content

Commit aa13f7b

Browse files
committed
implement some suggestions by refurb
1 parent 02d6dbd commit aa13f7b

23 files changed

+133
-207
lines changed

cwl_utils/cwl_v1_0_expression_refactor.py

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import hashlib
77
import uuid
88
from collections.abc import Mapping, MutableSequence, Sequence
9+
from contextlib import suppress
910
from typing import Any, Optional, cast
1011

1112
from ruamel import yaml
@@ -30,11 +31,9 @@ def expand_stream_shortcuts(process: cwl.CommandLineTool) -> cwl.CommandLineTool
3031
result = copy.deepcopy(process)
3132
stdout_path = process.stdout
3233
if not stdout_path:
33-
stdout_path = str(
34-
hashlib.sha1( # nosec
35-
json_dumps(cwl.save(process)).encode("utf-8")
36-
).hexdigest()
37-
)
34+
stdout_path = hashlib.sha1( # nosec
35+
json_dumps(cwl.save(process)).encode("utf-8")
36+
).hexdigest()
3837
result.stdout = stdout_path
3938
result.outputs[index].type_ = "File"
4039
output.outputBinding = cwl.CommandOutputBinding(stdout_path, None, None)
@@ -534,12 +533,10 @@ def empty_inputs(
534533
elif param.source is None and param.default:
535534
result[param_id] = param.default
536535
else:
537-
try:
536+
with suppress(WorkflowException):
538537
result[param_id] = example_input(
539538
utils.type_for_source(process_or_step.run, param.source, parent)
540539
)
541-
except WorkflowException:
542-
pass
543540
return result
544541

545542

@@ -797,8 +794,8 @@ def process_workflow_reqs_and_hints(
797794
isinstance(expr_result, Mapping)
798795
and "class" in expr_result
799796
and (
800-
expr_result["class"] == "File"
801-
or expr_result["class"] == "Directory"
797+
expr_result["class"]
798+
in ("File", "Directory")
802799
)
803800
):
804801
target = cwl.InputParameter(

cwl_utils/cwl_v1_1_expression_refactor.py

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import hashlib
77
import uuid
88
from collections.abc import Mapping, MutableSequence, Sequence
9+
from contextlib import suppress
910
from typing import Any, Optional, cast
1011

1112
from ruamel import yaml
@@ -30,11 +31,9 @@ def expand_stream_shortcuts(process: cwl.CommandLineTool) -> cwl.CommandLineTool
3031
result = copy.deepcopy(process)
3132
stdout_path = process.stdout
3233
if not stdout_path:
33-
stdout_path = str(
34-
hashlib.sha1( # nosec
35-
json_dumps(cwl.save(process)).encode("utf-8")
36-
).hexdigest()
37-
)
34+
stdout_path = hashlib.sha1( # nosec
35+
json_dumps(cwl.save(process)).encode("utf-8")
36+
).hexdigest()
3837
result.stdout = stdout_path
3938
result.outputs[index].type_ = "File"
4039
output.outputBinding = cwl.CommandOutputBinding(stdout_path, None, None)
@@ -532,12 +531,10 @@ def empty_inputs(
532531
elif param.source is None and param.default:
533532
result[param_id] = param.default
534533
else:
535-
try:
534+
with suppress(WorkflowException):
536535
result[param_id] = example_input(
537536
utils.type_for_source(process_or_step.run, param.source, parent)
538537
)
539-
except WorkflowException:
540-
pass
541538
return result
542539

543540

@@ -799,8 +796,8 @@ def process_workflow_reqs_and_hints(
799796
isinstance(expr_result, Mapping)
800797
and "class" in expr_result
801798
and (
802-
expr_result["class"] == "File"
803-
or expr_result["class"] == "Directory"
799+
expr_result["class"]
800+
in ("File", "Directory")
804801
)
805802
):
806803
target = cwl.WorkflowInputParameter(

cwl_utils/cwl_v1_2_expression_refactor.py

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import hashlib
77
import uuid
88
from collections.abc import Mapping, MutableSequence, Sequence
9+
from contextlib import suppress
910
from typing import Any, Optional, cast
1011

1112
from ruamel import yaml
@@ -30,11 +31,9 @@ def expand_stream_shortcuts(process: cwl.CommandLineTool) -> cwl.CommandLineTool
3031
result = copy.deepcopy(process)
3132
stdout_path = process.stdout
3233
if not stdout_path:
33-
stdout_path = str(
34-
hashlib.sha1( # nosec
35-
json_dumps(cwl.save(process)).encode("utf-8")
36-
).hexdigest()
37-
)
34+
stdout_path = hashlib.sha1( # nosec
35+
json_dumps(cwl.save(process)).encode("utf-8")
36+
).hexdigest()
3837
result.stdout = stdout_path
3938
result.outputs[index].type_ = "File"
4039
output.outputBinding = cwl.CommandOutputBinding(stdout_path, None, None)
@@ -532,12 +531,10 @@ def empty_inputs(
532531
elif param.source is None and param.default:
533532
result[param_id] = param.default
534533
else:
535-
try:
534+
with suppress(WorkflowException):
536535
result[param_id] = example_input(
537536
utils.type_for_source(process_or_step.run, param.source, parent)
538537
)
539-
except WorkflowException:
540-
pass
541538
return result
542539

543540

@@ -894,8 +891,8 @@ def process_workflow_reqs_and_hints(
894891
isinstance(expr_result, Mapping)
895892
and "class" in expr_result
896893
and (
897-
expr_result["class"] == "File"
898-
or expr_result["class"] == "Directory"
894+
expr_result["class"]
895+
in ("File", "Directory")
899896
)
900897
):
901898
target = cwl.WorkflowInputParameter(

cwl_utils/docker_extract.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
#!/usr/bin/env python3
22
# SPDX-License-Identifier: Apache-2.0
33
import argparse
4-
import os
54
import sys
65
from collections.abc import Iterator
6+
from pathlib import Path
77
from typing import cast
88

99
import ruamel.yaml
@@ -51,7 +51,7 @@ def run(args: argparse.Namespace) -> list[cwl.DockerRequirement]:
5151
sys.exit(1)
5252

5353
if args.dir:
54-
os.makedirs(args.dir, exist_ok=True)
54+
Path(args.dir).mkdir(parents=True, exist_ok=True)
5555

5656
top = cwl.load_document_by_uri(args.input)
5757
reqs: list[cwl.DockerRequirement] = []

cwl_utils/expression.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -252,8 +252,7 @@ def dump(string: str) -> str:
252252
scan = scan[w[1] :]
253253
w = scanner(scan)
254254
if convert_to_expression:
255-
parts.append(f'"{scan}"') # noqa: B907
256-
parts.append(";}")
255+
parts.extend((f'"{scan}"', ";}")) # noqa: B907
257256
else:
258257
parts.append(scan)
259258
return "".join(parts)
@@ -290,8 +289,8 @@ def do_eval(
290289
:param timeout: The maximum number of seconds to wait while executing.
291290
"""
292291
runtime = cast(MutableMapping[str, Union[int, str, None]], copy.deepcopy(resources))
293-
runtime["tmpdir"] = tmpdir if tmpdir else None
294-
runtime["outdir"] = outdir if outdir else None
292+
runtime["tmpdir"] = tmpdir or None
293+
runtime["outdir"] = outdir or None
295294

296295
rootvars = cast(
297296
CWLObjectType,

cwl_utils/expression_refactor.py

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -139,11 +139,7 @@ def refactor(args: argparse.Namespace) -> int:
139139
if not isinstance(result, MutableSequence):
140140
result_json = save(
141141
result,
142-
base_url=(
143-
result.loadingOptions.fileuri
144-
if result.loadingOptions.fileuri
145-
else ""
146-
),
142+
base_url=(result.loadingOptions.fileuri or ""),
147143
)
148144
# ^^ Setting the base_url and keeping the default value
149145
# for relative_uris=True means that the IDs in the generated
@@ -155,7 +151,7 @@ def refactor(args: argparse.Namespace) -> int:
155151
]
156152
walk_tree(result_json)
157153
# ^ converts multiline strings to nice multiline YAML
158-
with open(output, "w", encoding="utf-8") as output_filehandle:
154+
with output.open("w", encoding="utf-8") as output_filehandle:
159155
output_filehandle.write(
160156
"#!/usr/bin/env cwl-runner\n"
161157
) # TODO: teach the codegen to do this?

cwl_utils/graph_split.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ def arg_parser() -> argparse.ArgumentParser:
6767
"-C",
6868
"--outdir",
6969
type=str,
70-
default=os.getcwd(),
70+
default=Path.cwd(),
7171
help="Output folder for the unpacked CWL files.",
7272
)
7373
return parser
@@ -194,7 +194,7 @@ def rewrite(
194194
else:
195195
document[key] = f"{referrant_file}#{sub}"
196196
elif isinstance(value, list):
197-
new_sources = list()
197+
new_sources = []
198198
for entry in value:
199199
if entry.startswith("#" + doc_id):
200200
new_sources.append(entry[len(doc_id) + 2 :])

cwl_utils/image_puller.py

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
# SPDX-License-Identifier: Apache-2.0
22
"""Classes for docker-extract."""
33
import logging
4-
import os
54
import subprocess # nosec
65
from abc import ABC, abstractmethod
76
from pathlib import Path
@@ -53,11 +52,9 @@ class DockerImagePuller(ImagePuller):
5352

5453
def get_image_name(self) -> str:
5554
"""Get the name of the tarball."""
56-
name = "".join(self.req.split("/")) + ".tar"
5755
# Replace colons with underscores in the name.
5856
# See https://github.com/containers/podman/issues/489
59-
name = name.replace(":", "_")
60-
return name
57+
return ("".join(self.req.split("/")) + ".tar").replace(":", "_")
6158

6259
def generate_udocker_loading_command(self) -> str:
6360
"""Generate the udocker loading command."""
@@ -70,14 +67,14 @@ def save_docker_image(self) -> None:
7067
ImagePuller._run_command_pull(cmd_pull)
7168
_LOGGER.info(f"Image successfully pulled: {self.req}")
7269
if self.save_directory:
73-
dest = os.path.join(self.save_directory, self.get_image_name())
70+
dest = Path(self.save_directory, self.get_image_name())
7471
if self.save_directory and self.force_pull:
75-
os.remove(dest)
72+
dest.unlink()
7673
cmd_save = [
7774
self.cmd,
7875
"save",
7976
"-o",
80-
dest,
77+
str(dest),
8178
self.req,
8279
]
8380
subprocess.run(cmd_save, check=True) # nosec
@@ -111,10 +108,8 @@ def save_docker_image(self) -> None:
111108
save_directory: str | Path
112109
if self.save_directory:
113110
save_directory = self.save_directory
114-
if (
115-
os.path.exists(os.path.join(save_directory, self.get_image_name()))
116-
and not self.force_pull
117-
):
111+
target = Path(save_directory, self.get_image_name())
112+
if target.exists() and not self.force_pull:
118113
_LOGGER.info(f"Already cached {self.req} with Singularity.")
119114
return
120115
_LOGGER.info(f"Pulling {self.req} with Singularity...")
@@ -127,7 +122,7 @@ def save_docker_image(self) -> None:
127122
cmd_pull.extend(
128123
[
129124
"--name",
130-
os.path.join(save_directory, self.get_image_name()),
125+
str(target),
131126
f"docker://{self.req}",
132127
]
133128
)

0 commit comments

Comments
 (0)