Skip to content

Commit

Permalink
Add plumbum
Browse files Browse the repository at this point in the history
  • Loading branch information
kmagusiak committed Dec 28, 2023
1 parent 417d962 commit 2a4e13e
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 8 deletions.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,9 @@ alphaconf.setup_configuration({'backup': 'all'})
alphaconf.invoke.run(__name__, ns)
```

## Plumbum
Replace `invoke` with alphaconf and plumbum.

## Way to 1.0
- Run a specific function `alphaconf my.module.main`:
find functions and inject args
Expand Down
28 changes: 26 additions & 2 deletions alphaconf/cli.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import sys
from typing import Callable, Optional, Sequence, TypeVar, Union
from typing import Any, Callable, Optional, Sequence, TypeVar, Union

from omegaconf import MissingMandatoryValue, OmegaConf

Expand All @@ -16,7 +16,31 @@ class CommandAction(Action):


class CliApplication(Application):
pass
commands: dict[str, Callable[[], Any]]

def __init__(self, *, name: str | None = None, **properties) -> None:
super().__init__(name=name, **properties)
self.commands = {}

def command(self, name=None, inject=False):
def register_command(func):
reg_name = name or func.__name__
if inject:
from .inject import inject_auto

func = inject_auto()(func)
self.commands[reg_name] = func

return register_command

def _run(self):
# TODO
for cmd in self.commands.values():
return cmd()
return None

def run(self, **config):
return run(self._run, **config, app=self)


def run(
Expand Down
24 changes: 24 additions & 0 deletions example-plumbum.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#!/usr/bin/env python3
import logging

import plumbum

import alphaconf.cli

alphaconf.setup_configuration({"cmd": "ls"})

app = alphaconf.cli.CliApplication()


@app.command()
def main():
"""Simple demo of alphaconf with plumbum"""

log = logging.getLogger(__name__)
cmd = plumbum.local[alphaconf.get("cmd")]
log.info("Running a command %s", cmd)
return cmd.run_fg()


if __name__ == '__main__':
app.run()
11 changes: 5 additions & 6 deletions example-simple.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,10 @@ class Opts(BaseModel):
)


app = alphaconf.cli.CliApplication(name='example', version='0.1')


@app.command()
def main():
"""Simple demo of alphaconf"""

Expand Down Expand Up @@ -58,9 +62,4 @@ def main():


if __name__ == '__main__':
# running with explicit parameters
alphaconf.cli.run(
main,
name='example',
version='0.1',
)
app.run()

0 comments on commit 2a4e13e

Please sign in to comment.