lib.ExecContext.create(): Add method

Add a class method to instantiate an ExecContext by its URI.

Signed-off-by: Jan Lindemann <jan@janware.com>
This commit is contained in:
Jan Lindemann 2026-03-18 07:09:36 +01:00
commit 76b702f5b4

View file

@ -1,7 +1,12 @@
# -*- coding: utf-8 -*-
import abc
from typing import NamedTuple
from __future__ import annotations
import abc, re
from typing import NamedTuple, TYPE_CHECKING
if TYPE_CHECKING:
from typing import Self
class Result(NamedTuple):
@ -93,3 +98,18 @@ class ExecContext(abc.ABC):
async def sudo(self, cmd: list[str], mod_env: dict[str, str] = {}, opts: list[str]=[], verbose: bool|None=None) -> Result:
return await self._sudo(cmd, mod_env, opts, self._verbose(verbose))
@classmethod
def create(cls, uri: str, *args, **kwargs) -> Self:
tokens = re.split(r'://', uri)
schema = tokens[0]
match schema:
case 'local':
from .ec.Local import Local
return Local(uri, *args, **kwargs)
case 'ssh':
from .SSHClient import ssh_client
return ssh_client(uri, *args, **kwargs)
case _:
pass
raise Exception(f'Can\'t create execution context for {uri} with unknown schema "{schema}"')