Skip to content

Commit

Permalink
IR progress
Browse files Browse the repository at this point in the history
  • Loading branch information
nx10 committed Sep 4, 2024
1 parent ae70f7c commit 27f553a
Show file tree
Hide file tree
Showing 46 changed files with 1,653 additions and 2,361 deletions.
Empty file added src/styx/backend/__init__.py
Empty file.
1 change: 1 addition & 0 deletions src/styx/backend/python/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
"""Python wrapper backend."""
50 changes: 50 additions & 0 deletions src/styx/backend/python/core.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import dataclasses
from typing import Any, Generator, Iterable

from styx.backend.python.interface import compile_interface
from styx.backend.python.pycodegen.core import PyModule
from styx.backend.python.pycodegen.scope import Scope
from styx.backend.python.pycodegen.utils import python_snakify
from styx.ir.core import Interface, Package


@dataclasses.dataclass
class _PackageData:
package: Package
package_symbol: str
scope: Scope
module: PyModule


def to_python(interfaces: Iterable[Interface]) -> Generator[tuple[str, list[str]], Any, None]:
"""For a stream of IR interfaces return a stream of Python modules and their module paths.
Args:
interfaces: Stream of IR interfaces.
Returns:
Stream of tuples (Python module, module path).
"""
packages: dict[str, _PackageData] = {}
global_scope = Scope(parent=Scope.python())

for interface in interfaces:
if interface.package.name not in packages:
packages[interface.package.name] = _PackageData(
package=interface.package,
package_symbol=global_scope.add_or_dodge(python_snakify(interface.package.name)),
scope=Scope(parent=global_scope),
module=PyModule(),
)
package_data = packages[interface.package.name]

# interface_module_symbol = global_scope.add_or_dodge(python_snakify(interface.command.param.name))
interface_module_symbol = python_snakify(interface.command.param.name)

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]

for package_data in packages.values():
yield package_data.module.text(), [package_data.package_symbol]
52 changes: 52 additions & 0 deletions src/styx/backend/python/documentation.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
from styx.ir.core import (
Documentation,
)


def _ensure_period(s: str) -> str:
if not s.endswith("."):
return f"{s}."
return s


def _ensure_double_linebreak_if_not_empty(s: str) -> str:
if s == "" or s.endswith("\n\n"):
return s
if s.endswith("\n"):
return f"{s}\n"
return f"{s}\n\n"


def docs_to_docstring(docs: Documentation) -> str | None:
re = ""
if docs.title:
re += docs.title

if docs.description:
re = _ensure_double_linebreak_if_not_empty(re)
re += _ensure_period(docs.description)

if docs.authors:
re = _ensure_double_linebreak_if_not_empty(re)
if len(docs.authors) == 1:
re += f"Author: {docs.authors[0]}"
else:
re += f"Authors: {', '.join(docs.authors)}"

if docs.literature:
re = _ensure_double_linebreak_if_not_empty(re)
if len(docs.literature) == 1:
re += f"Literature: {docs.literature[0]}"
else:
re += f"Literature:\n{'\n'.join(docs.literature)}"

if docs.authors:
re = _ensure_double_linebreak_if_not_empty(re)
if len(docs.urls) == 1:
re += f"URL: {docs.urls[0]}"
else:
re += f"URLs:\n{'\n'.join(docs.urls)}"

if re:
return re
return None
Loading

0 comments on commit 27f553a

Please sign in to comment.