2014-10-09 05:37:52 +00:00
|
|
|
#!/usr/bin/env python3.2
|
2012-08-14 03:51:55 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
2014-10-09 05:37:52 +00:00
|
|
|
# Nemubot is a smart and modulable IM bot.
|
|
|
|
# Copyright (C) 2012-2014 Mercier Pierre-Olivier
|
2012-08-14 03:51:55 +00:00
|
|
|
#
|
|
|
|
# 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/>.
|
2011-12-31 10:14:19 +00:00
|
|
|
|
2012-05-21 10:21:14 +00:00
|
|
|
import imp
|
2014-08-14 10:49:38 +00:00
|
|
|
import logging
|
|
|
|
import os
|
|
|
|
import sys
|
2012-04-09 02:19:39 +00:00
|
|
|
|
2012-08-14 03:51:55 +00:00
|
|
|
import bot
|
|
|
|
import prompt
|
|
|
|
from prompt.builtins import load_file
|
|
|
|
import importer
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
2014-08-14 10:49:38 +00:00
|
|
|
# Setup loggin interface
|
2014-08-27 05:57:00 +00:00
|
|
|
logger = logging.getLogger("nemubot")
|
|
|
|
|
2014-11-09 13:11:54 +00:00
|
|
|
formatter = logging.Formatter(
|
|
|
|
'%(asctime)s %(name)s %(levelname)s %(message)s')
|
2014-08-27 05:57:00 +00:00
|
|
|
|
|
|
|
ch = logging.StreamHandler()
|
|
|
|
ch.setFormatter(formatter)
|
|
|
|
ch.setLevel(logging.INFO)
|
|
|
|
logger.addHandler(ch)
|
|
|
|
|
|
|
|
fh = logging.FileHandler('./nemubot.log')
|
|
|
|
fh.setFormatter(formatter)
|
|
|
|
fh.setLevel(logging.DEBUG)
|
|
|
|
logger.addHandler(fh)
|
2014-08-14 10:49:38 +00:00
|
|
|
|
2012-08-14 03:51:55 +00:00
|
|
|
# Create bot context
|
2014-08-29 09:53:32 +00:00
|
|
|
context = bot.Bot()
|
2012-08-14 03:51:55 +00:00
|
|
|
|
|
|
|
# Load the prompt
|
|
|
|
prmpt = prompt.Prompt()
|
|
|
|
|
|
|
|
# Register the hook for futur import
|
|
|
|
sys.meta_path.append(importer.ModuleFinder(context, prmpt))
|
|
|
|
|
2014-11-09 13:11:54 +00:00
|
|
|
# Add modules dir path
|
2012-08-14 03:51:55 +00:00
|
|
|
if os.path.isdir("./modules/"):
|
|
|
|
context.add_modules_path(
|
|
|
|
os.path.realpath(os.path.abspath("./modules/")))
|
|
|
|
|
|
|
|
# Parse command line arguments
|
|
|
|
if len(sys.argv) >= 2:
|
|
|
|
for arg in sys.argv[1:]:
|
|
|
|
if os.path.isdir(arg):
|
|
|
|
context.add_modules_path(arg)
|
|
|
|
else:
|
|
|
|
load_file(arg, context)
|
|
|
|
|
2014-08-29 09:53:32 +00:00
|
|
|
print ("Nemubot v%s ready, my PID is %i!" % (bot.__version__,
|
2012-08-14 03:51:55 +00:00
|
|
|
os.getpid()))
|
2014-08-30 17:15:14 +00:00
|
|
|
context.start()
|
2012-08-14 03:51:55 +00:00
|
|
|
while prmpt.run(context):
|
|
|
|
try:
|
|
|
|
# Reload context
|
|
|
|
imp.reload(bot)
|
|
|
|
context = bot.hotswap(context)
|
|
|
|
# Reload prompt
|
|
|
|
imp.reload(prompt)
|
|
|
|
prmpt = prompt.hotswap(prmpt)
|
|
|
|
# Reload all other modules
|
|
|
|
bot.reload()
|
2014-08-27 05:57:00 +00:00
|
|
|
print("\033[1;32mContext reloaded\033[0m, now in Nemubot %s" %
|
2014-08-29 09:53:32 +00:00
|
|
|
bot.__version__)
|
2014-09-09 05:02:41 +00:00
|
|
|
context.start()
|
2012-08-14 03:51:55 +00:00
|
|
|
except:
|
2014-11-09 13:11:54 +00:00
|
|
|
logger.exception("\033[1;31mUnable to reload the prompt due to "
|
|
|
|
"errors.\033[0m Fix them before trying to reload "
|
|
|
|
"the prompt.")
|
2012-08-14 03:51:55 +00:00
|
|
|
|
2014-10-23 13:31:38 +00:00
|
|
|
context.quit()
|
|
|
|
print("\nWaiting for other threads shuts down...")
|
2014-07-17 10:02:16 +00:00
|
|
|
|
2012-08-14 03:51:55 +00:00
|
|
|
sys.exit(0)
|