plugin handler
authorthomasv <thomasv@gitorious>
Sat, 2 Mar 2013 15:29:14 +0000 (16:29 +0100)
committerthomasv <thomasv@gitorious>
Sat, 2 Mar 2013 15:29:14 +0000 (16:29 +0100)
electrum
gui/gui_qt.py
gui/qt_console.py
lib/wallet.py

index 36f2166..3df586c 100755 (executable)
--- a/electrum
+++ b/electrum
@@ -17,6 +17,7 @@
 # along with this program. If not, see <http://www.gnu.org/licenses/>.
 
 import re
+import pkgutil
 import sys, os, time, json
 import optparse
 import platform
@@ -40,6 +41,13 @@ if os.path.exists("lib"):
     imp.load_module('electrum', fp, pathname, description)
     fp, pathname, description = imp.find_module('gui')
     imp.load_module('electrumGUI', 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 *
 
@@ -68,7 +76,6 @@ def arg_parser():
     parser.add_option("-a", "--all", action="store_true", dest="show_all", default=False, help="show all addresses")
     parser.add_option("-b", "--balance", action="store_true", dest="show_balance", default=False, help="show the balance of listed addresses")
     parser.add_option("-l", "--labels", action="store_true", dest="show_labels", default=False, help="show the labels of listed addresses")
-
     parser.add_option("-f", "--fee", dest="tx_fee", default="0.005", help="set tx fee")
     parser.add_option("-F", "--fromaddr", dest="from_addr", default=None, help="set source address for payto/mktx. if it isn't in the wallet, it will ask for the private key unless supplied in the format public_key:private_key. It's not saved in the wallet.")
     parser.add_option("-c", "--changeaddr", dest="change_addr", default=None, help="set the change address for payto/mktx. default is a spare address, or the source address if it's not in the wallet")
@@ -102,6 +109,8 @@ if __name__ == '__main__':
 
     config = SimpleConfig(config_options)
     wallet = Wallet(config)
+    wallet.init_plugins(plugins)
+
 
     if len(args)==0:
         url = None
index 1d3fa5e..a123910 100644 (file)
@@ -946,6 +946,11 @@ class ElectrumWindow(QMainWindow):
             self.show_message(str(e))
             return
 
+
+        for cb in self.wallet.plugin_hooks.get('send_tx'):
+            apply(cb, (wallet, self, tx))
+
+
         if label: 
             self.wallet.labels[tx.hash()] = label
 
@@ -1325,6 +1330,16 @@ class ElectrumWindow(QMainWindow):
         self.console = console = Console()
         self.console.history = self.config.get("console-history",[])
         self.console.history_index = len(self.console.history)
+
+        #init plugins
+        for p in self.wallet.plugins:
+            try:
+                p.init_console(self.console, self)
+            except:
+                import traceback
+                print_msg("Error:cannot initialize plugin",p)
+                traceback.print_exc(file=sys.stdout)
+
         
         console.updateNamespace({'wallet' : self.wallet, 'interface' : self.wallet.interface, 'gui':self})
         console.updateNamespace({'util' : util, 'bitcoin':bitcoin})
@@ -2543,3 +2558,5 @@ class ElectrumGui:
         w.show()
 
         self.app.exec_()
+
+
index 5bf15ad..c61251c 100644 (file)
@@ -158,6 +158,11 @@ class Console(QtGui.QPlainTextEdit):
         for i in range(len(self.prompt) + position):
             self.moveCursor(QtGui.QTextCursor.Right)
 
+    def register_command(self, c, func):
+        methods = { c: func}
+        self.updateNamespace(methods)
+        
+
     def runCommand(self):
         command = self.getCommand()
         self.addToHistory(command)
index b127909..958a911 100644 (file)
@@ -95,12 +95,10 @@ class Wallet:
         self.accounts              = config.get('accounts', {})   # this should not include public keys
         self.sequences = {}
 
-        mpk1 = self.config.get('master_public_key')
-        self.sequences[0] = DeterministicSequence(mpk1)
+        self.sequences[0] = DeterministicSequence(self.config.get('master_public_key'))
         if self.accounts.get(0) is None:
             self.accounts[0] = { 0:[], 1:[], 'name':'Main account' }
 
-
         self.transactions = {}
         tx = config.get('transactions',{})
         try:
@@ -108,7 +106,9 @@ 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
@@ -134,6 +134,28 @@ 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 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