a3ba5bc22bc75cc447542efd803406f6c446434a
[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     for p in plugins:
38
39         if not p.is_enabled():
40             continue
41
42         f = getattr(p, name, None)
43         if not callable(f):
44             continue
45
46         try:
47             f(*args)
48         except Exception:
49             print_error("Plugin error")
50             traceback.print_exc(file=sys.stdout)
51             
52     return
53
54
55
56 class BasePlugin:
57
58     def __init__(self, gui, name):
59         self.gui = gui
60         self.name = name
61         self.config = gui.config
62
63     def fullname(self):
64         return self.name
65
66     def description(self):
67         return 'undefined'
68
69     def requires_settings(self):
70         return False
71
72     def toggle(self):
73         if self.is_enabled():
74             if self.disable():
75                 self.close()
76         else:
77             if self.enable():
78                 self.init()
79
80         return self.is_enabled()
81
82     
83     def enable(self):
84         self.set_enabled(True)
85         return True
86
87     def disable(self):
88         self.set_enabled(False)
89         return True
90
91     def init(self): pass
92
93     def close(self): pass
94
95     def is_enabled(self):
96         return self.is_available() and self.config.get('use_'+self.name) is True
97
98     def is_available(self):
99         return True
100
101     def set_enabled(self, enabled):
102         self.config.set_key('use_'+self.name, enabled, True)
103
104     def settings_dialog(self):
105         pass