access to global configuration using set_config and get_config
[electrum-nvc.git] / lib / simple_config.py
index 62d5118..30c7f9d 100644 (file)
@@ -1,12 +1,19 @@
-import json, ast
-import os, ast
-from util import user_dir, print_error
-
-from version import ELECTRUM_VERSION, SEED_VERSION
+import json
+import ast
+import threading
+import os
 
+from util import user_dir, print_error, print_msg
 
 
+config = None
+def get_config():
+    global config
+    return config
 
+def set_config(c):
+    global config
+    config = c
 
 
 class SimpleConfig:
@@ -17,38 +24,45 @@ user configurations from electrum.conf into separate dictionaries within
 a SimpleConfig instance then reads the wallet file.
 """
     def __init__(self, options={}):
+        self.lock = threading.Lock()
 
         # system conf, readonly
         self.system_config = {}
         if options.get('portable') is not True:
             self.read_system_config()
 
-        # read path
-        self.path = self.system_config.get('electrum_path')
-        if self.path is None:
-            self.path = user_dir()
-
-        # user conf, writeable
-        self.user_config = {}
-        if options.get('portable') == False:
-            self.read_user_config()
-
         # command-line options
         self.options_config = options
 
         # init path
         self.init_path()
 
-        print_error( "electrum path", self.path)
+        # user conf, writeable
+        self.user_config = {}
+        self.read_user_config()
+
+        set_config(self)
+
 
 
     def init_path(self):
 
-        # Look for wallet file in the default data directory.
-        # Make wallet directory if it does not yet exist.
+        # Read electrum path in the command line configuration
+        self.path = self.options_config.get('electrum_path')
+
+        # Read electrum path in the system configuration
+        if self.path is None:
+            self.path = self.system_config.get('electrum_path')
+
+        # If not set, use the user's default data directory.
+        if self.path is None:
+            self.path = user_dir()
+
+        # Make directory if it does not yet exist.
         if not os.path.exists(self.path):
             os.mkdir(self.path)
 
+        print_error( "electrum directory", self.path)
 
         # portable wallet: use the same directory for wallet and headers file
         #if options.get('portable'):
@@ -65,8 +79,11 @@ a SimpleConfig instance then reads the wallet file.
                 print "Warning: not changing '%s' because it was set in the system configuration"%key
 
         else:
-            self.user_config[key] = value
-            if save: self.save_user_config()
+
+            with self.lock:
+                self.user_config[key] = value
+                if save: 
+                    self.save_user_config()
 
 
 
@@ -94,7 +111,7 @@ a SimpleConfig instance then reads the wallet file.
             import ast
             try:
                 out = ast.literal_eval(out)
-            except:
+            except Exception:
                 print "type error for '%s': using default value"%key
                 out = default
 
@@ -145,8 +162,9 @@ a SimpleConfig instance then reads the wallet file.
                 return
             try:
                 d = ast.literal_eval( data )  #parse raw data from reading wallet file
-            except:
-                raise IOError("Cannot read config file.")
+            except Exception:
+                print_msg("Error: Cannot read config file.")
+                return
 
             self.user_config = d