-
Notifications
You must be signed in to change notification settings - Fork 0
/
common.py
123 lines (96 loc) · 3.02 KB
/
common.py
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
from dataclasses import dataclass
import os
import yaml
import datetime
from loguru import logger
from typing import Callable
@dataclass
class OpenAIConfig:
apiKey: str
model: str
contextTimeout: int = 120
visionModel: str = "gpt-4-vision-preview"
visionMaxToken: int = 300
visionContextTimeout: int = 60
@dataclass
class GeminiConfig:
apiKey: str
model: str = "gemini-pro"
contextTimeout: int = 600
visionModel: str = "gemini-pro-vision"
@dataclass
class TelegramConfig:
botToken: str
escapeText: bool = False
parseMode: str = ""
@dataclass
class SystemConfig:
whitelistEnabled: bool
useGemini: bool = True
@dataclass
class Config:
botName: str
openAI: OpenAIConfig
gemini: GeminiConfig
telegram: TelegramConfig
system: SystemConfig
class ACL:
def __init__(self, data):
self.user = {}
self.chat = {}
if "allowedUserID" in data:
uAcl = data["allowedUserID"]
for id, allow in uAcl.items():
self.user[id] = allow
if "allowedChatID" in data:
cAcl = data["allowedChatID"]
for id, allow in cAcl.items():
self.chat[id] = allow
def allowUser(self, uid: str) -> bool:
if uid in self.user:
return self.user[uid]
if "allow_all" in self.user:
return self.user["allow_all"]
return False
def allowChat(self, cid: str) -> bool:
if cid in self.chat:
return self.chat[cid]
if "allow_all" in self.chat:
return self.chat["allow_all"]
return False
def getRoot() -> str:
return os.path.dirname(os.path.abspath(__file__))
def getConfig() -> Config:
with open(os.path.join(getRoot(), 'conf', 'config.yml')) as cfg:
data = yaml.load(cfg, yaml.FullLoader)
data['openAI'] = OpenAIConfig(**data['openAI'])
data['gemini'] = GeminiConfig(**data['gemini'])
data['telegram'] = TelegramConfig(**data['telegram'])
data['system'] = SystemConfig(**data['system'])
config = Config(**data)
return config
def getAcl() -> ACL:
with open(os.path.join(getRoot(), 'conf', 'acl.yml')) as acl:
data = yaml.load(acl, yaml.FullLoader)
acl = ACL(data)
return acl
def setLogger():
date = datetime.datetime.now().strftime("%Y_%m_%d")
logger.add(f"log/chloe.{date}.log", rotation="00:00", level="DEBUG", retention=30)
return logger
class Defer:
def __init__(self, *callbacks: Callable):
self.exitFuncList = [c for c in callbacks if c is not None]
def __enter__(self):
def defer(c: Callable):
if c is not None and callable(c):
self.exitFuncList.append(c)
return defer
def __exit__(self, exc_type, exc_val, exc_tb):
for f in reversed(self.exitFuncList):
if callable(f):
f()
def rmHandle(f: str) -> Callable:
def rmFunc():
os.remove(f)
return rmFunc