CopyContext: Replace src_uri, dst_uri by src, dst

Allow to pass ready-made FileContext objects to CopyContext's destructor so it doesn't need to instantiate them itself.

Signed-off-by: Jan Lindemann <jan@janware.com>
This commit is contained in:
Jan Lindemann 2026-04-24 13:59:47 +02:00
commit f2ca32a343
Signed by: Jan Lindemann
GPG key ID: 3750640C9E25DD61
4 changed files with 21 additions and 11 deletions

View file

@ -18,7 +18,7 @@ class Cmd(Base): # export
@asynccontextmanager
async def ctx(self, **kwargs) -> TarIo:
async with TarIo.create(src_uri=self.app.args.archive_path, **kwargs) as ret:
async with TarIo.create(src=self.app.args.archive_path, **kwargs) as ret:
ret.src.add_proc_filter(FileContext.Direction.In, ProcFilterGpg(ec=self.app.exec_context))
yield ret

View file

@ -21,7 +21,5 @@ class CmdExtract(Cmd): # export
parser.add_argument('dst', help='Destination root URI')
async def _run(self, args: Namespace) -> None:
async with self.ctx(
dst_uri = args.dst,
) as ctx:
async with self.ctx(dst=args.dst) as ctx:
await ctx.extract(ctx.dst.root)

View file

@ -6,15 +6,27 @@ from .FileContext import FileContext
class CopyContext:
def __init__(self, src_uri: str, dst_uri: str, chroot=False) -> None:
self.__src_uri = src_uri
self.__dst_uri = dst_uri
def __init__(self, src: str|FileContext, dst: str|FileContext, chroot=False) -> None:
if isinstance(src, FileContext):
self.__src = src
self.__src_uri = src.uri
else:
self.__src: FileContext|None = None
self.__src_uri = src
if isinstance(dst, FileContext):
self.__dst = dst
self.__dst_uri = dst.uri
else:
self.__dst: FileContext|None = None
self.__dst_uri = dst
self.__chroot = chroot
async def __aenter__(self) -> Self:
self.__src = FileContext.create(self.__src_uri, chroot=self.__chroot)
if self.__src is None:
self.__src = FileContext.create(self.__src_uri, chroot=self.__chroot)
await self.__src.open()
self.__dst = FileContext.create(self.__dst_uri, chroot=self.__chroot)
if self.__dst is None:
self.__dst = FileContext.create(self.__dst_uri, chroot=self.__chroot)
await self.__dst.open()
return self

View file

@ -14,8 +14,8 @@ from .log import *
class TarIo(CopyContext):
def __init__(self, src_uri: str, dst_uri: str) -> None:
super().__init__(src_uri=src_uri, dst_uri=dst_uri, chroot=False)
def __init__(self, *args, **kwargs) -> None:
super().__init__(*args, **kwargs, chroot=False)
def _match(self, path: str, path_filter: list[str]) -> bool:
return path in path_filter