Added SimpleConfig class to deal with simple config options added for fallback to...
authorMaran <maran.hidskes@gmail.com>
Wed, 29 Aug 2012 22:03:38 +0000 (00:03 +0200)
committerMaran <maran.hidskes@gmail.com>
Wed, 29 Aug 2012 22:03:38 +0000 (00:03 +0200)
electrum
lib/__init__.py
lib/simple_config.py [new file with mode: 0644]

index 9a6bb66..1e5046d 100755 (executable)
--- 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("<p>Sorry, the Electrum 'Lite GUI' requires Qt >= 4.7 to run. The pro GUI will be started instead.</p><p>Check your distributions packages for upgrades.</p>")
+              simple_config.config["gui"] = "qt"
+              simple_config.save_config
               app.exec_()
 
               try:
index a2e1aca..a9461ac 100644 (file)
@@ -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 (file)
index 0000000..47a17e0
--- /dev/null
@@ -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()
+