gallery.accords-library.com/server/szurubooru/func/files.py

44 lines
1.0 KiB
Python
Raw Permalink Normal View History

2016-04-09 19:41:10 +00:00
import os
from typing import Any, List, Optional
2016-04-09 19:41:10 +00:00
from szurubooru import config
def _get_full_path(path: str) -> str:
return os.path.join(config.config["data_dir"], path)
2016-04-30 21:17:08 +00:00
def delete(path: str) -> None:
2016-04-30 21:17:08 +00:00
full_path = _get_full_path(path)
if os.path.exists(full_path):
os.unlink(full_path)
def has(path: str) -> bool:
return os.path.exists(_get_full_path(path))
def scan(path: str) -> List[Any]:
2017-01-07 10:59:43 +00:00
if has(path):
return list(os.scandir(_get_full_path(path)))
2017-01-07 10:59:43 +00:00
return []
def move(source_path: str, target_path: str) -> None:
os.rename(_get_full_path(source_path), _get_full_path(target_path))
def get(path: str) -> Optional[bytes]:
2016-04-30 21:17:08 +00:00
full_path = _get_full_path(path)
if not os.path.exists(full_path):
return None
with open(full_path, "rb") as handle:
2016-04-30 21:17:08 +00:00
return handle.read()
def save(path: str, content: bytes) -> None:
2016-04-30 21:17:08 +00:00
full_path = _get_full_path(path)
2016-04-09 19:41:10 +00:00
os.makedirs(os.path.dirname(full_path), exist_ok=True)
with open(full_path, "wb") as handle:
2016-04-09 19:41:10 +00:00
handle.write(content)