]>
jfr.im git - erebus.git/blob - ctlmod.py
1 # Erebus IRC bot - Author: John Runyon
2 # module loading/unloading/tracking code
10 def isloaded(modname
): return modname
in modules
11 def modhas(modname
, attname
): return getattr(self
.modules
[modname
], attname
, None) is not None
13 def load(parent
, modname
):
14 if not isloaded(modname
):
15 mod
= __import__(modname
)
18 if 1 not in mod
.modinfo
['compatible']:
19 return modlib
.error('API-incompatible')
21 modules
[modname
] = mod
22 dependents
[modname
] = []
24 for dep
in mod
.modinfo
['depends']:
25 if dep
not in modules
:
26 depret
= load(parent
, dep
)
29 dependents
[dep
].append(modname
)
32 ret
= mod
.modstart(parent
)
33 if ret
is not None and not ret
:
35 del dependents
[modname
]
36 for dep
in mod
.modinfo
['depends']:
37 dependents
[dep
].remove(modname
)
39 else: #if not isloaded...else:
40 return modlib
.error('already loaded')
42 def unload(parent
, modname
):
44 for dependent
in dependents
[modname
]:
45 unload(parent
, dependent
)
46 for dep
in dependents
[modname
]:
47 dependents
[dep
].remove(modname
)
48 self
.modules
[modname
].modstop(parent
)
50 return modlib
.error('already unloaded')
52 def reloadmod(parent
, modname
):
54 if modhas(modname
, 'modrestart'): self
.modules
[modname
].modrestart(parent
)
55 else: self
.modules
[modname
].modstop(parent
)
57 reload(self
.modules
[modname
])
59 if modhas(modname
, 'modrestarted'): self
.modules
[modname
].modrestarted(parent
)
60 else: self
.modules
[modname
].modstart(parent
)
65 def loadall(parent
, modlist
):
66 for m
in modlist
: load(parent
, m
)
67 def unloadall(parent
, modlist
):
68 for m
in modlist
: unload(parent
, m
)
69 def reloadall(parent
, modlist
):
70 for m
in modlist
: reloadmod(parent
, m
)
72 sys
.path
.append('modules')