restrict plugins to the gui
authorthomasv <thomasv@gitorious>
Sun, 3 Mar 2013 15:01:47 +0000 (16:01 +0100)
committerthomasv <thomasv@gitorious>
Sun, 3 Mar 2013 15:01:47 +0000 (16:01 +0100)
electrum
gui/gui_classic.py
lib/wallet.py
plugins/pointofsale.py
plugins/qrscanner.py

index f9b795f..a3217ca 100755 (executable)
--- a/electrum
+++ b/electrum
@@ -41,12 +41,6 @@ if os.path.exists("lib"):
     imp.load_module('electrum', fp, pathname, description)
     fp, pathname, description = imp.find_module('gui')
     imp.load_module('electrum_gui', fp, pathname, description)
-    fp, pathname, description = imp.find_module('plugins')
-    imp.load_module('electrum_plugins', fp, pathname, description)
-    plugin_names = [name for _, name, _ in pkgutil.iter_modules(['plugins'])]
-    plugins = map(lambda name: imp.load_source('electrum_plugins.'+name, os.path.join(pathname,name+'.py')), plugin_names)
-else:
-    plugins = []
 
 
 from electrum import *
@@ -109,7 +103,6 @@ if __name__ == '__main__':
 
     config = SimpleConfig(config_options)
     wallet = Wallet(config)
-    wallet.init_plugins(plugins)
 
 
     if len(args)==0:
index ff58063..831b326 100644 (file)
@@ -269,6 +269,8 @@ class ElectrumWindow(QMainWindow):
         self.lite = None
         self.wallet = wallet
         self.config = config
+        self.init_plugins()
+
         self.wallet.interface.register_callback('updated', self.update_callback)
         self.wallet.interface.register_callback('banner', lambda: self.emit(QtCore.SIGNAL('banner_signal')) )
         self.wallet.interface.register_callback('disconnected', self.update_callback)
@@ -321,25 +323,51 @@ class ElectrumWindow(QMainWindow):
         # set initial message
         self.console.showMessage(self.wallet.banner)
 
-        #init plugins
-        for p in self.wallet.plugins:
+
+    # plugins
+    def init_plugins(self):
+        if os.path.exists("plugins"):
+            import imp, pkgutil
+            fp, pathname, description = imp.find_module('plugins')
+            imp.load_module('electrum_plugins', fp, pathname, description)
+            plugin_names = [name for a, name, b in pkgutil.iter_modules(['plugins'])]
+            self.plugins = map(lambda name: imp.load_source('electrum_plugins.'+name, os.path.join(pathname,name+'.py')), plugin_names)
+        else:
+            self.plugins = []
+
+        self.plugin_hooks = {}
+        for p in self.plugins:
             try:
-                p.init_gui(self)
+                p.init(self)
             except:
                 print_msg("Error:cannot initialize plugin",p)
                 traceback.print_exc(file=sys.stdout)
 
+    def set_hook(self, name, callback):
+        h = self.plugin_hooks.get(name, [])
+        h.append(callback)
+        self.plugin_hooks[name] = h
+
+    def unset_hook(self, name, callback):
+        h = self.plugin_hooks.get(name,[])
+        if callback in h: h.remove(callback)
+        self.plugin_hooks[name] = h
+
+    def run_hook(self, name, args):
+        for cb in self.plugin_hooks.get(name,[]):
+            apply(cb, args)
+
 
     def close(self):
         QMainWindow.close(self)
-        self.wallet.run_hook('close_main_window', (self,))
+        self.run_hook('close_main_window', (self,))
 
     def connect_slots(self, sender):
         self.connect(sender, QtCore.SIGNAL('timersignal'), self.timer_actions)
         self.previous_payto_e=''
 
     def timer_actions(self):
-        self.wallet.run_hook('timer_actions', (self,))
+        self.run_hook('timer_actions', (self,))
             
         if self.payto_e.hasFocus():
             return
@@ -547,11 +575,11 @@ class ElectrumWindow(QMainWindow):
                 
             self.current_item_changed(item)
 
-        self.wallet.run_hook('item_changed',(self, item, column))
+        self.run_hook('item_changed',(self, item, column))
 
 
     def current_item_changed(self, a):
-        self.wallet.run_hook('current_item_changed',(self, a))
+        self.run_hook('current_item_changed',(self, a))
 
 
 
