ff4a2462e69be60b7389d12b5887efdcbd7dee54
[electrum-nvc.git] / lib / plugins.py
1 from util import print_error
2 import traceback, sys
3 from util import *
4 from i18n import _
5
6 plugins = []
7
8
9 def init_plugins(self):
10     import imp, pkgutil, __builtin__, os
11     global plugins
12
13     if __builtin__.use_local_modules:
14         fp, pathname, description = imp.find_module('plugins')
15         plugin_names = [name for a, name, b in pkgutil.iter_modules([pathname])]
16         plugin_names = filter( lambda name: os.path.exists(os.path.join(pathname,name+'.py')), plugin_names)
17         imp.load_module('electrum_plugins', fp, pathname, description)
18         plugin_modules = map(lambda name: imp.load_source('electrum_plugins.'+name, os.path.join(pathname,name+'.py')), plugin_names)
19     else:
20         import electrum_plugins
21         plugin_names = [name for a, name, b in pkgutil.iter_modules(electrum_plugins.__path__)]
22         plugin_modules = [ __import__('electrum_plugins.'+name, fromlist=['electrum_plugins']) for name in plugin_names]
23
24     for name, p in zip(plugin_names, plugin_modules):
25         try:
26             plugins.append( p.Plugin(self, name) )
27         except Exception:
28             print_msg(_("Error: cannot initialize plugin"),p)
29             traceback.print_exc(file=sys.stdout)
30
31
32
33 def run_hook(name, *args):
34     
35     global plugins
36
37     results = []
38
39     for p in plugins:
40
41         if not p.is_enabled():
42             continue
43
44         f = getattr(p, name, None)
45         if not callable(f):
46             continue
47
48         try:
49             r = f(*args)
50         except Exception:
51             print_error("Plugin error")
52             traceback.print_exc(file=sys.stdout)
53
54         results.append((p.name,r))
55
56     return results
57
58
59
60 class BasePlugin:
61
62     def __init__(self, gui, name):
63         self.gui = gui
64         self.name = name
65         self.config = gui.config
66
67     def fullname(self):
68         return self.name
69
70     def description(self):
71         return 'undefined'
72
73     def requires_settings(self):
74         return False
75
76     def toggle(self):
77         if self.is_enabled():
78             if self.disable():
79                 self.close()
80         else:
81             if self.enable():
82                 self.init()
83
84         return self.is_enabled()
85
86     
87     def enable(self):
88         self.set_enabled(True)
89         return True
90
91     def disable(self):
92         self.set_enabled(False)
93         return True
94
95     def init(self): pass
96
97     def close(self): pass
98
99     def is_enabled(self):
100         return self.is_available() and self.config.get('use_'+self.name) is True
101
102     def is_available(self):
103         return True
104
105     def set_enabled(self, enabled):
106         self.config.set_key('use_'+self.name, enabled, True)
107
108     def settings_dialog(self):
109         pass