-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathenv_vars.py
More file actions
86 lines (78 loc) · 2.79 KB
/
env_vars.py
File metadata and controls
86 lines (78 loc) · 2.79 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
from enum import Enum
from botstate import BotState
class EnvVarScope(Enum):
GLOBAL = 0
"""Global scope"""
LOCAL = 1
"""Local scope, specific to a ChatID"""
EFFECTIVE = 2
"""Effective scope - local value if available, else global"""
class EnvVar:
"""
"""
@staticmethod
def get_scope(env_name: str, scopeid: int) -> str | None:
"""
Gets the env-var from a specific scope
@param env_name: Name of the variable
@param scopeid: Scope (chatID if local, 0 if global)
@return:
"""
results = BotState.DBLink.execute(("""
SELECT var_value FROM env_vars
WHERE var_scope = ?
AND var_name = ?
"""), (scopeid, env_name))
result = results.fetchone()
print(env_name, scopeid)
print(results)
print(result)
if result:
return result[0]
return None
@staticmethod
def get(env_name: str, chatid: int, option: EnvVarScope = EnvVarScope.EFFECTIVE) -> str:
"""
Gets the value of a specific environment variable.
@param env_name: Name of the variable.
@param chatid: ChatID of the chat in question.
@param option: Scope selection between global, local or effective
@return:
"""
print("Selecting scope...")
if option == EnvVarScope.LOCAL:
print("Local chosen.")
return EnvVar.get_scope(env_name=env_name, scopeid=chatid)
if option == EnvVarScope.GLOBAL:
print("Global chosen.")
return EnvVar.get_scope(env_name=env_name, scopeid=0)
if option == EnvVarScope.EFFECTIVE:
print("Effective chosen.")
final = EnvVar.get_scope(env_name=env_name, scopeid=chatid)
if not final:
final = EnvVar.get_scope(env_name=env_name, scopeid=0)
return final
@staticmethod
def set_scope(env_name: str, scopeid: int, value: str):
"""
Sets the value of an environment variable.
@param env_name: Name ov the variable.
@param scopeid: ChatID for local scopes or 0 for global scope
@param value: Value to set the variable to.
@return:
"""
exists = EnvVar.get_scope(env_name, scopeid)
if exists:
BotState.DBLink.execute(("""
UPDATE env_vars
SET var_value = ?
WHERE var_scope = ?
AND var_name = ?
"""), (value, scopeid, env_name))
BotState.write()
else:
BotState.DBLink.execute(("""
INSERT INTO env_vars
VALUES(?,?,?)
"""), (env_name, value, scopeid))
BotState.write()