lib.CopyContext: Add module

Add a CopyContext class. At this point it mostly acts as a context
manager for two FileContext instances, and copying data is the
canonical case to use it, hence the name.

Signed-off-by: Jan Lindemann <jan@janware.com>
This commit is contained in:
Jan Lindemann 2026-04-22 13:09:35 +02:00
commit d82bc20663
Signed by: jan
GPG key ID: 3750640C9E25DD61

View file

@ -0,0 +1,41 @@
# -*- coding: utf-8 -*-
from typing import Self
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
self.__chroot = chroot
async def __aenter__(self) -> Self:
self.__src = FileContext.create(self.__src_uri, chroot=self.__chroot)
await self.__src.open()
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()