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:
parent
772512aee0
commit
f2ca32a343
4 changed files with 21 additions and 11 deletions
|
|
@ -18,7 +18,7 @@ class Cmd(Base): # export
|
||||||
|
|
||||||
@asynccontextmanager
|
@asynccontextmanager
|
||||||
async def ctx(self, **kwargs) -> TarIo:
|
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))
|
ret.src.add_proc_filter(FileContext.Direction.In, ProcFilterGpg(ec=self.app.exec_context))
|
||||||
yield ret
|
yield ret
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -21,7 +21,5 @@ class CmdExtract(Cmd): # export
|
||||||
parser.add_argument('dst', help='Destination root URI')
|
parser.add_argument('dst', help='Destination root URI')
|
||||||
|
|
||||||
async def _run(self, args: Namespace) -> None:
|
async def _run(self, args: Namespace) -> None:
|
||||||
async with self.ctx(
|
async with self.ctx(dst=args.dst) as ctx:
|
||||||
dst_uri = args.dst,
|
|
||||||
) as ctx:
|
|
||||||
await ctx.extract(ctx.dst.root)
|
await ctx.extract(ctx.dst.root)
|
||||||
|
|
|
||||||
|
|
@ -6,14 +6,26 @@ from .FileContext import FileContext
|
||||||
|
|
||||||
class CopyContext:
|
class CopyContext:
|
||||||
|
|
||||||
def __init__(self, src_uri: str, dst_uri: str, chroot=False) -> None:
|
def __init__(self, src: str|FileContext, dst: str|FileContext, chroot=False) -> None:
|
||||||
self.__src_uri = src_uri
|
if isinstance(src, FileContext):
|
||||||
self.__dst_uri = dst_uri
|
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
|
self.__chroot = chroot
|
||||||
|
|
||||||
async def __aenter__(self) -> Self:
|
async def __aenter__(self) -> Self:
|
||||||
|
if self.__src is None:
|
||||||
self.__src = FileContext.create(self.__src_uri, chroot=self.__chroot)
|
self.__src = FileContext.create(self.__src_uri, chroot=self.__chroot)
|
||||||
await self.__src.open()
|
await self.__src.open()
|
||||||
|
if self.__dst is None:
|
||||||
self.__dst = FileContext.create(self.__dst_uri, chroot=self.__chroot)
|
self.__dst = FileContext.create(self.__dst_uri, chroot=self.__chroot)
|
||||||
await self.__dst.open()
|
await self.__dst.open()
|
||||||
return self
|
return self
|
||||||
|
|
|
||||||
|
|
@ -14,8 +14,8 @@ from .log import *
|
||||||
|
|
||||||
class TarIo(CopyContext):
|
class TarIo(CopyContext):
|
||||||
|
|
||||||
def __init__(self, src_uri: str, dst_uri: str) -> None:
|
def __init__(self, *args, **kwargs) -> None:
|
||||||
super().__init__(src_uri=src_uri, dst_uri=dst_uri, chroot=False)
|
super().__init__(*args, **kwargs, chroot=False)
|
||||||
|
|
||||||
def _match(self, path: str, path_filter: list[str]) -> bool:
|
def _match(self, path: str, path_filter: list[str]) -> bool:
|
||||||
return path in path_filter
|
return path in path_filter
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue