]> jfr.im git - z_archive/Ophion.git/blame - util.py
Cache.hookcmd() prototype changes
[z_archive/Ophion.git] / util.py
CommitLineData
b069ba10
JR
1from traceback import print_exc
2
3def toint(s):
4 i = None
5 try: i = int(s)
6 except: pass
7 return i
8
9def noaccess(bot, nick, chcmd=False):
10 if chcmd:
11 bot.msg(nick, "You don't have enough access to do that.")
12 else:
13 bot.msg(nick, "No such command or not enough access.")
14
15# except Exception as e:
16# print_exc(None, cache.excfile)
17# bot.msg(fromnick, "An error occurred, sorry! Try again later.")
18# cache.mainBot.cmsg('warn', "EXCEPTION! %r Caused by <%s> %s" % (e, fromnick, line))
19def loadmod(modf, cache, reloading=False):
20 # return: 0=success, 1=import error, 2=already loaded, 3=reload() failed
21 if modf in cache.modules:
22 return 2
23 elif modf in cache.unmodules:
24 mod = cache.unmodules[modf]
25 try:
26 reload(mod)
27 except Exception as e:
28 print "! reload"
29 print_exc(None, cache.excfile)
30 return 1
31 try:
32 if mod.init(cache):
33 print "not mod.init(cache)"
34 cache.excfile.write("%s refused init - returned True\n" % (modf))
35 return 1
36 except Exception as e:
37 print "! init"
38 print_exc(None, cache.excfile)
39 return 1
40 del cache.unmodules[modf]
41 else:
42 try:
43 mod = __import__(modf)
44 except Exception as e:
45 print "! import"
46 print_exc(None, cache.excfile)
47 return 1
48 try:
49 if mod.init(cache):
50 cache.excfile.write("%s refused init - returned True\n" % (modf))
51 except Exception as e:
52 print "! init"
53 print_exc(None, cache.excfile)
54 return 1
55 cache.modules[modf] = mod
56 return 0
57def unloadmod(modf, cache, reloading=False):
58 # return: 0=success, 1=refused unload, 2=not loaded
59 if modf not in cache.modules:
60 return 2
61 else:
62 try:
63 ret = cache.modules[modf].deinit(cache, reloading)
64 except Exception as e:
65 print_exc(None, cache.excfile)
66 return 1
67 if ret:
68 return 1
69 else:
70 cache.unmodules[modf] = cache.modules[modf]
71 del cache.modules[modf]
72 return 0
73def reloadmod(modf, cache):
74 # return: 0 = success; 1,2=unload error; 3,4,5=load error
75 unloadret = unloadmod(modf, cache, True)
76 if unloadret != 0: return unloadret
77 loadret = loadmod(modf, cache, True)
78 if loadret != 0: return loadret+2
79 return 0