-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstorage.py
More file actions
123 lines (92 loc) · 3.52 KB
/
storage.py
File metadata and controls
123 lines (92 loc) · 3.52 KB
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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
import contextlib
import pickle
import os
from pathlib import Path
import sys
from typing import Any, TypedDict
class ApplicationClass(TypedDict):
name: str
platform: str
def AppConfig(app_class: ApplicationClass):
"""allows you to have config directory for your application"""
_config_path = Path(f"~/.config/{app_class['name']}").expanduser()
if not _config_path.exists():
_config_path.mkdir()
return _config_path
class EnvironmentStorage:
"""allows you to set environment variables and files"""
def __init__(self, app_class: ApplicationClass) -> None:
self.app_class = app_class
def create_file(self, path: str, data: str):
self[path] = {"path": path, "data": data}
def get_file(self, path: str):
file = self[path]
assert not (file), f"file {path!r} not found!"
return file["data"] # type: ignore
def __setitem__(self, key, value):
os.environ[key] = value
def __getitem__(self, key):
return os.environ.get(key, None)
def CacheStorage(app_class: ApplicationClass) -> Path:
"""returns the path the application cache directory"""
match app_class["platform"]:
case "win32":
first = Path("C:/ProgramData/").expanduser()
second = first / app_class["name"]
second.mkdir(exist_ok=True)
return second
case "darwin":
first = Path("/Library/Caches").expanduser()
second = first / app_class["name"]
second.mkdir(exist_ok=True)
return second
case "linux":
first = Path("/var/cache").expanduser()
second = first / app_class["name"]
second.mkdir(exist_ok=True)
return second
raise OSError(f"un-supported operating system")
class RuntimeStorage:
def __init__(self, app_class) -> None:
self.app_class = app_class
self.rt_path = RuntimeStorage.get_runtime_path(app_class)
self._runtime_storage: dict[str, Any] = {}
@classmethod
def get_runtime_path(cls, Class: ApplicationClass):
match Class["platform"]:
case "win32":
first = Path("~/AppData/Local").expanduser()
second = first / Class["name"]
second.mkdir(exist_ok=True)
return second
case "linux":
first = Path("~/.local/share").expanduser()
second = first / Class["name"]
second.mkdir(exist_ok=True)
return second
case "darwin":
first = Path("~/Library/Application Support").expanduser()
second = first / Class["name"]
second.mkdir(exist_ok=True)
return second
raise OSError(f"un-supported operating system")
def open(self, filename: str, mode: str = "r"):
return (self.rt_path / filename).open(mode=mode)
def set(self, variable: str, value: Any):
self._runtime_storage[variable] = value
def get(self, variable: str):
return self._runtime_storage.get(variable, None)
def save(self):
with self.open("data", "wb") as output:
pickle.dump(self._runtime_storage, output)
def load(self):
with self.open("data", "rb") as _input:
self._runtime_storage = pickle.load(_input)
@contextlib.contextmanager
def storage(name: str):
_class: ApplicationClass = {"name": name, "platform": sys.platform}
st = RuntimeStorage(_class)
try:
yield st
finally:
st.save()