From 3252b5ae4e01e027cb7f6238d46f161cd3898b3c Mon Sep 17 00:00:00 2001 From: Maran Date: Thu, 30 Aug 2012 00:03:38 +0200 Subject: [PATCH] Added SimpleConfig class to deal with simple config options added for fallback to other gui when missing deps --- electrum | 11 +++++++++-- lib/__init__.py | 1 + lib/simple_config.py | 40 ++++++++++++++++++++++++++++++++++++++++ 3 files changed, 50 insertions(+), 2 deletions(-) create mode 100644 lib/simple_config.py diff --git a/electrum b/electrum index 9a6bb66b6..1e5046d89 100755 --- a/electrum +++ b/electrum @@ -40,8 +40,9 @@ try: from lib import Wallet, WalletSynchronizer, format_satoshis, mnemonic, prompt_password except ImportError: from electrum import Wallet, WalletSynchronizer, format_satoshis, mnemonic, prompt_password - + from decimal import Decimal +from lib import SimpleConfig known_commands = { 'help':'Prints this help', @@ -101,9 +102,12 @@ protected_commands = ['payto', 'password', 'mktx', 'seed', 'import','signmessage if __name__ == '__main__': + # Load simple config class + simple_config = SimpleConfig() + usage = "usage: %prog [options] command\nCommands: "+ (', '.join(known_commands)) parser = optparse.OptionParser(prog=usage) - parser.add_option("-g", "--gui", dest="gui", default="lite", help="gui") + parser.add_option("-g", "--gui", dest="gui", default=simple_config.config["gui"], help="gui") parser.add_option("-w", "--wallet", dest="wallet_path", help="wallet path (default: electrum.dat)") parser.add_option("-o", "--offline", action="store_true", dest="offline", default=False, help="remain offline") parser.add_option("-a", "--all", action="store_true", dest="show_all", default=False, help="show all addresses") @@ -115,6 +119,7 @@ if __name__ == '__main__': parser.add_option("-r", "--remote", dest="remote_url", default=None, help="URL of a remote wallet") options, args = parser.parse_args() + wallet = Wallet() wallet.set_path(options.wallet_path) wallet.read() @@ -161,6 +166,8 @@ if __name__ == '__main__': error_message = QErrorMessage() error_message.setFixedSize(350,200) error_message.showMessage("

Sorry, the Electrum 'Lite GUI' requires Qt >= 4.7 to run. The pro GUI will be started instead.

Check your distributions packages for upgrades.

") + simple_config.config["gui"] = "qt" + simple_config.save_config app.exec_() try: diff --git a/lib/__init__.py b/lib/__init__.py index a2e1aca78..a9461ac88 100644 --- a/lib/__init__.py +++ b/lib/__init__.py @@ -1,3 +1,4 @@ from wallet import Wallet, format_satoshis, prompt_password from interface import WalletSynchronizer from interface import TcpStratumInterface +from simple_config import SimpleConfig diff --git a/lib/simple_config.py b/lib/simple_config.py new file mode 100644 index 000000000..47a17e08f --- /dev/null +++ b/lib/simple_config.py @@ -0,0 +1,40 @@ +import json +import os + +class SimpleConfig: + default_options = {"gui": "lite"} + + def save_config(self): + f = open(self.config_file_path(), "w+") + f.write(json.dumps(self.config)) + + def load_config(self): + f = open(self.config_file_path(), "r") + file_contents = f.read() + if file_contents: + self.config = json.loads(file_contents) + else: + self.config = self.default_options + self.save_config() + + def config_file_path(self): + return "%s" % (self.config_folder + "/config.json") + + def __init__(self): + # Find electrum data folder + if "HOME" in os.environ: + self.config_folder = os.path.join(os.environ["HOME"], ".electrum") + elif "LOCALAPPDATA" in os.environ: + self.config_folder = os.path.join(os.environ["LOCALAPPDATA"], "Electrum") + elif "APPDATA" in os.environ: + self.config_folder = os.path.join(os.environ["APPDATA"], "Electrum") + else: + raise BaseException("No home directory found in environment variables.") + + # Read the file + if os.path.exists(self.config_file_path()): + self.load_config() + else: + self.config = self.default_options + self.save_config() +