commit a3830e5903fb11319fdd4397bb94fae1ff71b76c
parent b615fe0c8c91a23d3c723f9d454b96810df8ab7a
Author: Julian Tosh <Julian@Tosh.us>
Date: Sat, 7 Jul 2012 06:39:25 -0700
Standardizing message format and implementing stderr for errors
Diffstat:
8 files changed, 94 insertions(+), 53 deletions(-)
diff --git a/electrum b/electrum
@@ -21,13 +21,15 @@ import re, sys
try:
import ecdsa
except:
- print "python-ecdsa does not seem to be installed. Try 'sudo pip install ecdsa'"
+ sys.stderr.write("Error: python-ecdsa does not seem to be installed. Try 'sudo pip install ecdsa'\n")
+ sys.stderr.flush()
sys.exit(1)
try:
import aes
except:
- print "AES does not seem to be installed. Try 'sudo pip install slowaes'"
+ sys.stderr.write("Error: AES does not seem to be installed. Try 'sudo pip install slowaes'\n")
+ sys.stderr.flush()
sys.exit(1)
try:
@@ -142,7 +144,8 @@ if __name__ == '__main__':
except ImportError:
import electrum.gui_lite as gui
else:
- print "unknown gui", options.gui
+ sys.stderr.write("Error: Unknown GUI: " + options.gui + "\n")
+ sys.stderr.flush()
exit(1)
gui = gui.ElectrumGui(wallet)
@@ -170,13 +173,15 @@ if __name__ == '__main__':
cmd = 'help'
if not wallet.file_exists and cmd not in ['help','create','restore']:
- print "Wallet file not found."
- print "Type 'electrum create' to create a new wallet, or provide a path to a wallet with the -w option"
+ sys.stderr.write("Error: Wallet file not found.\n")
+ sys.stderr.write("Type 'electrum create' to create a new wallet, or provide a path to a wallet with the -w option\n")
+ sys.stderr.flush()
sys.exit(0)
if cmd in ['create', 'restore']:
if wallet.file_exists:
- print "remove the existing wallet first!"
+ sys.stderr.write("Error: Remove the existing wallet first!\n")
+ sys.stderr.flush()
sys.exit(0)
password = prompt_password("Password (hit return if you do not wish to encrypt your wallet):")
@@ -198,27 +203,31 @@ if __name__ == '__main__':
try:
seed.decode('hex')
except:
- print "not hex, trying decode"
+ sys.stderr.write("Warning: Not hex, trying decode.\n")
+ sys.stderr.flush()
seed = mnemonic.mn_decode( seed.split(' ') )
if not seed:
- print "no seed"
+ sys.stderr.write("Error: No seed\n")
+ sys.stderr.flush()
sys.exit(1)
wallet.seed = str(seed)
wallet.init_mpk( wallet.seed )
if not options.offline:
WalletSynchronizer(wallet).start()
- print "recovering wallet..."
+ print "Recovering wallet..."
wallet.up_to_date_event.clear()
wallet.up_to_date = False
wallet.update()
if wallet.is_found():
- print "recovery successful"
+ print "Recovery successful"
else:
- print "found no history for this wallet"
+ sys.stderr.write("Warning: Found no history for this wallet\n")
+ sys.stderr.flush()
wallet.fill_addressbook()
wallet.save()
- print "Wallet saved in '%s'"%wallet.path
+ sys.stderr.write("Wallet saved in '" + wallet.path + "'\n")
+ sys.stderr.flush()
else:
wallet.new_seed(None)
wallet.init_mpk( wallet.seed )
@@ -267,7 +276,8 @@ if __name__ == '__main__':
try:
wallet.pw_decode( wallet.seed, password)
except:
- print "Error: This password does not decode this wallet."
+ sys.stderr.write("Error: This password does not decode this wallet.\n")
+ sys.stderr.flush()
exit(1)
if cmd == 'import':
@@ -275,16 +285,17 @@ if __name__ == '__main__':
try:
wallet.import_key(keypair,password)
wallet.save()
- print "keypair imported"
+ print "Keypair imported"
except BaseException, e:
- print( 'Error:' + str(e) )
+ sys.stderr.write("Error: Keypair import failed: " + str(e) + "\n")
+ sys.stderr.flush()
if cmd=='help':
cmd2 = firstarg
if cmd2 not in known_commands:
- print "type 'electrum help <command>' to see the help for a specific command"
- print "type 'electrum --help' to see the list of options"
- print "list of commands:", ', '.join(known_commands)
+ print "Type 'electrum help <command>' to see the help for a specific command"
+ print "Type 'electrum --help' to see the list of options"
+ print "List of commands:", ', '.join(known_commands)
else:
print known_commands[cmd2]
@@ -294,9 +305,11 @@ if __name__ == '__main__':
elif cmd == 'deseed':
if not wallet.seed:
- print "Error: This wallet has no seed"
+ sys.stderr.write("Error: This wallet has no seed\n")
+ sys.stderr.flush()
elif wallet.use_encryption:
- print "Error: This wallet is encrypted"
+ sys.stderr.write("Error: This wallet is encrypted\n")
+ sys.stderr.flush()
else:
ns = wallet.path + '.seed'
print "Warning: you are going to extract the seed from '%s'\nThe seed will be saved in '%s'"%(wallet.path,ns)
@@ -309,11 +322,12 @@ if __name__ == '__main__':
wallet.save()
print "Done."
else:
- print "Action canceled."
+ sys.stderr.write("Action canceled.\n")
+ sys.stderr.flush()
elif cmd == 'reseed':
if wallet.seed:
- print "This wallet already has a seed", wallet.seed
+ print "Warning: This wallet already has a seed", wallet.seed
else:
ns = wallet.path + '.seed'
try:
@@ -321,7 +335,8 @@ if __name__ == '__main__':
data = f.read()
f.close()
except:
- print "seed file not found"
+ sys.stderr.write("Error: Seed file not found\n")
+ sys.stderr.flush()
sys.exit()
try:
import ast
@@ -329,7 +344,8 @@ if __name__ == '__main__':
seed = d['seed']
imported_keys = d.get('imported_keys',{})
except:
- print "error with seed file"
+ sys.stderr.write("Error: Error with seed file\n")
+ sys.stderr.flush()
sys.exit(1)
mpk = wallet.master_public_key
@@ -341,7 +357,8 @@ if __name__ == '__main__':
wallet.save()
print "Done: " + wallet.path
else:
- print "error: master public key does not match"
+ sys.stderr.write("Error: Master public key does not match\n")
+ sys.stderr.flush()
elif cmd == 'validateaddress':
addr = args[1]
@@ -420,7 +437,8 @@ if __name__ == '__main__':
tx = args[1]
label = ' '.join(args[2:])
except:
- print "syntax: label <tx_hash> <text>"
+ sys.stderr.write("Error. Syntax: label <tx_hash> <text>\n")
+ sys.stderr.flush()
sys.exit(1)
wallet.labels[tx] = label
wallet.save()
@@ -433,7 +451,8 @@ if __name__ == '__main__':
keypair = from_addr
from_addr = keypair.split(':')[0]
if not wallet.import_key(keypair,password):
- print "invalid key pair"
+ sys.stderr.write("Error: Invalid key pair\n")
+ sys.stderr.flush()
exit(1)
wallet.history[from_addr] = interface.retrieve_history(from_addr)
wallet.update_tx_history()
@@ -477,7 +496,8 @@ if __name__ == '__main__':
try:
seed = wallet.pw_decode( wallet.seed, password)
except:
- print "Error: Password does not decrypt this wallet."
+ sys.stderr.write("Error: Password does not decrypt this wallet.\n")
+ sys.stderr.flush()
sys.exit(1)
new_password = prompt_password('New password:')
@@ -496,7 +516,8 @@ if __name__ == '__main__':
signature = args[2]
message = ' '.join(args[3:])
except:
- print "Not all parameters were given, displaying help instead."
+ sys.stderr.write("Error: Not all parameters were given, displaying help instead.\n")
+ sys.stderr.flush()
print known_commands[cmd]
sys.exit(1)
if len(args) > 4:
diff --git a/lib/gui.py b/lib/gui.py
@@ -135,7 +135,7 @@ def restore_create_dialog(wallet):
# history and addressbook
wallet.update_tx_history()
wallet.fill_addressbook()
- print "recovery successful"
+ print "Recovery successful"
gobject.idle_add( dialog.destroy )
@@ -206,7 +206,8 @@ def run_recovery_dialog(wallet):
try:
seed.decode('hex')
except:
- print "not hex, trying decode"
+ sys.stderr.write("Warning: Not hex, trying decode\n")
+ sys.stderr.flush()
seed = mnemonic.mn_decode( seed.split(' ') )
if not seed:
show_message("no seed")
diff --git a/lib/gui_qt.py b/lib/gui_qt.py
@@ -22,8 +22,9 @@ from i18n import _
try:
import PyQt4
except:
- print "could not import PyQt4"
- print "on Linux systems, you may try 'sudo apt-get install python-qt4'"
+ sys.stderr.write("Error: Could not import PyQt4\n")
+ sys.stderr.write("on Linux systems, you may try 'sudo apt-get install python-qt4'\n")
+ sys.stderr.flush()
sys.exit(1)
from PyQt4.QtGui import *
@@ -35,8 +36,9 @@ from interface import DEFAULT_SERVERS
try:
import icons_rc
except:
- print "Could not import icons_rc.py"
- print "Please generate it with: 'pyrcc4 icons.qrc -o lib/icons_rc.py'"
+ sys.stderr.write("Error: Could not import icons_rc.py\n")
+ sys.stderr.write("Please generate it with: 'pyrcc4 icons.qrc -o lib/icons_rc.py'\n")
+ sys.stderr.flush()
sys.exit(1)
from wallet import format_satoshis
@@ -388,7 +390,8 @@ class ElectrumWindow(QMainWindow):
if text not in self.wallet.aliases.keys():
self.wallet.labels[addr] = text
else:
- print "error: this is one of your aliases"
+ sys.stderr.write("Error: This is one of your aliases\n")
+ sys.stderr.flush()
label = self.wallet.labels.get(addr,'')
item.setText(column_label, QString(label))
else:
@@ -1141,7 +1144,8 @@ class ElectrumWindow(QMainWindow):
seed = unicode(seed_e.text())
seed.decode('hex')
except:
- print "not hex, trying decode"
+ sys.stderr.write("Warning: Not hex, trying decode\n")
+ sys.stderr.flush()
try:
seed = mnemonic.mn_decode( seed.split(' ') )
except:
@@ -1473,7 +1477,7 @@ class ElectrumGui:
# history and addressbook
wallet.update_tx_history()
wallet.fill_addressbook()
- print "recovery successful"
+ print "Recovery successful"
wallet.save()
else:
QMessageBox.information(None, _('Error'), _("No transactions found for this seed"), _('OK'))
diff --git a/lib/interface.py b/lib/interface.py
@@ -247,7 +247,8 @@ class TcpStratumInterface(Interface):
print "Connected to %s:%d"%(self.host,self.port)
except:
self.is_connected = False
- print "Not connected"
+ sys.stderr.write("Not connected\n")
+ sys.stderr.flush()
def run(self):
try:
@@ -262,7 +263,7 @@ class TcpStratumInterface(Interface):
self.bytes_received += len(msg)
if msg == '':
self.is_connected = False
- print "disconnected."
+ print "Disconnected."
while True:
s = out.find('\n')
@@ -276,7 +277,7 @@ class TcpStratumInterface(Interface):
traceback.print_exc(file=sys.stdout)
self.is_connected = False
- print "poking"
+ print "Poking"
self.poke()
def send(self, messages):
@@ -327,7 +328,8 @@ class WalletSynchronizer(threading.Thread):
elif protocol == 'h':
InterfaceClass = HttpStratumInterface
else:
- print "unknown protocol"
+ sys.stderr.write("Error: Unknown protocol\n")
+ sys.stderr.flush()
InterfaceClass = TcpStratumInterface
self.interface = InterfaceClass(host, port, self.wallet.debug_server)
@@ -384,7 +386,8 @@ class WalletSynchronizer(threading.Thread):
pass
else:
- print "unknown message:", method, params, result
+ sys.stderr.write("Error: Unknown message:" + method + ", " + params + ", " + result)
+ sys.stderr.flush()
def start_interface(self):
diff --git a/lib/wallet.py b/lib/wallet.py
@@ -156,7 +156,8 @@ def prompt_password(prompt, confirm=True):
password2 = getpass.getpass("Confirm: ")
if password != password2:
- print "Error: Passwords do not match."
+ sys.stderr.write("Error: Passwords do not match.\n")
+ sys.stderr.flush()
sys.exit(1)
else:
@@ -920,7 +921,7 @@ class Wallet:
if not self.use_change and not change_addr:
change_addr = inputs[0][0]
- print "sending change to", change_addr
+ print "Sending change to", change_addr
outputs = self.choose_tx_outputs( to_address, amount, fee, total, change_addr )
s_inputs = self.sign_inputs( inputs, outputs, password )
diff --git a/remote_wallet.py b/remote_wallet.py
@@ -99,7 +99,8 @@ if __name__ == '__main__':
elif cmd == 'stop':
out = server.stop()
except socket.error:
- print "server not running"
+ sys.stderr.write("Server not running\n")
+ sys.stderr.flush()
sys.exit(1)
print out
sys.exit(0)
diff --git a/setup.py b/setup.py
@@ -7,7 +7,8 @@ from lib.version import ELECTRUM_VERSION as version
import lib.util as util
import os, sys, platform
if sys.version_info[:3] < (2,6,0):
- print "Electrum requires Python version >= 2.6.0... exiting"
+ sys.stderr.write("Electrum requires Python version >= 2.6.0... exiting\n")
+ sys.stderr.flush()
sys.exit(1)
diff --git a/upgrade.py b/upgrade.py
@@ -1,6 +1,12 @@
-import electrum, getpass, base64,ast,sys,os
+import electrum, base64, ast, sys, os
from version import SEED_VERSION
+try:
+ from lib import prompt_password
+except ImportError:
+ from electrum import prompt_password
+
+
@@ -24,13 +30,15 @@ if __name__ == "__main__":
data = f.read()
f.close()
except:
- print "file not found", path
+ sys.stderr.write("Error: File not found: " + path + "\n")
+ sys.stderr.flush()
exit(1)
try:
x = ast.literal_eval(data)
except:
- print "error: could not parse wallet"
+ sys.stderr.write("Error: Could not parse wallet\n")
+ sys.stderr.flush()
exit(1)
# version <= 0.33 uses a tuple
@@ -50,14 +58,15 @@ if __name__ == "__main__":
EncodeAES = lambda secret, s: base64.b64encode(AES.new(secret).encrypt(pad(s)))
DecodeAES = lambda secret, e: AES.new(secret).decrypt(base64.b64decode(e)).rstrip(PADDING)
- print "please enter your password"
- password = getpass.getpass("Password:")
+ print "Please enter your password"
+ password = prompt_password("Password:")
secret = electrum.Hash(password)
try:
seed = DecodeAES( secret, wallet.seed )
private_keys = ast.literal_eval( DecodeAES( secret, wallet.private_keys ) )
except:
- print "sorry"
+ sys.stderr.write("Error: Password does not decrypt this wallet.\n")
+ sys.stderr.flush()
exit(1)
seed_version = 2
s = repr( (seed_version, use_encryption, fee, host, port, blocks, seed, all_addresses, private_keys, change_indexes, status, history, labels, addressbook ))