]>
jfr.im git - erebus.git/blob - ctlmod.py
1 # Erebus IRC bot - Author: John Runyon
2 # module loading/unloading/tracking code
4 from __future__
import print_function
9 if sys
.version_info
.major
>= 3:
10 from importlib
import reload
14 #dependents[modname] = [list of modules which depend on modname]
16 def isloaded(modname
): return modname
in modules
17 def modhas(modname
, attname
): return getattr(modules
[modname
], attname
, None) is not None
19 def load(parent
, modname
, dependent
=False):
20 #wrapper to call _load and print return
22 print("(Loading dependency %s..." % (modname
), end
=' ')
24 print("%09.3f [MOD] [?] Loading %s..." % (time
.time() % 100000, modname
), end
=' ')
25 modstatus
= _load(parent
, modname
, dependent
)
28 print("failed: %s)" % (modstatus
), end
=' ')
30 print("failed: %s." % (modstatus
))
31 elif modstatus
== True:
38 print("OK: %s)" % (modstatus
), end
=' ')
40 print("OK: %s." % (modstatus
))
43 def _load(parent
, modname
, dependent
=False):
45 if not isloaded(modname
):
47 mod
= __import__('modules.'+modname
, globals(), locals(), ['*'], 0)
48 # ^ fromlist doesn't actually do anything(?) but it means we don't have to worry about this returning the top-level "modules" object
49 reload(mod
) #in case it's been previously loaded.
50 except Exception as e
:
51 return modlib
.error(e
)
54 if not hasattr(mod
, 'modinfo'):
55 return modlib
.error('no modinfo')
57 if parent
.APIVERSION
not in mod
.modinfo
['compatible']:
58 return modlib
.error('API-incompatible')
60 modules
[modname
] = mod
61 dependents
[modname
] = []
63 for dep
in mod
.modinfo
['depends']:
64 if bool(int(parent
.cfg
.get('autoloads', dep
, default
=1))):
65 if dep
not in modules
:
66 depret
= load(parent
, dep
, dependent
=True)
67 if depret
is not None and not depret
:
70 return modlib
.error("dependent %s disabled" % (dep
))
71 dependents
[dep
].append(modname
)
73 for dep
in mod
.modinfo
['softdeps']:
74 if bool(int(parent
.cfg
.get('autoloads', dep
, default
=1))):
75 if dep
not in modules
:
76 depret
= load(parent
, dep
, dependent
=True)
77 if depret
is not None:
79 successstatus
.append("softdep %s failed" % (dep
))
81 successstatus
.append("softdep %s disabled" % (dep
))
82 #swallow errors loading - softdeps are preferred, not required
85 ret
= mod
.modstart(parent
)
90 del dependents
[modname
]
91 for dep
in mod
.modinfo
['depends']:
92 dependents
[dep
].remove(modname
)
94 successstatus
= ';'.join(successstatus
)
95 if len(successstatus
) > 0 and ret
:
99 return "%s (%s)" % (ret
, successstatus
)
102 else: #if not isloaded...else:
103 return modlib
.error('already loaded')
105 def unload(parent
, modname
):
106 if isloaded(modname
):
107 for dependent
in dependents
[modname
]:
108 unload(parent
, dependent
)
109 for dep
in modules
[modname
].modinfo
['depends']:
110 dependents
[dep
].remove(modname
)
111 ret
= modules
[modname
].modstop(parent
)
115 return modlib
.error('already unloaded')
117 def reloadmod(parent
, modname
):
118 if isloaded(modname
):
119 if modhas(modname
, 'modrestart'): modules
[modname
].modrestart(parent
)
120 else: modules
[modname
].modstop(parent
)
123 reload(modules
[modname
])
124 except BaseException
as e
:
125 return modlib
.error(e
)
127 if modhas(modname
, 'modrestarted'): ret
= modules
[modname
].modrestarted(parent
)
128 else: ret
= modules
[modname
].modstart(parent
)
132 return load(parent
, modname
)
135 def loadall(parent
, modlist
):
136 for m
in modlist
: load(parent
, m
)
137 def unloadall(parent
, modlist
):
138 for m
in modlist
: unload(parent
, m
)
139 def reloadall(parent
, modlist
):
140 for m
in modlist
: reloadmod(parent
, m
)