@@ -697,7 +725,7 @@ class ElectrumWindow(QMainWindow):
         self.amount_e.textChanged.connect(lambda: entry_changed(False) )
         self.fee_e.textChanged.connect(lambda: entry_changed(True) )
 
-        self.wallet.run_hook('create_send_tab',(self,grid))
+        self.run_hook('create_send_tab',(self,grid))
         return w2
 
 
@@ -760,7 +788,7 @@ class ElectrumWindow(QMainWindow):
             self.show_message(str(e))
             return
 
-        self.wallet.run_hook('send_tx', (wallet, self, tx))
+        self.run_hook('send_tx', (wallet, self, tx))
 
         if label: 
             self.wallet.labels[tx.hash()] = label
@@ -953,7 +981,7 @@ class ElectrumWindow(QMainWindow):
             t = _("Unprioritize") if addr in self.wallet.prioritized_addresses else _("Prioritize")
             menu.addAction(t, lambda: self.toggle_priority(addr))
             
-        self.wallet.run_hook('receive_menu', (self, menu,))
+        self.run_hook('receive_menu', (self, menu,))
         menu.exec_(self.receive_list.viewport().mapToGlobal(position))
 
 
@@ -1010,7 +1038,7 @@ class ElectrumWindow(QMainWindow):
         label = self.wallet.labels.get(address,'')
         item.setData(1,0,label)
 
-        self.wallet.run_hook('update_receive_item', (self, address, item))
+        self.run_hook('update_receive_item', (self, address, item))
                 
         c, u = self.wallet.get_addr_balance(address)
         balance = format_satoshis( c + u, False, self.wallet.num_zeros )
@@ -2019,7 +2047,7 @@ class ElectrumWindow(QMainWindow):
         tabs.addTab(tab5, _('Plugins') )
         def mk_toggle(cb, p):
             return lambda: cb.setChecked(p.toggle(self))
-        for i, p in enumerate(self.wallet.plugins):
+        for i, p in enumerate(self.plugins):
             try:
                 name, description = p.get_info()
                 cb = QCheckBox(name)
index 1ef67dd..4b5fb6f 100644 (file)
@@ -106,10 +106,6 @@ class Wallet:
         except:
             print_msg("Warning: Cannot deserialize transactions. skipping")
         
-        # plugins
-        self.plugins = []
-        self.plugin_hooks = {}
-
         # not saved
         self.prevout_values = {}     # my own transaction outputs
         self.spent_outputs = []
@@ -134,32 +130,6 @@ class Wallet:
             self.update_tx_outputs(tx_hash)
 
 
-    # plugins 
-    def set_hook(self, name, callback):
-        h = self.plugin_hooks.get(name, [])
-        h.append(callback)
-        self.plugin_hooks[name] = h
-
-    def unset_hook(self, name, callback):
-        h = self.plugin_hooks.get(name,[])
-        if callback in h: h.remove(callback)
-        self.plugin_hooks[name] = h
-
-    def run_hook(self, name, args):
-        for cb in self.plugin_hooks.get(name,[]):
-            apply(cb, args)
-
-    def init_plugins(self, plugins):
-        self.plugins = plugins
-        for p in plugins:
-            try:
-                p.init(self)
-            except:
-                import traceback
-                print_msg("Error:cannot initialize plugin",p)
-                traceback.print_exc(file=sys.stdout)
-
-
     def set_up_to_date(self,b):
         with self.lock: self.up_to_date = b
 
index 3b87f8b..228dabc 100644 (file)
@@ -96,11 +96,8 @@ class QR_Window(QWidget):
 def get_info():
     return 'Point of Sale', _('Show QR code window and amounts requested for each address. Add menu item to request amount.')
 
-def init(wallet):
-    wallet.requested_amounts = wallet.config.get('requested_amounts',{}) 
-
-
-def init_gui(gui):
+def init(gui):
+    gui.requested_amounts = gui.config.get('requested_amounts',{}) 
     gui.qr_window = None
 
 
@@ -116,19 +113,19 @@ def toggle(gui):
     toggle_QR_window(gui, enabled)
 
     if enabled:
