2011-09-25 00:39:00 +00:00
|
|
|
import os
|
|
|
|
from configparser import ConfigParser
|
2011-10-29 05:04:32 +00:00
|
|
|
import config
|
2011-10-02 07:09:50 +00:00
|
|
|
import inspect
|
|
|
|
import traceback
|
2011-09-25 00:39:00 +00:00
|
|
|
|
2011-10-29 05:04:32 +00:00
|
|
|
class PluginConfig(config.Config):
|
2011-09-25 00:39:00 +00:00
|
|
|
def __init__(self, filename):
|
|
|
|
ConfigParser.__init__(self)
|
|
|
|
self.__config_file__ = filename
|
|
|
|
self.read()
|
|
|
|
|
|
|
|
def read(self):
|
|
|
|
"""Read the config file"""
|
|
|
|
ConfigParser.read(self, self.__config_file__)
|
|
|
|
|
|
|
|
def write(self):
|
|
|
|
"""Write the config to the disk"""
|
|
|
|
try:
|
|
|
|
fp = open(self.__config_file__, 'w')
|
|
|
|
ConfigParser.write(self, fp)
|
|
|
|
fp.close()
|
|
|
|
return True
|
|
|
|
except IOError:
|
|
|
|
return False
|
|
|
|
|
2011-10-29 05:04:32 +00:00
|
|
|
|
2011-10-02 07:09:50 +00:00
|
|
|
class SafetyMetaclass(type):
|
|
|
|
# A hack
|
|
|
|
core = None
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def safe_func(f):
|
|
|
|
def helper(*args, **kwargs):
|
|
|
|
try:
|
|
|
|
return f(*args, **kwargs)
|
|
|
|
except:
|
|
|
|
if inspect.stack()[1][1] == inspect.getfile(f):
|
|
|
|
raise
|
|
|
|
elif SafetyMetaclass.core:
|
|
|
|
SafetyMetaclass.core.information(traceback.format_exc())
|
|
|
|
return None
|
|
|
|
return helper
|
|
|
|
|
|
|
|
def __new__(meta, name, bases, class_dict):
|
|
|
|
for k, v in class_dict.items():
|
|
|
|
if inspect.isfunction(v):
|
|
|
|
class_dict[k] = SafetyMetaclass.safe_func(v)
|
|
|
|
return type.__new__(meta, name, bases, class_dict)
|
|
|
|
|
|
|
|
class BasePlugin(object, metaclass=SafetyMetaclass):
|
2011-09-23 15:43:01 +00:00
|
|
|
"""
|
|
|
|
Class that all plugins derive from. Any methods beginning with command_
|
|
|
|
are interpreted as a command and beginning with on_ are interpreted as
|
|
|
|
event handlers
|
|
|
|
"""
|
|
|
|
|
2011-09-25 00:39:00 +00:00
|
|
|
def __init__(self, plugin_manager, core, plugins_conf_dir):
|
2011-09-23 15:43:01 +00:00
|
|
|
self.core = core
|
2011-10-02 07:09:50 +00:00
|
|
|
# More hack; luckily we'll never have more than one core object
|
|
|
|
SafetyMetaclass.core = core
|
2011-09-24 20:26:31 +00:00
|
|
|
self.plugin_manager = plugin_manager
|
2011-09-25 00:39:00 +00:00
|
|
|
conf = os.path.join(plugins_conf_dir, self.__module__+'.cfg')
|
|
|
|
self.config = PluginConfig(conf)
|
2011-09-23 15:43:01 +00:00
|
|
|
self.init()
|
|
|
|
|
|
|
|
def init(self):
|
|
|
|
pass
|
|
|
|
|
|
|
|
def cleanup(self):
|
|
|
|
pass
|
|
|
|
|
|
|
|
def unload(self):
|
|
|
|
self.cleanup()
|
2011-09-24 20:26:31 +00:00
|
|
|
|
|
|
|
def add_command(self, name, handler, help, completion=None):
|
|
|
|
return self.plugin_manager.add_command(self.__module__, name, handler, help, completion)
|
|
|
|
|
2011-10-02 11:21:51 +00:00
|
|
|
def del_command(self, name):
|
|
|
|
return self.plugin_manager.del_command(self.__module__, name)
|
|
|
|
|
2011-11-09 13:02:17 +00:00
|
|
|
def add_event_handler(self, event_name, handler, position=0):
|
|
|
|
return self.plugin_manager.add_event_handler(self.__module__, event_name, handler, position)
|
2011-09-24 20:26:31 +00:00
|
|
|
|
|
|
|
def del_event_handler(self, event_name, handler):
|
|
|
|
return self.plugin_manager.del_event_handler(self.__module__, event_name, handler)
|