forked from dullage/flatnotes
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathglobal_config.py
More file actions
118 lines (98 loc) · 3.94 KB
/
global_config.py
File metadata and controls
118 lines (98 loc) · 3.94 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
import sys
from enum import Enum
from helpers import CustomBaseModel, get_env
from logger import logger
class GlobalConfig:
def __init__(self) -> None:
logger.debug("Loading global config...")
self.auth_type: AuthType = self._load_auth_type()
self.quick_access_hide: bool = self._quick_access_hide()
self.quick_access_title: str = self._quick_access_title()
self.quick_access_term: str = self._quick_access_term()
self.quick_access_sort: str = self._quick_access_sort()
self.quick_access_limit: int = self._quick_access_limit()
self.path_prefix: str = self._load_path_prefix()
def load_auth(self):
if self.auth_type in (AuthType.NONE, AuthType.READ_ONLY):
return None
elif self.auth_type in (AuthType.PASSWORD, AuthType.TOTP):
from auth.local import LocalAuth
return LocalAuth()
def load_note_storage(self):
from notes.file_system import FileSystemNotes
return FileSystemNotes()
def load_attachment_storage(self):
from attachments.file_system import FileSystemAttachments
return FileSystemAttachments()
def _load_auth_type(self):
key = "FLATNOTES_AUTH_TYPE"
auth_type = get_env(
key, mandatory=False, default=AuthType.PASSWORD.value
)
try:
auth_type = AuthType(auth_type.lower())
except ValueError:
logger.error(
f"Invalid value '{auth_type}' for {key}. "
+ "Must be one of: "
+ ", ".join([auth_type.value for auth_type in AuthType])
+ "."
)
sys.exit(1)
return auth_type
def _quick_access_hide(self):
key = "FLATNOTES_QUICK_ACCESS_HIDE"
value = get_env(key, mandatory=False, default=False, cast_bool=True)
if value is False:
depricated_key = "FLATNOTES_HIDE_RECENTLY_MODIFIED"
value = get_env(
depricated_key, mandatory=False, default=False, cast_bool=True
)
if value is True:
logger.warning(
f"{depricated_key} is depricated. Please use {key} instead."
)
return value
def _quick_access_title(self):
key = "FLATNOTES_QUICK_ACCESS_TITLE"
return get_env(key, mandatory=False, default="RECENTLY MODIFIED")
def _quick_access_term(self):
key = "FLATNOTES_QUICK_ACCESS_TERM"
return get_env(key, mandatory=False, default="*")
def _quick_access_sort(self):
key = "FLATNOTES_QUICK_ACCESS_SORT"
value = get_env(key, mandatory=False, default="lastModified")
valid_values = ["score", "title", "lastModified"]
if value not in valid_values:
logger.error(
f"Invalid value '{value}' for {key}. "
+ "Must be one of: "
+ ", ".join(valid_values)
)
sys.exit(1)
return value
def _quick_access_limit(self):
key = "FLATNOTES_QUICK_ACCESS_LIMIT"
return get_env(key, mandatory=False, default=4, cast_int=True)
def _load_path_prefix(self):
key = "FLATNOTES_PATH_PREFIX"
value = get_env(key, mandatory=False, default="")
if value and (not value.startswith("/") or value.endswith("/")):
logger.error(
f"Invalid value '{value}' for {key}. "
+ "Must start with '/' and not end with '/'."
)
sys.exit(1)
return value
class AuthType(str, Enum):
NONE = "none"
READ_ONLY = "read_only"
PASSWORD = "password"
TOTP = "totp"
class GlobalConfigResponseModel(CustomBaseModel):
auth_type: AuthType
quick_access_hide: bool
quick_access_title: str
quick_access_term: str
quick_access_sort: str
quick_access_limit: int