def run(self, path, minpri, args, kwargs): if stringlike(path): return self.run(path.split("."), minpri, args, kwargs) else: ok = None # checks further up the tree run first, and higher # priority checks run first for pri in sorted( (pri for pri in self.checks.keys() if pri >= minpri), reverse=True): for (name, check) in self.checks[pri]: if self.wrap(path, pri, name, check, *args, **kwargs): self.sprint("[passed {}/{}/{}]", ".".join(path), pri, name) ok = True else: self.fprint("[failed {}/{}/{}]", ".".join(path), pri, name) ok = False for (pathelt, subnode) in sorted(self.subnodes.iteritems(), key=lambda i: i[0]): snok = subnode.run( tuple(path) + (pathelt, ), minpri, args, kwargs) if ok is not False and snok is not None: ok = snok return ok
def fpath(f): if stringlike(f): return f.split('.') elif arraylike(f): return f else: return (f, )
def __init__(self, source=None, prefix=()): features = None if source is not None: if stringlike(source): try: with open(source) as json: features = load(json) except Exception as e: raise FbundleJSONBadness(src=source, wrapped=e) if not isinstance(features, dict): raise TypeError("JSON source should specify a dict") elif isinstance(source, dict): features = source else: raise TypeError("bogus source for feature bundle") def prefixify(p): if features is None: return None elif len(p) == 0: return features elif len(p) == 1: return {p[0]: features} else: return {p[0]: prefixify(p[1:])} d = prefixify(prefix) if d is not None: for (k, v) in d.iteritems(): super(FeatureBundle, self).__setitem__(k, v)
def find(self, branch): if stringlike(branch): return self.find(branch.split(".")) elif len(branch) == 0: return self elif branch[0] in self.subnodes: return self.subnodes[branch[0]].find(branch[1:]) else: return None
def add(self, branch, priority, name, check): """Add a check. - branch is where to add it: either an arraylike object or a dotted stringlike object: "foo.bar" is equivalent to ("foo", "bar"). - priority is the priority of the check: higher priority checks run first and are harder to suppress. - name is the name of the check, a stringlike object. - check is the check function itself, which should return a true value if it passes. Return value is the check function. """ if stringlike(branch): self.add(branch.split("."), priority, name, check) elif len(branch) == 0: self.checks[priority].append((name, check)) else: self.subnodes[branch[0]].add(branch[1:], priority, name, check) return check