2023-10-27 21:12:28 +00:00
|
|
|
import logging
|
2023-09-08 02:45:00 +00:00
|
|
|
import re
|
2023-09-08 16:19:26 +00:00
|
|
|
from io import BytesIO
|
2023-09-08 02:45:00 +00:00
|
|
|
|
2023-10-27 21:12:28 +00:00
|
|
|
from asyncify import asyncify
|
2023-10-29 16:08:16 +00:00
|
|
|
from cachetools import cachedmethod
|
|
|
|
from redis import Redis
|
2023-09-08 02:45:00 +00:00
|
|
|
|
2023-10-29 16:08:16 +00:00
|
|
|
from ..settings import SETTINGS
|
|
|
|
from .helpers import RedisCache, WebDAVclient, davkey
|
2023-09-08 02:45:00 +00:00
|
|
|
|
2023-10-27 21:12:28 +00:00
|
|
|
_logger = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
|
2023-09-08 02:45:00 +00:00
|
|
|
class WebDAV:
|
2023-10-29 16:08:16 +00:00
|
|
|
_webdav_client = WebDAVclient(
|
2023-09-08 02:45:00 +00:00
|
|
|
{
|
|
|
|
"webdav_hostname": SETTINGS.webdav.url,
|
|
|
|
"webdav_login": SETTINGS.webdav.username,
|
|
|
|
"webdav_password": SETTINGS.webdav.password,
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
2023-10-29 16:08:16 +00:00
|
|
|
_cache = RedisCache(
|
|
|
|
cache=Redis(
|
|
|
|
host=SETTINGS.redis.host,
|
|
|
|
port=SETTINGS.redis.port,
|
|
|
|
db=SETTINGS.redis.db,
|
|
|
|
protocol=SETTINGS.redis.protocol,
|
|
|
|
),
|
2023-10-27 21:12:28 +00:00
|
|
|
ttl=SETTINGS.webdav.cache_ttl,
|
|
|
|
)
|
|
|
|
|
2023-09-08 02:45:00 +00:00
|
|
|
@classmethod
|
2023-10-27 21:12:28 +00:00
|
|
|
@asyncify
|
2023-11-09 11:55:10 +00:00
|
|
|
@cachedmethod(cache=lambda cls: cls._cache, key=davkey("list_files"))
|
2023-10-27 21:12:28 +00:00
|
|
|
def list_files(
|
2023-09-08 02:45:00 +00:00
|
|
|
cls,
|
|
|
|
directory: str = "",
|
2023-09-08 18:17:18 +00:00
|
|
|
*,
|
2023-09-08 02:45:00 +00:00
|
|
|
regex: re.Pattern[str] = re.compile(""),
|
|
|
|
) -> list[str]:
|
|
|
|
"""
|
2023-10-27 21:12:28 +00:00
|
|
|
List files in directory `directory` matching RegEx `regex`
|
2023-09-08 02:45:00 +00:00
|
|
|
"""
|
|
|
|
|
2023-10-27 21:12:28 +00:00
|
|
|
_logger.debug(f"list_files {directory!r}")
|
|
|
|
ls = cls._webdav_client.list(directory)
|
2023-09-08 02:45:00 +00:00
|
|
|
|
2023-10-27 21:12:28 +00:00
|
|
|
return [path for path in ls if regex.search(path)]
|
2023-09-08 02:45:00 +00:00
|
|
|
|
|
|
|
@classmethod
|
2023-10-27 21:12:28 +00:00
|
|
|
@asyncify
|
2023-11-09 11:55:10 +00:00
|
|
|
@cachedmethod(cache=lambda cls: cls._cache, key=davkey("exists"))
|
2023-10-27 21:12:28 +00:00
|
|
|
def exists(cls, path: str) -> bool:
|
2023-09-08 02:45:00 +00:00
|
|
|
"""
|
2023-10-27 21:12:28 +00:00
|
|
|
`True` iff there is a WebDAV resource at `path`
|
2023-09-08 02:45:00 +00:00
|
|
|
"""
|
|
|
|
|
2023-10-27 21:12:28 +00:00
|
|
|
_logger.debug(f"file_exists {path!r}")
|
|
|
|
return cls._webdav_client.check(path)
|
2023-09-08 02:45:00 +00:00
|
|
|
|
|
|
|
@classmethod
|
2023-10-27 21:12:28 +00:00
|
|
|
@asyncify
|
2023-11-09 11:55:10 +00:00
|
|
|
@cachedmethod(cache=lambda cls: cls._cache, key=davkey("read_bytes"))
|
2023-10-27 21:12:28 +00:00
|
|
|
def read_bytes(cls, path: str) -> bytes:
|
2023-09-08 02:45:00 +00:00
|
|
|
"""
|
2023-10-27 21:12:28 +00:00
|
|
|
Load WebDAV file from `path` as bytes
|
2023-09-08 02:45:00 +00:00
|
|
|
"""
|
|
|
|
|
2023-10-27 21:12:28 +00:00
|
|
|
_logger.debug(f"read_bytes {path!r}")
|
2023-09-08 16:19:26 +00:00
|
|
|
buffer = BytesIO()
|
2023-10-27 21:12:28 +00:00
|
|
|
cls._webdav_client.download_from(buffer, path)
|
2023-09-08 19:08:13 +00:00
|
|
|
buffer.seek(0)
|
|
|
|
|
2023-09-08 16:19:26 +00:00
|
|
|
return buffer.read()
|
2023-09-08 02:45:00 +00:00
|
|
|
|
|
|
|
@classmethod
|
2023-09-08 16:19:26 +00:00
|
|
|
async def read_str(cls, path: str, encoding="utf-8") -> str:
|
2023-09-08 02:45:00 +00:00
|
|
|
"""
|
2023-10-27 21:12:28 +00:00
|
|
|
Load WebDAV file from `path` as string
|
2023-09-08 02:45:00 +00:00
|
|
|
"""
|
|
|
|
|
2023-10-27 21:12:28 +00:00
|
|
|
_logger.debug(f"read_str {path!r}")
|
2023-09-08 16:19:26 +00:00
|
|
|
return (await cls.read_bytes(path)).decode(encoding=encoding).strip()
|
2023-09-08 02:45:00 +00:00
|
|
|
|
|
|
|
@classmethod
|
2023-10-27 21:12:28 +00:00
|
|
|
@asyncify
|
|
|
|
def write_bytes(cls, path: str, buffer: bytes) -> None:
|
2023-09-08 02:45:00 +00:00
|
|
|
"""
|
2023-10-27 21:12:28 +00:00
|
|
|
Write bytes from `buffer` into WebDAV file at `path`
|
2023-09-08 02:45:00 +00:00
|
|
|
"""
|
|
|
|
|
2023-10-27 21:12:28 +00:00
|
|
|
_logger.debug(f"write_bytes {path!r}")
|
|
|
|
cls._webdav_client.upload_to(buffer, path)
|
2023-09-11 03:12:24 +00:00
|
|
|
|
2023-10-27 21:12:28 +00:00
|
|
|
# invalidate cache entry
|
2023-11-21 23:10:37 +00:00
|
|
|
# explicit slice as there is no "cls" argument
|
|
|
|
del cls._cache[davkey("read_bytes", slice(0, None))(path)]
|
2023-09-11 02:59:11 +00:00
|
|
|
|
2023-09-08 02:45:00 +00:00
|
|
|
@classmethod
|
2023-09-08 16:19:26 +00:00
|
|
|
async def write_str(cls, path: str, content: str, encoding="utf-8") -> None:
|
2023-09-08 02:45:00 +00:00
|
|
|
"""
|
2023-10-27 21:12:28 +00:00
|
|
|
Write string from `content` into WebDAV file at `path`
|
2023-09-08 02:45:00 +00:00
|
|
|
"""
|
|
|
|
|
2023-10-27 21:12:28 +00:00
|
|
|
_logger.debug(f"write_str {path!r}")
|
2023-09-08 19:08:13 +00:00
|
|
|
await cls.write_bytes(path, content.encode(encoding=encoding))
|