jw-pkg/src/python/jw/pkg/lib/CopyContext.py

53 lines
1.5 KiB
Python
Raw Normal View History

# -*- coding: utf-8 -*-
from typing import Self
from .FileContext import FileContext
class CopyContext:
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:
if self.__src is None:
self.__src = FileContext.create(self.__src_uri, chroot=self.__chroot)
await self.__src.open()
if self.__dst is None:
self.__dst = FileContext.create(self.__dst_uri, chroot=self.__chroot)
await self.__dst.open()
return self
async def __aexit__(self, exc_type, exc, tb):
if self.__src is not None:
await self.__src.close()
self.__src = None
if self.__dst is not None:
await self.__dst.close()
self.__dst = None
@property
def src(self) -> FileContext:
return self.__src
@property
def dst(self) -> FileContext:
return self.__dst
async def _run(self) -> None:
await self._run()
async def run(self) -> None:
await self._run()