Skip to content

Commit

Permalink
refactored plugins for run_for_prompt
Browse files Browse the repository at this point in the history
  • Loading branch information
faermanj committed Oct 20, 2023
1 parent 6de74c3 commit 332581e
Show file tree
Hide file tree
Showing 15 changed files with 91 additions and 53 deletions.
4 changes: 2 additions & 2 deletions up_ansible/up_ansible/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from up_cli import pm
from up_ansible import image_for_prompt
from up_ansible import run_for_prompt

pm.register(image_for_prompt)
pm.register(run_for_prompt)
5 changes: 0 additions & 5 deletions up_ansible/up_ansible/image_for_prompt.py

This file was deleted.

11 changes: 11 additions & 0 deletions up_ansible/up_ansible/run_for_prompt.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
from up_cli import hookimpl, match_prompt, RunConfig

def mk_run_config(prompt):
result = RunConfig(
image="cytopia/ansible:latest",
command=prompt)
return result

@hookimpl
def run_for_prompt(prompt):
return match_prompt(mk_run_config, prompt, "ansible")
42 changes: 37 additions & 5 deletions up_cli/poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion up_cli/pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ packages = [
]

[tool.poetry.dependencies]
python = "^3.11"
python = "^3.12"
pluggy = "^1.3.0"
docker = "^6.1.3"
up_ansible = { path = "../up_ansible", develop = true }
Expand Down
36 changes: 24 additions & 12 deletions up_cli/src/up_cli/__init__.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import pluggy
from dataclasses import dataclass
import logging as log
from dataclasses import dataclass, field
from typing import TypeAlias
from typing import Callable

Expand All @@ -9,24 +10,35 @@
# https://docker-py.readthedocs.io/en/stable/containers.html
@dataclass
class RunConfig:
name: str
image: str
command: list[str]
environment: dict[str, str]
ports: dict[str, str]
auto_remove: bool = True
network_mode: str = "host"
# volumes: dict[str, str]
name: str = field(kw_only=True, default="")
image: str = field(kw_only=True, default="")
command: list[str] = field(kw_only=True, default_factory=list)
environment: dict[str, str] = field(kw_only=True, default_factory=dict)
ports: dict[str, str] = field(kw_only=True, default_factory=dict)
volumes: dict[str, str] = field(kw_only=True, default_factory=dict)
auto_remove: bool = field(kw_only=True, default=True)
network_mode: str = field(kw_only=True, default="host")


RunConfigs:TypeAlias = list[RunConfig]
Prompt:TypeAlias = list[str]

def match_prompt(prompt: Prompt,
mk_run_config: Callable[[], RunConfig],
def does_match(prompt: Prompt, args) -> bool:
for p, a in zip(prompt, args):
if p != a:
return False
return True

def match_prompt(mk_run_config: Callable[[], RunConfig],
prompt: Prompt,
*args) -> list[RunConfig]:
if not prompt:
return None
#TODO: implement matching
if does_match(prompt, args):
log.info(f"MATCH: prompt={prompt}, args={args}")
return [mk_run_config(prompt)]
log.info(f"NO MATCH: prompt={prompt}, args={args}")
return None



2 changes: 0 additions & 2 deletions up_cli/src/up_cli/containers.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@ class DockerContainers:
def run(self, run: RunConfig):
log.debug("Running container: %s", run)
client = docker.from_env()
image = run.image
prompt = run.prompt
#TODO: Catch errors, print properly, pass all params
result = client.containers.run(
image=run.image,
Expand Down
5 changes: 0 additions & 5 deletions up_cli/src/up_cli/defaults/image_for_prompt.py

This file was deleted.

2 changes: 1 addition & 1 deletion up_cli/src/up_cli/hookspec.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@
hookspec = pluggy.HookspecMarker("up")

@hookspec(firstresult=True)
def to_run_configs(prompt: Prompt) -> RunConfigs:
def run_for_prompt(prompt: Prompt) -> RunConfigs:
"""Present run configurations to execute for prompt"""

2 changes: 1 addition & 1 deletion up_cli/src/up_cli/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ def main():
containers.run(run_config)

def to_run_configs(prompt:list[str]) -> list[RunConfig]:
result = pm.hook.to_run_configs(prompt=prompt)
result = pm.hook.run_for_prompt(prompt=prompt)
if not result:
result = []
return result
Expand Down
2 changes: 0 additions & 2 deletions up_cli/src/up_cli/plugins.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import pkgutil

from up_cli import pm, hookspec
from .defaults import image_for_prompt

def load_plugins(context):
log.info("Loading plugins")
Expand All @@ -17,6 +16,5 @@ def load_plugins(context):
# create a manager and add the spec
pm.add_hookspecs(hookspec)
pm.load_setuptools_entrypoints("up")
pm.register(image_for_prompt)
log.debug("Plugins loaded.")
log.debug(str(pm.get_plugins()))
7 changes: 2 additions & 5 deletions up_splat/up_splat/__init__.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
from up_cli import pm
from up_splat import image_for_prompt
from up_splat import substitutions_for_prompt
from up_splat import run_for_prompt


pm.register(image_for_prompt)
pm.register(substitutions_for_prompt)
pm.register(run_for_prompt)
5 changes: 0 additions & 5 deletions up_splat/up_splat/image_for_prompt.py

This file was deleted.

12 changes: 12 additions & 0 deletions up_splat/up_splat/run_for_prompt.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
from up_cli import hookimpl, match_prompt, RunConfig

def mk_run_config(prompt):
result = RunConfig(
image="fedora",
command=['echo', 'Thank you for trying UP: '] + prompt)
return result

@hookimpl
def run_for_prompt(prompt):
return match_prompt(mk_run_config, prompt, "splat")

7 changes: 0 additions & 7 deletions up_splat/up_splat/substitutions_for_prompt.py

This file was deleted.

0 comments on commit 332581e

Please sign in to comment.