diff options
author | Todd Eisenberger <todd@teisen.be> | 2011-10-02 00:09:50 -0700 |
---|---|---|
committer | Todd Eisenberger <todd@teisen.be> | 2011-10-02 00:09:50 -0700 |
commit | b7279678df346488c8a0454b7d5d372d2236ce65 (patch) | |
tree | 6ab006548292e7176a8f52b5db5a7fdefbd446ae | |
parent | ed87f26db763432505072eb5a2875f30fc4061d1 (diff) | |
download | poezio-b7279678df346488c8a0454b7d5d372d2236ce65.tar.gz poezio-b7279678df346488c8a0454b7d5d372d2236ce65.tar.bz2 poezio-b7279678df346488c8a0454b7d5d372d2236ce65.tar.xz poezio-b7279678df346488c8a0454b7d5d372d2236ce65.zip |
Frumious hacks to make plugins less likely to kill a client
-rw-r--r-- | src/plugin.py | 29 |
1 files changed, 28 insertions, 1 deletions
diff --git a/src/plugin.py b/src/plugin.py index d332ca01..a5aae1f6 100644 --- a/src/plugin.py +++ b/src/plugin.py @@ -1,5 +1,7 @@ import os from configparser import ConfigParser +import inspect +import traceback class PluginConfig(ConfigParser): def __init__(self, filename): @@ -21,7 +23,30 @@ class PluginConfig(ConfigParser): except IOError: return False -class BasePlugin(object): +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 @@ -30,6 +55,8 @@ class BasePlugin(object): 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) |