def __init__(self, defaults=None, templates=None): MultiOrderedDict.__init__(self) # track an ordered id of sections Section.count += 1 self.id = Section.count self._defaults = [] if defaults is None else defaults self._templates = [] if templates is None else templates
def get(self, key, from_self=True, from_templates=True, from_defaults=True): """ Get the values corresponding to a given key. The parameters to this function form a hierarchy that determines priority of the search. from_self takes priority over from_templates, and from_templates takes priority over from_defaults. Parameters: from_self - If True, search within the given section. from_templates - If True, search in this section's templates. from_defaults - If True, search within this section's defaults. """ if from_self and key in self: return MultiOrderedDict.__getitem__(self, key) if from_templates: if self in self._templates: return [] for t in self._templates: try: # fail if not found on the search - doing it this way # allows template's templates to be searched. return t.get(key, True, from_templates, from_defaults) except KeyError: pass if from_defaults: for d in self._defaults: try: return d.get(key, True, from_templates, from_defaults) except KeyError: pass raise KeyError(key)
def keys(self, self_only=False): res = MultiOrderedDict.keys(self) if self_only: return res for d in self._templates: for key in d.keys(): if key not in res: res.append(key) for d in self._defaults: for key in d.keys(): if key not in res: res.append(key) return res
def keys(self, self_only=False): """ Get the keys from this section. If self_only is True, then keys from this section's defaults and templates are not included in the returned value """ res = MultiOrderedDict.keys(self) if self_only: return res for d in self._templates: for key in d.keys(): if key not in res: res.append(key) for d in self._defaults: for key in d.keys(): if key not in res: res.append(key) return res
def get(self, key, from_self=True, from_templates=True, from_defaults=True): if from_self and key in self: return MultiOrderedDict.__getitem__(self, key) if from_templates: if self in self._templates: return [] for t in self._templates: try: # fail if not found on the search - doing it this way # allows template's templates to be searched. return t.get(key, True, from_templates, from_defaults) except KeyError: pass if from_defaults: for d in self._defaults: try: return d.get(key, True, from_templates, from_defaults) except KeyError: pass raise KeyError(key)
def __init__(self, parent=None): self._parent = parent self._defaults = MultiOrderedDict() self._sections = MultiOrderedDict() self._includes = OrderedDict()