From f2ca32a3431e9c200d303cddeb33bb6b77abd079 Mon Sep 17 00:00:00 2001 From: Jan Lindemann Date: Fri, 24 Apr 2026 13:59:47 +0200 Subject: [PATCH] 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 --- src/python/jw/pkg/cmds/posix/tar/Cmd.py | 2 +- .../jw/pkg/cmds/posix/tar/CmdExtract.py | 4 +--- src/python/jw/pkg/lib/CopyContext.py | 22 ++++++++++++++----- src/python/jw/pkg/lib/TarIo.py | 4 ++-- 4 files changed, 21 insertions(+), 11 deletions(-) diff --git a/src/python/jw/pkg/cmds/posix/tar/Cmd.py b/src/python/jw/pkg/cmds/posix/tar/Cmd.py index 9a51bac1..277906ca 100644 --- a/src/python/jw/pkg/cmds/posix/tar/Cmd.py +++ b/src/python/jw/pkg/cmds/posix/tar/Cmd.py @@ -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 diff --git a/src/python/jw/pkg/cmds/posix/tar/CmdExtract.py b/src/python/jw/pkg/cmds/posix/tar/CmdExtract.py index 3f8628c3..d3c7a1fe 100644 --- a/src/python/jw/pkg/cmds/posix/tar/CmdExtract.py +++ b/src/python/jw/pkg/cmds/posix/tar/CmdExtract.py @@ -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) diff --git a/src/python/jw/pkg/lib/CopyContext.py b/src/python/jw/pkg/lib/CopyContext.py index 6582fb26..57fb60a3 100644 --- a/src/python/jw/pkg/lib/CopyContext.py +++ b/src/python/jw/pkg/lib/CopyContext.py @@ -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 diff --git a/src/python/jw/pkg/lib/TarIo.py b/src/python/jw/pkg/lib/TarIo.py index cd402181..7a553786 100644 --- a/src/python/jw/pkg/lib/TarIo.py +++ b/src/python/jw/pkg/lib/TarIo.py @@ -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