Repository navigation
Expand file tree
/
Copy pathstructures.py
More file actions
1106 lines (882 loc) · 39.2 KB
/
Copy pathstructures.py
File metadata and controls
1106 lines (882 loc) · 39.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
from collections import defaultdict, OrderedDict
import io
import similarity
import yc_yaml as yaml
from .elements import (
ConfigElement,
RequiredError,
DiscontinuedError,
NullList,
ConfigDict,
)
from .scalars import ScalarElem
class ListElem(ConfigElement):
"""A list of configuration items. All items in the list must be the same
ConfigElement type. Configuration inheritance appends new, unique items
to these lists.
A shortcut is allowed in the interpretation of lists, such that a single
value is interpreted as a single valued list. Each of the following is
valid. ::
colors: red
colors: [red, yellow, green]
colors:
- red
- yellow
- blue
However, if the sub-element is another list, the lists must always be
explicit. ::
collections:
- [quarter, dime, nickel]
- [thing1, thing2, thing3]
collections: [[quarter, dime, nickel], [thing1, thing2, thing3]
When dumping configs, the lists are always given explicitly.
"""
type = list
type_name = 'list of items'
def __init__(self, name=None, sub_elem=None, min_length=0, max_length=None,
defaults=None, **kwargs):
"""
:param sub_elem: A ConfigItem that each item in the list must conform
to.
:param min_length: The minimum number of items allowed, inclusive.
Default: 0
:param max_length: The maximum number of items allowed, inclusive. None
denotes unlimited.
:param [] defaults: Defaults on a list are items that are added to the
list if no items are explicitly added.
"""
if defaults is None:
defaults = NullList()
if sub_elem is None:
raise ValueError("ListElem sub_elem argument is required.")
if min_length < 0:
raise ValueError("min_length must be a positive integer.")
self.min_length = min_length
self.max_length = max_length
super(ListElem, self).__init__(name=name, _sub_elem=sub_elem,
default=defaults, **kwargs)
def _check_range(self, value):
"""Make sure our list is within the appropriate length range."""
vlen = len(value)
if vlen < self.min_length:
return False
if self.max_length is None or vlen <= self.max_length:
return True
else:
return False
def normalize(self, value, root_name=None):
"""Lists with a value of None remain as None, and empty lists stay
empty. Single values, however, become a list of that value. All
contained value are recursively normalized."""
if root_name is not None:
name = root_name
else:
name = self.name
if value is None or value == [None]:
return None
elif isinstance(value, self.type):
return [self._sub_elem.normalize(v, root_name=name) for v in value]
else:
return [self._sub_elem.normalize(value, root_name=name)]
def validate(self, value, partial=False):
"""Just like the parent function, except converts None to an empty
list, and single values of other types into the subtype for this
list. Subvalues are recursively validated against the subitem type
(whose name is ignored).
:param list value: A list of values to validate and add.
:param bool partial: Ignore 'required'
"""
if value is None:
return self.default.copy()
vvals = self.type()
for val in value:
vvals.append(self._sub_elem.validate(val, partial=partial))
if not self._check_range(value):
raise ValueError(
"Expected [{}-{}] list items for {} field {}, got {}."
.format(
self.min_length,
self.max_length if self.max_length is not None else 'inf',
self.__class__.__name__,
self.name,
len(value)))
for i in range(len(value)):
vvals[i] = self._run_post_validator(self._sub_elem, value,
vvals[i])
if self.required and not (partial or value):
raise RequiredError("Required ListElem '{}' is empty."
.format(self.name))
return vvals
def _choices_doc(self):
return "May contain {min} - {max} items.".format(
min=self.min_length,
max=self.max_length if self.max_length is not None else 'inf'
)
def find(self, dotted_key):
if dotted_key == '':
return self
else:
parts = dotted_key.split('.', maxsplit=1)
key = parts[0]
next_key = parts[1] if len(parts) == 2 else ''
if key != '*':
raise KeyError(
"Invalid dotted key for {} called '{}'. List elements "
"must have their element name given as a *, since it's "
"sub-element isn't named. Got '{}' from '{}' instead."
.format(self.__class__.__name__, self.name, key,
dotted_key))
return self._sub_elem.find(next_key)
def yaml_events(self, value, show_comments, show_choices):
"""Create the events for a ListElem."""
events = list()
events.append(yaml.SequenceStartEvent(
anchor=None,
tag=None,
implicit=True,
flow_style=False,
))
if show_comments and (
(not isinstance(self._sub_elem, ScalarElem) or
self._sub_elem.help_text)):
comment = self._sub_elem.make_comment(
show_choices=show_choices,
recursive=True,
)
events.append(yaml.CommentEvent(value=comment))
if not value:
value = self.type()
# Value is expected to be a list of items at this point.
for val in value:
events.extend(
self._sub_elem.yaml_events(
value=val,
show_comments=False,
show_choices=show_choices))
events.append(yaml.SequenceEndEvent())
return events
def merge(self, old, new, extend=False):
"""When merging lists, a new list simply replaces the old list, unless
the new list is empty."""
# Don't override with an empty list that was defined implicitly.
if ((not new and isinstance(new, NullList)) or
new is None):
if old is None:
return list()
else:
return old
if extend and old:
return old.copy() + new.copy()
else:
return new.copy()
def comment_type_str(self):
return '[{}]'.format(self._sub_elem.comment_type_str())
class _DictElem(ConfigElement):
type = ConfigDict
KC_LOWER = 'lower'
KC_UPPER = 'upper'
KC_MIXED = 'mixed'
def __init__(self, key_case=None, **kwargs):
if key_case is None:
key_case = self.KC_MIXED
if key_case not in (self.KC_LOWER, self.KC_UPPER, self.KC_MIXED):
raise ValueError(
"Invalid key case. Expected one of <cls>.KC_LOWER, "
"<cls>.KC_UPPER, <cls>.KC_MIXED")
self._key_case = key_case
super(_DictElem, self).__init__(**kwargs)
def find(self, dotted_key):
raise NotImplementedError
def validate(self, value, partial=False):
raise NotImplementedError
def _key_check(self, values_dict):
"""Raises a KeyError if keys don't match the key regex, or there are
duplicates. Keys are converted to upper, lower, or left in their
original case depending on 'self._key_case'.
:param {} values_dict: The dictionary to check.
"""
if not isinstance(values_dict, self.type):
try:
values_dict = self.type(values_dict)
except (ValueError, KeyError):
raise ValueError(
"Invalid values ({}) for element {}. Expected '{}'"
.format(values_dict, self.name, self.type))
# Check for duplicate keys.
keys = defaultdict(lambda: [])
for key in values_dict.keys():
if key is None:
key_mod = key
elif self._key_case is self.KC_LOWER:
key_mod = key.lower()
elif self._key_case is self.KC_UPPER:
key_mod = key.upper()
else:
key_mod = key
if key_mod and key_mod.endswith('+'):
key_mod = key_mod[:-1]
keys[key_mod].append(key)
if key_mod is not None and self._NAME_RE.match(key_mod) is None:
raise KeyError(
"Invalid key '{}' in {} called {}. Key does not match "
"expected regular expression '{}'"
.format(key_mod, self.__class__.__name__, self.name,
self._NAME_RE.pattern))
for k_list in keys.values():
if len(k_list) != 1:
raise KeyError("Duplicate keys given {} in {} called {}. ("
"Keys in this config object are automatically "
"converted to {}case."
.format(k_list, self.__class__.__name__,
self.name, self._key_case))
def _merge_extend(self, key, sub_elem, old_value, value):
"""Append list items to a list element."""
if not isinstance(sub_elem, ListElem):
raise KeyError("Key '{}+' given under '{}', but underlying config item "
"isn't a list.".format(key, self.name))
return sub_elem.merge(old_value, value, extend=True)
class KeyedElem(_DictElem):
"""A dictionary configuration item with predefined keys that may have
non-uniform types. The valid keys are are given as ConfigItem objects,
with their names being used as the key name."""
type = ConfigDict
_type_name = ''
def __init__(self, name=None, elements=None, key_case=_DictElem.KC_MIXED,
**kwargs):
"""
:param key_case: Must be one of the <cls>.KC_* values. Determines
whether keys are automatically converted to lower or upper case,
or left alone.
:param elements: This list of config elements is also forms the list
of accepted keys, with the element.name being the key.
"""
self.config_elems = OrderedDict()
if elements is None:
elements = []
super(KeyedElem, self).__init__(name=name, key_case=key_case, **kwargs)
for i in range(len(elements)):
self.add_element(elements[i])
def add_element(self, elem):
# Make sure each sub-element has a usable name that can be used
# as a key..
if elem.name is None:
raise ValueError(
"In KeyedConfig item ({}), subitem {} has name of None."
.format(
self.name if self.name is not None else '<unnamed>',
elem))
# Make sure derived elements have a resolver defined somewhere.
if isinstance(elem, DerivedElem):
self._find_resolver(elem)
self.config_elems[elem.name] = elem
def _find_resolver(self, elem):
"""Find the resolver function for the given DerivedElem. Try the
elem.resolve method first, then look for a 'get_<elem.name>' method
next. Raises a ValueError on failure."""
if elem.resolve is not None:
return elem.resolve
elif hasattr(self, 'resolve_' + elem.name):
return getattr(self, 'resolve_' + elem.name)
else:
raise ValueError(
"Could not find resolver for derived element '{}' in {} "
"called {}."
.format(elem.name, self.__class__.__name__, self.name))
def find(self, dotted_key):
if dotted_key == '':
return self
else:
parts = dotted_key.split('.', maxsplit=1)
key = parts[0]
next_key = parts[1] if len(parts) == 2 else ''
if key not in self.config_elems:
raise KeyError(
"Invalid dotted key for {} called '{}'. KeyedElem "
"element names must be in the defined keys. "
"Got '{}' from '{}', but valid keys are {}"
.format(self.__class__.__name__, self.name,
key, dotted_key, self.config_elems.keys()))
return self.config_elems[key].find(next_key)
find.__doc__ = ConfigElement.find.__doc__
def find_key_matches(self, word, min_score=0.8):
"""Find similar keys to the one given."""
return similarity.find_matches(word, self.config_elems.keys(), min_score=min_score)
def find_sub_key_matches(self, key):
"""Find potential matches in KeyedElements under this one."""
sub_key_matches = []
for sub_key, config_elem in self.config_elems.items():
if isinstance(config_elem, KeyedElem):
similar = config_elem.find_key_matches(key, min_score=0.8)
if similar:
sub_key_matches.append((sub_key, similar))
return sub_key_matches
def get_sub_comments(self, show_choices, show_name):
"""Add the comment description for each sub element at the top
of Keyed Elements."""
sub_comments = []
for key, elem in self.config_elems.items():
sub_comments.append(' ' + elem.make_comment(
show_choices=True,
show_name=show_name,
recursive=True
))
return sub_comments
def merge(self, old, new):
if old is None:
return new
base = old.copy()
if new is None:
return base
for key, value in new.items():
if key and key.endswith('+'):
key = key[:-1]
base[key] = self._merge_extend(key, self.config_elems[key],
old[key], value)
elif value is not None:
try:
base[key] = self.config_elems[key].merge(old.get(key), new[key])
except:
raise
return base
def _make_missing_key_message(self, root_name, key, value):
"""Generate a message for when a matching key isn't found. Searches for similar
keys in this and child KeyedElements."""
name = root_name if root_name is not None else self.name
msg = ["Invalid config key '{}' given under '{}'."
.format(key, name)]
matches_found = False
similar = self.find_key_matches(key)
if similar:
msg.append("Did you mean one of these?")
matches_found = True
for skey in similar:
help_text = self.config_elems[skey].help_text
if help_text:
help_text = help_text[:60] + '...'
msg.append(' {} - "{}"'.format(skey, help_text))
else:
msg.append(' {}')
sub_key_matches = self.find_sub_key_matches(key)
if sub_key_matches:
matches_found = True
msg.append("Config elements under this one have similar keys:")
msg.append("{}:".format(name))
for sub_key, matches in sub_key_matches:
msg.append(" {}:".format(sub_key))
for match in matches:
help_text = self.config_elems[sub_key].config_elems[match].help_text
msg.append(" {}: # {}...".format(match, help_text[:60]))
if not matches_found:
for sub_key, sub_elem in self.config_elems.items():
if not isinstance(sub_elem, CategoryElem):
continue
try:
sub_elem.normalize(value)
except (ValueError, KeyError, AttributeError):
continue
msg.append("Key '{}' not found under '{}', but the value works as a key under '{}':"
.format(key, root_name, sub_key))
new_val = {root_name: {sub_key: value}}
buffer = io.StringIO()
yaml.dump(new_val, buffer)
for line in buffer.getvalue().split('\n'):
msg.append(' ' + line)
return '\n'.join(msg)
def normalize(self, value, root_name=None):
"""None remains None. Everything else is recursively normalized
by their element objects. Unknown keys and non-dict 'values'
result in an error.
:param dict value: The dict of values to normalize.
:param root_name: What to call this if it is the root element.
:raises KeyError: For unknown keys.
:raises TypeError: if values isn't a dict.
"""
name = root_name if root_name is not None else self.name
if value is None:
return None
if not isinstance(value, dict):
raise TypeError("Config element '{}' is expected to be "
"a dict/mapping, but got '{}'"
.format(self.name, value))
ndict = self.type()
for key, val in value.items():
if key and key.endswith('+'):
final_key = key[:-1]
else:
final_key = key
elem = self.config_elems.get(final_key, None)
if elem is None:
msg = self._make_missing_key_message(root_name, key, value)
raise KeyError(msg, key)
try:
ndict[key] = elem.normalize(val, root_name=key)
except DiscontinuedError as err:
# This is thrown because the key is no longer allowed.
raise
except ValueError as err:
if isinstance(val, dict):
msg = [err.args[0]]
msg.append("Key '{}.{}' does not take dictionary values. "
"Perhaps these values belong under a different key?"
.format(name, key))
raise ValueError("\n".join(msg))
else:
raise
except KeyError as err:
# Check to see if this level takes the key that was erroneous at the next
# level, and suggest a course of action.
msg = err.args[0]
if len(err.args) > 1:
err_key = err.args[1]
if err_key in self.config_elems:
addtl_msg = "The level above '{}' takes key '{}' - maybe key '{}' "\
"is over-indented?".format(key, err_key, err_key)
msg = '\n'.join([msg, addtl_msg])
raise KeyError(msg)
return ndict
def validate(self, value, partial=False):
"""Ensure the given values conform to the config specification. Also
adds any derived element values.
:param dict value: The dictionary of values to validate.
:param bool partial: Ignore 'required'.
:returns dict: The validated and type converted value dict.
:raises TypeError: if type conversion can't occur.
:raises ValueError: When the value is not within range.
:raises RequiredError: When a required value is missing.
:raises KeyError: For duplicate or malformed keys.
"""
if value is None:
if self.required and not partial:
raise RequiredError("Missing required KeyedElem '{}' in "
"config.".format(self.name))
value = self.type()
self._key_check(value)
# Change the key case.
for key in list(value.keys()):
if self._key_case == self.KC_LOWER:
new_key = key.lower()
elif self._key_case == self.KC_UPPER:
new_key = key.upper()
else:
new_key = key
# Make sure extended keys are trying to extend lists.
if new_key and new_key.endswith('+'):
sub_elem = self.config_elems.get(key[:-1], None)
if not isinstance(sub_elem, ListElem):
raise KeyError(
"Key '{}' given (which would extend the list of values under key '{}'), "
"but the values under that key aren't a list."
.format(key, new_key[:-1]))
# When validating, it's ok if we end up with a '+' key. Just
# drop the plus.
new_key = new_key[:-1]
if key != new_key:
value[new_key] = value[key]
del value[key]
# Make sure each key is defined in the KeyedElement
for key in value:
if key not in self.config_elems:
raise KeyError(
"Key '{}' under KeyedElem '{}' does not appear in the "
"elements list: {}."
.format(key, self.name, self.config_elems.keys())
)
derived_elements = []
for key, elem in self.config_elems.items():
if isinstance(elem, DerivedElem):
derived_elements.append(elem)
else:
# Validate each of the subkeys in this dict.
value[key] = self.config_elems[key].validate(
value.get(key),
partial=partial,
)
# Handle any derived elements
for elem in derived_elements:
value[elem.name] = self._find_resolver(elem)(value)
# Run custom post validation against each key, if given.
for key, elem in self.config_elems.items():
value[key] = self._run_post_validator(elem, value,
value[key])
return value
def yaml_events(self, value,
show_comments,
show_choices):
if value is None:
value = dict()
events = list()
events.append(yaml.MappingStartEvent(anchor=None, tag=None,
implicit=True))
for key, elem in self.config_elems.items():
if elem.hidden:
continue
# Don't output anything for Derived Elements
if isinstance(elem, DerivedElem):
continue
val = value.get(key, None)
if show_comments:
comment = elem.make_comment(show_choices=show_choices)
events.append(yaml.CommentEvent(value=comment))
# Add the mapping key
events.append(yaml.ScalarEvent(value=key, anchor=None,
tag=None, implicit=(True, True)))
# Add the mapping value
events.extend(elem.yaml_events(val,
show_comments,
show_choices))
events.append(yaml.MappingEndEvent())
return events
class CategoryElem(_DictElem):
"""A dictionary config item where all the keys must be of the same type,
but the key values themselves do not have to be predefined. The possible
keys may still be restricted with the choices argument."""
type = ConfigDict
_type_name = ''
def __init__(self, name=None, sub_elem=None, defaults=None, allow_empty_keys=False,
key_case=_DictElem.KC_MIXED, **kwargs):
"""Initialize the Config Element.
:param name: The name of this Config Element
:param ConfigElement sub_elem: The type all keys in this mapping must
conform to. (required)
:param Union(None,list) choices: The possible keys for this element.
None denotes that any are valid.
:param bool required: Whether this element is required.
:param Union[dict,None] defaults: An optional dictionary of default
key:value pairs.
:param bool allow_empty_keys: Allow dict keys to be None when validating.
:param str key_case: Must be one of the <cls>.KC_* values. Determines
whether keys are automatically converted to lower or upper case,
or left alone.
:param help_text: Description of the purpose and usage of this element.
"""
if defaults is None:
defaults = dict()
if isinstance(sub_elem, DerivedElem):
raise ValueError(
"Using a derived element as the sub-element in a CategoryElem "
"does not make sense.")
self.allow_empty_keys = allow_empty_keys
self._sub_elem = sub_elem
super(CategoryElem, self).__init__(name=name, _sub_elem=sub_elem,
key_case=key_case,
default=defaults, **kwargs)
def normalize(self, value: dict, root_name=None):
"""Make sure values is a dict, and recursively normalize the contained
keys. Returns None if values is None."""
if value is None:
return None
out_dict = self.type()
if not isinstance(value, dict):
raise TypeError("Expected a dict/mapping for key '{}', got '{}'."
.format(self.name, value))
for key, val in value.items():
out_dict[key] = self._sub_elem.normalize(val, root_name=key)
return out_dict
def validate(self, value, partial=False):
"""Check the keys, and validate each value against the sub-elements
validator."""
value_dict = value
if value_dict is None:
if self.required and not partial:
raise RequiredError("Missing CategoryElem '{}' in config."
.format(self.name))
value_dict = {}
out_dict = self.type()
# Pre-fill our output dictionary with hard coded defaults.
if self._default is not None:
for key, val in self._default:
out_dict[key] = val
# Change the key case.
for key in list(value_dict.keys()):
if key is None:
if not self.allow_empty_keys:
raise ValueError("Invalid key for {} - key value is None.".format(self.name))
new_key = key
elif self._key_case == self.KC_LOWER:
new_key = key.lower()
elif self._key_case == self.KC_UPPER:
new_key = key.upper()
else:
new_key = key
if new_key and new_key.endswith('+'):
if not isinstance(self._sub_elem, ListElem):
raise KeyError(
"Key '{}' tried to extend non-list key '{}'."
.format(key, new_key[:-1]))
new_key = new_key[:-1]
if key != new_key:
value_dict[new_key] = value_dict[key]
del value_dict[key]
for key, val in value_dict.items():
if self._choices is not None and key not in self._choices:
raise ValueError(
"Invalid key for {} called {}. '{}' not in given choices."
.format(self.__class__, self.name, key)
)
validated_value = self._sub_elem.validate(val, partial=partial)
# Merge the validated values with the defaults from the hard
# coded defaults if present.
if key in out_dict:
out_dict[key] = self._sub_elem.merge(out_dict[key],
validated_value)
else:
out_dict[key] = validated_value
# Make sure the keys are sane
self._key_check(value_dict)
for key, val in out_dict.items():
out_dict[key] = self._run_post_validator(self._sub_elem, out_dict,
val)
return out_dict
def merge(self, old, new):
if old is None:
return new
base = old.copy()
if new is None:
return base
for key, value in new.items():
if key and key.endswith('+'):
key = key[:-1]
old_value = old.get(key, [])
base[key] = self._merge_extend(key, self._sub_elem, old_value, value)
else:
old_value = old.get(key)
if old_value is None:
base[key] = value
else:
base[key] = self._sub_elem.merge(old_value, value)
return base
def find(self, dotted_key):
if dotted_key == '':
return self
else:
parts = dotted_key.split('.', maxsplit=1)
key = parts[0]
next_key = parts[1] if len(parts) == 2 else ''
if key != '*':
raise KeyError(
"Invalid dotted key for {} called '{}'. CategoryElem "
"must have their element name given as a *, since it's "
"sub-element isn't named. Got '{}' from '{}' instead."
.format(self.__class__.__name__, self.name,
key, dotted_key))
return self._sub_elem.find(next_key)
def yaml_events(self, value, show_comments, show_choices):
"""Create a mapping event list, based on the values given."""
if value is None:
value = dict()
events = list()
events.append(yaml.MappingStartEvent(anchor=None, tag=None,
implicit=True))
if show_comments:
comment = self._sub_elem.make_comment(
show_choices=show_choices,
show_name=False)
events.append(yaml.CommentEvent(value=comment))
if value:
for key, val in value.items():
# Add the mapping key.
events.append(yaml.ScalarEvent(value=key, anchor=None,
tag=None,
implicit=(True, True)))
# Add the mapping value.
events.extend(
self._sub_elem.yaml_events(
val,
show_comments,
show_choices))
events.append(yaml.MappingEndEvent())
return events
# ext_print: 30
class DefaultedCategoryElem(CategoryElem):
"""This allows you to create a category with user defined defaults. It's
essentially a category element with a KeyedElem as the sub-element. If
the '__default__' key is present, values from that dict act as defaults
for the others. The elements of the '__default__' key are all considered
optional, regardless of whether they are marked as required or not.
Example: ::
import yaml_config as yc
cars = yc.DefaultedElement(elements=[
yc.IntElem('wheels', required=True),
yc.StrElem('drivetrain', required=True, default='2WD'),
yc.StrElem('color')
]
results = cars.validate({
'__default__': {
'wheels': 4,
'color': 'red'},
'jeep': {
'color': 'blue',
'drivetrain': '4WD'},
'reliant_robin': {
'wheels': 3}
})
# Should be 4
results.jeep.wheels
"""
def __init__(self, name=None, elements=None, default_key='_', **kwargs):
"""
:param elements: A list of ConfigElement instances, just like for a
KeyedElem.
:param default_key: The key to use for default values.
"""
self.default_key = default_key
super(DefaultedCategoryElem, self).__init__(name=name,
sub_elem=KeyedElem(
elements),
**kwargs)
def validate(self, value, partial=False):
value_dict = value
out_dict = self.type()
# Make sure the keys are sane
self._key_check(value_dict)
defaults = {}
if self.default_key in value_dict:
defaults = value_dict[self.default_key]
del value_dict[self.default_key]
# Prefill our output dictionary with hard coded defaults.
if self._default is not None:
for key, val in self._default:
out_dict[key] = val
for key, base_value in value_dict.items():
key = key.lower()
if self._choices is not None and key not in self._choices:
raise ValueError(
"Invalid key for {} called {}. '{}' not in given choices.")
# Use the defaults from self.DEFAULT_KEY as the base for each value.
val = defaults.copy()
# We know our sub-element is a dict in this case.
val.update(base_value)
validated_value = self._sub_elem.validate(val, partial)
# Merge the validated values with the defaults from the hard
# coded defaults if present.
if key in out_dict:
out_dict[key] = self._sub_elem.merge(out_dict[key],
validated_value)
else:
out_dict[key] = validated_value
for key, val in out_dict.items():
out_dict[key] = self._run_post_validator(self._sub_elem, out_dict,
val)
return out_dict
class DerivedElem(ConfigElement):
"""The value is derived from the values of other elements. This is only
valid when used as an element in a KeyedElem (or YamlConfigLoader),
trying to use it elsewhere will raise an exception (It simply doesn't
make sense anywhere else).
Resolution of this element is deferred until after all non-derived
elements are resolved. All derived elements are then resolved in the
order they were listed. This resolution is performed by a function,
which can be given either:
- As the 'resolver' argument to __init__
- The 'resolve' method of this class
- The 'resolve_<name>' method of the parent KeyedElem or
YamlConfigLoader class.
This function is expected to take one positional argument, which is the
dictionary of validated values from the KeyedElem so far.
"""
def __init__(self, name, resolver=None, **kwargs):
if name is None:
raise ValueError("Derived Elements must be named.")
if resolver is not None:
self.resolve = resolver
else:
self.resolve = self._resolve
super(DerivedElem, self).__init__(name=name, _sub_elem=None, **kwargs)
def _resolve(self, siblings):
"""A resolver function gets a dictionary of its returned sibling's
values, and should return the derived value. A resolver passed as an
argument to DerivedElem's __init__ should have the same signature (
without the self).
:param {} siblings: The dictionary of validated values from the
keyed element that this is a part of.
:returns: The derived element value
"""
# Using vars to get rid of syntax notifications.
_ = self, siblings
return None