Mercurial > piecrust2
comparison piecrust/configuration.py @ 2:40fa08b261b9
Added unit tests (using `py.test`) for `Configuration`.
Fixed some configuration module bugs.
author | Ludovic Chabant <ludovic@chabant.com> |
---|---|
date | Wed, 25 Dec 2013 22:16:46 -0800 |
parents | a212a3f2e3ee |
children | f485ba500df3 |
comparison
equal
deleted
inserted
replaced
1:aaa8fb7c8918 | 2:40fa08b261b9 |
---|---|
1 import re | 1 import re |
2 import logging | |
2 import yaml | 3 import yaml |
4 | |
5 | |
6 logger = logging.getLogger(__name__) | |
3 | 7 |
4 | 8 |
5 class Configuration(object): | 9 class Configuration(object): |
6 def __init__(self, values=None, validate=True): | 10 def __init__(self, values=None, validate=True): |
7 self._values = {} | |
8 if values is not None: | 11 if values is not None: |
9 self.set_all(values, validate) | 12 self.set_all(values, validate) |
13 else: | |
14 self._values = None | |
10 | 15 |
11 def set_all(self, values, validate=True): | 16 def set_all(self, values, validate=True): |
12 if validate: | 17 if validate: |
13 self._validateAll(values) | 18 self._validateAll(values) |
14 self._values = values | 19 self._values = values |
66 | 71 |
67 def _validateValue(self, key_path, value): | 72 def _validateValue(self, key_path, value): |
68 return value | 73 return value |
69 | 74 |
70 | 75 |
71 def merge_dicts(local_cur, incoming_cur, parent_path=None, validator=None): | 76 def merge_dicts(source, merging, validator=None): |
72 if validator is None: | 77 if validator is None: |
73 validator = lambda k, v: v | 78 validator = lambda k, v: v |
79 _recurse_merge_dicts(source, merging, None, validator) | |
74 | 80 |
81 | |
82 def _recurse_merge_dicts(local_cur, incoming_cur, parent_path, validator): | |
75 for k, v in incoming_cur.iteritems(): | 83 for k, v in incoming_cur.iteritems(): |
76 key_path = k | 84 key_path = k |
77 if parent_path is not None: | 85 if parent_path is not None: |
78 key_path = parent_path + '/' + k | 86 key_path = parent_path + '/' + k |
79 | 87 |
80 local_v = local_cur.get(k) | 88 local_v = local_cur.get(k) |
81 if local_v is not None: | 89 if local_v is not None: |
82 if isinstance(v, dict) and isinstance(local_v, dict): | 90 if isinstance(v, dict) and isinstance(local_v, dict): |
83 local_cur[k] = merge_dicts(local_v, v) | 91 _recurse_merge_dicts(local_v, v, key_path, validator) |
84 elif isinstance(v, list) and isinstance(local_v, list): | 92 elif isinstance(v, list) and isinstance(local_v, list): |
85 local_cur[k] = v + local_v | 93 local_cur[k] = v + local_v |
86 else: | 94 else: |
87 local_cur[k] = validator(key_path, v) | 95 local_cur[k] = validator(key_path, v) |
88 else: | 96 else: |