tImplementing a better print_error routine - electrum - Electrum Bitcoin wallet HTML git clone https://git.parazyd.org/electrum DIR Log DIR Files DIR Refs DIR Submodules --- DIR commit 4e5dfbeade64b7004e798114dfdd0300e8cdda35 DIR parent c533b797e0d2d976da3ee1ab1afb7f5562f5747b HTML Author: Julian Tosh <Julian@Tosh.us> Date: Sat, 7 Jul 2012 09:24:52 -0700 Implementing a better print_error routine Diffstat: M electrum | 66 +++++++++++-------------------- M lib/gui.py | 4 ++-- M lib/gui_qt.py | 17 +++++++---------- M lib/interface.py | 10 ++++------ M lib/util.py | 7 +++++++ M lib/wallet.py | 4 ++-- M remote_wallet.py | 4 ++-- M setup.py | 5 +++-- M upgrade.py | 10 ++++------ 9 files changed, 55 insertions(+), 72 deletions(-) --- DIR diff --git a/electrum b/electrum t@@ -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: t@@ -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) t@@ -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):") t@@ -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) t@@ -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 ) t@@ -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': t@@ -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) t@@ -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) t@@ -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: t@@ -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 t@@ -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 t@@ -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] t@@ -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() t@@ -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() t@@ -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:') t@@ -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: DIR diff --git a/lib/gui.py b/lib/gui.py t@@ -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 t@@ -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") DIR diff --git a/lib/gui_qt.py b/lib/gui_qt.py t@@ -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 * t@@ -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 t@@ -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: t@@ -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: DIR diff --git a/lib/interface.py b/lib/interface.py t@@ -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', 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: t@@ -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) t@@ -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): DIR diff --git a/lib/util.py b/lib/util.py t@@ -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") DIR diff --git a/lib/wallet.py b/lib/wallet.py t@@ -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 ##################### t@@ -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: DIR diff --git a/remote_wallet.py b/remote_wallet.py t@@ -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. t@@ -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) DIR diff --git a/setup.py b/setup.py t@@ -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) DIR diff --git a/upgrade.py b/upgrade.py t@@ -6,6 +6,7 @@ try: except ImportError: from electrum import prompt_password +from lib.uril import print_error t@@ -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 t@@ -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 ))