Skip to content

Commit

Permalink
Merge pull request #5 from FragileTech/bugfix
Browse files Browse the repository at this point in the history
Improve terminal effects. Add as kwargs. Fix kwargs not resolved
  • Loading branch information
Guillemdb authored Sep 20, 2024
2 parents 046d34b + f1cfef3 commit 9370e93
Show file tree
Hide file tree
Showing 4 changed files with 184 additions and 71 deletions.
25 changes: 15 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ import sys
from omegaconf import DictConfig
from hydraclick import hydra_command

@hydra_command(config_path="configs", config_name="app_config", run_mode="kwargs")
@hydra_command(config_path="configs", config_name="app_config", as_kwargs=True)
def main(**kwargs):
print(f"Running with config: {kwargs}")

Expand All @@ -66,16 +66,20 @@ if __name__ == "__main__":
Decorator to create CLI commands.

```python
import omegaconf


def hydra_command(
config_path: str | Path | None = None,
config_name: str | None = "config",
version_base: str | None = None,
run_mode: str = "config",
preprocess_config: Callable[[DictConfig], DictConfig] | None = None,
print_config: bool = True,
resolve: bool = True,
use_flogging: bool = True,
**flogging_kwargs: Any,
config_path: str | Path | None = None,
config_name: str | None = "config",
version_base: str | None = None,
as_kwargs: bool = False,
preprocess_config: Callable[[DictConfig], DictConfig] | None = None,
print_config: bool = True,
resolve: bool = True,
use_flogging: bool = True,
terminal_effect: Callable | None = omegaconf.MISSING,
**flogging_kwargs: Any,
) -> Callable:
```

Expand All @@ -95,6 +99,7 @@ Defaults to `False`.
- `resolve`: Whether to resolve the configuration.
- `use_flogging`: Whether to use flogging for structured logging.
- `**flogging_kwargs`: Additional keyword arguments for flogging.
- `terminal_effect`: The terminal effect function to use when rendering the command help.

## Logging with Flogging

Expand Down
2 changes: 1 addition & 1 deletion src/hydraclick/__init__.py
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
from hydraclick.terminal_effects import set_terminal_effect, NO_TERMINAL_EFFECTS
from hydraclick.terminal_effects import set_terminal_effect
from hydraclick.core import hydra_command
15 changes: 14 additions & 1 deletion src/hydraclick/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,11 @@
from typing import Callable, Any

import hydra
import omegaconf
from omegaconf import DictConfig, OmegaConf
from unittest.mock import patch


from hydraclick import set_terminal_effect
from hydraclick.display_config import display_config
from hydraclick.options import (
hydra_args_argument,
Expand All @@ -25,6 +26,7 @@
config_name_option,
shell_completion_option,
)
from hydraclick.terminal_effects import display_terminal_effect

_logger = logging.getLogger(__name__)

Expand Down Expand Up @@ -171,6 +173,7 @@ def command_api(
print_config: bool = True,
resolve: bool = True,
use_flogging: bool = True,
terminal_effect: Callable | None = omegaconf.MISSING,
**flogging_kwargs: Any,
) -> Callable:
"""Integrate Hydra's configuration management capabilities with a Click-based CLI.
Expand All @@ -197,6 +200,8 @@ def command_api(
function. Defaults to `True`.
use_flogging (bool, optional): Whether to use the `flogging` library for structured \
logging. Defaults to `True`.
terminal_effect(Callable | None, optional): The terminal effect function to use when \
rendering the command help.
**flogging_kwargs (Any, optional): Additional keyword arguments to pass to the \
`flogging.setup` function.
Expand Down Expand Up @@ -237,6 +242,10 @@ def my_function(config: DictConfig):
configuration before it is passed to the main function.
"""
if terminal_effect == omegaconf.MISSING:
terminal_effect = display_terminal_effect
if terminal_effect is not None:
set_terminal_effect(terminal_effect)
config_path = get_default_dir() if config_path is None else str(config_path)
if config_name is not None:
config_name = str(config_name).replace(".yaml", "").replace(".yml", "")
Expand Down Expand Up @@ -329,6 +338,7 @@ def hydra_command(
print_config: bool = True,
resolve: bool = True,
use_flogging: bool = True,
terminal_effect: Callable | None = omegaconf.MISSING,
**flogging_kwargs: Any,
) -> Callable:
"""Integrate Hydra's configuration management capabilities with a Click-based CLI.
Expand All @@ -353,6 +363,8 @@ def hydra_command(
the function. Defaults to `True`.
use_flogging (bool, optional): Whether to use the `flogging` library for structured \
logging. Defaults to `True`.
terminal_effect(Callable | None, optional): The terminal effect function to use when \
rendering the command help.
**flogging_kwargs (Any, optional): Additional keyword arguments to pass to the \
`flogging.setup` function.
Expand Down Expand Up @@ -380,6 +392,7 @@ def decorator(function: Callable):
print_config=print_config,
preprocess_config=preprocess_config,
resolve=resolve,
terminal_effect=terminal_effect,
**flogging_kwargs,
)

Expand Down
Loading

0 comments on commit 9370e93

Please sign in to comment.