-        gui.wallet.set_hook('item_changed', item_changed)
-        gui.wallet.set_hook('current_item_changed', recv_changed)
-        gui.wallet.set_hook('receive_menu', receive_menu)
-        gui.wallet.set_hook('update_receive_item', update_receive_item)
-        gui.wallet.set_hook('timer_actions', timer_actions)
-        gui.wallet.set_hook('close_main_window', close_main_window)
+        gui.set_hook('item_changed', item_changed)
+        gui.set_hook('current_item_changed', recv_changed)
+        gui.set_hook('receive_menu', receive_menu)
+        gui.set_hook('update_receive_item', update_receive_item)
+        gui.set_hook('timer_actions', timer_actions)
+        gui.set_hook('close_main_window', close_main_window)
     else:
-        gui.wallet.unset_hook('item_changed', item_changed)
-        gui.wallet.unset_hook('current_item_changed', recv_changed)
-        gui.wallet.unset_hook('receive_menu', receive_menu)
-        gui.wallet.unset_hook('update_receive_item', update_receive_item)
-        gui.wallet.unset_hook('timer_actions', timer_actions)
-        gui.wallet.unset_hook('close_main_window', close_main_window)
+        gui.unset_hook('item_changed', item_changed)
+        gui.unset_hook('current_item_changed', recv_changed)
+        gui.unset_hook('receive_menu', receive_menu)
+        gui.unset_hook('update_receive_item', update_receive_item)
+        gui.unset_hook('timer_actions', timer_actions)
+        gui.unset_hook('close_main_window', close_main_window)
         
 
     return enabled
@@ -143,7 +140,7 @@ def toggle_QR_window(self, show):
         if item:
             address = str(item.text(1))
             label = self.wallet.labels.get(address)
-            amount, currency = self.wallet.requested_amounts.get(address, (None, None))
+            amount, currency = self.requested_amounts.get(address, (None, None))
             self.qr_window.set_content( address, label, amount, currency )
 
     elif show and self.qr_window and not self.qr_window.isVisible():
@@ -182,8 +179,8 @@ def item_changed(self, item, column):
             else:
                 currency = currency.upper()
                     
-            self.wallet.requested_amounts[address] = (amount, currency)
-            self.wallet.config.set_key('requested_amounts', self.wallet.requested_amounts, True)
+            self.requested_amounts[address] = (amount, currency)
+            self.wallet.config.set_key('requested_amounts', self.requested_amounts, True)
 
             label = self.wallet.labels.get(address)
             if label is None:
@@ -195,8 +192,8 @@ def item_changed(self, item, column):
 
         else:
             item.setText(column,'')
-            if address in self.wallet.requested_amounts:
-                self.wallet.requested_amounts.pop(address)
+            if address in self.requested_amounts:
+                self.requested_amounts.pop(address)
             
         self.update_receive_item(self.receive_list.currentItem())
 
@@ -206,7 +203,7 @@ def recv_changed(self, a):
         address = str(a.text(0))
         label = self.wallet.labels.get(address)
         try:
-            amount, currency = self.wallet.requested_amounts.get(address, (None, None))
+            amount, currency = self.requested_amounts.get(address, (None, None))
         except:
             amount, currency = None, None
         self.qr_window.set_content( address, label, amount, currency )
@@ -226,9 +223,9 @@ def receive_menu(self, menu):
 
 def update_receive_item(self, address, item):
     try:
-        amount, currency = self.wallet.requested_amounts.get(address, (None, None))
+        amount, currency = self.requested_amounts.get(address, (None, None))
     except:
-        print "cannot get requested amount", address, self.wallet.requested_amounts.get(address)
+        print "cannot get requested amount", address, self.requested_amounts.get(address)
         amount, currency = None, None
             
     amount_str = amount + (' ' + currency if currency else '') if amount is not None  else ''
index 2604590..b13e0d5 100644 (file)
@@ -7,14 +7,12 @@ except ImportError:
     zbar = None
 
 
-def init(wallet):
-    pass
 
-def init_gui(gui):
+def init(gui):
     if is_enabled():
-        gui.wallet.set_hook('create_send_tab', create_send_tab)
+        gui.set_hook('create_send_tab', create_send_tab)
     else:
-        gui.wallet.unset_hook('create_send_tab', create_send_tab)
+        gui.unset_hook('create_send_tab', create_send_tab)
 
 def get_info():
     return 'QR scans', "QR Scans.\nInstall the zbar package to enable this plugin"