[mousetrap/gnome3-wip: 230/240] Simplify mousetrap.config and add tests.
- From: Heidi Ellis <heidiellis src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [mousetrap/gnome3-wip: 230/240] Simplify mousetrap.config and add tests.
- Date: Mon, 8 Sep 2014 15:31:54 +0000 (UTC)
commit 923a8c050815bfd7e3ec35f8a7be7a1268727f5f
Author: Stoney Jackson <dr stoney gmail com>
Date: Mon Jun 30 19:59:52 2014 -0400
Simplify mousetrap.config and add tests.
src/mousetrap/config.py | 42 ++++++++++++------------------
src/mousetrap/main.py | 14 +++++++++-
src/mousetrap/tests/run_python_tests.py | 4 +-
src/mousetrap/tests/test_config.py | 43 ++++++++++++++++++++++++++-----
src/mousetrap/tests/test_gui.py | 2 +-
src/mousetrap/tests/test_vision.py | 2 +-
6 files changed, 70 insertions(+), 37 deletions(-)
---
diff --git a/src/mousetrap/config.py b/src/mousetrap/config.py
index 5c6bda1..d8959bc 100644
--- a/src/mousetrap/config.py
+++ b/src/mousetrap/config.py
@@ -3,34 +3,24 @@ 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 = OrderedDict([
- ('default', dirname(__file__) + '/mousetrap.yaml'),
- ('user', expanduser('~/.mousetrap.yaml')),
- ('user_specified_file', None),
- ])
-
- @classmethod
- def get_config_path(cls, key):
- return cls.SEARCH_PATH[key]
-
- def __init__(self, user_specified_file=None):
- self.SEARCH_PATH['user_specified_file']=user_specified_file
- self._load()
-
- def _load(self):
- for name, path in self.SEARCH_PATH.items():
- if path is not None and isfile(path):
- print "# Loading %s" % (path)
- with open(path) as config_file:
- config = safe_load(config_file)
- if config is not None:
- _rmerge(self, config)
- else:
- print "# Warning: config is empty."
+
+ def load(self, paths):
+ for path in paths:
+ self.load_path(path)
+ return self
+
+ def load_default(self):
+ return self.load_path(dirname(__file__) + '/mousetrap.yaml')
+
+ def load_path(self, path):
+ print "# Loading %s" % (path)
+ with open(path) as config_file:
+ config = safe_load(config_file)
+ _rmerge(self, config)
+ return self
def __getitem__(self, key):
'''
@@ -54,6 +44,8 @@ def _rmerge(target, source):
Recursively update values in target from source.
Only dicts are updated, all other values are deepcopied.
'''
+ if source is None:
+ return
for key, value in source.items():
if isinstance(value, dict):
if key not in target:
diff --git a/src/mousetrap/main.py b/src/mousetrap/main.py
index 0c8c1df..6ac965d 100644
--- a/src/mousetrap/main.py
+++ b/src/mousetrap/main.py
@@ -6,6 +6,7 @@ import logging
import logging.config
import sys
import yaml
+from os.path import dirname, expanduser, exists
from mousetrap.config import Config
@@ -16,12 +17,22 @@ class Main(object):
try:
self._args = CommandLineArguments()
self._handle_dump_annotated()
- self._config = Config(self._args.config)
+ self._config = Config().load(self._get_config_paths())
self._handle_dump_config()
self._configure_logging()
except ExitException:
sys.exit(0)
+ def _get_config_paths(self):
+ paths = [dirname(__file__) + '/mousetrap.yaml']
+ user_config = expanduser('~/.mousetrap.yaml')
+ if exists(user_config):
+ paths.append(user_config)
+ if self._args.config is not None:
+ paths.append(self._args.config)
+ return paths
+
+
def _handle_dump_annotated(self):
if self._args.dump_annotated:
self._dump_annotated()
@@ -76,3 +87,4 @@ def main():
if __name__ == '__main__':
main()
+
diff --git a/src/mousetrap/tests/run_python_tests.py b/src/mousetrap/tests/run_python_tests.py
index bde139b..9e84801 100755
--- a/src/mousetrap/tests/run_python_tests.py
+++ b/src/mousetrap/tests/run_python_tests.py
@@ -5,9 +5,9 @@ from os.path import dirname, abspath
import sys
-
from mousetrap.config import Config
-CONFIG = Config()
+CONFIG = Config().load_default()
+print CONFIG['camera']
import logging
diff --git a/src/mousetrap/tests/test_config.py b/src/mousetrap/tests/test_config.py
index 0cd2b5e..00f4688 100644
--- a/src/mousetrap/tests/test_config.py
+++ b/src/mousetrap/tests/test_config.py
@@ -1,5 +1,5 @@
import unittest
-from mousetrap.config import _rmerge
+from mousetrap.config import _rmerge, Config
class test__rmerge(unittest.TestCase):
@@ -8,21 +8,50 @@ class test__rmerge(unittest.TestCase):
self.a = {
'red': 1,
'white': ['washington', [2, 3], {'lincoln': 4}],
- 'blue': {'alpha': 5, 'list': [6, 7], 'dict': {'charlie' : 8}},
- }
+ 'blue': {
+ 'alpha': 5,
+ 'list': [6, 7],
+ 'dict': {
+ 'charlie' : 8}}}
self.b = {
'new': 9,
'white': ['replacement'],
- 'blue': {'new': 10, 'dict': {'charlie': 11, 'new': 12, 'newdict': {'some':'dict'}}},
- }
+ 'blue': {
+ 'new': 10,
+ 'dict': {
+ 'charlie': 11,
+ 'new': 12,
+ 'newdict': {
+ 'some':'dict'}}}}
self.ab = {
'new': 9,
'red': 1,
'white': ['replacement'],
- 'blue': {'new': 10, 'alpha': 5, 'list': [6, 7], 'dict': {'charlie' : 11, 'new': 12, 'newdict':
{'some':'dict'}}},
- }
+ 'blue': {
+ 'new': 10,
+ 'alpha': 5,
+ 'list': [6, 7],
+ 'dict': {
+ 'charlie' : 11,
+ 'new': 12,
+ 'newdict': {
+ 'some':'dict'}}}}
def test__rmerge(self):
_rmerge(self.a, self.b)
self.assertEqual(self.ab, self.a)
+
+ def test__rmerge_None(self):
+ from copy import deepcopy
+ original = deepcopy(self.a)
+ _rmerge(self.a, None)
+ self.assertEqual(original, self.a)
+
+
+class test_Config(unittest.TestCase):
+ def setUp(self):
+ self.config = Config().load_default()
+
+ def test_load(self):
+ self.assertTrue(isinstance(self.config['assembly'], list))
diff --git a/src/mousetrap/tests/test_gui.py b/src/mousetrap/tests/test_gui.py
index 00b143c..0d69bc2 100644
--- a/src/mousetrap/tests/test_gui.py
+++ b/src/mousetrap/tests/test_gui.py
@@ -6,7 +6,7 @@ from mousetrap.main import Config
class test_pointer(unittest.TestCase):
def setUp(self):
- self.pointer = Pointer(Config())
+ self.pointer = Pointer(Config().load_default())
def test_get_position(self):
pointer_x, pointer_y = self.pointer.get_position()
diff --git a/src/mousetrap/tests/test_vision.py b/src/mousetrap/tests/test_vision.py
index 876693b..82989ce 100644
--- a/src/mousetrap/tests/test_vision.py
+++ b/src/mousetrap/tests/test_vision.py
@@ -6,7 +6,7 @@ from mousetrap.main import Config
class test_camera(unittest.TestCase):
def setUp(self):
- self.camera = Camera(Config())
+ self.camera = Camera(Config().load_default())
def test_get_image_imageReturned(self):
image = self.camera.read_image()
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]