apply experiments to WebDAV helper
This commit is contained in:
parent
e894aad746
commit
d51db8b836
2 changed files with 11 additions and 82 deletions
|
@ -1,7 +1,5 @@
|
||||||
import re
|
import re
|
||||||
from contextlib import asynccontextmanager
|
from io import BytesIO
|
||||||
from io import BytesIO, TextIOWrapper
|
|
||||||
from typing import AsyncContextManager, AsyncIterator
|
|
||||||
|
|
||||||
from cache import AsyncTTL
|
from cache import AsyncTTL
|
||||||
from webdav3.client import Client as WebDAVclient
|
from webdav3.client import Client as WebDAVclient
|
||||||
|
@ -44,38 +42,25 @@ class WebDAV:
|
||||||
|
|
||||||
return cls._webdav_client.check(path)
|
return cls._webdav_client.check(path)
|
||||||
|
|
||||||
|
@AsyncTTL(time_to_live=SETTINGS.cache_ttl)
|
||||||
@classmethod
|
@classmethod
|
||||||
async def read_buffer(cls, path: str) -> AsyncContextManager[BytesIO]:
|
async def read_bytes(cls, path: str) -> bytes:
|
||||||
"""
|
"""
|
||||||
Datei aus Pfad `path` in einen `BytesIO` Puffer laden
|
Datei aus Pfad `path` als bytes laden
|
||||||
"""
|
"""
|
||||||
|
|
||||||
@AsyncTTL(time_to_live=SETTINGS.cache_ttl)
|
buffer = BytesIO()
|
||||||
async def inner() -> BytesIO:
|
cls._webdav_client.resource(path).write_to(buffer)
|
||||||
buffer = BytesIO()
|
return buffer.read()
|
||||||
cls._webdav_client.resource(path).write_to(buffer)
|
|
||||||
return buffer
|
|
||||||
|
|
||||||
@asynccontextmanager
|
|
||||||
async def ctx() -> AsyncIterator[BytesIO]:
|
|
||||||
buffer = await inner()
|
|
||||||
buffer.seek(0)
|
|
||||||
yield buffer
|
|
||||||
|
|
||||||
return ctx()
|
|
||||||
|
|
||||||
@AsyncTTL(time_to_live=SETTINGS.cache_ttl)
|
@AsyncTTL(time_to_live=SETTINGS.cache_ttl)
|
||||||
@classmethod
|
@classmethod
|
||||||
async def read_text(cls, path: str, encoding="utf-8") -> str:
|
async def read_str(cls, path: str, encoding="utf-8") -> str:
|
||||||
"""
|
"""
|
||||||
Datei aus Pfad `path` als string zurückgeben
|
Datei aus Pfad `path` als string laden
|
||||||
"""
|
"""
|
||||||
|
|
||||||
async with await cls.read_buffer(path) as buffer:
|
return (await cls.read_bytes(path)).decode(encoding=encoding).strip()
|
||||||
tio = TextIOWrapper(buffer, encoding=encoding)
|
|
||||||
tio.seek(0)
|
|
||||||
|
|
||||||
return tio.read().strip()
|
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
async def write_buffer(cls, path: str, buffer: BytesIO) -> None:
|
async def write_buffer(cls, path: str, buffer: BytesIO) -> None:
|
||||||
|
@ -86,7 +71,7 @@ class WebDAV:
|
||||||
cls._webdav_client.resource(path).read_from(buffer)
|
cls._webdav_client.resource(path).read_from(buffer)
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
async def write_text(cls, path: str, content: str, encoding="utf-8") -> None:
|
async def write_str(cls, path: str, content: str, encoding="utf-8") -> None:
|
||||||
"""
|
"""
|
||||||
String `content` in Datei in Pfad `path` schreiben
|
String `content` in Datei in Pfad `path` schreiben
|
||||||
"""
|
"""
|
||||||
|
|
|
@ -1,56 +0,0 @@
|
||||||
import asyncio
|
|
||||||
import functools
|
|
||||||
from io import BytesIO
|
|
||||||
|
|
||||||
from cache import AsyncLRU
|
|
||||||
|
|
||||||
FILES = __file__, "pyproject.toml"
|
|
||||||
REPS = 3
|
|
||||||
|
|
||||||
#
|
|
||||||
# sync impl
|
|
||||||
#
|
|
||||||
|
|
||||||
|
|
||||||
@functools.lru_cache
|
|
||||||
def get_bytes_sync(fn) -> bytes:
|
|
||||||
print("sync open")
|
|
||||||
with open(fn, "rb") as file:
|
|
||||||
return file.readline()
|
|
||||||
|
|
||||||
|
|
||||||
def get_buf_sync(fn) -> BytesIO:
|
|
||||||
return BytesIO(get_bytes_sync(fn))
|
|
||||||
|
|
||||||
|
|
||||||
def main_sync() -> None:
|
|
||||||
for fn in FILES:
|
|
||||||
for _ in range(REPS):
|
|
||||||
print(get_buf_sync(fn).read())
|
|
||||||
|
|
||||||
|
|
||||||
#
|
|
||||||
# async impl
|
|
||||||
#
|
|
||||||
|
|
||||||
|
|
||||||
@AsyncLRU()
|
|
||||||
async def get_bytes_async(fn) -> bytes:
|
|
||||||
print("async open")
|
|
||||||
with open(fn, "rb") as file:
|
|
||||||
return file.readline()
|
|
||||||
|
|
||||||
|
|
||||||
async def get_buf_async(fn) -> BytesIO:
|
|
||||||
return BytesIO(await get_bytes_async(fn))
|
|
||||||
|
|
||||||
|
|
||||||
async def main_async() -> None:
|
|
||||||
for fn in FILES:
|
|
||||||
for _ in range(REPS):
|
|
||||||
print((await get_buf_async(fn)).read())
|
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
|
||||||
main_sync()
|
|
||||||
asyncio.run(main_async())
|
|
Loading…
Reference in a new issue