New port numbers
[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_nvc_plugins', fp, pathname, description)
18         plugin_modules = map(lambda name: imp.load_source('electrum_nvc_plugins.'+name, os.path.join(pathname,name+'.py')), plugin_names)
19     else:
20         import electrum_nvc_plugins
21         plugin_names = [name for a, name, b in pkgutil.iter_modules(electrum_nvc_plugins.__path__)]
22         plugin_modules = [ __import__('electrum_nvc_plugins.'+name, fromlist=['electrum_nvc_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         if r:
55             results.append(r)
56
57     if results:
58         assert len(results) == 1, results
59         return results[0]
60
61
62
63 class BasePlugin:
64
65     def __init__(self, gui, name):
66         self.gui = gui
67         self.name = name
68         self.config = gui.config
69
70     def fullname(self):
71         return self.name
72
73     def description(self):
74         return 'undefined'
75
76     def requires_settings(self):
77         return False
78
79     def toggle(self):
80         if self.is_enabled():
81             if self.disable():
82                 self.close()
83         else:
84             if self.enable():
85                 self.init()
86
87         return self.is_enabled()
88
89     
90     def enable(self):
91         self.set_enabled(True)
92         return True
93
94     def disable(self):
95         self.set_enabled(False)
96         return True
97
98     def init(self): pass
99
100     def close(self): pass
101
102     def is_enabled(self):
103         return self.is_available() and self.config.get('use_'+self.name) is True
104
105     def is_available(self):
106         return True
107
108     def set_enabled(self, enabled):
109         self.config.set_key('use_'+self.name, enabled, True)
110
111     def settings_dialog(self):
112         pass