Terarea  2
The automation project
Loading...
Searching...
No Matches
constants.py
Go to the documentation of this file.
1"""_summary_
2 This is the file in charge of containing the constants that run the server.
3"""
4
5from typing import List, Any
6
7import toml
8import dotenv
9from display_tty import IDISP
10IDISP.logger.name = "Constants"
11
12
13# Environement initialisation
14dotenv.load_dotenv(".env")
15ENV = dict(dotenv.dotenv_values())
16
17# toml config file
18TOML_CONF = toml.load("config.toml")
19
20
21def _get_environement_variable(environement: dotenv, variable_name: str) -> str:
22 """_summary_
23 Get the content of an environement variable.
24
25 Args:
26 variable_name (str): _description_
27
28 Returns:
29 str: _description_: the value of that variable, otherwise an exception is raised.
30 """
31 data = environement.get(variable_name)
32 if data is None:
33 raise ValueError(
34 f"Variable {variable_name} not found in the environement"
35 )
36 return data
37
38
39def _get_toml_variable(toml_conf: dict, section: str, key: str, default=None) -> Any:
40 """
41 Get the value of a configuration variable from the TOML file.
42
43 Args:
44 toml_conf (dict): The loaded TOML configuration as a dictionary.
45 section (str): The section of the TOML file to search in.
46 key (str): The key within the section to fetch.
47 default: The default value to return if the key is not found. Defaults to None.
48
49 Returns:
50 str: The value of the configuration variable, or the default value if the key is not found.
51
52 Raises:
53 KeyError: If the section is not found in the TOML configuration.
54 """
55 try:
56 keys = section.split('.')
57 current_section = toml_conf
58
59 for k in keys:
60 if k in current_section:
61 current_section = current_section[k]
62 else:
63 raise KeyError(
64 f"Section '{section}' not found in TOML configuration."
65 )
66
67 if key in current_section:
68 msg = f"current_section[{key}] = {current_section[key]} : "
69 msg += f"{type(current_section[key])}"
70 IDISP.log_debug(msg, "_get_toml_variable")
71 if current_section[key] == "none":
72 IDISP.log_debug(
73 "The value none has been converted to None.",
74 "_get_toml_variable"
75 )
76 return None
77 return current_section[key]
78 if default is None:
79 msg = f"Key '{key}' not found in section '{section}' "
80 msg += "of TOML configuration."
81 raise KeyError(msg)
82 return default
83
84 except KeyError as e:
85 IDISP.log_warning(f"{e}", "_get_toml_variable")
86 return default
87
88
89# Enable debugging for the functions in the constants file.
90IDISP.debug = _get_toml_variable(
91 TOML_CONF, "Server_configuration.debug_mode", "debug", False
92)
93
94
95# Mail management
96SENDER_ADDRESS = _get_environement_variable(ENV, "SENDER_ADDRESS")
97SENDER_KEY = _get_environement_variable(ENV, "SENDER_KEY")
98SENDER_HOST = _get_environement_variable(ENV, "SENDER_HOST")
99SENDER_PORT = int(_get_environement_variable(ENV, "SENDER_PORT"))
100
101# Server oath variables
102REDIRECT_URI = _get_environement_variable(ENV, "REDIRECT_URI")
103
104# Database management
105DB_HOST = _get_environement_variable(ENV, "DB_HOST")
106DB_PORT = int(_get_environement_variable(ENV, "DB_PORT"))
107DB_USER = _get_environement_variable(ENV, "DB_USER")
108DB_PASSWORD = _get_environement_variable(ENV, "DB_PASSWORD")
109DB_DATABASE = _get_environement_variable(ENV, "DB_DATABASE")
110
111# Minio management
112MINIO_HOST = _get_environement_variable(ENV, "MINIO_HOST")
113MINIO_PORT = int(_get_environement_variable(ENV, "MINIO_PORT"))
114MINIO_ROOT_USER = _get_environement_variable(ENV, "MINIO_ROOT_USER")
115MINIO_ROOT_PASSWORD = _get_environement_variable(ENV, "MINIO_ROOT_PASSWORD")
116
117# TOML variables
118# |- Server configurations
119SERVER_WORKERS = _get_toml_variable(
120 TOML_CONF, "Server_configuration", "workers", None
121)
122SERVER_LIFESPAN = _get_toml_variable(
123 TOML_CONF, "Server_configuration", "lifespan", "auto"
124)
125SERVER_TIMEOUT_KEEP_ALIVE = _get_toml_variable(
126 TOML_CONF, "Server_configuration", "timeout_keep_alive", 30
127)
128
129# |- Server configuration -> Status codes
130SUCCESS = int(_get_toml_variable(
131 TOML_CONF, "Server_configuration.status_codes", "success", 0
132))
134 TOML_CONF, "Server_configuration.status_codes", "error", -84
135))
136
137# |- Server configuration -> Debug
139 TOML_CONF, "Server_configuration.debug_mode", "debug", False
140)
141
142# |- Server configuration -> development
143SERVER_DEV_RELOAD = _get_toml_variable(
144 TOML_CONF, "Server_configuration.development", "reload", False
145)
146SERVER_DEV_RELOAD_DIRS = _get_toml_variable(
147 TOML_CONF, "Server_configuration.development", "reload_dirs", None
148)
149SERVER_DEV_LOG_LEVEL = _get_toml_variable(
150 TOML_CONF, "Server_configuration.development", "log_level", "info"
151)
152SERVER_DEV_USE_COLOURS = _get_toml_variable(
153 TOML_CONF, "Server_configuration.development", "use_colours", True
154)
155
156# |- Server configuration -> production
157SERVER_PROD_PROXY_HEADERS = _get_toml_variable(
158 TOML_CONF, "Server_configuration.production", "proxy_headers", True
159)
160SERVER_PROD_FORWARDED_ALLOW_IPS = _get_toml_variable(
161 TOML_CONF, "Server_configuration.production", "forwarded_allow_ips", None
162)
163
164# |- Server configuration -> database settings
165DATABASE_POOL_NAME = _get_toml_variable(
166 TOML_CONF, "Server_configuration.database", "pool_name", None
167)
168DATABASE_MAX_POOL_CONNECTIONS = int(_get_toml_variable(
169 TOML_CONF, "Server_configuration.database", "max_pool_connections", 10
170))
171DATABASE_RESET_POOL_NODE_CONNECTION = _get_toml_variable(
172 TOML_CONF, "Server_configuration.database", "reset_pool_node_connection", True
173)
174DATABASE_CONNECTION_TIMEOUT = int(_get_toml_variable(
175 TOML_CONF, "Server_configuration.database", "connection_timeout", 10
176))
177DATABASE_LOCAL_INFILE = _get_toml_variable(
178 TOML_CONF, "Server_configuration.database", "local_infile", False
179)
180DATABASE_INIT_COMMAND = _get_toml_variable(
181 TOML_CONF, "Server_configuration.database", "init_command", None
182)
183DATABASE_DEFAULT_FILE = _get_toml_variable(
184 TOML_CONF, "Server_configuration.database", "default_file", None
185)
186DATABASE_SSL_KEY = _get_toml_variable(
187 TOML_CONF, "Server_configuration.database", "ssl_key", None
188)
189DATABASE_SSL_CERT = _get_toml_variable(
190 TOML_CONF, "Server_configuration.database", "ssl_cert", None
191)
192DATABASE_SSL_CA = _get_toml_variable(
193 TOML_CONF, "Server_configuration.database", "ssl_ca", None
194)
195DATABASE_SSL_CIPHER = _get_toml_variable(
196 TOML_CONF, "Server_configuration.database", "ssl_cipher", None
197)
198DATABASE_SSL_VERIFY_CERT = _get_toml_variable(
199 TOML_CONF, "Server_configuration.database", "ssl_verify_cert", False
200)
201DATABASE_SSL = _get_toml_variable(
202 TOML_CONF, "Server_configuration.database", "ssl", None
203)
204DATABASE_AUTOCOMMIT = _get_toml_variable(
205 TOML_CONF, "Server_configuration.database", "autocommit", False
206)
207DATABASE_COLLATION = _get_toml_variable(
208 TOML_CONF, "Server_configuration.database", "collation", "utf8mb4_unicode_ci"
209)
210
211# |- Cron settings
212CLEAN_TOKENS = _get_toml_variable(TOML_CONF, "Crons", "clean_tokens", True)
213CLEAN_TOKENS_INTERVAL = int(_get_toml_variable(
214 TOML_CONF, "Crons", "clean_tokens_interval", 1800
215))
216ENABLE_TEST_CRONS = _get_toml_variable(
217 TOML_CONF, "Crons", "enable_test_crons", False
218)
219TEST_CRONS_INTERVAL = int(_get_toml_variable(
220 TOML_CONF, "Crons", "test_cron_interval", 200
221))
222CHECK_ACTIONS_INTERVAL = int(_get_toml_variable(
223 TOML_CONF, "Crons", "check_actions_interval", 300
224))
225CLEAN_VERIFICATION = _get_toml_variable(
226 TOML_CONF, "Crons", "clean_verification", True
227)
228CLEAN_VERIFICATION_INTERVAL = _get_toml_variable(
229 TOML_CONF, "Crons", "clean_verification_interval", 900
230)
231RENEW_OATH_TOKENS = _get_toml_variable(
232 TOML_CONF, "Crons", "renew_oath_tokens", True
233)
234RENEW_OATH_TOKENS_INTERVAL = _get_toml_variable(
235 TOML_CONF, "Crons", "renew_oath_tokens_interval", 1800
236)
237
238# |- Verification
239EMAIL_VERIFICATION_DELAY = int(_get_toml_variable(
240 TOML_CONF, "Verification", "email_verification_delay", 120
241))
242CHECK_TOKEN_SIZE = int(_get_toml_variable(
243 TOML_CONF, "Verification", "check_token_size", 4
244))
245RANDOM_MIN = int(_get_toml_variable(
246 TOML_CONF, "Verification", "random_min", 100000
247))
248RANDOM_MAX = int(_get_toml_variable(
249 TOML_CONF, "Verification", "random_max", 999999
250))
251
252# |- Services
253API_REQUEST_DELAY = int(_get_toml_variable(
254 TOML_CONF, "Services", "api_request_delay", 5
255))
256
257# Json default keys
258JSON_TITLE: str = "title"
259JSON_MESSAGE: str = "msg"
260JSON_ERROR: str = "error"
261JSON_RESP: str = "resp"
262JSON_LOGGED_IN: str = "logged in"
263JSON_UID: str = "user_uid"
264
265# JSON Header keys
266JSON_HEADER_APP_NAME: str = "app_sender"
267JSON_HEADER_HOST: str = "serving_host"
268JSON_HEADER_PORT: str = "serving_port"
269CONTENT_TYPE: str = "JSON"
270
271# Database table names
272TAB_ACCOUNTS = "Users"
273TAB_ACTIONS = "Actions"
274TAB_SERVICES = "Services"
275TAB_CONNECTIONS = "Connections"
276TAB_VERIFICATION = "Verification"
277TAB_ACTIVE_OAUTHS = "ActiveOauths"
278TAB_ACTION_LOGGING = "ActionLoging"
279TAB_ACTION_TEMPLATE = "ActionTemplate"
280TAB_USER_OAUTH_CONNECTION = "UserOauthConnection"
281
282# Character info config
283CHAR_NODE_KEY: str = "node"
284CHAR_ACTIVE_KEY: str = "active"
285CHAR_NAME_KEY: str = "name"
286CHAR_UID_KEY: str = "uid"
287CHAR_ID_DEFAULT_INDEX: int = 0
288
289
290# User info database table
291USERNAME_INDEX_DB: int = 1
292PASSWORD_INDEX_DB: int = 2
293FIRSTNAME_INDEX_DB: int = 3
294LASTNAME_INDEX_DB: int = 4
295BIRTHDAY_INDEX_DB: int = 5
296GENDER_INDEX_DB: int = 7
297ROLE_INDEX_DB: int = 10
298UD_USERNAME_KEY: str = "username"
299UD_FIRSTNAME_KEY: str = "firstname"
300UD_LASTNAME_KEY: str = "lastname"
301UD_BIRTHDAY_KEY: str = "birthday"
302UD_GENDER_KEY: str = "gender"
303UD_ROLE_KEY: str = "role"
304UD_ADMIN_KEY: str = "admin"
305UD_LOGIN_TIME_KEY: str = "login_time"
306UD_LOGGED_IN_KEY: str = "logged_in"
307
308# For Path creation variables
309PATH_KEY: str = "path"
310ENDPOINT_KEY: str = "endpoint"
311METHOD_KEY: str = "method"
312ALLOWED_METHODS: List[str] = [
313 "GET", "POST",
314 "PUT", "PATCH",
315 "DELETE", "HEAD",
316 "OPTIONS"
317]
318
319# Incoming header variables
320REQUEST_TOKEN_KEY = "token"
321REQUEST_BEARER_KEY = "authorization"
322
323# Cache loop
324THREAD_CACHE_REFRESH_DELAY = 10
325
326# User sql data
327UA_TOKEN_LIFESPAN: int = 7200
328UA_EMAIL_KEY: str = "email"
329UA_LIFESPAN_KEY: str = "lifespan"
330
331# Get user info banned columns (filtered out columns)
332USER_INFO_BANNED: List[str] = ["password", "method", "favicon"]
333USER_INFO_ADMIN_NODE: str = "admin"
str _get_environement_variable(dotenv environement, str variable_name)
Definition constants.py:21
Any _get_toml_variable(dict toml_conf, str section, str key, default=None)
Definition constants.py:39