[mousetrap/gnome3-wip: 135/240] Load default configuration from a YAML file.
- From: Heidi Ellis <heidiellis src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [mousetrap/gnome3-wip: 135/240] Load default configuration from a YAML file.
- Date: Mon, 8 Sep 2014 15:23:54 +0000 (UTC)
commit e65f025180122c41bc5f6421da97d8c5afef66fd
Author: Stoney Jackson <dr stoney gmail com>
Date: Mon Jun 23 18:45:03 2014 -0400
Load default configuration from a YAML file.
src/mousetrap/config.py | 106 +++---------------------------------
src/mousetrap/config_default.yaml | 59 ++++++++++++++++++++
src/mousetrap/main.py | 2 +-
3 files changed, 69 insertions(+), 98 deletions(-)
---
diff --git a/src/mousetrap/config.py b/src/mousetrap/config.py
index 714da20..aba91db 100644
--- a/src/mousetrap/config.py
+++ b/src/mousetrap/config.py
@@ -1,102 +1,15 @@
-class Config(dict):
- defaults = {
- 'loops_per_second': 10,
-
- 'camera': {
- 'device_index': -1, # -1 to search for device
- 'width': 400,
- 'height': 300,
- },
-
- # The plugins to load in the order they will load and run.
- 'assembly': [
- 'mousetrap.plugins.camera.CameraPlugin',
- 'mousetrap.plugins.display.DisplayPlugin',
- 'mousetrap.plugins.nose_joystick.NoseJoystickPlugin',
- 'mousetrap.plugins.eyes.EyesPlugin',
- ],
-
- 'haar_files': {
- "face": "haars/haarcascade_frontalface_default.xml",
- "nose": "haars/haarcascade_mcs_nose.xml",
- "left_eye": "haars/haarcascade_mcs_lefteye.xml",
- "open_eye": "haars/haarcascade_eye.xml",
- },
-
- # See `logging` and `logging.config`
- 'logging': {
- 'version': 1,
- 'root': {
- 'level': 'DEBUG',
- 'formatters': ['default'],
- 'handlers': ['console']
- },
- 'formatters': {
- 'default': {
- 'format': '%(asctime)s - %(name)s - %(levelname)s - %(message)s'
- }
- },
- 'handlers': {
- 'console': {
- 'class': 'logging.StreamHandler',
- 'level': 'DEBUG',
- 'formatter' : 'default',
- 'stream' : 'ext://sys.stdout'
- }
- }
- },
-
- 'classes': {
- 'mousetrap.plugins.display.DisplayPlugin': {
- 'window_title': 'MouseTrap',
- },
-
- 'mousetrap.plugins.eyes.MotionDetector': {
- 'max_samples': 5,
- },
+from yaml import safe_load
+from os.path import dirname
+from copy import deepcopy
- 'mousetrap.plugins.eyes.ClosedDetector': {
- 'max_samples': 15,
- 'min_fraction_to_be_closed': 0.8,
- },
- 'mousetrap.plugins.eyes.LeftEyeLocator': {
- 'face_detector': {
- 'scale_factor': 1.5,
- 'min_neighbors': 5,
- },
-
- 'open_eye_detector': {
- 'scale_factor': 1.1,
- 'min_neighbors': 3,
- },
-
- 'left_eye_detector': {
- 'scale_factor': 1.5,
- 'min_neighbors': 10,
- },
- },
-
- 'mousetrap.plugins.nose_joystick.NoseJoystickPlugin': {
- 'threshold': 5,
- },
-
- 'mousetrap.plugins.nose.NoseLocator': {
- 'face_detector': {
- 'scale_factor': 1.5,
- 'min_neighbors': 5
- },
-
- 'nose_detector': {
- 'scale_factor': 1.1,
- 'min_neighbors': 5
- },
- },
- },
- }
+class Config(dict):
+ DEFAULT_CONFIG_PATH = dirname(__file__) + '/' + 'config_default.yaml'
def __init__(self):
- _rmerge(self, self.defaults)
+ with open(self.DEFAULT_CONFIG_PATH) as config_file:
+ defaults = safe_load(config_file)
+ _rmerge(self, defaults)
def __getitem__(self, key):
'''
@@ -120,11 +33,10 @@ def _rmerge(target, source):
Recursively update values in target from source.
Only dicts are updated, all other values are deepcopied.
'''
- import copy
for key, value in source.items():
if isinstance(value, dict):
if key not in target:
target[key] = {}
_rmerge(target[key], value)
else:
- target[key] = copy.deepcopy(value)
+ target[key] = deepcopy(value)
diff --git a/src/mousetrap/config_default.yaml b/src/mousetrap/config_default.yaml
new file mode 100644
index 0000000..19dd8af
--- /dev/null
+++ b/src/mousetrap/config_default.yaml
@@ -0,0 +1,59 @@
+assembly:
+- mousetrap.plugins.camera.CameraPlugin
+- mousetrap.plugins.display.DisplayPlugin
+- mousetrap.plugins.nose_joystick.NoseJoystickPlugin
+- mousetrap.plugins.eyes.EyesPlugin
+camera:
+ device_index: -1
+ height: 300
+ width: 400
+classes:
+ mousetrap.plugins.display.DisplayPlugin:
+ window_title: MouseTrap
+ mousetrap.plugins.eyes.ClosedDetector:
+ max_samples: 15
+ min_fraction_to_be_closed: 0.8
+ mousetrap.plugins.eyes.LeftEyeLocator:
+ face_detector:
+ min_neighbors: 5
+ scale_factor: 1.5
+ left_eye_detector:
+ min_neighbors: 10
+ scale_factor: 1.5
+ open_eye_detector:
+ min_neighbors: 3
+ scale_factor: 1.1
+ mousetrap.plugins.eyes.MotionDetector:
+ max_samples: 5
+ mousetrap.plugins.nose.NoseLocator:
+ face_detector:
+ min_neighbors: 5
+ scale_factor: 1.5
+ nose_detector:
+ min_neighbors: 5
+ scale_factor: 1.1
+ mousetrap.plugins.nose_joystick.NoseJoystickPlugin:
+ threshold: 5
+haar_files:
+ face: haars/haarcascade_frontalface_default.xml
+ left_eye: haars/haarcascade_mcs_lefteye.xml
+ nose: haars/haarcascade_mcs_nose.xml
+ open_eye: haars/haarcascade_eye.xml
+logging:
+ formatters:
+ default:
+ format: '%(asctime)s - %(name)s - %(levelname)s - %(message)s'
+ handlers:
+ console:
+ class: logging.StreamHandler
+ formatter: default
+ level: DEBUG
+ stream: ext://sys.stdout
+ root:
+ formatters:
+ - default
+ handlers:
+ - console
+ level: DEBUG
+ version: 1
+loops_per_second: 10
diff --git a/src/mousetrap/main.py b/src/mousetrap/main.py
index a2ea270..950b5b1 100644
--- a/src/mousetrap/main.py
+++ b/src/mousetrap/main.py
@@ -12,7 +12,7 @@ import logging.config
logging.config.dictConfig(CONFIG['logging'])
LOGGER = logging.getLogger('mousetrap.main')
import yaml
-LOGGER.debug(yaml.dump(CONFIG))
+LOGGER.debug(yaml.dump(dict(CONFIG), default_flow_style=False))
from gi.repository import GObject, Gdk, Gtk
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]