generated from childmindresearch/template-python-repository
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
46 changed files
with
1,653 additions
and
2,361 deletions.
There are no files selected for viewing
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
"""Python wrapper backend.""" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
Oops, something went wrong.