def __init__(self, initializer=None, age=None, **kwargs): if isinstance(initializer, dict): conf = initializer initializer = None else: conf = kwargs super(Method, self).__init__(initializer=initializer, age=age) probe = conf.get("probe", {}) resource = conf.get("resource", {}) hint = conf.get("hint", {}) target = conf.get("target", {}) if hint: # Add the hint to children. for cfg in probe: cfg["hint"] = hints.Overlay(child=cfg.get("hint", {}), parent=hint) self.probe = [Probe(**cfg) for cfg in probe] self.hint = Hint(hint, reformat=False) self.match = MatchStrToList(kwargs.get("match")) self.matcher = Matcher(self.match, self.hint) self.resource = [rdf_protodict.Dict(**r) for r in resource] self.target = triggers.Target(**target) self.triggers = triggers.Triggers() for p in self.probe: # If the probe has a target, use it. Otherwise, use the method's target. target = p.target or self.target self.triggers.Add(p.artifact, target, p)
def __init__(self, initializer=None, age=None, check_id=None, target=None, match=None, method=None, hint=None): super(Check, self).__init__(initializer=initializer, age=age) self.check_id = check_id self.match = MatchStrToList(match) self.hint = Hint(hint, reformat=False) self.target = target if method is None: method = [] self.triggers = triggers.Triggers() self.matcher = Matcher(self.match, self.hint) for cfg in method: # Use the value of "target" as a default for each method, if defined. # Targets defined in methods or probes override this default value. if hint: cfg["hint"] = hints.Overlay(child=cfg.get("hint", {}), parent=hint) if target: cfg.setdefault("target", target) # Create the method and add its triggers to the check. m = Method(**cfg) self.method.append(m) self.triggers.Update(m.triggers, callback=m) self.artifacts = set([t.artifact for t in self.triggers.conditions])
def testCheckOverlay(self): """Overlay(hint1, hint2) should populate hint2 with the values of hint1.""" # Fully populated hint. full = { "problem": "Terminator needs trousers.\n", "fix": "Give me your clothes.\n", "format": "{mission}, {target}\n", "summary": "I'll be back." } # Partial hint partial = { "problem": "Terminator needs to go shopping.", "fix": "Phased plasma rifle in the 40-watt range.", "format": "", "summary": "" } # Partial overlaid with full. overlay = { "problem": "Terminator needs to go shopping.", "fix": "Phased plasma rifle in the 40-watt range.", "format": "{mission}, {target}", "summary": "I'll be back." } # Empty hint. empty = {"problem": "", "fix": "", "format": "", "summary": ""} # Empty hint should not clobber populated hint. starts_full = full.copy() starts_empty = empty.copy() hints.Overlay(starts_full, starts_empty) self.assertDictEqual(full, starts_full) self.assertDictEqual(empty, starts_empty) # Populate empty hint from partially populated hint. starts_partial = partial.copy() starts_empty = empty.copy() hints.Overlay(starts_empty, starts_partial) self.assertDictEqual(partial, starts_partial) self.assertDictEqual(partial, starts_empty) # Overlay the full and partial hints to get the hybrid. starts_full = full.copy() starts_partial = partial.copy() hints.Overlay(starts_partial, starts_full) self.assertDictEqual(full, starts_full) self.assertDictEqual(overlay, starts_partial)