lib.FileContext.open(): Add method

Add an async open() method which should allow to do what __init__() couldn't, because it's not async, and to match the already existing .close(). It's called by __aenter__() __aexit__() if the FileContext is instantiated as context manager, or at will when the user finds it a good idea.

Signed-off-by: Jan Lindemann <jan@janware.com>
This commit is contained in:
Jan Lindemann 2026-04-22 07:55:44 +02:00
commit 58f7997bc6
Signed by: Jan Lindemann
GPG key ID: 3750640C9E25DD61
3 changed files with 51 additions and 33 deletions

View file

@ -24,6 +24,7 @@ class FileContext(abc.ABC):
assert verbose_default is not None
async def __aenter__(self):
await self.open()
return self
async def __aexit__(self, exc_type, exc, tb):
@ -38,6 +39,18 @@ class FileContext(abc.ABC):
return self.__root + path
return self.__root + '/' + path
async def _open(self) -> None:
pass
async def open(self) -> None:
await self._open()
async def _close(self) -> None:
pass
async def close(self) -> None:
await self._close()
@classmethod
def schema_from_uri(cls, uri: str) -> str:
tokens = re.split(r'://', uri)
@ -225,12 +238,6 @@ class FileContext(abc.ABC):
async def is_dir(self, path: str) -> bool:
return self._is_dir(self._chroot(path))
async def _close(self) -> None:
pass
async def close(self) -> None:
await self._close()
@classmethod
def create(cls, uri: str, *args, **kwargs) -> Self:
match cls.schema_from_uri(uri):