Skip to content

Commit

Permalink
fix: tidy up 0.9.2 merge, mostly label corrections
Browse files Browse the repository at this point in the history
  • Loading branch information
philtweir committed Aug 24, 2024
1 parent 0be1fea commit 0bae23c
Show file tree
Hide file tree
Showing 9 changed files with 54 additions and 51 deletions.
16 changes: 2 additions & 14 deletions src/dewret/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
import json

from .core import set_configuration, set_render_configuration
from .utils import load_module_or_package
from .render import get_render_method, RawRenderModule, StructuredRenderModule
from .tasks import Backend, construct

Expand Down Expand Up @@ -130,21 +131,8 @@ def _opener(key, mode):
opener = _opener

render = get_render_method(render_module, pretty=pretty)
workflow_init = workflow_py.parent
pkg = "__workflow__"

# Try to import the workflow as a package, if possible, to allow relative imports.
try:
spec = importlib.util.spec_from_file_location(pkg, str(workflow_py.parent / "__init__.py"))
if spec is None or spec.loader is None:
raise ImportError(f"Could not open {pkg} package")
module = importlib.util.module_from_spec(spec)
sys.modules[pkg] = module
spec.loader.exec_module(module)
workflow = importlib.import_module(f"{pkg}.{workflow_py.stem}", pkg)
except ImportError:
loader = importlib.machinery.SourceFileLoader(pkg, str(workflow_py))
workflow = loader.load_module()
workflow = load_module_or_package(pkg, workflow_py)
task_fn = getattr(workflow, task)

try:
Expand Down
9 changes: 2 additions & 7 deletions src/dewret/render.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
from .workflow import Workflow, NestedStep
from .core import RawType
from .workflow import Workflow
from .utils import load_module_or_package

RenderConfiguration = TypeVar("RenderConfiguration", bound=dict[str, Any])

Expand All @@ -34,13 +35,7 @@ def get_render_method(renderer: Path | RawRenderModule | StructuredRenderModule,

# Attempt to load renderer as package, falling back to a single module otherwise.
# This enables relative imports in renderers and therefore the ability to modularize.
try:
loader = importlib.machinery.SourceFileLoader("__renderer__", str(renderer.parent / "__init__.py"))
sys.modules["__renderer__"] = loader.load_module(f"__renderer__")
render_module = importlib.import_module(f"__renderer__.{renderer.stem}", "__renderer__")
except ImportError:
loader = importlib.machinery.SourceFileLoader("__renderer__", str(renderer))
render_module = loader.load_module()
render_module = load_module_or_package("__renderer__", renderer)
sys.modules["__renderer_mod__"] = render_module
else:
render_module = renderer
Expand Down
23 changes: 22 additions & 1 deletion src/dewret/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,11 @@
import hashlib
import json
import sys
from types import FrameType, TracebackType, UnionType
import importlib
import importlib.util
from types import FrameType, TracebackType, UnionType, ModuleType
from typing import Any, cast, Union, Protocol, ClassVar, Callable, Iterable, get_args, get_origin, Annotated
from pathlib import Path
from collections.abc import Sequence, Mapping
from sympy import Basic, Integer, Float, Rational

Expand Down Expand Up @@ -58,6 +61,24 @@ def make_traceback(skip: int = 2) -> TracebackType | None:
frame = frame.f_back
return tb

def load_module_or_package(target_name: str, path: Path) -> ModuleType:
# Try to import the workflow as a package, if possible, to allow relative imports.
try:
spec = importlib.util.spec_from_file_location(target_name, str(path.parent / "__init__.py"))
if spec is None or spec.loader is None:
raise ImportError(f"Could not open {path.parent} package")
module = importlib.util.module_from_spec(spec)
sys.modules[target_name] = module
spec.loader.exec_module(module)
workflow = importlib.import_module(f"{target_name}.{path.stem}", target_name)
except ImportError as exc:
spec = importlib.util.spec_from_file_location(target_name, str(path))
if spec is None or spec.loader is None:
raise ImportError(f"Could not open {path} module") from exc
workflow = importlib.util.module_from_spec(spec)
spec.loader.exec_module(workflow)

return workflow

def flatten_if_set(value: Any) -> RawType | Unset:
"""Takes a Raw-like structure and makes it RawType or Unset.
Expand Down
8 changes: 4 additions & 4 deletions tests/test_annotations.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,15 +70,15 @@ def test_at_render() -> None:
inputs:
increment-1-num:
default: 3
label: increment-1-num
label: num
type: int
outputs:
out:
label: out
outputSource: to_int-1/out
type:
- int
- double
- float
steps:
increment-1:
in:
Expand Down Expand Up @@ -108,15 +108,15 @@ def test_at_render() -> None:
inputs:
increment-1-num:
default: 3
label: increment-1-num
label: num
type: int
outputs:
out:
label: out
outputSource: to_int-1/out
type:
- int
- double
- float
steps:
increment-1:
in:
Expand Down
32 changes: 15 additions & 17 deletions tests/test_cwl.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ def get_now() -> datetime:
days_in_future-1-num:
default: 3
type: int
label: days_in_future-1-num
label: num
get_now-1:
label: get_now-1
type: datetime
Expand Down Expand Up @@ -115,7 +115,7 @@ def get_now() -> datetime:
days_in_future-1-num:
default: 3
type: int
label: days_in_future-1-num
label: num
outputs:
out:
label: out
Expand Down Expand Up @@ -154,7 +154,7 @@ def test_cwl_with_parameter() -> None:
class: Workflow
inputs:
increment-{hsh}-num:
label: increment-{hsh}-num
label: num
type: int
default: 3
outputs:
Expand All @@ -171,7 +171,7 @@ def test_cwl_with_parameter() -> None:
out: [out]
""")

