commit bb7b0884e31f5c7d1a4f93cf63c65401c7f496fd
parent 079cb311ecec0b42df6d28e22b9dd107cb5f6c2c
Author: ThomasV <thomasv@electrum.org>
Date: Thu, 29 Oct 2015 11:06:09 +0100
Various fixes for command line. Make 'payto' command require network (fixes #1525)
Diffstat:
8 files changed, 36 insertions(+), 33 deletions(-)
diff --git a/electrum b/electrum
@@ -79,7 +79,7 @@ if is_bundle or is_local or is_android:
from electrum import util
from electrum import SimpleConfig, Network, Wallet, WalletStorage
-from electrum.util import print_msg, print_error, print_stderr, print_json, set_verbosity, InvalidPassword
+from electrum.util import print_msg, print_error, print_stderr, json_encode, json_decode, set_verbosity, InvalidPassword
from electrum.plugins import Plugins, run_hook, always_hook
from electrum.commands import get_parser, known_commands, Commands, config_variables
@@ -251,15 +251,9 @@ def run_command(config, network, password):
# arguments passed to function
args = map(lambda x: config.get(x), cmd.params)
# decode json arguments
- def json_decode(x):
- try:
- return json.loads(x)
- except:
- return x
args = map(json_decode, args)
# options
args += map(lambda x: config.get(x), cmd.options)
-
cmd_runner = Commands(config, wallet, network)
cmd_runner.password = password
func = getattr(cmd_runner, cmd.name)
@@ -467,13 +461,15 @@ if __name__ == '__main__':
gui_name = config.get('gui', 'qt') if cmd_name == 'gui' else 'cmdline'
plugins = Plugins(config, is_bundle or is_local or is_android, gui_name)
- # get password if needed
+ # run command offline
if cmd_name not in ['gui', 'daemon']:
cmd, password = init_cmdline(config)
if not cmd.requires_network or config.get('offline'):
result = run_command(config, None, password)
- print_json(result)
- sys.exit(1)
+ print_msg(json_encode(result))
+ sys.exit(0)
+ else:
+ config_options['password'] = password
# check if daemon is running
s = get_daemon(config, False)
@@ -485,11 +481,10 @@ if __name__ == '__main__':
s.close()
if type(result) in [str, unicode]:
print_msg(result)
+ elif type(result) is dict and result.get('error'):
+ print_stderr(result.get('error'))
elif result is not None:
- if type(result) is dir and result.get('error'):
- print_stderr(result.get('error'))
- else:
- print_json(result)
+ print_msg(json_encode(result))
sys.exit(0)
# daemon is not running
diff --git a/gui/qt/console.py b/gui/qt/console.py
@@ -213,9 +213,10 @@ class Console(QtGui.QPlainTextEdit):
try:
# eval is generally considered bad practice. use it wisely!
result = eval(command, self.namespace, self.namespace)
+ result = util.json_encode(result)
if result != None:
if self.is_json:
- util.print_json(result)
+ util.print_msg(result)
else:
self.appendPlainText(repr(result))
except SyntaxError:
diff --git a/lib/__init__.py b/lib/__init__.py
@@ -1,5 +1,5 @@
from version import ELECTRUM_VERSION
-from util import format_satoshis, print_msg, print_json, print_error, set_verbosity
+from util import format_satoshis, print_msg, print_error, set_verbosity
from wallet import Synchronizer, WalletStorage
from wallet import Wallet, Imported_Wallet
from network import Network, DEFAULT_SERVERS, DEFAULT_PORTS, pick_random_server
diff --git a/lib/commands.py b/lib/commands.py
@@ -418,14 +418,14 @@ class Commands:
self.wallet.sign_transaction(tx, self.password)
return tx
- @command('wp')
+ @command('wpn')
def payto(self, destination, amount, tx_fee=None, from_addr=None, change_addr=None, nocheck=False, unsigned=False, deserialized=False):
"""Create a transaction. """
domain = [from_addr] if from_addr else None
tx = self._mktx([(destination, amount)], tx_fee, change_addr, domain, nocheck, unsigned)
return tx.deserialize() if deserialized else tx
- @command('wp')
+ @command('wpn')
def paytomany(self, outputs, tx_fee=None, from_addr=None, change_addr=None, nocheck=False, unsigned=False, deserialized=False):
"""Create a multi-output transaction. """
domain = [from_addr] if from_addr else None
@@ -657,8 +657,8 @@ arg_types = {
'pubkeys': json.loads,
'inputs': json.loads,
'outputs': json.loads,
- 'tx_fee': lambda x: Decimal(x) if x is not None else None,
- 'amount': lambda x: Decimal(x) if x!='!' else '!',
+ 'tx_fee': lambda x: float(x) if x is not None else None,
+ 'amount': lambda x: float(x) if x!='!' else '!',
}
config_variables = {
diff --git a/lib/util.py b/lib/util.py
@@ -112,14 +112,18 @@ def print_msg(*args):
sys.stdout.write(" ".join(args) + "\n")
sys.stdout.flush()
-def print_json(obj):
+def json_encode(obj):
try:
s = json.dumps(obj, sort_keys = True, indent = 4, cls=MyEncoder)
except TypeError:
s = repr(obj)
- sys.stdout.write(s + "\n")
- sys.stdout.flush()
+ return s
+def json_decode(x):
+ try:
+ return json.loads(x)
+ except:
+ return x
# decorator that prints execution time
def profiler(func):
diff --git a/scripts/block_headers b/scripts/block_headers
@@ -3,11 +3,12 @@
# A simple script that connects to a server and displays block headers
import time
-import electrum
+from electrum import SimpleConfig, Network
+from electrum.util import print_msg, json_encode
# start network
-c = electrum.SimpleConfig()
-network = electrum.Network(c)
+c = SimpleConfig()
+network = Network(c)
network.start()
# wait until connected
@@ -19,7 +20,7 @@ if not network.is_connected():
sys.exit(1)
# 2. send the subscription
-callback = lambda response: electrum.print_json(response.get('result'))
+callback = lambda response: print_msg(json_encode(response.get('result')))
network.send([('blockchain.headers.subscribe',[])], callback)
# 3. wait for results
diff --git a/scripts/get_history b/scripts/get_history
@@ -1,7 +1,8 @@
#!/usr/bin/env python
import sys
-from electrum import Network, print_json
+from electrum import Network
+from electrum.util import json_encode, print_msg
try:
addr = sys.argv[1]
@@ -12,4 +13,4 @@ except Exception:
n = Network()
n.start()
h = n.synchronous_get(('blockchain.address.get_history',[addr]))
-print_json(h)
+print_msg(json_encode(h))
diff --git a/scripts/watch_address b/scripts/watch_address
@@ -2,7 +2,8 @@
import sys
import time
-import electrum
+from electrum import SimpleConfig, Network
+from electrum.util import print_msg, json_encode
try:
addr = sys.argv[1]
@@ -11,8 +12,8 @@ except Exception:
sys.exit(1)
# start network
-c = electrum.SimpleConfig()
-network = electrum.Network(c)
+c = SimpleConfig()
+network = Network(c)
network.start()
# wait until connected
@@ -24,7 +25,7 @@ if not network.is_connected():
sys.exit(1)
# 2. send the subscription
-callback = lambda response: electrum.print_json(response.get('result'))
+callback = lambda response: print_msg(json_encode(response.get('result')))
network.send([('blockchain.address.subscribe',[addr])], callback)
# 3. wait for results