FileContext.open() / close(): Only call wrapped once
FileContext's _open() and _close() are called everytime their wrapper is called, which tasks the caller with keeping track of whether they were already called or not. Be a little easier on the caller, keep track in an open count, and call _open() only once for multiple calls to open(), and close() likewise. The caller still needs to make sure the number of open() and close() calls matches.
Signed-off-by: Jan Lindemann <jan@janware.com>
This commit is contained in:
parent
514d66dac1
commit
772512aee0
1 changed files with 8 additions and 2 deletions
|
|
@ -37,6 +37,7 @@ class FileContext(abc.ABC):
|
|||
self.__log_name: str|None = None
|
||||
self.__in_pipe = in_pipe
|
||||
self.__out_pipe = out_pipe
|
||||
self.__open_count = 0
|
||||
assert verbose_default is not None
|
||||
|
||||
async def __aenter__(self):
|
||||
|
|
@ -75,13 +76,18 @@ class FileContext(abc.ABC):
|
|||
pass
|
||||
|
||||
async def open(self) -> None:
|
||||
self.__open_count += 1
|
||||
if self.__open_count == 1:
|
||||
await self._open()
|
||||
|
||||
async def _close(self) -> None:
|
||||
pass
|
||||
|
||||
async def close(self) -> None:
|
||||
if self.__open_count == 1:
|
||||
await self._close()
|
||||
self.__open_count -= 1
|
||||
assert self.__open_count >= 0
|
||||
|
||||
@classmethod
|
||||
def schema_from_uri(cls, uri: str) -> str:
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue