forked from i-am-fyre/Scraper-Discord-Notification
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnotification_agent_lib.py
More file actions
307 lines (242 loc) · 9.2 KB
/
notification_agent_lib.py
File metadata and controls
307 lines (242 loc) · 9.2 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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
#!/usr/bin/env python3
import yaml
import sys
import os
from importlib import util, machinery
#import json
import inspect
import reflection_lib as refl
import logger_lib as log
import uuid
import re
import creator_utils_lib as creator
class notif_agent:
enabled = True
def __init__(self, id, name, module, module_properties):
self.id = id
self.name = name
self.module = module
self.module_properties = module_properties
# looks for sub diretories inside "{directory}"
# and inspects its contents for a "agent.py" file and grabs the class inside that file
# uses config files inside of {directory}/{agent_dir}/config.yaml
# RETURNS: a dictionary {agent_name : agent_instance}
def get_modules(directory, agent_dir):
result = {}
filename = "agent.py"
config_file = "config.yaml"
subdirs = refl.get_directories(f"{directory}/{agent_dir}")
modules = {}
for subdir in subdirs:
scraper_name = subdir
path = f"{directory}/{agent_dir}/{subdir}/{filename}"
if not os.path.exists(path):
continue
namespace = refl.path_to_namespace(f"{agent_dir}/{subdir}/{filename}")
finder = machinery.PathFinder()
spec = util.find_spec(f"{namespace}")
#spec = machinery.find_spec(f"{path}")
module = util.module_from_spec(spec)
# sys.modules[module_name] = module
spec.loader.exec_module(module)
# namespace = refl.path_to_namespace(path)
# module = refl.get_module(namespace)
module_class_name, module_class = refl.get_class(module, namespace)
result[subdir] = module_class()
return result
def get_agents(directory, agents_file, modules_dir):
modules = get_modules(directory, modules_dir)
result = {}
if not os.path.exists(agents_file):
open(agents_file, "w+")
with open(f"{directory}/{agents_file}", "r") as stream:
config = yaml.safe_load(stream)
if config is None:
return {}
for c in config:
agent = notif_agent(
c.get("id", str(uuid.uuid4())),
c.get("name"),
c.get("module"),
c.get("module_properties")
)
agent.enabled = c.get("enabled", True)
log.debug(f"Adding notification agent: {agent.id} {agent.name}")
result[agent.id] = agent
return result
def get_notif_agents_by_ids(notif_agents, ids):
result = []
for id in ids:
result.append(notif_agents[id])
return result
def get_names(notif_agents):
names = []
for a in notif_agents:
names.append(a.name)
return names
def get_enabled(agents):
print (agents)
result = []
for a in agents:
if a.enabled:
result.append(a)
return result
# <-- don't output yaml class tags
def noop(self, *args, **kw):
pass
yaml.emitter.Emitter.process_tag = noop
def save(notif_agents, file, preserve_comments=False):
if isinstance(notif_agents, dict):
old_notif_agents = notif_agents
notif_agents = []
for s in old_notif_agents:
notif_agents.append(old_notif_agents[s])
elif isinstance(notif_agents, list) == False:
raise ValueError(f"notif_agents must by list or dict, not: {type(notif_agents)}")
if preserve_comments:
# preserve comments in file
with open(file, "r") as stream:
filestream = stream.read()
match = re.findall("([#][^\n]*[\n]|[#][\n])", filestream)
with open(file, "w") as stream:
if preserve_comments and match:
for m in match:
stream.write(m)
yaml.dump(notif_agents, stream, default_flow_style=False, sort_keys=False)
def notif_agent_creator(cur_notif_agents, modules, file, edit_notif_agent=None):
n = {}
if edit_notif_agent is not None:
e = edit_notif_agent
old_name = e.name
n["id"] = e.id
n["name"] = e.name
n["module"] = e.module
for p in e.module_properties:
n[f"prop_{p}"] = e.module_properties[p]
while True:
n["id"] = creator.prompt_id(creator.get_id_list(cur_notif_agents), default = n.get("id", None))
n["name"] = creator.prompt_string("Name", default=n.get("name", None))
n["module"] = create_notif_agent_choose_module(modules, default=n.get("module", None))
module = modules[n["module"]]
props = module.get_properties()
print("Module Properties")
for p in props:
default = n.get(f"prop_{p}", None)
while True:
n[f"prop_{p}"] = creator.prompt_string(f"{p}", default=default)
is_valid, error_msg = module.is_property_valid(p, n[f"prop_{p}"])
if is_valid:
break
else:
print (error_msg)
default = None
print()
print(f"Id: {n['id']}")
print(f"Name: {n['name']}")
print(f"Module: {n['module']}")
print ("-----------------------------")
for p in props:
val = n[f"prop_{p}"]
print(f"{p}: {val}")
# print("}")
print ("-----------------------------")
"""
if edit_notif_agent is None:
confirm_msg = "Create this notif_agent?"
else:
confirm_msg = f"Apply changes to notification agent '{old_name}'"
"""
confirm = creator.prompt_options("Choose an option", ["save","edit","quit"])
if confirm == "save":
break
elif confirm == "edit":
continue
elif confirm == "quit":
return
set_props = {}
for p in props:
set_props[p] = n[f"prop_{p}"]
if edit_notif_agent is None:
save_notif_agent = notif_agent(
id=n["id"],
name=n["name"],
module=n["module"],
module_properties=set_props
)
else:
edit_notif_agent.id = n["id"]
edit_notif_agent.name = n["name"]
edit_notif_agent.module = n["module"]
edit_notif_agent.module_properties=set_props
save_notif_agent = edit_notif_agent
cur_notif_agents[n["id"]] = save_notif_agent
save(cur_notif_agents, file)
def create_notif_agent(cur_notif_agents, modules, file, edit_notif_agent=None):
creator.print_title("Add Notification Agent")
notif_agent_creator(cur_notif_agents, modules, file, edit_notif_agent=edit_notif_agent)
def edit_notif_agent(cur_notif_agents, modules, file):
creator.print_title("Edit Notification Agent")
notif_agent = creator.prompt_complex_dict("Choose a notification agent", cur_notif_agents, "name", extra_options=["d"], extra_options_desc=["done"])
if notif_agent == "d":
return
else:
create_notif_agent(cur_notif_agents, modules, file, edit_notif_agent=notif_agent)
def delete_notif_agent(notif_agents_dict, notif_agents_file, tasks_list, tasks_file):
creator.print_title("Delete Notification Agent")
save = False
changes_made = False
while True:
notif_agents_list = []
for n in notif_agents_dict:
notif_agents_list.append(notif_agents_dict[n])
for i in range(len(notif_agents_list)):
print(f"{i} - {notif_agents_list[i].name} [id: {notif_agents_list[i].id}]")
print ("s - save and quit")
print ("q - quit without saving")
tnum_str = creator.prompt_string("Delete notif_agent")
if tnum_str == "s":
save = True
break
elif tnum_str == "q":
if changes_made and creator.yes_no("Quit without saving","n") == "y":
return
elif not changes_made:
return
if re.match("[0-9]+$", tnum_str):
tnum = int(tnum_str)
if tnum >= 0 and tnum < len(notif_agents_list):
if creator.yes_no(f"Delete {notif_agents_list[tnum].name}", "y") == "y":
do_delete_notif_agent(notif_agents_list[tnum].id, notif_agents_dict, tasks_list)
changes_made = True
if save:
save(notif_agents_list, notif_agents_file)
tasklib.save(tasks_list, tasks_file)
def do_delete_notif_agent(id, notif_agents_dict, tasks_list):
for t in tasks_list:
if id in t.notif_agent_ids:
t.notif_agent_ids.remove(id)
del notif_agents_dict[id]
def create_notif_agent_choose_module(modules, default=None):
default_str = ""
if default is not None:
default_str = f" [{default}]"
while True:
modules_list = []
i = 0
for m in modules:
modules_list.append(m)
print(f"{i} - {m}")
i = i + 1
choices = "0"
if len(modules_list) > 1:
choices = f"0-{len(modules_list) - 1}"
module_index_str = input(f"Module [Choose from {choices}]:{default_str} ")
if default is not None and module_index_str == "":
return default
if len(modules_list) == 0 and module_index_str == "":
module_index = 0
break
if re.match("[0-9]+$", module_index_str):
module_index = int(module_index_str)
if module_index >= 0 and module_index < len(modules_list):
return modules_list[module_index]