[mousetrap/gnome3-wip: 181/240] Copy default config to user config if it doesn't exist.



commit 3178b36594ad8ca3f7436bf3b0d2751c398a8693
Author: Stoney Jackson <dr stoney gmail com>
Date:   Fri Jun 27 10:52:44 2014 -0400

    Copy default config to user config if it doesn't exist.

 src/mousetrap/config.py |   27 +++++++++++++++++++--------
 1 files changed, 19 insertions(+), 8 deletions(-)
---
diff --git a/src/mousetrap/config.py b/src/mousetrap/config.py
index 2ecb76f..ee99a22 100644
--- a/src/mousetrap/config.py
+++ b/src/mousetrap/config.py
@@ -1,19 +1,30 @@
 from yaml import safe_load
-from os.path import dirname, expanduser, exists
+from os.path import dirname, expanduser, isfile
 from os import getcwd
+from shutil import copy
 from copy import deepcopy
 
 
 class Config(dict):
-    SEARCH_PATH = [
-        dirname(__file__) + '/' + 'mousetrap.yaml',
-        expanduser('~') + '/.mousetrap.yaml',
-        getcwd() + '/mousetrap.yaml',
-    ]
+    SEARCH_PATH = {
+        'default': dirname(__file__) + '/mousetrap.yaml',
+        'user': expanduser('~/.mousetrap.yaml'),
+        'local_hidden': getcwd() + '/.mousetrap.yaml',
+        'local': getcwd() + '/mousetrap.yaml',
+    }
 
     def __init__(self):
-        for path in self.SEARCH_PATH:
-            if exists(path):
+        self._install()
+        self._load()
+
+    def _install(self):
+        if not isfile(self.SEARCH_PATH['user']):
+            print("Copying %s to %s" % (self.SEARCH_PATH['default'], self.SEARCH_PATH['user']))
+            copy(self.SEARCH_PATH['default'], self.SEARCH_PATH['user'])
+
+    def _load(self):
+        for name, path in self.SEARCH_PATH.items():
+            if isfile(path):
                 print("loading %s" % (path))
                 with open(path) as config_file:
                     config = safe_load(config_file)


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]