rr- ad842ee8a5 server: refactor + add type hinting
- Added type hinting (for now, 3.5-compatible)
- Split `db` namespace into `db` module and `model` namespace
- Changed elastic search to be created lazily for each operation
- Changed to class based approach in entity serialization to allow
  stronger typing
- Removed `required` argument from `context.get_*` family of functions;
  now it's implied if `default` argument is omitted
- Changed `unalias_dict` implementation to use less magic inputs
2017-02-05 16:34:45 +01:00

43 lines
1.0 KiB
Python

from typing import Any, Optional, List
import os
from szurubooru import config
def _get_full_path(path: str) -> str:
return os.path.join(config.config['data_dir'], path)
def delete(path: str) -> None:
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[os.DirEntry]:
if has(path):
return list(os.scandir(_get_full_path(path)))
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]:
full_path = _get_full_path(path)
if not os.path.exists(full_path):
return None
with open(full_path, 'rb') as handle:
return handle.read()
def save(path: str, content: bytes) -> None:
full_path = _get_full_path(path)
os.makedirs(os.path.dirname(full_path), exist_ok=True)
with open(full_path, 'wb') as handle:
handle.write(content)