From 6da25727f8ea4a411b28b2b381ed05fa8762405d Mon Sep 17 00:00:00 2001 From: Maran Date: Thu, 30 Aug 2012 18:00:08 +0200 Subject: [PATCH] Refactored user_dir to utils and replaced it in wallet and config --- electrum | 1 - lib/simple_config.py | 11 ++--------- lib/util.py | 10 ++++++++++ lib/wallet.py | 11 +++-------- 4 files changed, 15 insertions(+), 18 deletions(-) diff --git a/electrum b/electrum index a414d94f..00126cea 100755 --- a/electrum +++ b/electrum @@ -150,7 +150,6 @@ if __name__ == '__main__': except ImportError: import electrum.gui_qt as gui elif options.gui == 'lite': - print 'ohhai' # Let's do some dep checking and handle missing ones gracefully try: from PyQt4.QtCore import * diff --git a/lib/simple_config.py b/lib/simple_config.py index ce64c28d..e2c8b101 100644 --- a/lib/simple_config.py +++ b/lib/simple_config.py @@ -1,5 +1,6 @@ import json import os +from lib.util import user_dir class SimpleConfig: default_options = {"gui": "lite"} @@ -27,15 +28,7 @@ class SimpleConfig: 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.") - + self.config_folder = user_dir() # Read the file if os.path.exists(self.config_file_path()): self.load_config() diff --git a/lib/util.py b/lib/util.py index 2aabcedf..506d0941 100644 --- a/lib/util.py +++ b/lib/util.py @@ -8,6 +8,16 @@ def print_error(*args): sys.stderr.write(" ".join(args) + "\n") sys.stderr.flush() +def user_dir(): + if "HOME" in os.environ: + return os.path.join(os.environ["HOME"], ".electrum") + elif "LOCALAPPDATA" in os.environ: + return os.path.join(os.environ["LOCALAPPDATA"], "Electrum") + elif "APPDATA" in os.environ: + return os.path.join(os.environ["APPDATA"], "Electrum") + else: + raise BaseException("No home directory found in environment variables.") + def appdata_dir(): """Find the path to the application data directory; add an electrum folder and return path.""" if platform.system() == "Windows": diff --git a/lib/wallet.py b/lib/wallet.py index 18125891..80d28599 100644 --- a/lib/wallet.py +++ b/lib/wallet.py @@ -32,6 +32,7 @@ import ecdsa from ecdsa.util import string_to_number, number_to_string from util import print_error +from util import user_dir ############ functions from pywallet ##################### @@ -365,14 +366,8 @@ class Wallet: return # Look for wallet file in the default data directory. # Keeps backwards compatibility. - if "HOME" in os.environ: - wallet_dir = os.path.join(os.environ["HOME"], ".electrum") - elif "LOCALAPPDATA" in os.environ: - wallet_dir = os.path.join(os.environ["LOCALAPPDATA"], "Electrum") - elif "APPDATA" in os.environ: - wallet_dir = os.path.join(os.environ["APPDATA"], "Electrum") - else: - raise BaseException("No home directory found in environment variables.") + wallet_dir = user_dir() + # Make wallet directory if it does not yet exist. if not os.path.exists(wallet_dir): os.mkdir(wallet_dir)