Skip to content

Python

PythonEnv

Bases: StrEnum

All possible python environments.

Source code in saritasa_invocations/python.py
14
15
16
17
18
class PythonEnv(StrEnum):
    """All possible python environments."""

    DOCKER = "docker"
    LOCAL = "local"

StrEnum

Bases: str, Enum

Support python 3.10.

Source code in saritasa_invocations/python.py
10
11
class StrEnum(str, enum.Enum):
    """Support python 3.10."""

get_python_env()

Get python environment.

Source code in saritasa_invocations/python.py
21
22
23
24
25
26
27
28
29
30
31
32
33
def get_python_env() -> PythonEnv:
    """Get python environment."""
    python_env = os.environ.get("PYTHON_ENV", PythonEnv.LOCAL.value).lower()
    supported_envs = tuple(PythonEnv)
    if python_env not in supported_envs:
        raise invoke.Exit(
            code=1,
            message=(
                "Invalid `PYTHON_ENV` variable, "
                f"expected: {', '.join(PythonEnv)}, got {python_env}!"
            ),
        )
    return PythonEnv(python_env)

run(context, command, docker_params=None, watchers=(), env=None, **kwargs)

Execute python command.

Source code in saritasa_invocations/python.py
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
@invoke.task
def run(
    context: invoke.Context,
    command: str,
    docker_params: str | None = None,
    watchers: collections.abc.Sequence[invoke.StreamWatcher] = (),
    env: dict[str, str] | None = None,
    **kwargs,
) -> invoke.runners.Result | None:
    """Execute python command."""
    match get_python_env():
        case PythonEnv.LOCAL:
            return run_local_python(
                context=context,
                command=command,
                watchers=watchers,
                env=env,
                **kwargs,
            )
        case PythonEnv.DOCKER:
            return run_docker_python(
                context=context,
                command=command,
                params=docker_params,
                watchers=watchers,
                env=env,
                **kwargs,
            )

run_docker(context, command, params=None, watchers=(), env=None, **kwargs)

Run command in python container.

Source code in saritasa_invocations/python.py
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
def run_docker(
    context: invoke.Context,
    command: str,
    params: str | None = None,
    watchers: collections.abc.Sequence[invoke.StreamWatcher] = (),
    env: dict[str, str] | None = None,
    **kwargs,
) -> invoke.runners.Result | None:
    """Run command in `python` container."""
    config = _config.Config.from_context(context)
    if params is None:
        params = config.python.docker_service_params
    return docker.docker_compose_run(
        context,
        params=params,
        container=config.python.docker_service,
        command=command,
        watchers=watchers,
        env=env,
        **kwargs,
    )

run_docker_python(context, command, params=None, watchers=(), env=None, **kwargs)

Run command using docker python interpreter.

Source code in saritasa_invocations/python.py
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
def run_docker_python(
    context: invoke.Context,
    command: str,
    params: str | None = None,
    watchers: collections.abc.Sequence[invoke.StreamWatcher] = (),
    env: dict[str, str] | None = None,
    **kwargs,
) -> invoke.runners.Result | None:
    """Run command using docker python interpreter."""
    config = _config.Config.from_context(context)
    return run_docker(
        context=context,
        params=params,
        command=f"{config.python.entry} {command}",
        watchers=watchers,
        env=env,
        **kwargs,
    )

run_local_python(context, command, watchers=(), env=None, **kwargs)

Run command using local python interpreter.

Source code in saritasa_invocations/python.py
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
def run_local_python(
    context: invoke.Context,
    command: str,
    watchers: collections.abc.Sequence[invoke.StreamWatcher] = (),
    env: dict[str, str] | None = None,
    **kwargs,
) -> invoke.runners.Result | None:
    """Run command using local python interpreter."""
    config = _config.Config.from_context(context)
    return context.run(
        command=f"{config.python.entry} {command}",
        watchers=watchers,
        env=env,
        **kwargs,
    )