Skip to content

Commit

Permalink
Merge branch 'master' of github.com:Wytamma/snk
Browse files Browse the repository at this point in the history
  • Loading branch information
Wytamma committed Jan 11, 2024
2 parents 02bb218 + f6ad1a2 commit 115d238
Show file tree
Hide file tree
Showing 5 changed files with 83 additions and 16 deletions.
24 changes: 9 additions & 15 deletions snk/cli/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
from art import text2art

from snk.cli.dynamic_typer import DynamicTyper
from snk.cli.subcommands import EnvApp, ConfigApp, RunApp, ScriptApp
from snk.cli.subcommands import EnvApp, ConfigApp, RunApp, ScriptApp, ProfileApp

from .config.config import (
SnkConfig,
Expand Down Expand Up @@ -83,8 +83,6 @@ def __init__(self, pipeline_dir_path: Path = None) -> None:
context_settings={"help_option_names": ["-h", "--help"]},
)
self.register_command(self.info, help="Display information about the pipeline.")
if self.pipeline.profiles:
self.register_command(self.profile, help="Access the pipeline profiles.")

run_app = RunApp(
conda_prefix_dir=self.conda_prefix_dir,
Expand Down Expand Up @@ -131,6 +129,14 @@ def __init__(self, pipeline_dir_path: Path = None) -> None:
name="script",
help="Access the pipeline scripts.",
)
if self.pipeline.profiles:
self.register_group(
ProfileApp(
pipeline=self.pipeline,
),
name="profile",
help="Access the pipeline profiles.",
)

def _print_pipline_version(self, ctx: typer.Context, value: bool):
if value:
Expand Down Expand Up @@ -218,15 +224,3 @@ def info(self):
info_dict["pipeline_dir_path"] = str(self.pipeline.path)
typer.echo(json.dumps(info_dict, indent=2))

def profile(
self,
name: str = typer.Argument(None, help="The name of the profile."),
):
profiles_dir_yellow = typer.style(
self.pipeline.path / "profiles", fg=typer.colors.YELLOW
)
typer.echo(
f"Found {len(self.pipeline.profiles)} profiles in {profiles_dir_yellow}"
)
for profile in self.pipeline.profiles:
typer.echo(f"- {profile.name}")
1 change: 1 addition & 0 deletions snk/cli/subcommands/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@
from .config import ConfigApp
from .run import RunApp
from .script import ScriptApp
from .profile import ProfileApp
1 change: 0 additions & 1 deletion snk/cli/subcommands/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ def __init__(self, pipeline: Pipeline, options: List[Option]):
self.options = options
self.pipeline = pipeline
self.register_default_command(self.show)
self.register_command(self.show, help="Show the pipeline configuration.")

def show(
self, ctx: typer.Context, pretty: bool = typer.Option(False, "--pretty", "-p")
Expand Down
54 changes: 54 additions & 0 deletions snk/cli/subcommands/profile.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
import typer
from snk.cli.dynamic_typer import DynamicTyper
from snk.pipeline import Pipeline
from rich.console import Console
from rich.syntax import Syntax
from pathlib import Path


class ProfileApp(DynamicTyper):
def __init__(
self,
pipeline: Pipeline,
):
self.pipeline = pipeline
self.register_command(self.list, help="List the profiles in the pipeline.")
self.register_command(
self.show, help="Show the contents of a profile."
)

def list(
self,
verbose: bool = typer.Option(False, "--verbose", "-v", help="Show profiles as paths."),
):
number_of_profiles = len(self.pipeline.profiles)
typer.echo(
f"Found {number_of_profiles} profile{'' if number_of_profiles == 1 else 's'}:"
)
for profile in self.pipeline.profiles:
if verbose:
typer.echo(f"- {typer.style(profile, fg=typer.colors.YELLOW)}")
else:
typer.echo(f"- {profile.stem}")

def _get_profile_path(self, name: str) -> Path:
profile = [e for e in self.pipeline.profiles if e.name == name or e.stem == name]
if not profile:
self.error(f"Profile {name} not found!")
return profile[0]

def show(
self,
name: str = typer.Argument(..., help="The name of the profile."),
pretty: bool = typer.Option(
False, "--pretty", "-p", help="Pretty print the profile."
),
):
profile_path = self._get_profile_path(name)
profile_file_text = profile_path.read_text()
if pretty:
syntax = Syntax(profile_file_text, "yaml")
console = Console()
console.print(syntax)
else:
typer.echo(profile_file_text)
19 changes: 19 additions & 0 deletions tests/cli/test_pipline_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,21 @@ def test_help(local_runner: CLIRunner):
assert res.code == 0, res.stderr
assert "Usage:" in res.stdout

def test_version(local_runner: CLIRunner):
res = local_runner(["--version"])
assert res.code == 0, res.stderr
assert "0.13.0" in res.stdout

def test_path(local_runner: CLIRunner):
res = local_runner(["--path"])
assert res.code == 0, res.stderr
assert "/pipeline" in res.stdout

def test_profile(local_runner: CLIRunner):
res = local_runner(["profile", "-h"])
assert res.code == 0, res.stderr
assert "list" in res.stdout, res.stderr
assert "show" in res.stdout, res.stderr

def test_info(local_runner: CLIRunner):
res = local_runner(["info"])
Expand All @@ -37,6 +52,10 @@ def test_info(local_runner: CLIRunner):
assert "version" in res.stdout
assert "pipeline_dir_path" in res.stdout

def test_config(local_runner: CLIRunner):
res = local_runner(["config"])
assert res.code == 0, res.stderr
assert "output: hello.txt" in res.stdout

def test_run_cli(local_runner: CLIRunner):
res = local_runner(["run", "-h"])
Expand Down

0 comments on commit 115d238

Please sign in to comment.