lib + cmds.projects: Use lib.Uri

Remove the feeble attempts at unifying URI handling, and use class Uri from lib.Uri instead.

Signed-off-by: Jan Lindemann <jan@janware.com>
This commit is contained in:
Jan Lindemann 2026-04-27 09:37:28 +02:00
commit d9746cd20b
Signed by: Jan Lindemann
GPG key ID: 3750640C9E25DD61
8 changed files with 65 additions and 98 deletions

View file

@ -12,6 +12,7 @@ if TYPE_CHECKING:
from .log import *
from .base import Input, InputMode, Result, StatResult
from .Uri import Uri
from .ProcFilter import ProcPipeline
class FileContext(abc.ABC):
@ -22,15 +23,14 @@ class FileContext(abc.ABC):
def __init__(
self,
uri: str,
uri: str|Uri,
interactive: bool|None = None,
verbose_default = False,
chroot: bool = False,
in_pipe: ProcPipeline|None = None,
out_pipe: ProcPipeline|None = None,
):
self.__uri = uri
self.__id, self.__root = self.split_uri(uri)
self.__uri = Uri.pimp(uri)
self.__chroot = chroot
self.__interactive = interactive
self.__verbose_default = verbose_default
@ -48,6 +48,9 @@ class FileContext(abc.ABC):
async def __aexit__(self, exc_type, exc, tb):
await self.close()
def __repr__(self) -> str:
return self.__uri.id
def __pipe(self, d: Direction):
match d:
case self.Direction.In:
@ -65,10 +68,10 @@ class FileContext(abc.ABC):
if not self.__chroot:
return path
if not len(path):
return self.__root
return self.root
if path[-1] == '/':
return self.__root + path
return self.__root + '/' + path
return self.root + path
return self.root + '/' + path
def add_proc_filter(self, d: Direction, proc_filter: ProcFilter):
self.__pipe(d).append(proc_filter)
@ -88,47 +91,27 @@ class FileContext(abc.ABC):
if self.__open_count == 1:
await self._close()
self.__open_count -= 1
assert self.__open_count >= 0, f'Closed file context "{self.__uri}" more often than opened'
@classmethod
def schema_from_uri(cls, uri: str) -> str:
tokens = re.split(r'://', uri)
return tokens[0] if tokens[0] != uri else 'file'
@classmethod
@cache
def split_uri(cls, uri: str) -> tuple[str, str]:
from urllib.parse import urlparse
p = urlparse(uri)
netloc = p.netloc if p.netloc else ''
return f'{cls.schema_from_uri(uri)}://{netloc}', p.path
assert self.__open_count >= 0, f'Closed file context "{self}" more often than opened'
@property
def uri(self) -> str:
def uri(self) -> Uri:
return self.__uri
@property
def id(self) -> str:
return self.__id
return self.__uri.id
@cached_property
def root(self) -> str:
return self.__uri.path
@property
def root(self) -> str:
return self.__root
def username(self) -> str|None:
return self.__uri.username
@property
def log_name(self) -> str:
if self.__log_name is None:
self.__log_name = self.__class__.__name__.lower()
from urllib.parse import urlparse
parsed = urlparse(self.__uri)
uri: list[str] = []
if parsed.scheme:
uri.append(parsed.scheme)
if parsed.hostname:
uri.append(parsed.hostname)
if uri:
self.__log_name += ' ' + '://'.join(uri)
return self.__log_name
return self.__uri.id
@property
def interactive(self) -> bool|None:
@ -295,8 +278,9 @@ class FileContext(abc.ABC):
return await self._is_dir(self._chroot(path), follow_symlinks=follow_symlinks)
@classmethod
def create(cls, uri: str, *args, **kwargs) -> Self:
match cls.schema_from_uri(uri):
def create(cls, uri: str|Uri, *args, **kwargs) -> Self:
uri = Uri.pimp(uri)
match uri.protocol:
case 'local' | 'file':
from .ec.Local import Local
return Local(uri, *args, **kwargs)
@ -308,4 +292,4 @@ class FileContext(abc.ABC):
return Curl(uri, *args, **kwargs)
case _:
pass
raise Exception(f'Can\'t create file transfer instance for {uri} with unknown schema "{cls.schema_from_uri(uri)}"')
raise Exception(f'Can\'t create file context instance for "{uri}" with unsupported protocol "{uri.protocol}"')