[mousetrap/gnome3-wip: 204/240] Add --dump-config and --config options.



commit 084054e3c95c5df31ea972387bb5c94f1784b87f
Author: Stoney Jackson <dr stoney gmail com>
Date:   Sun Jun 29 02:21:42 2014 -0400

    Add --dump-config and --config options.

 README.md               |   28 +++++++++++++++++++++++++++-
 src/mousetrap/config.py |   34 +++++++++++++++++-----------------
 src/mousetrap/main.py   |   18 +++++++++++++++++-
 3 files changed, 61 insertions(+), 19 deletions(-)
---
diff --git a/README.md b/README.md
index 6d0b092..44bbd7b 100644
--- a/README.md
+++ b/README.md
@@ -35,6 +35,7 @@ License: GPL v2.0 (see LICENCSE)
 
 ## Running
 
+    mousetrap --help      # a list of command-line options
     mousetrap
 
 
@@ -49,7 +50,18 @@ close your left eye for about 1.5 seconds.
 
 ## Configuring
 
-Run MouseTrap at least once, and then edit ~/.mousetrap.yaml.
+MouseTrap loads its configuration from following in order:
+
+* The built-in default configuration
+* ~/.mousetrap.yaml
+* $PWD/.mousetrap.yaml
+* $PWD/mousetrap.yaml
+* From the file specified using the `--config` option.
+For example: `mousetrap --config path/to/myconfig.yaml`
+
+You can dump the current configuration as follows
+
+    mousetrap --dump-config
 
 
 ## Translating
@@ -117,3 +129,17 @@ classes:
 ```
 
 For more examples, see the plugins in `src/mousetrap/plugins`.
+
+## Developing
+
+Makefile targets
+* `all` - builds
+* `install` - installs
+* `run` - runs without install
+* `check` - tests
+* `lint` - style checker
+* `clean` - deletes files created by `all`
+* `pristine` - deletes all ignored or not tracked by git (requires git)
+* `dist` - makes distribution
+* `distcheck` - makes and tests distribution
+* `distclean` - cleans extra files generated by `dist`
diff --git a/src/mousetrap/config.py b/src/mousetrap/config.py
index bcbdc52..f9ba5e3 100644
--- a/src/mousetrap/config.py
+++ b/src/mousetrap/config.py
@@ -3,32 +3,32 @@ from os.path import dirname, expanduser, isfile
 from os import getcwd
 from shutil import copy
 from copy import deepcopy
+from collections import OrderedDict
 
 
 class Config(dict):
-    SEARCH_PATH = {
-        'default': dirname(__file__) + '/mousetrap.yaml',
-        'user': expanduser('~/.mousetrap.yaml'),
-        'local_hidden': getcwd() + '/.mousetrap.yaml',
-        'local': getcwd() + '/mousetrap.yaml',
-    }
-
-    def __init__(self):
-        self._install()
+    SEARCH_PATH = OrderedDict([
+        ('default', dirname(__file__) + '/mousetrap.yaml'),
+        ('user', expanduser('~/.mousetrap.yaml')),
+        ('local_hidden', getcwd() + '/.mousetrap.yaml'),
+        ('local', getcwd() + '/mousetrap.yaml'),
+        ('user_specified_file', None),
+        ])
+
+    def __init__(self, user_specified_file=None):
+        self.SEARCH_PATH['user_specified_file']=user_specified_file
         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))
+            if path is not None and isfile(path):
+                print "# Loading %s" % (path)
                 with open(path) as config_file:
                     config = safe_load(config_file)
-                    _rmerge(self, config)
+                    if config is not None:
+                        _rmerge(self, config)
+                    else:
+                        print "# Warning: config is empty."
 
     def __getitem__(self, key):
         '''
diff --git a/src/mousetrap/main.py b/src/mousetrap/main.py
index 04c9305..d233234 100644
--- a/src/mousetrap/main.py
+++ b/src/mousetrap/main.py
@@ -3,8 +3,24 @@ Where it all begins.
 '''
 
 
+import argparse
+parser = argparse.ArgumentParser()
+parser.add_argument("--dump-config",
+        help="Loads and dumps configuration to standard out.",
+        action="store_true")
+parser.add_argument("--config",
+        metavar="FILE",
+        help="Loads configuration from FILE.")
+args = parser.parse_args()
+
+
 from mousetrap.config import Config
-CONFIG = Config()
+CONFIG = Config(args.config)
+if args.dump_config:
+    import sys
+    import yaml
+    print yaml.dump(dict(CONFIG), default_flow_style=False)
+    sys.exit(0)
 
 
 import logging


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