Use a new way to reload modules

This commit is contained in:
Némunaire 2012-08-04 19:10:34 +02:00
parent a6b69aa672
commit afdf951758
8 changed files with 191 additions and 57 deletions

24
DCC.py
View File

@ -1,14 +1,28 @@
import imp # -*- coding: utf-8 -*-
# Nemubot is a modulable IRC bot, built around XML configuration files.
# Copyright (C) 2012 Mercier Pierre-Olivier
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
import os import os
import re import re
import socket import socket
import sys
import threading import threading
import time import time
import traceback
message = __import__("message") import message
imp.reload(message)
#Store all used ports #Store all used ports
PORTS = list() PORTS = list()

View File

@ -1,18 +1,30 @@
# coding=utf-8 # -*- coding: utf-8 -*-
# Nemubot is a modulable IRC bot, built around XML configuration files.
# Copyright (C) 2012 Mercier Pierre-Olivier
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
from datetime import datetime from datetime import datetime
from datetime import timedelta
import imp
import re import re
import shlex import shlex
import string
import sys
import time import time
from credits import Credits
import credits import credits
dcc = __import__("DCC") from credits import Credits
imp.reload(dcc) import DCC
import module_states_file as xmlparser
CREDITS = {} CREDITS = {}
filename = "" filename = ""

View File

@ -1,8 +1,23 @@
# coding=utf-8 # -*- coding: utf-8 -*-
# Nemubot is a modulable IRC bot, built around XML configuration files.
# Copyright (C) 2012 Mercier Pierre-Olivier
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
import xml.sax import xml.sax
from datetime import datetime from datetime import datetime
from datetime import date
import time import time
class ModuleState: class ModuleState:

24
module_states_file.py Normal file → Executable file
View File

@ -1,12 +1,24 @@
#!/usr/bin/python3 # -*- coding: utf-8 -*-
# coding=utf-8
# Nemubot is a modulable IRC bot, built around XML configuration files.
# Copyright (C) 2012 Mercier Pierre-Olivier
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
import os
import imp
import xml.sax import xml.sax
module_state = __import__("module_state") import module_state
imp.reload(module_state)
class ModuleStatesFile(xml.sax.ContentHandler): class ModuleStatesFile(xml.sax.ContentHandler):
def startDocument(self): def startDocument(self):

View File

@ -1,14 +1,31 @@
#!/usr/bin/python3 #!/usr/bin/python3
# coding=utf-8 # -*- coding: utf-8 -*-
# Nemubot is a modulable IRC bot, built around XML configuration files.
# Copyright (C) 2012 Mercier Pierre-Olivier
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
import sys import sys
import os import os
import imp import imp
import traceback import traceback
servers = dict() import reloader
import prompt
prompt = __import__ ("prompt") servers = dict()
#Add modules dir path #Add modules dir path
if os.path.isdir("./modules/"): if os.path.isdir("./modules/"):
@ -26,17 +43,18 @@ if len(sys.argv) >= 2:
print ("Nemubot ready, my PID is %i!" % (os.getpid())) print ("Nemubot ready, my PID is %i!" % (os.getpid()))
while prompt.launch(servers): while prompt.launch(servers):
try: try:
if prompt.MODS is None: imp.reload(reloader)
imp.reload(prompt) if prompt.MODS is None:
else: reloader.reload()
mods = prompt.MODS else:
imp.reload(prompt) mods = prompt.MODS
prompt.MODS = mods reloader.reload()
except: prompt.MODS = mods
print ("Unable to reload the prompt due to errors. Fix them before trying to reload the prompt.") except:
exc_type, exc_value, exc_traceback = sys.exc_info() print ("Unable to reload the prompt due to errors. Fix them before trying to reload the prompt.")
sys.stdout.write (traceback.format_exception_only(exc_type, exc_value)[0]) exc_type, exc_value, exc_traceback = sys.exc_info()
sys.stdout.write (traceback.format_exception_only(exc_type, exc_value)[0])
print ("Bye") print ("Bye")
sys.exit(0) sys.exit(0)

38
prompt.py Normal file → Executable file
View File

@ -1,17 +1,29 @@
# -*- coding: utf-8 -*-
# Nemubot is a modulable IRC bot, built around XML configuration files.
# Copyright (C) 2012 Mercier Pierre-Olivier
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
import imp import imp
import os import os
import shlex import shlex
import sys import sys
import traceback import traceback
server = __import__("server") import server
imp.reload(server) import module_states_file as xmlparser
xmlparser = __import__("module_states_file")
imp.reload(xmlparser)
server.xmlparser = xmlparser
server.message.xmlparser = xmlparser
xmlparser.module_state.xmlparser = xmlparser
selectedServer = None selectedServer = None
modules_path = "./modules/" modules_path = "./modules/"
@ -47,7 +59,7 @@ def getPS1():
return selectedServer.id return selectedServer.id
def launch(servers): def launch(servers):
"""Launch the prompt""" """Launch the prompt and readline"""
global MODS global MODS
if MODS is None: if MODS is None:
MODS = list() MODS = list()
@ -129,10 +141,8 @@ 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: if hasattr(mod, 'reload'):
mod.reload() mod.reload()
except AttributeError:
pass
break break
if not loaded: if not loaded:
mod = __import__(name) mod = __import__(name)
@ -140,7 +150,7 @@ def load_module_from_name(name, servers, config=None):
try: try:
if mod.nemubotversion < 3.0: if mod.nemubotversion < 3.0:
print (" Module `%s' is not compatible with this version." % name) print (" Module `%s' is not compatible with this version." % name)
return return false
#Set module common functions and datas #Set module common functions and datas
mod.name = name mod.name = name
@ -328,7 +338,7 @@ def connect(cmds, servers):
servers[s].launch(MODS) servers[s].launch(MODS)
else: else:
print ("connect: server `%s' not found." % s) print ("connect: server `%s' not found." % s)
elif selectedServer is not None: elif selectedServer is not None:
selectedServer.launch(MODS) selectedServer.launch(MODS)
else: else:

39
reloader.py Normal file
View File

@ -0,0 +1,39 @@
# -*- coding: utf-8 -*-
# Nemubot is a modulable IRC bot, built around XML configuration files.
# Copyright (C) 2012 Mercier Pierre-Olivier
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
import imp
import credits
import channel
import DCC
import message
import module_state
import module_states_file
import prompt
import server
def reload():
imp.reload(credits)
imp.reload(channel)
imp.reload(DCC)
imp.reload(message)
imp.reload(module_state)
imp.reload(module_states_file)
imp.reload(prompt)
imp.reload(server)

View File

@ -1,16 +1,30 @@
import imp # -*- coding: utf-8 -*-
# Nemubot is a modulable IRC bot, built around XML configuration files.
# Copyright (C) 2012 Mercier Pierre-Olivier
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
import socket import socket
import sys import sys
import threading import threading
import traceback import traceback
import time
message = __import__("message") import channel
imp.reload(message) import DCC
channel = __import__("channel") import message
imp.reload(channel) import module_states_file as xmlparser
dcc = __import__("DCC")
imp.reload(dcc)
class Server(threading.Thread): class Server(threading.Thread):
def __init__(self, node, nick, owner, realname, socket = None): def __init__(self, node, nick, owner, realname, socket = None):