diff --git a/README.md b/README.md index 18ff3c5..efd2dc4 100644 --- a/README.md +++ b/README.md @@ -21,4 +21,4 @@ set_global_runner(GraphRunner(get_global_runner())) # Use GraphRunner middlewar print(get_global_runner().mermaid()) # Print mermaid diagram -``` \ No newline at end of file +``` diff --git a/poetry.lock b/poetry.lock index fc67000..25d5d4b 100644 --- a/poetry.lock +++ b/poetry.lock @@ -1,4 +1,4 @@ -# This file is automatically @generated by Poetry 1.8.3 and should not be changed by hand. +# This file is automatically @generated by Poetry 1.8.2 and should not be changed by hand. [[package]] name = "cfgv" @@ -528,6 +528,17 @@ files = [ docs = ["furo", "jaraco.packaging (>=9.3)", "jaraco.tidelift (>=1.4)", "pygments-github-lexers (==0.0.5)", "pyproject-hooks (!=1.1)", "rst.linker (>=1.9)", "sphinx (>=3.5)", "sphinx-favicon", "sphinx-inline-tabs", "sphinx-lint", "sphinx-notfound-page (>=1,<2)", "sphinx-reredirects", "sphinxcontrib-towncrier"] testing = ["build[virtualenv] (>=1.0.3)", "filelock (>=3.4.0)", "importlib-metadata", "ini2toml[lite] (>=0.14)", "jaraco.develop (>=7.21)", "jaraco.envs (>=2.2)", "jaraco.path (>=3.2.0)", "mypy (==1.9)", "packaging (>=23.2)", "pip (>=19.1)", "pyproject-hooks (!=1.1)", "pytest (>=6,!=8.1.1)", "pytest-checkdocs (>=2.4)", "pytest-cov", "pytest-enabler (>=2.2)", "pytest-home (>=0.5)", "pytest-mypy", "pytest-perf", "pytest-ruff (>=0.2.1)", "pytest-subprocess", "pytest-timeout", "pytest-xdist (>=3)", "tomli", "tomli-w (>=1.0.0)", "virtualenv (>=13.0.0)", "wheel"] +[[package]] +name = "styxdefs" +version = "0.1.5" +description = "Styx definitions and minimal runtime" +optional = false +python-versions = ">=3.10" +files = [ + {file = "styxdefs-0.1.5-py3-none-any.whl", hash = "sha256:41b93e16a22d9edbf422a6c8e51cd2204a972ec92280382ef97df28bb6beb543"}, + {file = "styxdefs-0.1.5.tar.gz", hash = "sha256:33c2e57aeae3826f93314d3aef7ea8f7220efb61291ba1bbb2e7025344907944"}, +] + [[package]] name = "typing-extensions" version = "4.9.0" @@ -562,4 +573,4 @@ test = ["covdefaults (>=2.3)", "coverage (>=7.2.7)", "coverage-enable-subprocess [metadata] lock-version = "2.0" python-versions = "~3.11" -content-hash = "3144076938a7ab8b4f584898351705cb6d0a7402154150afed6f8d33a5332f21" +content-hash = "9aae9d66dabc6552c058b2c2fdd90645d79e401890a3283a56828eacab0a28c1" diff --git a/pyproject.toml b/pyproject.toml index 5c928ca..82afdb6 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -9,6 +9,7 @@ packages = [{include = "styxgraph", from = "src"}] [tool.poetry.dependencies] python = "~3.11" +styxdefs = "^0.1.5" [tool.poetry.group.dev.dependencies] pytest = "^8.2.2" diff --git a/src/styxgraph/__init__.py b/src/styxgraph/__init__.py index 8ea7b85..7a58ee5 100644 --- a/src/styxgraph/__init__.py +++ b/src/styxgraph/__init__.py @@ -1,26 +1,43 @@ """.. include:: ../../README.md""" # noqa: D415 import pathlib -from styxdefs import set_global_runner, get_global_runner, Runner, Execution, Metadata, InputPathType, OutputPathType + +from styxdefs import ( + Execution, + InputPathType, + Metadata, + OutputPathType, + Runner, +) class _GraphExecution(Execution): - def __init__(self, base: Execution, graph_runner: 'GraphRunner', metadata: Metadata) -> None: + """Graph execution.""" + + def __init__( + self, base: Execution, graph_runner: "GraphRunner", metadata: Metadata + ) -> None: + """Create a new GraphExecution.""" self.base = base self.graph_runner = graph_runner self.metadata = metadata - self.input_files = [] - self.output_files = [] + self.input_files: list[InputPathType] = [] + self.output_files: list[OutputPathType] = [] def input_file(self, host_file: InputPathType) -> str: + """Resolve input file.""" self.input_files.append(host_file) return self.base.input_file(host_file) def run(self, cargs: list[str]) -> None: - self.graph_runner.graph_append(self.metadata, self.input_files, self.output_files) + """Run the command.""" + self.graph_runner.graph_append( + self.metadata, self.input_files, self.output_files + ) return self.base.run(cargs) def output_file(self, local_file: str, optional: bool = False) -> OutputPathType: + """Resolve output file.""" output_file = self.base.output_file(local_file, optional) self.output_files.append(output_file) return output_file @@ -28,21 +45,32 @@ def output_file(self, local_file: str, optional: bool = False) -> OutputPathType # Define a new runner class GraphRunner(Runner): - def __init__(self, base: Runner): + """Graph runner.""" + + def __init__(self, base: Runner) -> None: + """Create a new GraphRunner.""" self.base = base self.graph: list[tuple[str, list[InputPathType], list[OutputPathType]]] = [] def start_execution(self, metadata: Metadata) -> Execution: + """Start a new execution.""" return _GraphExecution(self.base.start_execution(metadata), self, metadata) - - def graph_append(self, metadata: Metadata, input_file: list[InputPathType], output_file: list[OutputPathType]) -> None: - print(f'Appending {metadata.name} with {input_file} and {output_file}') + + def graph_append( + self, + metadata: Metadata, + input_file: list[InputPathType], + output_file: list[OutputPathType], + ) -> None: + """Append a node to the graph.""" + print(f"Appending {metadata.name} with {input_file} and {output_file}") self.graph.append((metadata.name, input_file, output_file)) def mermaid(self) -> str: - connections = [] - inputs_lookup = {} - outputs_lookup = {} + """Generate a mermaid graph of the graph.""" + connections: list[str] = [] + inputs_lookup: dict[str, list[str]] = {} + outputs_lookup: dict[str, str] = {} for id, inputs, outputs in self.graph: for input in inputs: if input not in inputs_lookup: @@ -54,13 +82,15 @@ def mermaid(self) -> str: for id, inputs, _ in self.graph: for input in inputs: for output, output_id in outputs_lookup.items(): - if pathlib.Path(input).is_relative_to(output): # is subfolder/file in output root - connections.append(f'{output_id} --> {id}') - + if pathlib.Path(input).is_relative_to( + output + ): # is subfolder/file in output root + connections.append(f"{output_id} --> {id}") + # Generate mermaid - mermaid = 'graph TD\n' + mermaid = "graph TD\n" for id, _, _ in self.graph: - mermaid += f' {id}\n' + mermaid += f" {id}\n" for connection in connections: - mermaid += f' {connection}\n' + mermaid += f" {connection}\n" return mermaid