Skip to content

util

Utility functions for CLI commands.

wrap_exceptions

wrap_exceptions(wrapped, instance, args, kwargs)

Format and log exceptions for cli commands.

Source code in src/somesy/cli/util.py
30
31
32
33
34
35
36
37
38
39
40
41
42
43
@wrapt.decorator
def wrap_exceptions(wrapped, instance, args, kwargs):
    """Format and log exceptions for cli commands."""
    try:
        return wrapped(*args, **kwargs)

    except Exception as e:
        # Escape the error message to prevent Rich from misinterpreting it
        escaped_error_message = escape(str(e))
        escaped_traceback = escape(traceback.format_exc())

        logger.error(f"[bold red]Error: {escaped_error_message}[/bold red]")
        logger.debug(f"[red]{escaped_traceback}[/red]")
        raise typer.Exit(code=1) from e

resolved_somesy_input

resolved_somesy_input(**cli_args) -> SomesyInput

Return a combined SomesyInput based on config file and passed CLI args.

Will also adjust log levels accordingly.

Source code in src/somesy/cli/util.py
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
def resolved_somesy_input(**cli_args) -> SomesyInput:
    """Return a combined `SomesyInput` based on config file and passed CLI args.

    Will also adjust log levels accordingly.
    """
    # figure out what input file to use
    input_file = discover_input(cli_args.pop("input_file", None))

    # create config based on passed arguments
    passed_args = {k: v for k, v in cli_args.items() if v is not None}
    somesy_conf = SomesyConfig(input_file=input_file, **passed_args)

    # cli_log_level is None if the user did not pass a log level (-> "default")
    cli_log_level: Optional[SomesyLogLevel] = get_log_level()

    if cli_log_level is not None:
        # update log level flags if cli log level was set
        somesy_conf.update_log_level(cli_log_level)

    somesy_input: SomesyInput = somesy_conf.get_input()

    if cli_log_level is None:
        # no cli log level -> set it according to the loaded configuration
        set_log_level(somesy_input.config.log_level())

    logger.debug(
        f"Combined config (Defaults + File + CLI):\n{pretty_repr(somesy_input.config)}"
    )
    return somesy_input