Upload files from skel directory

This commit is contained in:
Jörn-Michael Miehe 2022-09-19 11:42:43 +00:00
parent 5867b1f3c4
commit abedef1583
5 changed files with 72 additions and 8 deletions

View file

@ -4,6 +4,8 @@ Definition of WebDAV and CalDAV clients.
from functools import lru_cache from functools import lru_cache
from logging import getLogger from logging import getLogger
from os import path
from pathlib import Path
from time import sleep from time import sleep
from typing import Any, Iterator from typing import Any, Iterator
@ -12,6 +14,7 @@ from caldav import Principal as CalDAVPrincipal
from webdav3.client import Client as WebDAVclient from webdav3.client import Client as WebDAVclient
from webdav3.client import Resource as WebDAVResource from webdav3.client import Resource as WebDAVResource
from . import __file__ as OVD_INIT
from .async_helpers import run_in_executor from .async_helpers import run_in_executor
from .settings import SETTINGS from .settings import SETTINGS
@ -67,7 +70,7 @@ def webdav_check() -> None:
_logger.debug("WebDAV prefix directory found.") _logger.debug("WebDAV prefix directory found.")
def webdav_ensure_path(remote_path: str) -> None: def webdav_ensure_path(remote_path: str) -> bool:
remote_path = f"{SETTINGS.webdav_prefix}/{remote_path}" remote_path = f"{SETTINGS.webdav_prefix}/{remote_path}"
if _WEBDAV_CLIENT.check(remote_path): if _WEBDAV_CLIENT.check(remote_path):
@ -75,7 +78,7 @@ def webdav_ensure_path(remote_path: str) -> None:
"WebDAV path %s found.", "WebDAV path %s found.",
repr(remote_path), repr(remote_path),
) )
return return True
_logger.info( _logger.info(
"WebDAV path %s not found, creating ...", "WebDAV path %s not found, creating ...",
@ -83,6 +86,45 @@ def webdav_ensure_path(remote_path: str) -> None:
) )
_WEBDAV_CLIENT.mkdir(remote_path) _WEBDAV_CLIENT.mkdir(remote_path)
return False
def get_skel_path(skel_file: str) -> Path:
skel_path = path.dirname(Path(OVD_INIT).absolute())
return Path(skel_path).joinpath("skel", skel_file)
def webdav_upload_skel(remote_path: str, *skel_files: str) -> None:
remote_path = f"{SETTINGS.webdav_prefix}/{remote_path}"
for skel_file in skel_files:
_logger.debug(
"Creating WebDAV file %s ...",
repr(skel_file),
)
_WEBDAV_CLIENT.upload_file(
f"{remote_path}/{skel_file}",
get_skel_path(skel_file),
)
def webdav_ensure_files(remote_path: str, *file_names: str) -> None:
missing_files = (
file_name
for file_name in file_names
if not _WEBDAV_CLIENT.check(path.join(
SETTINGS.webdav_prefix,
remote_path,
file_name,
))
)
webdav_upload_skel(
remote_path,
*missing_files,
)
@lru_cache(maxsize=SETTINGS.cache_size) @lru_cache(maxsize=SETTINGS.cache_size)
def webdav_resource(remote_path: Any) -> WebDAVResource: def webdav_resource(remote_path: Any) -> WebDAVResource:

View file

@ -15,7 +15,7 @@ from fastapi import APIRouter, Depends
from fastapi.responses import StreamingResponse from fastapi.responses import StreamingResponse
from magic import Magic from magic import Magic
from ...dav_common import webdav_ensure_path from ...dav_common import webdav_ensure_files, webdav_ensure_path
from ...dav_file import DavFile from ...dav_file import DavFile
from ._common import FileNameLister, PrefixFinder, PrefixUnique from ._common import FileNameLister, PrefixFinder, PrefixUnique
@ -40,7 +40,11 @@ file_unique = PrefixUnique(file_finder)
async def start_router() -> None: async def start_router() -> None:
_logger.debug(f"{router.prefix} router starting.") _logger.debug(f"{router.prefix} router starting.")
webdav_ensure_path(await file_lister.remote_path) if not webdav_ensure_path(await file_lister.remote_path):
webdav_ensure_files(
await file_lister.remote_path,
"logo.svg",
)
@router.get( @router.get(

View file

@ -16,7 +16,7 @@ from fastapi.responses import StreamingResponse
from PIL import Image from PIL import Image
from ...config import Config, ImageUIConfig from ...config import Config, ImageUIConfig
from ...dav_common import webdav_ensure_path from ...dav_common import webdav_ensure_files, webdav_ensure_path
from ...dav_file import DavFile from ...dav_file import DavFile
from ._common import FileNameLister, PrefixFinder, PrefixUnique from ._common import FileNameLister, PrefixFinder, PrefixUnique
@ -40,7 +40,13 @@ image_unique = PrefixUnique(image_finder)
async def start_router() -> None: async def start_router() -> None:
_logger.debug(f"{router.prefix} router starting.") _logger.debug(f"{router.prefix} router starting.")
webdav_ensure_path(await image_lister.remote_path) if not webdav_ensure_path(await image_lister.remote_path):
webdav_ensure_files(
await image_lister.remote_path,
"img1.jpg",
"img2.jpg",
"img3.jpg",
)
@router.get( @router.get(

View file

@ -14,7 +14,7 @@ from typing import Iterator
from fastapi import APIRouter, Depends from fastapi import APIRouter, Depends
from markdown import markdown from markdown import markdown
from ...dav_common import webdav_ensure_path from ...dav_common import webdav_ensure_files, webdav_ensure_path
from ...dav_file import DavFile from ...dav_file import DavFile
from ._common import FileNameLister, PrefixFinder, PrefixUnique from ._common import FileNameLister, PrefixFinder, PrefixUnique
@ -40,6 +40,13 @@ async def start_router() -> None:
webdav_ensure_path(await text_lister.remote_path) webdav_ensure_path(await text_lister.remote_path)
webdav_ensure_files(
await text_lister.remote_path,
"message.txt",
"title.txt",
"ticker.txt",
)
@router.get( @router.get(
"/list", "/list",

View file

@ -13,7 +13,7 @@ from fastapi import APIRouter, Depends
from markdown import markdown from markdown import markdown
from ...config import Config, TickerUIConfig from ...config import Config, TickerUIConfig
from ...dav_common import webdav_ensure_path from ...dav_common import webdav_ensure_files, webdav_ensure_path
from ...dav_file import DavFile from ...dav_file import DavFile
from .text import text_lister, text_unique from .text import text_lister, text_unique
@ -28,6 +28,11 @@ async def start_router() -> None:
webdav_ensure_path(await text_lister.remote_path) webdav_ensure_path(await text_lister.remote_path)
webdav_ensure_files(
await text_lister.remote_path,
"ticker.txt",
)
async def get_ticker_lines() -> Iterator[str]: async def get_ticker_lines() -> Iterator[str]:
cfg = await Config.get() cfg = await Config.get()