Skip to content

Commit

Permalink
Move styxdefs.py runtime to external package
Browse files Browse the repository at this point in the history
  • Loading branch information
nx10 committed May 21, 2024
1 parent 9e2f29c commit d96c759
Show file tree
Hide file tree
Showing 13 changed files with 38 additions and 167 deletions.
18 changes: 2 additions & 16 deletions src/styx/compiler/compile/definitions.py
Original file line number Diff line number Diff line change
@@ -1,20 +1,6 @@
from styx.compiler.settings import CompilerSettings, DefsMode
from styx.pycodegen.core import PyModule


def compile_definitions() -> str:
"""Compile the definitions to Python code."""
import styx.runners.styxdefs

defs_file = styx.runners.styxdefs.__file__
with open(defs_file, "r") as f:
return f.read()


def generate_definitions(module: PyModule, settings: CompilerSettings) -> None:
def generate_definitions(module: PyModule) -> None:
"""Generate the definition code in the header."""
if settings.defs_mode == DefsMode.INLINE:
module.header.append(compile_definitions())
elif settings.defs_mode == DefsMode.IMPORT:
defs_module_path = "styx.runners.styxdefs" if settings.defs_module_path is None else settings.defs_module_path
module.header.append(f"from {defs_module_path} import *")
module.header.append("from styxdefs import *")
17 changes: 10 additions & 7 deletions src/styx/compiler/compile/descriptor.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
from styx.compiler.compile.common import SharedScopes, SharedSymbols
from styx.compiler.compile.constraints import generate_constraint_checks
from styx.compiler.compile.definitions import compile_definitions, generate_definitions
from styx.compiler.compile.definitions import generate_definitions
from styx.compiler.compile.inputs import build_input_arguments, generate_command_line_args_building
from styx.compiler.compile.metadata import generate_static_metadata
from styx.compiler.compile.outputs import generate_output_building, generate_outputs_definition
from styx.compiler.compile.subcommand import generate_sub_command_classes
from styx.compiler.settings import CompilerSettings, DefsMode
from styx.compiler.settings import CompilerSettings
from styx.model.core import Descriptor, InputArgument, OutputArgument, SubCommand, WithSymbol
from styx.pycodegen.core import PyArg, PyFunc, PyModule
from styx.pycodegen.scope import Scope
Expand Down Expand Up @@ -37,8 +37,13 @@ def _generate_run_function(
module.funcs.append(func)

# Function arguments
func.args.append(PyArg(name="runner", type="Runner", default=None, docstring="Command runner"))
func.args.extend(build_input_arguments(inputs, sub_aliases))
func.args.append(PyArg(name="runner", type="Runner", default="None", docstring="Command runner"))

# Function body: Runner instantiation
func.body.extend([
f"{symbols.runner} = {symbols.runner} or get_global_runner()",
])

# Constraint checking
generate_constraint_checks(func, command.group_constraints, inputs)
Expand Down Expand Up @@ -66,9 +71,6 @@ def _generate_run_function(

def compile_descriptor(descriptor: Descriptor, settings: CompilerSettings) -> str:
"""Compile a descriptor to Python code."""
if settings.defs_mode == DefsMode.DEFS_ONLY:
return compile_definitions()

# --- Scopes and symbols ---

_module_scope = Scope(parent=Scope.python())
Expand Down Expand Up @@ -111,9 +113,10 @@ def compile_descriptor(descriptor: Descriptor, settings: CompilerSettings) -> st
# --- Code generation ---
module = PyModule()
module.imports.append("import typing")
module.imports.append("import pathlib")

# Definitions
generate_definitions(module, settings)
generate_definitions(module)
module.header.extend(["", ""]) # Two blank lines

# Static metadata
Expand Down
4 changes: 2 additions & 2 deletions src/styx/compiler/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@
from styx.model.from_boutiques import descriptor_from_boutiques # type: ignore


def compile_boutiques_dict(boutiques_descriptor: dict, settings: CompilerSettings) -> str:
def compile_boutiques_dict(boutiques_descriptor: dict, settings: CompilerSettings | None = None) -> str:
descriptor = descriptor_from_boutiques(boutiques_descriptor)
return compile_descriptor(descriptor, settings)
return compile_descriptor(descriptor, settings if settings is not None else CompilerSettings())
3 changes: 0 additions & 3 deletions src/styx/compiler/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,4 @@ class CompilerSettings:
input_path: pathlib.Path | None = None
output_path: pathlib.Path | None = None

defs_module_path: str | None = None
defs_mode: DefsMode = DefsMode.IMPORT

debug_mode: bool = False
17 changes: 1 addition & 16 deletions src/styx/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,8 @@

import tomli as tomllib # Remove once we move to python 3.11

from styx.compiler.compile.definitions import compile_definitions
from styx.compiler.core import compile_boutiques_dict
from styx.compiler.settings import CompilerSettings, DefsMode
from styx.compiler.settings import CompilerSettings
from styx.pycodegen.utils import python_snakify


Expand All @@ -29,8 +28,6 @@ def load_settings_from_toml(
return CompilerSettings(
input_path=override_input_folder or pathlib.Path(settings.get("input_path", ".")),
output_path=override_output_folder or pathlib.Path(settings.get("output_path", ".")),
defs_module_path=settings.get("defs_module_path", None),
defs_mode=DefsMode[settings.get("defs_mode", "IMPORT")],
)


Expand Down Expand Up @@ -93,16 +90,6 @@ def main() -> None:
)
settings.debug_mode = settings.debug_mode or args.debug

if settings.defs_mode == DefsMode.IMPORT:
defs_path: str | pathlib.Path = "styxdefs.py"
if settings.output_path is not None:
# write out the definitions to a separate file
defs_path = settings.output_path / defs_path
defs = compile_definitions()
with open(defs_path, "w") as defs_file:
defs_file.write(defs)
print(f"Compiled definitions to {defs_path}")

assert settings.input_path is not None
json_files = settings.input_path.glob("**/*.json")

Expand All @@ -115,8 +102,6 @@ def main() -> None:
output_module_path = tuple(python_snakify(part) for part in output_module_path)
output_file_name = f"{python_snakify(json_path.stem)}.py"

settings.defs_module_path = "." * (len(output_module_path) + 1) + "styxdefs"

with open(json_path, "r", encoding="utf-8") as json_file:
try:
json_data = json.load(json_file)
Expand Down
2 changes: 1 addition & 1 deletion src/styx/runners/docker.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
from subprocess import PIPE, CalledProcessError, Popen
from typing import Callable

from styx.runners.styxdefs import Execution, Metadata, Runner
from styxdefs import Execution, Metadata, Runner


def _docker_mount(host_path: str, container_path: str, readonly: bool) -> str:
Expand Down
2 changes: 1 addition & 1 deletion src/styx/runners/dummy.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from styx.runners.styxdefs import Execution, InputPathType, Metadata, OutputPathType, Runner
from styxdefs import Execution, InputPathType, Metadata, OutputPathType, Runner


class DummyRunner(Runner, Execution):
Expand Down
77 changes: 0 additions & 77 deletions src/styx/runners/styxdefs.py

This file was deleted.

27 changes: 9 additions & 18 deletions tests/test_carg_building.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@

def test_positional_string_arg() -> None:
"""Positional string argument."""
settings = styx.compiler.settings.CompilerSettings(defs_mode=styx.compiler.settings.DefsMode.IMPORT)
model = boutiques_dummy({
"command-line": "dummy [X]",
"inputs": [
Expand All @@ -28,7 +27,7 @@ def test_positional_string_arg() -> None:
],
})

compiled_module = styx.compiler.core.compile_boutiques_dict(model, settings)
compiled_module = styx.compiler.core.compile_boutiques_dict(model)

test_module = dynamic_module(compiled_module, "test_module")
dummy_runner = styx.runners.dummy.DummyRunner()
Expand All @@ -40,7 +39,6 @@ def test_positional_string_arg() -> None:

def test_positional_number_arg() -> None:
"""Positional number argument."""
settings = styx.compiler.settings.CompilerSettings(defs_mode=styx.compiler.settings.DefsMode.IMPORT)
model = boutiques_dummy({
"command-line": "dummy [X]",
"inputs": [
Expand All @@ -53,7 +51,7 @@ def test_positional_number_arg() -> None:
],
})

compiled_module = styx.compiler.core.compile_boutiques_dict(model, settings)
compiled_module = styx.compiler.core.compile_boutiques_dict(model)

test_module = dynamic_module(compiled_module, "test_module")
dummy_runner = styx.runners.dummy.DummyRunner()
Expand All @@ -65,7 +63,6 @@ def test_positional_number_arg() -> None:

def test_positional_file_arg() -> None:
"""Positional file argument."""
settings = styx.compiler.settings.CompilerSettings(defs_mode=styx.compiler.settings.DefsMode.IMPORT)
model = boutiques_dummy({
"command-line": "dummy [X]",
"inputs": [
Expand All @@ -78,7 +75,7 @@ def test_positional_file_arg() -> None:
],
})

compiled_module = styx.compiler.core.compile_boutiques_dict(model, settings)
compiled_module = styx.compiler.core.compile_boutiques_dict(model)

test_module = dynamic_module(compiled_module, "test_module")
dummy_runner = styx.runners.dummy.DummyRunner()
Expand All @@ -90,7 +87,6 @@ def test_positional_file_arg() -> None:

def test_flag_arg() -> None:
"""Flag argument."""
settings = styx.compiler.settings.CompilerSettings(defs_mode=styx.compiler.settings.DefsMode.IMPORT)
model = boutiques_dummy({
"command-line": "dummy [X]",
"inputs": [
Expand All @@ -104,7 +100,7 @@ def test_flag_arg() -> None:
],
})

compiled_module = styx.compiler.core.compile_boutiques_dict(model, settings)
compiled_module = styx.compiler.core.compile_boutiques_dict(model)

test_module = dynamic_module(compiled_module, "test_module")
dummy_runner = styx.runners.dummy.DummyRunner()
Expand All @@ -116,7 +112,6 @@ def test_flag_arg() -> None:

def test_named_arg() -> None:
"""Named argument."""
settings = styx.compiler.settings.CompilerSettings(defs_mode=styx.compiler.settings.DefsMode.IMPORT)
model = boutiques_dummy({
"command-line": "dummy [X]",
"inputs": [
Expand All @@ -130,7 +125,7 @@ def test_named_arg() -> None:
],
})

compiled_module = styx.compiler.core.compile_boutiques_dict(model, settings)
compiled_module = styx.compiler.core.compile_boutiques_dict(model)

test_module = dynamic_module(compiled_module, "test_module")
dummy_runner = styx.runners.dummy.DummyRunner()
Expand All @@ -142,7 +137,6 @@ def test_named_arg() -> None:

def test_list_of_strings_arg() -> None:
"""List of strings."""
settings = styx.compiler.settings.CompilerSettings(defs_mode=styx.compiler.settings.DefsMode.IMPORT)
model = boutiques_dummy({
"command-line": "dummy [X] [Y]",
"inputs": [
Expand All @@ -165,7 +159,7 @@ def test_list_of_strings_arg() -> None:
],
})

compiled_module = styx.compiler.core.compile_boutiques_dict(model, settings)
compiled_module = styx.compiler.core.compile_boutiques_dict(model)

test_module = dynamic_module(compiled_module, "test_module")
dummy_runner = styx.runners.dummy.DummyRunner()
Expand All @@ -177,7 +171,6 @@ def test_list_of_strings_arg() -> None:

def test_list_of_numbers_arg() -> None:
"""List of numbers."""
settings = styx.compiler.settings.CompilerSettings(defs_mode=styx.compiler.settings.DefsMode.IMPORT)
model = boutiques_dummy({
"command-line": "dummy [X] [Y]",
"inputs": [
Expand All @@ -200,7 +193,7 @@ def test_list_of_numbers_arg() -> None:
],
})

compiled_module = styx.compiler.core.compile_boutiques_dict(model, settings)
compiled_module = styx.compiler.core.compile_boutiques_dict(model)

test_module = dynamic_module(compiled_module, "test_module")
dummy_runner = styx.runners.dummy.DummyRunner()
Expand All @@ -213,7 +206,6 @@ def test_list_of_numbers_arg() -> None:

def test_static_args() -> None:
"""Static arguments."""
settings = styx.compiler.settings.CompilerSettings(defs_mode=styx.compiler.settings.DefsMode.IMPORT)
model = boutiques_dummy({
"command-line": "dummy -a 1 -b 2 [X] -c 3 -d 4",
"inputs": [
Expand All @@ -226,7 +218,7 @@ def test_static_args() -> None:
],
})

compiled_module = styx.compiler.core.compile_boutiques_dict(model, settings)
compiled_module = styx.compiler.core.compile_boutiques_dict(model)

test_module = dynamic_module(compiled_module, "test_module")
dummy_runner = styx.runners.dummy.DummyRunner()
Expand All @@ -253,7 +245,6 @@ def test_arg_order() -> None:
The wrapper should respect the order of the arguments
in the Boutiques descriptor input array.
"""
settings = styx.compiler.settings.CompilerSettings(defs_mode=styx.compiler.settings.DefsMode.IMPORT)
model = boutiques_dummy({
"command-line": "[B] [A]",
"inputs": [
Expand All @@ -272,7 +263,7 @@ def test_arg_order() -> None:
],
})

compiled_module = styx.compiler.core.compile_boutiques_dict(model, settings)
compiled_module = styx.compiler.core.compile_boutiques_dict(model)

test_module = dynamic_module(compiled_module, "test_module")
dummy_runner = styx.runners.dummy.DummyRunner()
Expand Down
3 changes: 1 addition & 2 deletions tests/test_default_values.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@

def test_default_string_arg() -> None:
"""Default string argument."""
settings = styx.compiler.settings.CompilerSettings(defs_mode=styx.compiler.settings.DefsMode.IMPORT)
model = boutiques_dummy({
"command-line": "dummy [X]",
"inputs": [
Expand All @@ -26,7 +25,7 @@ def test_default_string_arg() -> None:
],
})

compiled_module = styx.compiler.core.compile_boutiques_dict(model, settings)
compiled_module = styx.compiler.core.compile_boutiques_dict(model)

test_module = dynamic_module(compiled_module, "test_module")
dummy_runner = styx.runners.dummy.DummyRunner()
Expand Down
Loading

0 comments on commit d96c759

Please sign in to comment.