def test_cwl_with_parameter() -> None:
def test_cwl_with_positional_parameter() -> None:
"""Check whether we can move raw input to parameters.
Produces CWL for a call with a changeable raw value, that is converted
Expand All @@ -191,7 +191,7 @@ def test_cwl_with_parameter() -> None:
class: Workflow
inputs:
increment-{hsh}-num:
label: increment-{hsh}-num
label: num
type: int
default: 3
outputs:
Expand Down Expand Up @@ -357,14 +357,14 @@ def test_cwl_references() -> None:
class: Workflow
inputs:
increment-{hsh_increment}-num:
label: increment-{hsh_increment}-num
label: num
type: int
default: 3
outputs:
out:
label: out
outputSource: double-{hsh_double}/out
type:
type:
- int
- float
steps:
Expand Down Expand Up @@ -397,14 +397,14 @@ def test_complex_cwl_references() -> None:
class: Workflow
inputs:
increment-1-num:
label: increment-1-num
label: num
type: int
default: 23
outputs:
out:
label: out
outputSource: sum-1/out
type:
type:
- int
- float
steps:
Expand Down Expand Up @@ -459,7 +459,7 @@ def test_cwl_with_subworkflow_and_raw_params() -> None:
type: int
sum-1-right:
default: 3
label: sum-1-right
label: right
type: int
outputs:
out:
Expand Down Expand Up @@ -548,8 +548,7 @@ def test_tuple_floats() -> None:
"""
result = tuple_float_return()
workflow = construct(result, simplify_ids=True)
rendered = render(workflow)
print(yaml.dump(rendered))
rendered = render(workflow)["__root__"]
assert rendered == yaml.safe_load("""
cwlVersion: 1.2
class: Workflow
Expand All @@ -558,11 +557,10 @@ def test_tuple_floats() -> None:
out:
label: out
outputSource: tuple_float_return-1/out
type:
items:
- type: float
- type: float
type: array
items:
- float
- float
type: array
steps:
tuple_float_return-1:
run: tuple_float_return
Expand Down
4 changes: 2 additions & 2 deletions tests/test_fieldable.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ def test_fields_of_parameters_usable() -> None:
outputSource: sum-1-1/out
type:
- int
- double
- float
steps:
sum-1-1:
in:
Expand Down Expand Up @@ -83,7 +83,7 @@ class MyDataclass:
outputSource: sum-1/out
type:
- int
- double
- float
steps:
sum-1:
in:
Expand Down
5 changes: 3 additions & 2 deletions tests/test_multiresult_steps.py
Original file line number Diff line number Diff line change
Expand Up @@ -287,8 +287,9 @@ def test_pair_can_be_returned_from_step() -> None:

def test_list_can_be_returned_from_step() -> None:
"""Tests whether a task can insert result fields into other steps."""
workflow = construct(list_cast(iterable=algorithm_with_pair()), simplify_ids=True)
rendered = render(workflow)["__root__"]
with set_configuration(flatten_all_nested=True):
workflow = construct(list_cast(iterable=algorithm_with_pair()), simplify_ids=True)
rendered = render(workflow)["__root__"]

assert rendered == yaml.safe_load("""
class: Workflow
Expand Down
4 changes: 2 additions & 2 deletions tests/test_nested.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,14 @@ def test_can_supply_nested_raw():
pi:
default: 3.141592653589793
label: pi
type: double
type: float
outputs:
out:
label: out
outputSource: max_list-1/out
type:
- int
- double
- float
steps:
max_list-1:
in:
Expand Down
4 changes: 2 additions & 2 deletions tests/test_parameters.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ def test_cwl_parameters() -> None:
type: int
default: 3
rotate-1-num:
label: rotate-1-num
label: num
type: int
default: 3
outputs:
Expand Down Expand Up @@ -77,7 +77,7 @@ def test_complex_parameters() -> None:
type: int
default: 23
rotate-2-num:
label: rotate-2-num
label: num
type: int
default: 23
outputs:
Expand Down

0 comments on commit 0bae23c

Please sign in to comment.