File size: 1,712 Bytes
71b4454
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
import json
from pathlib import Path
from typing import Any, Dict, Optional

from .models import CMSProject


class CMSManager:
    def __init__(self, root: Optional[str] = None):
        self.root = Path(
            root or Path.cwd() / "storage" / "cms"
        )
        self.root.mkdir(parents=True, exist_ok=True)

    def _path(self, project_id: str) -> Path:
        safe = "".join(
            c if c.isalnum() or c in "-_" else "_"
            for c in project_id
        )
        if not safe:
            raise ValueError("project_id is required")
        return self.root / f"{safe}.json"

    def get(self, project_id: str) -> Optional[Dict[str, Any]]:
        path = self._path(project_id)

        if not path.exists():
            return None

        return json.loads(path.read_text())

    def save(
        self,
        project_id: str,
        data: Dict[str, Any]
    ) -> Dict[str, Any]:
        project = CMSProject.create(
            project_id,
            **data
        )

        path = self._path(project_id)
        temporary = path.with_suffix(".tmp")

        temporary.write_text(
            json.dumps(
                project.to_dict(),
                indent=2,
                ensure_ascii=False
            )
        )

        temporary.replace(path)

        return project.to_dict()

    def delete(self, project_id: str) -> bool:
        path = self._path(project_id)

        if not path.exists():
            return False

        path.unlink()
        return True


_cms_manager: Optional[CMSManager] = None


def get_cms_manager() -> CMSManager:
    global _cms_manager

    if _cms_manager is None:
        _cms_manager = CMSManager()

    return _cms_manager