Skip to content

duty ¤

duty package.

A simple task runner.

Modules:

  • callables

    Module containing callables for many tools.

  • cli

    Module that contains the command line application.

  • collection

    Module containing all the logic.

  • context

    Module containing the context definition.

  • debug

    Debugging utilities.

  • decorator

    Module containing the decorator provided to users.

  • exceptions

    Module containing the project's exceptions.

  • validation

    This module contains logic used to validate parameters passed to duties.

Functions:

  • duty

    Decorate a callable to transform it and register it as a duty.

duty ¤

duty(**kwargs: Any) -> Callable[[Callable], Duty]
duty(func: Callable) -> Duty
duty(*args: Any, **kwargs: Any) -> Callable | Duty

Decorate a callable to transform it and register it as a duty.

Parameters:

  • args (Any, default: () ) –

    One callable.

  • kwargs (Any, default: {} ) –

    Context options.

Raises:

Examples:

Decorate a function:

@duty
def clean(ctx):
    ctx.run("rm -rf build", silent=True)

Pass options to the context:

@duty(silent=True)
def clean(ctx):
    ctx.run("rm -rf build")  # silent=True is implied

Returns:

  • Callable | Duty

    A duty when used without parentheses, a decorator otherwise.

Source code in src/duty/decorator.py
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
def duty(*args: Any, **kwargs: Any) -> Callable | Duty:
    """Decorate a callable to transform it and register it as a duty.

    Parameters:
        args: One callable.
        kwargs: Context options.

    Raises:
        ValueError: When the decorator is misused.

    Examples:
        Decorate a function:

        ```python
        @duty
        def clean(ctx):
            ctx.run("rm -rf build", silent=True)
        ```

        Pass options to the context:

        ```python
        @duty(silent=True)
        def clean(ctx):
            ctx.run("rm -rf build")  # silent=True is implied
        ```

    Returns:
        A duty when used without parentheses, a decorator otherwise.
    """
    if args:
        if len(args) > 1:
            raise ValueError("The duty decorator accepts only one positional argument")
        return create_duty(args[0], **kwargs)

    def decorator(func: Callable) -> Duty:
        return create_duty(func, **kwargs)

    return decorator