summaryrefslogtreecommitdiff
path: root/src/plugin.py
diff options
context:
space:
mode:
authorFlorent Le Coz <louiz@louiz.org>2011-11-09 01:26:54 +0100
committerFlorent Le Coz <louiz@louiz.org>2011-11-09 01:26:54 +0100
commit75ae1772e49a59b373c26d1c942f25edd473921c (patch)
tree65d8faee081db6c3800a48eff7007a4b626e4252 /src/plugin.py
parent8f02b6eb8e022ed1d34e0a0b0c7376eaf58acc32 (diff)
parent23d81901e7725bf67363113bc36a6e171bdb2033 (diff)
downloadpoezio-75ae1772e49a59b373c26d1c942f25edd473921c.tar.gz
poezio-75ae1772e49a59b373c26d1c942f25edd473921c.tar.bz2
poezio-75ae1772e49a59b373c26d1c942f25edd473921c.tar.xz
poezio-75ae1772e49a59b373c26d1c942f25edd473921c.zip
Merge branch 'master' of http://git.louiz.org/poezio
Diffstat (limited to 'src/plugin.py')
-rw-r--r--src/plugin.py92
1 files changed, 92 insertions, 0 deletions
diff --git a/src/plugin.py b/src/plugin.py
new file mode 100644
index 00000000..80bc4dfc
--- /dev/null
+++ b/src/plugin.py
@@ -0,0 +1,92 @@
+import os
+from configparser import ConfigParser
+import config
+import inspect
+import traceback
+
+class PluginConfig(config.Config):
+ 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
+
+
+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):
+ """
+ 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
+ """
+
+ def __init__(self, plugin_manager, core, plugins_conf_dir):
+ self.core = core
+ # More hack; luckily we'll never have more than one core object
+ SafetyMetaclass.core = core
+ self.plugin_manager = plugin_manager
+ conf = os.path.join(plugins_conf_dir, self.__module__+'.cfg')
+ self.config = PluginConfig(conf)
+ self.init()
+
+ def init(self):
+ pass
+
+ def cleanup(self):
+ pass
+
+ def unload(self):
+ self.cleanup()
+
+ def add_command(self, name, handler, help, completion=None):
+ return self.plugin_manager.add_command(self.__module__, name, handler, help, completion)
+
+ def del_command(self, name):
+ return self.plugin_manager.del_command(self.__module__, name)
+
+ def add_event_handler(self, event_name, handler):
+ return self.plugin_manager.add_event_handler(self.__module__, event_name, handler)
+
+ def del_event_handler(self, event_name, handler):
+ return self.plugin_manager.del_event_handler(self.__module__, event_name, handler)
+
+ def add_poezio_event_handler(self, event_name, handler, position=0):
+ return self.plugin_manager.add_poezio_event_handler(self.__module__, event_name, handler, position)
+
+ def del_poezio_event_handler(self, event_name, handler):
+ return self.plugin_manager.del_poezio_event_handler(self.__module__, event_name, handler)