Skip to content

Commit

Permalink
Format code after generation
Browse files Browse the repository at this point in the history
  • Loading branch information
nx10 committed Mar 7, 2024
1 parent cfe868b commit c8f3292
Show file tree
Hide file tree
Showing 7 changed files with 56 additions and 23 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ Compile Python command line tool wrappers from Boutiques descriptors.
- [x] Documentation generation
- [x] Custom execution environments via dependency injection
- [x] Runtime input validation
- [ ] Unit test generation
- [ ] Unit & integration test generation

## Boutiques descriptor implementation status: 77%

Expand Down
38 changes: 19 additions & 19 deletions poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,14 @@ packages = [{include = "styx", from = "src"}]
[tool.poetry.dependencies]
python = "^3.11"
pydantic = "^2.6.3"
ruff = "^0.3.1"

[tool.poetry.group.dev.dependencies]
pytest = "^8.0.2"
mypy = "^1.7.1"
pre-commit = "^3.6.0"
pytest-cov = "^4.1.0"
ruff = "^0.3.0"
ruff = "^0.3.1"

[tool.poetry.group.docs.dependencies]
pdoc = "^14.2.0"
Expand Down
9 changes: 7 additions & 2 deletions src/styx/compiler/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
from styx.compiler.settings import CompilerSettings, DefsMode
from styx.compiler.utils import optional_float_to_int
from styx.pycodegen.core import LineBuffer, PyArg, PyFunc, PyModule, collapse, expand, indent
from styx.pycodegen.format import format_code
from styx.pycodegen.scope import Scope
from styx.pycodegen.utils import (
as_py_literal,
Expand Down Expand Up @@ -519,7 +520,6 @@ def _from_boutiques(tool: bt.Tool, settings: CompilerSettings) -> str: # type:
if tool.container_image.root.image is not None:
metadata["container_image_tag"] = tool.container_image.root.image

print(tool.container_image)
# Static metadata
buf_header.extend([
*defs,
Expand Down Expand Up @@ -575,7 +575,12 @@ def _from_boutiques(tool: bt.Tool, settings: CompilerSettings) -> str: # type:
mod.imports.extend(["import typing"])
mod.imports.sort()

return mod.text()
code = mod.text()
code_formatted = format_code(code)
if code_formatted is None:
print("WARNING: Failed to format code")
return code
return code_formatted


def compile_descriptor(descriptor: bt.Tool, settings: CompilerSettings) -> str: # type: ignore
Expand Down
23 changes: 23 additions & 0 deletions src/styx/pycodegen/format.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
"""Format code using ruff."""

import subprocess
from pathlib import Path


def format_code(code: str) -> str | None:
"""Format code using ruff."""
try:
formatted = subprocess.check_output(
[
"ruff",
"format",
"--config",
Path(__file__).parent / "ruff.toml",
"-",
],
input=code,
encoding="utf-8",
)
return formatted
except subprocess.CalledProcessError as _:
return None
4 changes: 4 additions & 0 deletions src/styx/pycodegen/ruff.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
line-length = 80

[lint.pydocstyle]
convention = "google"
Empty file added src/styx/runners/local.py
Empty file.

0 comments on commit c8f3292

Please sign in to comment.