Skip to content

Commit

Permalink
IR progress
Browse files Browse the repository at this point in the history
  • Loading branch information
nx10 committed Sep 6, 2024
1 parent f30e225 commit 6daecb8
Show file tree
Hide file tree
Showing 6 changed files with 35 additions and 18 deletions.
9 changes: 6 additions & 3 deletions src/styx/backend/python/core.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import dataclasses
from typing import Any, Generator, Iterable

from styx.backend.python.documentation import docs_to_docstring
from styx.backend.python.interface import compile_interface
from styx.backend.python.pycodegen.core import PyModule
from styx.backend.python.pycodegen.scope import Scope
Expand Down Expand Up @@ -34,7 +35,9 @@ def to_python(interfaces: Iterable[Interface]) -> Generator[tuple[str, list[str]
package=interface.package,
package_symbol=global_scope.add_or_dodge(python_snakify(interface.package.name)),
scope=Scope(parent=global_scope),
module=PyModule(),
module=PyModule(
docstr=docs_to_docstring(interface.package.docs),
),
)
package_data = packages[interface.package.name]

Expand All @@ -44,7 +47,7 @@ def to_python(interfaces: Iterable[Interface]) -> Generator[tuple[str, list[str]
interface_module = PyModule()
compile_interface(interface=interface, package_scope=package_data.scope, interface_module=interface_module)
package_data.module.imports.append(f"from .{interface_module_symbol} import *")
yield interface_module.text(), [interface.package.name, interface_module_symbol]
yield interface_module.text(), [package_data.package_symbol, interface_module_symbol]

for package_data in packages.values():
yield package_data.module.text(), [package_data.package_symbol]
yield package_data.module.text(), [package_data.package_symbol, "__init__"]

Check warning on line 53 in src/styx/backend/python/core.py

View check run for this annotation

Codecov / codecov/patch

src/styx/backend/python/core.py#L52-L53

Added lines #L52 - L53 were not covered by tests
8 changes: 5 additions & 3 deletions src/styx/backend/python/documentation.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,14 +38,16 @@ def docs_to_docstring(docs: Documentation) -> str | None:
if len(docs.literature) == 1:
re += f"Literature: {docs.literature[0]}"

Check warning on line 39 in src/styx/backend/python/documentation.py

View check run for this annotation

Codecov / codecov/patch

src/styx/backend/python/documentation.py#L37-L39

Added lines #L37 - L39 were not covered by tests
else:
re += f"Literature:\n{'\n'.join(docs.literature)}"
entries = '\n'.join(docs.literature)
re += f"Literature:\n{entries}"

Check warning on line 42 in src/styx/backend/python/documentation.py

View check run for this annotation

Codecov / codecov/patch

src/styx/backend/python/documentation.py#L41-L42

Added lines #L41 - L42 were not covered by tests

if docs.authors:
if docs.urls:
re = _ensure_double_linebreak_if_not_empty(re)
if len(docs.urls) == 1:
re += f"URL: {docs.urls[0]}"

Check warning on line 47 in src/styx/backend/python/documentation.py

View check run for this annotation

Codecov / codecov/patch

src/styx/backend/python/documentation.py#L45-L47

Added lines #L45 - L47 were not covered by tests
else:
re += f"URLs:\n{'\n'.join(docs.urls)}"
entries = '\n'.join(docs.urls)
re += f"URLs:\n{entries}"

Check warning on line 50 in src/styx/backend/python/documentation.py

View check run for this annotation

Codecov / codecov/patch

src/styx/backend/python/documentation.py#L49-L50

Added lines #L49 - L50 were not covered by tests

if re:
return re
Expand Down
6 changes: 3 additions & 3 deletions src/styx/backend/python/interface.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,6 @@ def _compile_struct(
docstring_body=docs_to_docstring(param.param.docs),
)
pyargs = func_cargs_building.args
interface_module.funcs.append(func_cargs_building)
else:
func_cargs_building = PyFunc(

Check warning on line 52 in src/styx/backend/python/interface.py

View check run for this annotation

Codecov / codecov/patch

src/styx/backend/python/interface.py#L52

Added line #L52 was not covered by tests
name="run",
Expand Down Expand Up @@ -78,7 +77,6 @@ def _compile_struct(
)
struct_class.methods.append(func_outputs)
pyargs = struct_class.fields

Check warning on line 79 in src/styx/backend/python/interface.py

View check run for this annotation

Codecov / codecov/patch

src/styx/backend/python/interface.py#L78-L79

Added lines #L78 - L79 were not covered by tests
interface_module.funcs.append(struct_class)

# Collect param python symbols
for elem in param.struct.iter_params():
Expand Down Expand Up @@ -131,20 +129,22 @@ def _compile_struct(
"execution.run(cargs)",
"return ret",
])
interface_module.funcs_and_classes.append(func_cargs_building)
else:
if has_outputs:
_compile_outputs_building(

Check warning on line 135 in src/styx/backend/python/interface.py

View check run for this annotation

Codecov / codecov/patch

src/styx/backend/python/interface.py#L134-L135

Added lines #L134 - L135 were not covered by tests
param=param,
func=func_outputs,
lookup=lookup,
access_via_self=False,
access_via_self=True,
)
func_outputs.body.extend([

Check warning on line 141 in src/styx/backend/python/interface.py

View check run for this annotation

Codecov / codecov/patch

src/styx/backend/python/interface.py#L141

Added line #L141 was not covered by tests
"return ret",
])
func_cargs_building.body.extend([

Check warning on line 144 in src/styx/backend/python/interface.py

View check run for this annotation

Codecov / codecov/patch

src/styx/backend/python/interface.py#L144

Added line #L144 was not covered by tests
"return cargs",
])
interface_module.funcs_and_classes.append(struct_class)

Check warning on line 147 in src/styx/backend/python/interface.py

View check run for this annotation

Codecov / codecov/patch

src/styx/backend/python/interface.py#L147

Added line #L147 was not covered by tests


def _compile_cargs_building(
Expand Down
2 changes: 1 addition & 1 deletion src/styx/backend/python/lookup.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ def _collect_output_field_symbols(
) -> None:
scope = Scope(parent=package_scope)
for output in param.param.outputs:
output_field_symbol = scope.add_or_dodge(output.name)
output_field_symbol = scope.add_or_dodge(python_snakify(output.name))
assert output.id_ not in lookup_output_field_symbol
lookup_output_field_symbol[output.id_] = output_field_symbol

Expand Down
8 changes: 6 additions & 2 deletions src/styx/backend/python/pycodegen/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -201,9 +201,10 @@ class PyModule(PyGen):

imports: LineBuffer = field(default_factory=list)
header: LineBuffer = field(default_factory=list)
funcs: list[PyFunc | PyDataClass] = field(default_factory=list)
funcs_and_classes: list[PyFunc | PyDataClass] = field(default_factory=list)
footer: LineBuffer = field(default_factory=list)
exports: list[str] = field(default_factory=list)
docstr: str | None = None

def generate(self) -> LineBuffer:
exports = (
Expand All @@ -217,13 +218,16 @@ def generate(self) -> LineBuffer:
)

return blank_after([
*(
['"""', *linebreak_paragraph(self.docstr), '"""'] if self.docstr else []
),
*comment([
"This file was auto generated by Styx.",
"Do not edit this file directly.",
]),
*blank_before(self.imports),
*blank_before(self.header),
*[line for func in self.funcs for line in blank_before(func.generate(), 2)],
*[line for func in self.funcs_and_classes for line in blank_before(func.generate(), 2)],
*blank_before(self.footer),
*blank_before(exports, 2),
])
20 changes: 14 additions & 6 deletions src/styx/frontend/boutiques/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -331,6 +331,9 @@ def _get_urls(bt: dict) -> list[str]:

parent_input: dict | None = None
if "type" not in bt: # Root boutiques descriptor
if (bt_id := bt.get("id", bt.get("name"))) is None:
raise Exception(f"Descriptor is missing id/name: {bt_id}")

Check warning on line 335 in src/styx/frontend/boutiques/core.py

View check run for this annotation

Codecov / codecov/patch

src/styx/frontend/boutiques/core.py#L335

Added line #L335 was not covered by tests

groups, ir_id_lookup = _collect_inputs(bt, id_counter)
outputs = _collect_outputs(bt, ir_id_lookup, id_counter)

Expand All @@ -342,11 +345,11 @@ def _get_urls(bt: dict) -> list[str]:

return ir.DParam(
id_=id_counter.next(),
name=bt.get("id", bt["name"]),
name=bt_id,
outputs=outputs,
docs=docs,
), ir.DStruct(
name=bt.get("id", bt["name"]),
name=bt_id,
groups=groups,
docs=docs,
)
Expand Down Expand Up @@ -444,7 +447,11 @@ def _collect_inputs(bt, id_counter):
return groups, ir_id_lookup


def from_boutiques(tool: dict, package_name: str) -> ir.Interface:
def from_boutiques(
tool: dict,
package_name: str,
package_docs: ir.Documentation | None = None,
) -> ir.Interface:
"""Convert a Boutiques tool to a Styx descriptor."""
hash_ = _hash_from_boutiques(tool)

Expand All @@ -462,6 +469,7 @@ def from_boutiques(tool: dict, package_name: str) -> ir.Interface:
name=package_name,
version=tool.get("tool-version"),
docker=docker,
docs=package_docs if package_docs else ir.Documentation(),
),
command=ir.PStruct(
param=dparam,
Expand All @@ -476,7 +484,7 @@ def from_boutiques(tool: dict, package_name: str) -> ir.Interface:
print(json_path)
with open(json_path, "r", encoding="utf-8") as json_file:
json_data = json.load(json_file)
ir_data = from_boutiques(json_data, package_name="AFNI")
ir_data = from_boutiques(json_data, package_name="afni", package_docs=ir.Documentation(urls=["Helo hehe"]))
from styx.ir.pretty_print import pretty_print

Check warning on line 488 in src/styx/frontend/boutiques/core.py

View check run for this annotation

Codecov / codecov/patch

src/styx/frontend/boutiques/core.py#L483-L488

Added lines #L483 - L488 were not covered by tests

pretty_print(ir_data)
Expand All @@ -485,8 +493,8 @@ def from_boutiques(tool: dict, package_name: str) -> ir.Interface:
print(stats(ir_data))
from styx.backend.python.core import to_python

Check warning on line 494 in src/styx/frontend/boutiques/core.py

View check run for this annotation

Codecov / codecov/patch

src/styx/frontend/boutiques/core.py#L493-L494

Added lines #L493 - L494 were not covered by tests

for module, module_path in to_python([ir_data]):
for py, module_path in to_python([ir_data]):
print("=" * 80)
print("File: " + ".".join(module_path))
print("-" * 80)
print(module.text())
print(py)

Check warning on line 500 in src/styx/frontend/boutiques/core.py

View check run for this annotation

Codecov / codecov/patch

src/styx/frontend/boutiques/core.py#L496-L500

Added lines #L496 - L500 were not covered by tests

0 comments on commit 6daecb8

Please sign in to comment.