Source code for gxformat2.examples
"""Galaxy workflow examples shipped with gxformat2 for testing and downstream use."""
import os
from enum import Enum
import yaml
from pydantic import BaseModel
from gxformat2.yaml import ordered_load
EXAMPLES_DIR = os.path.dirname(__file__)
FORMAT2_DIR = os.path.join(EXAMPLES_DIR, "format2")
NATIVE_DIR = os.path.join(EXAMPLES_DIR, "native")
[docs]
class ExampleOrigin(str, Enum):
"""How the workflow was produced."""
real = "real"
real_hacked = "real-hacked"
synthetic = "synthetic"
converted = "converted"
[docs]
class CatalogEntry(BaseModel):
"""Metadata for an example workflow in the catalog."""
file: str
origin: ExampleOrigin
format: ExampleFormat
tests: list[str]
@property
def name(self) -> str:
"""Filename without directory prefix."""
return os.path.basename(self.file)
@property
def path(self) -> str:
"""Absolute path to the workflow file."""
return os.path.join(EXAMPLES_DIR, self.file)
[docs]
def load(self) -> dict:
"""Load and parse the workflow file."""
with open(self.path) as f:
return ordered_load(f)
[docs]
def load_contents(self) -> str:
"""Return raw file contents."""
with open(self.path) as f:
return f.read()
@property
def workflow_label(self) -> str | None:
"""Workflow label or name extracted from the file."""
wf = self.load()
return wf.get("label") or wf.get("name")
@property
def workflow_annotation(self) -> str | None:
"""Workflow doc or annotation extracted from the file."""
wf = self.load()
doc = wf.get("doc") or wf.get("annotation") or ""
if isinstance(doc, list):
doc = "\n".join(doc)
return doc.strip() or None
[docs]
def load_catalog() -> list[CatalogEntry]:
"""Load and validate the example workflow catalog."""
with open(os.path.join(EXAMPLES_DIR, "catalog.yml")) as f:
raw = yaml.safe_load(f)
return [CatalogEntry.model_validate(entry) for entry in raw]
[docs]
def get_path(name: str) -> str:
"""Return absolute path to an example workflow file by name (with extension)."""
for subdir in [FORMAT2_DIR, NATIVE_DIR]:
path = os.path.join(subdir, name)
if os.path.exists(path):
return path
raise FileNotFoundError(f"Example workflow not found: {name}")
[docs]
def load(name: str) -> dict:
"""Load and parse an example workflow by name."""
with open(get_path(name)) as f:
return ordered_load(f)
[docs]
def load_contents(name: str) -> str:
"""Return raw file contents of an example workflow by name."""
with open(get_path(name)) as f:
return f.read()