-
Notifications
You must be signed in to change notification settings - Fork 0
/
autoload.py
156 lines (118 loc) · 4.54 KB
/
autoload.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
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
# autoload.py
import re
import sys
from importlib import reload
from watchdog.events import FileSystemEventHandler
from watchdog.observers import Observer
import util
from util import colorize
from moduleconfig import loadlist, subscriptions
observer = Observer()
watchlist = []
def recursively_add_to_watchlist(module):
global watchlist
watchlist.append(module)
if module in subscriptions:
for sub in subscriptions[module]:
recursively_add_to_watchlist(sub)
for module in loadlist:
recursively_add_to_watchlist(module)
def load_one(core, name):
try:
exec(f'import {name}')
if hasattr(module := sys.modules[name], 'load'):
print(f'Initial load of {name}')
module.load(core)
except Exception as e:
print(colorize(f'&cFailed to load module {name}: {e.__class__.__name__}: {e}'))
def load_initial(core):
for name in watchlist:
load_one(core, name)
print()
def load_asyncs(core):
for w in watchlist:
if w not in sys.modules:
print(colorize(f'&6Tried to load_async on module {w} but it was never loaded!'))
continue
if hasattr(module := sys.modules[w], 'load_async'):
print()
core.bot.dispatch('load_require_async', module)
# print()
def reload_filename(filename, core):
await_this = core.exports.get('await_this')
reloading_str = f'\nReloading module {filename}...'
async def shit():
channel = await core.channel_provider(942838937427271771)
reloading_str_2 = f'Reloading module **{filename}**...'
await channel.send(embed=util.ifinfo(reloading_str_2))
await_this()(shit())
if filename not in sys.modules:
print(colorize(f'&6Trying to reload module {filename} (it was not loaded already)...'))
load_one(core, filename)
return
module = sys.modules[filename]
print(reloading_str)
if filename == 'autoload':
observer.stop()
reload(module)
if hasattr(module, 'load'):
module.load(core)
if hasattr(module, 'load_async'):
core.bot.dispatch('load_require_async', module)
if filename in subscriptions.keys():
print(f'{filename} has subscribers, reloading them now.')
for subscriber in subscriptions[filename]:
reload_filename(subscriber, core)
def load_command(core):
command_class = core.exports.get('command/Command')
register_command = core.exports.get('command/register')
lang = core.exports.get('lang')
async def reload_command(message, args):
channel = message.channel
if len(args) < 1:
await channel.send(embed=util.iferror(lang()('supply.generic', 'a module to reload, or "all"')))
return
file = args[0]
# global watchlist
if file in watchlist:
await channel.send(embed=util.ifinfo(f'Reloading module {file}'))
reload_filename(file, core)
elif file == 'all':
# watchlist = util.remove_duplicates(watchlist)
for w in watchlist:
await channel.send(f'Reloading module {w}')
reload_filename(w, core)
else:
pass
await channel.send(embed=util.iferror(lang()('error.invalid.generic', 'module')))
register_command()('reload', reload_command, [command_class().Check.is_andyinnie])
def load(core):
print('Loading autoload.py (so meta!)')
load_command(core)
class MyEventHandler(FileSystemEventHandler):
@staticmethod
def handle(e):
if e.is_directory:
return
filenames = re.findall(r'(?<=\./)\w+(?=\.py)', e.src_path)
if not bool(filenames):
return
filename = filenames[0]
# global watchlist
# watchlist = util.remove_duplicates(watchlist)
if filename not in watchlist:
return
reload_filename(filename, core)
# def on_deleted(self, e):
# self.handle(e)
def on_closed(self, e):
self.handle(e)
# def on_any_event(self, e):
# print(e)
# print(e.event_type)
# print(e.src_path)
# print(e.is_directory)
path = sys.argv[1] if len(sys.argv) > 1 else '.'
observer.schedule(MyEventHandler(), path)
observer.start()
print(f'Done, watching {len(watchlist)} files')