Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(491)

Side by Side Diff: recipe_engine/config.py

Issue 2816083003: config: fix config.Dict with no value type (Closed)
Patch Set: Created 3 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « no previous file | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 # Copyright 2013 The LUCI Authors. All rights reserved. 1 # Copyright 2013 The LUCI Authors. All rights reserved.
2 # Use of this source code is governed under the Apache License, Version 2.0 2 # Use of this source code is governed under the Apache License, Version 2.0
3 # that can be found in the LICENSE file. 3 # that can be found in the LICENSE file.
4 4
5 """Recipe Configuration Meta DSL. 5 """Recipe Configuration Meta DSL.
6 6
7 This module contains, essentially, a DSL for writing composable configurations. 7 This module contains, essentially, a DSL for writing composable configurations.
8 You start by defining a schema which describes how your configuration blobs will 8 You start by defining a schema which describes how your configuration blobs will
9 be structured, and what data they can contain. For example: 9 be structured, and what data they can contain. For example:
10 10
(...skipping 566 matching lines...) Expand 10 before | Expand all | Expand 10 after
577 577
578 def __init__(self, item_fn=lambda i: i, jsonish_fn=dict, value_type=None, 578 def __init__(self, item_fn=lambda i: i, jsonish_fn=dict, value_type=None,
579 hidden=AutoHide): 579 hidden=AutoHide):
580 """ 580 """
581 Args: 581 Args:
582 item_fn - A function which renders (k, v) pairs to input items for 582 item_fn - A function which renders (k, v) pairs to input items for
583 jsonish_fn. Defaults to the identity function. 583 jsonish_fn. Defaults to the identity function.
584 jsonish_fn - A function which renders a list of outputs of item_fn to a 584 jsonish_fn - A function which renders a list of outputs of item_fn to a
585 JSON-compatiple python datatype. Defaults to dict(). 585 JSON-compatiple python datatype. Defaults to dict().
586 value_type - A type object used for constraining/validating the values 586 value_type - A type object used for constraining/validating the values
587 assigned to this dictionary. 587 assigned to this dictionary.
iannucci 2017/04/13 21:41:02 "If None, the value can be any type."
nodir 2017/05/10 19:55:19 Done.
588 hidden - See ConfigBase. 588 hidden - See ConfigBase.
589 """ 589 """
590 super(Dict, self).__init__(hidden) 590 super(Dict, self).__init__(hidden)
591 self.value_type = value_type 591 self.value_type = value_type
592 self.item_fn = item_fn 592 self.item_fn = item_fn
593 self.jsonish_fn = jsonish_fn 593 self.jsonish_fn = jsonish_fn
594 self.data = {} 594 self.data = {}
595 595
596 def __getitem__(self, k): 596 def __getitem__(self, k):
597 return self.data.__getitem__(k) 597 return self.data.__getitem__(k)
(...skipping 15 matching lines...) Expand all
613 def __repr__(self): 613 def __repr__(self):
614 return repr(self.data) 614 return repr(self.data)
615 615
616 def __str__(self): 616 def __str__(self):
617 return str(self.data) 617 return str(self.data)
618 618
619 def set_val(self, val): 619 def set_val(self, val):
620 if isinstance(val, Dict): 620 if isinstance(val, Dict):
621 val = val.data 621 val = val.data
622 typeAssert(val, collections.Mapping) 622 typeAssert(val, collections.Mapping)
623 for v in val.itervalues(): 623 if self.value_type:
624 typeAssert(v, self.value_type) 624 for v in val.itervalues():
625 typeAssert(v, self.value_type)
625 self.data = val 626 self.data = val
626 627
627 def as_jsonish(self, _include_hidden=None): 628 def as_jsonish(self, _include_hidden=None):
628 return self.jsonish_fn(map( 629 return self.jsonish_fn(map(
629 self.item_fn, sorted(self.data.iteritems(), key=lambda x: x[0]))) 630 self.item_fn, sorted(self.data.iteritems(), key=lambda x: x[0])))
630 631
631 def reset(self): 632 def reset(self):
632 self.data.clear() 633 self.data.clear()
633 634
634 def complete(self): 635 def complete(self):
(...skipping 269 matching lines...) Expand 10 before | Expand all | Expand 10 after
904 905
905 def _is_default(self): 906 def _is_default(self):
906 return self.data is None 907 return self.data is None
907 908
908 def schema_proto(self): 909 def schema_proto(self):
909 ret = doc.Doc.Schema() 910 ret = doc.Doc.Schema()
910 ret.enum.values_json.extend(json.dumps(self.jsonish_fn(v)) 911 ret.enum.values_json.extend(json.dumps(self.jsonish_fn(v))
911 for v in self.values) 912 for v in self.values)
912 ret.enum.required = self.required 913 ret.enum.required = self.required
913 return ret 914 return ret
OLDNEW
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698