electrum

Electrum Bitcoin wallet
git clone https://git.parazyd.org/electrum
Log | Files | Refs | Submodules

commit 4e5dfbeade64b7004e798114dfdd0300e8cdda35
parent c533b797e0d2d976da3ee1ab1afb7f5562f5747b
Author: Julian Tosh <Julian@Tosh.us>
Date:   Sat,  7 Jul 2012 09:24:52 -0700

Implementing a better print_error routine

Diffstat:
Melectrum | 66++++++++++++++++++++++++------------------------------------------
Mlib/gui.py | 4++--
Mlib/gui_qt.py | 17+++++++----------
Mlib/interface.py | 10++++------
Mlib/util.py | 7+++++++
Mlib/wallet.py | 4++--
Mremote_wallet.py | 4++--
Msetup.py | 5+++--
Mupgrade.py | 10++++------
9 files changed, 55 insertions(+), 72 deletions(-)

diff --git a/electrum b/electrum @@ -17,19 +17,18 @@ # along with this program. If not, see <http://www.gnu.org/licenses/>. import re, sys +from lib.util import print_error try: import ecdsa except: - sys.stderr.write("Error: python-ecdsa does not seem to be installed. Try 'sudo pip install ecdsa'\n") - sys.stderr.flush() + print_error("Error: python-ecdsa does not seem to be installed. Try 'sudo pip install ecdsa'") sys.exit(1) try: import aes except: - sys.stderr.write("Error: AES does not seem to be installed. Try 'sudo pip install slowaes'\n") - sys.stderr.flush() + print_error("Error: AES does not seem to be installed. Try 'sudo pip install slowaes'") sys.exit(1) try: @@ -144,8 +143,7 @@ if __name__ == '__main__': except ImportError: import electrum.gui_lite as gui else: - sys.stderr.write("Error: Unknown GUI: " + options.gui + "\n") - sys.stderr.flush() + print_error("Error: Unknown GUI: " + options.gui) exit(1) gui = gui.ElectrumGui(wallet) @@ -173,14 +171,13 @@ if __name__ == '__main__': cmd = 'help' if not wallet.file_exists and cmd not in ['help','create','restore']: - 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() + print_error("Error: Wallet file not found.") + print_error("Type 'electrum create' to create a new wallet, or provide a path to a wallet with the -w option") sys.exit(0) if cmd in ['create', 'restore']: if wallet.file_exists: - sys.stderr.write("Error: Remove the existing wallet first!\n") + print_error("Error: Remove the existing wallet first!") sys.stderr.flush() sys.exit(0) password = prompt_password("Password (hit return if you do not wish to encrypt your wallet):") @@ -203,12 +200,10 @@ if __name__ == '__main__': try: seed.decode('hex') except: - sys.stderr.write("Warning: Not hex, trying decode.\n") - sys.stderr.flush() + print_error("Warning: Not hex, trying decode.") seed = mnemonic.mn_decode( seed.split(' ') ) if not seed: - sys.stderr.write("Error: No seed\n") - sys.stderr.flush() + print_error("Error: No seed") sys.exit(1) wallet.seed = str(seed) @@ -222,12 +217,10 @@ if __name__ == '__main__': if wallet.is_found(): print "Recovery successful" else: - sys.stderr.write("Warning: Found no history for this wallet\n") - sys.stderr.flush() + print_error("Warning: Found no history for this wallet") wallet.fill_addressbook() wallet.save() - sys.stderr.write("Wallet saved in '" + wallet.path + "'\n") - sys.stderr.flush() + print_error("Wallet saved in '" + wallet.path) else: wallet.new_seed(None) wallet.init_mpk( wallet.seed ) @@ -276,8 +269,7 @@ if __name__ == '__main__': try: wallet.pw_decode( wallet.seed, password) except: - sys.stderr.write("Error: This password does not decode this wallet.\n") - sys.stderr.flush() + print_error("Error: This password does not decode this wallet.") exit(1) if cmd == 'import': @@ -287,12 +279,12 @@ if __name__ == '__main__': wallet.save() print "Keypair imported" except BaseException, e: - sys.stderr.write("Error: Keypair import failed: " + str(e) + "\n") - sys.stderr.flush() + print_error("Error: Keypair import failed: " + str(e)) if cmd=='help': cmd2 = firstarg if cmd2 not in known_commands: + print_error("Error: Command not found.") 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) @@ -305,11 +297,9 @@ if __name__ == '__main__': elif cmd == 'deseed': if not wallet.seed: - sys.stderr.write("Error: This wallet has no seed\n") - sys.stderr.flush() + print_error("Error: This wallet has no seed") elif wallet.use_encryption: - sys.stderr.write("Error: This wallet is encrypted\n") - sys.stderr.flush() + print_error("Error: This wallet is encrypted") 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) @@ -322,8 +312,7 @@ if __name__ == '__main__': wallet.save() print "Done." else: - sys.stderr.write("Action canceled.\n") - sys.stderr.flush() + print_error("Action canceled.") elif cmd == 'reseed': if wallet.seed: @@ -335,8 +324,7 @@ if __name__ == '__main__': data = f.read() f.close() except: - sys.stderr.write("Error: Seed file not found\n") - sys.stderr.flush() + print_error("Error: Seed file not found") sys.exit() try: import ast @@ -344,8 +332,7 @@ if __name__ == '__main__': seed = d['seed'] imported_keys = d.get('imported_keys',{}) except: - sys.stderr.write("Error: Error with seed file\n") - sys.stderr.flush() + print_error("Error: Error with seed file") sys.exit(1) mpk = wallet.master_public_key @@ -357,8 +344,7 @@ if __name__ == '__main__': wallet.save() print "Done: " + wallet.path else: - sys.stderr.write("Error: Master public key does not match\n") - sys.stderr.flush() + print_error("Error: Master public key does not match") elif cmd == 'validateaddress': addr = args[1] @@ -437,8 +423,7 @@ if __name__ == '__main__': tx = args[1] label = ' '.join(args[2:]) except: - sys.stderr.write("Error. Syntax: label <tx_hash> <text>\n") - sys.stderr.flush() + print_error("Error. Syntax: label <tx_hash> <text>") sys.exit(1) wallet.labels[tx] = label wallet.save() @@ -451,8 +436,7 @@ if __name__ == '__main__': keypair = from_addr from_addr = keypair.split(':')[0] if not wallet.import_key(keypair,password): - sys.stderr.write("Error: Invalid key pair\n") - sys.stderr.flush() + print_error("Error: Invalid key pair") exit(1) wallet.history[from_addr] = interface.retrieve_history(from_addr) wallet.update_tx_history() @@ -496,8 +480,7 @@ if __name__ == '__main__': try: seed = wallet.pw_decode( wallet.seed, password) except: - sys.stderr.write("Error: Password does not decrypt this wallet.\n") - sys.stderr.flush() + print_error("Error: Password does not decrypt this wallet.") sys.exit(1) new_password = prompt_password('New password:') @@ -516,8 +499,7 @@ if __name__ == '__main__': signature = args[2] message = ' '.join(args[3:]) except: - sys.stderr.write("Error: Not all parameters were given, displaying help instead.\n") - sys.stderr.flush() + print_error("Error: Not all parameters were given, displaying help instead.") print known_commands[cmd] sys.exit(1) if len(args) > 4: diff --git a/lib/gui.py b/lib/gui.py @@ -23,6 +23,7 @@ import pygtk pygtk.require('2.0') import gtk, gobject from decimal import Decimal +from lib.util import print_error import pyqrnative, mnemonic @@ -206,8 +207,7 @@ def run_recovery_dialog(wallet): try: seed.decode('hex') except: - sys.stderr.write("Warning: Not hex, trying decode\n") - sys.stderr.flush() + print_error("Warning: Not hex, trying decode") 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 @@ -18,13 +18,13 @@ import sys, time, datetime, re from i18n import _ +from lib.util import print_error try: import PyQt4 except: - 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() + print_error("Error: Could not import PyQt4") + print_error("on Linux systems, you may try 'sudo apt-get install python-qt4'") sys.exit(1) from PyQt4.QtGui import * @@ -36,9 +36,8 @@ from interface import DEFAULT_SERVERS try: import icons_rc except: - 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() + print_error("Error: Could not import icons_rc.py") + print_error("Please generate it with: 'pyrcc4 icons.qrc -o lib/icons_rc.py'") sys.exit(1) from wallet import format_satoshis @@ -390,8 +389,7 @@ class ElectrumWindow(QMainWindow): if text not in self.wallet.aliases.keys(): self.wallet.labels[addr] = text else: - sys.stderr.write("Error: This is one of your aliases\n") - sys.stderr.flush() + print_error("Error: This is one of your aliases") label = self.wallet.labels.get(addr,'') item.setText(column_label, QString(label)) else: @@ -1144,8 +1142,7 @@ class ElectrumWindow(QMainWindow): seed = unicode(seed_e.text()) seed.decode('hex') except: - sys.stderr.write("Warning: Not hex, trying decode\n") - sys.stderr.flush() + print_error("Warning: Not hex, trying decode") try: seed = mnemonic.mn_decode( seed.split(' ') ) except: diff --git a/lib/interface.py b/lib/interface.py @@ -21,6 +21,7 @@ import random, socket, ast, re import threading, traceback, sys, time, json, Queue from version import ELECTRUM_VERSION +from lib.util import print_error DEFAULT_TIMEOUT = 5 DEFAULT_SERVERS = [ 'ecdsa.org:50001:t', @@ -247,8 +248,7 @@ class TcpStratumInterface(Interface): print "Connected to %s:%d"%(self.host,self.port) except: self.is_connected = False - sys.stderr.write("Not connected\n") - sys.stderr.flush() + print_error("Not connected") def run(self): try: @@ -328,8 +328,7 @@ class WalletSynchronizer(threading.Thread): elif protocol == 'h': InterfaceClass = HttpStratumInterface else: - sys.stderr.write("Error: Unknown protocol\n") - sys.stderr.flush() + print_error("Error: Unknown protocol") InterfaceClass = TcpStratumInterface self.interface = InterfaceClass(host, port, self.wallet.debug_server) @@ -386,8 +385,7 @@ class WalletSynchronizer(threading.Thread): pass else: - sys.stderr.write("Error: Unknown message:" + method + ", " + params + ", " + result) - sys.stderr.flush() + print_error("Error: Unknown message:" + method + ", " + params + ", " + result) def start_interface(self): diff --git a/lib/util.py b/lib/util.py @@ -2,6 +2,13 @@ import os import platform import sys +def print_error(*args): + for item in args: + sys.stderr.write(str(item)) + + sys.stderr.write("\n") + sys.stderr.flush() + def appdata_dir(): if platform.system() == "Windows": return os.path.join(os.environ["APPDATA"], "Electrum") diff --git a/lib/wallet.py b/lib/wallet.py @@ -20,6 +20,7 @@ import sys, base64, os, re, hashlib, copy, operator, ast, threading, random, getpass import aes, ecdsa from ecdsa.util import string_to_number, number_to_string +from lib.util import print_error ############ functions from pywallet ##################### @@ -156,8 +157,7 @@ def prompt_password(prompt, confirm=True): password2 = getpass.getpass("Confirm: ") if password != password2: - sys.stderr.write("Error: Passwords do not match.\n") - sys.stderr.flush() + print_error("Error: Passwords do not match.") sys.exit(1) else: diff --git a/remote_wallet.py b/remote_wallet.py @@ -21,6 +21,7 @@ import time, thread, sys, socket # see http://code.google.com/p/jsonrpclib/ import jsonrpclib from wallet import Wallet +from lib.util import print_error """ Simple wallet daemon for webservers. @@ -99,8 +100,7 @@ if __name__ == '__main__': elif cmd == 'stop': out = server.stop() except socket.error: - sys.stderr.write("Server not running\n") - sys.stderr.flush() + print_error("Server not running") sys.exit(1) print out sys.exit(0) diff --git a/setup.py b/setup.py @@ -6,9 +6,10 @@ from distutils.core import setup from lib.version import ELECTRUM_VERSION as version import lib.util as util import os, sys, platform +from lib.util import print_error + if sys.version_info[:3] < (2,6,0): - sys.stderr.write("Electrum requires Python version >= 2.6.0... exiting\n") - sys.stderr.flush() + print_error("Error: Electrum requires Python version >= 2.6.0...") sys.exit(1) diff --git a/upgrade.py b/upgrade.py @@ -6,6 +6,7 @@ try: except ImportError: from electrum import prompt_password +from lib.uril import print_error @@ -30,15 +31,13 @@ if __name__ == "__main__": data = f.read() f.close() except: - sys.stderr.write("Error: File not found: " + path + "\n") - sys.stderr.flush() + print_error("Error: File not found: " + path) exit(1) try: x = ast.literal_eval(data) except: - sys.stderr.write("Error: Could not parse wallet\n") - sys.stderr.flush() + print_error("Error: Could not parse wallet") exit(1) # version <= 0.33 uses a tuple @@ -65,8 +64,7 @@ if __name__ == "__main__": seed = DecodeAES( secret, wallet.seed ) private_keys = ast.literal_eval( DecodeAES( secret, wallet.private_keys ) ) except: - sys.stderr.write("Error: Password does not decrypt this wallet.\n") - sys.stderr.flush() + print_error("Error: Password does not decrypt this wallet.") 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 ))