2011-09-25 00:39:00 +00:00
|
|
|
import os
|
2011-11-12 23:58:16 +00:00
|
|
|
from configparser import RawConfigParser
|
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-11-12 23:58:16 +00:00
|
|
|
def __init__(self, filename, module_name):
|
|
|
|
self.file_name = filename
|
|
|
|
self.module_name = module_name
|
|
|
|
RawConfigParser.__init__(self, None)
|
2011-09-25 00:39:00 +00:00
|
|
|
self.read()
|
|
|
|
|
2011-11-13 23:45:15 +00:00
|
|
|
def get(self, option, default, section=None):
|
|
|
|
if not section:
|
|
|
|
section = self.module_name
|
|
|
|
return config.Config.get(self, option, default, section)
|
|
|
|
|
2011-11-14 19:45:59 +00:00
|
|
|
def set(self, option, default, section=None):
|
|
|
|
if not section:
|
|
|
|
section = self.module_name
|
|
|
|
return config.Config.set(self, option, default, section)
|
|
|
|
|
2011-09-25 00:39:00 +00:00
|
|
|
def read(self):
|
|
|
|
"""Read the config file"""
|
2011-11-12 23:58:16 +00:00
|
|
|
RawConfigParser.read(self, self.file_name)
|
|
|
|
if not self.has_section(self.module_name):
|
|
|
|
self.add_section(self.module_name)
|
2011-09-25 00:39:00 +00:00
|
|
|
|
2011-11-15 22:26:24 +00:00
|
|
|
def options(self, section=None):
|
|
|
|
if not section:
|
|
|
|
section = self.module_name
|
|
|
|
if not self.has_section(section):
|
|
|
|
self.add_section(section)
|
|
|
|
return config.Config.options(self, section)
|
|
|
|
|
2011-09-25 00:39:00 +00:00
|
|
|
def write(self):
|
|
|
|
"""Write the config to the disk"""
|
|
|
|
try:
|
2011-11-12 23:58:16 +00:00
|
|
|
fp = open(self.file_name, 'w')
|
|
|
|
RawConfigParser.write(self, fp)
|
2011-09-25 00:39:00 +00:00
|
|
|
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
|
|
|
"""
|
2011-11-09 17:38:56 +00:00
|
|
|
Class that all plugins derive from.
|
2011-09-23 15:43:01 +00:00
|
|
|
"""
|
|
|
|
|
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')
|
2011-11-12 23:58:16 +00:00
|
|
|
self.config = PluginConfig(conf, self.__module__)
|
2011-09-23 15:43:01 +00:00
|
|
|
self.init()
|
|
|
|
|
|
|
|
def init(self):
|
2011-11-09 17:38:56 +00:00
|
|
|
"""
|
|
|
|
Method called at the creation of the plugin.
|
|
|
|
Do not overwrite __init__ and use this instead.
|
|
|
|
"""
|
2011-09-23 15:43:01 +00:00
|
|
|
pass
|
|
|
|
|
|
|
|
def cleanup(self):
|
2011-11-09 17:38:56 +00:00
|
|
|
"""
|
|
|
|
Called when the plugin is unloaded.
|
|
|
|
Overwrite this if you want to erase or save things before the plugin is disabled.
|
|
|
|
"""
|
2011-09-23 15:43:01 +00:00
|
|
|
pass
|
|
|
|
|
|
|
|
def unload(self):
|
|
|
|
self.cleanup()
|
2011-09-24 20:26:31 +00:00
|
|
|
|
|
|
|
def add_command(self, name, handler, help, completion=None):
|
2011-11-09 17:38:56 +00:00
|
|
|
"""
|
|
|
|
Add a global command.
|
|
|
|
You cannot overwrite the existing commands.
|
|
|
|
"""
|
2011-09-24 20:26:31 +00:00
|
|
|
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):
|
2011-11-09 17:38:56 +00:00
|
|
|
"""
|
|
|
|
Remove a global command.
|
|
|
|
This only works if the command was added by the plugin
|
|
|
|
"""
|
2011-10-02 11:21:51 +00:00
|
|
|
return self.plugin_manager.del_command(self.__module__, name)
|
|
|
|
|
2011-11-13 18:43:31 +00:00
|
|
|
def add_key(self, key, handler):
|
|
|
|
"""
|
|
|
|
Add a global keybind
|
|
|
|
"""
|
|
|
|
return self.plugin_manager.add_key(self.__module__, key, handler)
|
|
|
|
|
|
|
|
def del_key(self, key):
|
|
|
|
"""
|
|
|
|
Remove a global keybind
|
|
|
|
"""
|
|
|
|
return self.plugin_manager.del_key(self.__module__, key)
|
|
|
|
|
2011-11-13 19:17:33 +00:00
|
|
|
def add_tab_key(self, tab_type, key, handler):
|
|
|
|
"""
|
|
|
|
Add a keybind only for a type of tab.
|
|
|
|
"""
|
|
|
|
return self.plugin_manager.add_tab_key(self.__module__, tab_type, key, handler)
|
|
|
|
|
|
|
|
def del_tab_key(self, tab_type, key):
|
|
|
|
"""
|
|
|
|
Remove a keybind added through add_tab_key.
|
|
|
|
"""
|
|
|
|
return self.plugin_manager.del_tab_key(self.__module__, tab_type, key)
|
|
|
|
|
2011-11-10 13:39:19 +00:00
|
|
|
def add_tab_command(self, tab_type, name, handler, help, completion=None):
|
|
|
|
"""
|
|
|
|
Add a command only for a type of tab.
|
|
|
|
"""
|
|
|
|
return self.plugin_manager.add_tab_command(self.__module__, tab_type, name, handler, help, completion)
|
|
|
|
|
|
|
|
def del_tab_command(self, tab_type, name):
|
|
|
|
"""
|
|
|
|
Delete a command added through add_tab_command.
|
|
|
|
"""
|
|
|
|
return self.plugin_manager.del_tab_command(self.__module__, tab_type, name)
|
|
|
|
|
2011-11-09 13:02:17 +00:00
|
|
|
def add_event_handler(self, event_name, handler, position=0):
|
2011-11-09 17:38:56 +00:00
|
|
|
"""
|
|
|
|
Add an event handler to the event event_name.
|
|
|
|
An optional position in the event handler list can be provided.
|
|
|
|
"""
|
2011-11-09 13:02:17 +00:00
|
|
|
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):
|
2011-11-09 17:38:56 +00:00
|
|
|
"""
|
|
|
|
Remove 'handler' from the event list for 'event_name'.
|
|
|
|
"""
|
2011-09-24 20:26:31 +00:00
|
|
|
return self.plugin_manager.del_event_handler(self.__module__, event_name, handler)
|