Add the ability to reload submodule or a module part after a import reload (useful for reload imports of this module)

This commit is contained in:
Némunaire 2012-07-03 04:13:10 +02:00
parent d37aeeda4c
commit b4b24c4543
6 changed files with 29 additions and 10 deletions

View File

@ -68,7 +68,7 @@ def parseanswer (msg):
def parseask (msg): def parseask (msg):
global ALIAS global ALIAS
if re.match(".*(set|cr[ée]{2}|nouvel(le)?) alias.*", msg.content) is not None: if re.match(".*(set|cr[ée]{2}|nouvel(le)?) alias.*", msg.content) is not None:
result = re.match(".*alias !?([a-zA-Z0-9_-]+) (pour|=|:) (.+)$", msg.content) result = re.match(".*alias !?([^ ]+) (pour|=|:) (.+)$", msg.content)
if result.group(1) in DATAS.getNode("aliases").index or result.group(3).find("alias") >= 0: if result.group(1) in DATAS.getNode("aliases").index or result.group(3).find("alias") >= 0:
msg.send_snd("Cet alias est déjà défini.") msg.send_snd("Cet alias est déjà défini.")
else: else:

View File

@ -1,8 +1,10 @@
# coding=utf-8 # coding=utf-8
import imp
nemubotversion = 3.0 nemubotversion = 3.0
from .DDGSearch import DDGSearch from . import DDGSearch
from . import WFASearch from . import WFASearch
lastSearch = dict() lastSearch = dict()
@ -11,6 +13,10 @@ def load():
global CONF global CONF
WFASearch.CONF = CONF WFASearch.CONF = CONF
def reload():
imp.reload(DDGSearch)
imp.reload(WFASearch)
def parseanswer(msg): def parseanswer(msg):
global lastSearch global lastSearch
req = None req = None
@ -30,11 +36,12 @@ def parseanswer(msg):
if len(msg.cmd) > 1: if len(msg.cmd) > 1:
if req == "wfa": if req == "wfa":
s = WFASearch.WFASearch(' '.join(msg.cmd[1:])) s = WFASearch.WFASearch(' '.join(msg.cmd[1:]))
#print (s.wfares)
if not s.success: if not s.success:
msg.send_chn("An error occurs during computation") msg.send_chn(s.error)
return True return True
else: else:
s = DDGSearch(' '.join(msg.cmd[1:])) s = DDGSearch.DDGSearch(' '.join(msg.cmd[1:]))
if req == "def": if req == "def":
msg.send_chn(s.definition) msg.send_chn(s.definition)

View File

@ -1,5 +1,6 @@
# coding=utf-8 # coding=utf-8
import imp
import re import re
import sys import sys
from datetime import timedelta from datetime import timedelta
@ -7,10 +8,10 @@ from datetime import datetime
import time import time
import threading import threading
from module_state import ModuleState
nemubotversion = 3.0 nemubotversion = 3.0
from module_state import ModuleState
from . import Manager from . import Manager
def help_tiny (): def help_tiny ():
@ -32,6 +33,9 @@ def load():
threadManager = Manager.Manager(DATAS, SRVS) threadManager = Manager.Manager(DATAS, SRVS)
threadManager.start() threadManager.start()
def reload():
imp.reload(Manager)
def close(): def close():
global threadManager global threadManager
if threadManager is not None: if threadManager is not None:

View File

@ -2,6 +2,7 @@
import http.client import http.client
import threading import threading
import re
class Tinyfier(threading.Thread): class Tinyfier(threading.Thread):
def __init__(self, url, msg): def __init__(self, url, msg):

View File

@ -1,10 +1,11 @@
# coding=utf-8 # coding=utf-8
import http.client import http.client
import imp
import re import re
import sys import sys
from .Tinyfier import Tinyfier from . import Tinyfier
nemubotversion = 3.0 nemubotversion = 3.0
@ -13,8 +14,10 @@ def help_tiny ():
return "Gets YCC urls" return "Gets YCC urls"
def help_full (): def help_full ():
return "TODO" return "!ycc [<url>]: with an argument, reduce the given <url> thanks to ycc.fr; without argument, reduce the last URL said on the current channel."
def reload():
imp.reload(Tinyfier)
def parseanswer(msg): def parseanswer(msg):
global LAST_URLS global LAST_URLS
@ -22,14 +25,14 @@ def parseanswer(msg):
if len(msg.cmd) == 1: if len(msg.cmd) == 1:
if msg.channel in LAST_URLS and len(LAST_URLS[msg.channel]) > 0: if msg.channel in LAST_URLS and len(LAST_URLS[msg.channel]) > 0:
url = LAST_URLS[msg.channel].pop() url = LAST_URLS[msg.channel].pop()
t = Tinyfier(url, msg) t = Tinyfier.Tinyfier(url, msg)
t.start() t.start()
else: else:
msg.send_chn("%s: je n'ai pas d'autre URL reduire" % msg.sender) msg.send_chn("%s: je n'ai pas d'autre URL reduire" % msg.sender)
else: else:
if len(msg.cmd) < 6: if len(msg.cmd) < 6:
for url in msg.cmd[1:]: for url in msg.cmd[1:]:
t = Tinyfier(url, msg) t = Tinyfier.Tinyfier(url, msg)
t.start() t.start()
else: else:
msg.send_chn("%s: je ne peux pas réduire autant d'URL d'un seul coup." % msg.sender) msg.send_chn("%s: je ne peux pas réduire autant d'URL d'un seul coup." % msg.sender)

View File

@ -120,6 +120,10 @@ def load_module_from_name(name, servers, config=None):
if md.name == name: if md.name == name:
mod = imp.reload(md) mod = imp.reload(md)
loaded = True loaded = True
try:
mod.reload()
except AttributeError:
pass
break break
if not loaded: if not loaded:
mod = __import__(name) mod = __import__(name)