def __init__(self, **kwargs): """ This initialization implementation allows ordinary initialization of CommentNode objects as well as creating a DualCommentNode object using precreated or fetched CommentNode objects if provided as optional arguments primary and secondary. Parameters other than the following are from interfaces.CommentNode: :param CommentNode primary: Primary pre-created CommentNode, mainly used when creating new DualParser nodes using add_* methods. :param CommentNode secondary: Secondary pre-created CommentNode """ kwargs.setdefault("primary", None) kwargs.setdefault("secondary", None) primary = kwargs.pop("primary") secondary = kwargs.pop("secondary") if primary or secondary: assert primary and secondary self.primary = primary self.secondary = secondary else: self.primary = augeasparser.AugeasCommentNode(**kwargs) self.secondary = apacheparser.ApacheCommentNode(**kwargs) assertions.assertEqual(self.primary, self.secondary)
def _create_matching_list(self, primary_list, secondary_list): """ Matches the list of primary_list to a list of secondary_list and returns a list of tuples. This is used to create results for find_ methods. This helper function exists, because we cannot ensure that the list of search results returned by primary.find_* and secondary.find_* are ordered in a same way. The function pairs the same search results from both implementations to a list of tuples. """ matched = list() for p in primary_list: match = None for s in secondary_list: try: assertions.assertEqual(p, s) match = s break except AssertionError: continue if match: matched.append((p, match)) else: raise AssertionError("Could not find a matching node.") return matched
def set_parameters(self, parameters): """ Sets parameters and asserts that both implementation successfully set the parameter sequence """ self.primary.set_parameters(parameters) self.secondary.set_parameters(parameters) assertions.assertEqual(self.primary, self.secondary)
def add_child_comment(self, comment="", position=None): """ Creates a new child CommentNode, asserts that both implementations did it in a similar way, and returns a newly created DualCommentNode object encapsulating both of the newly created objects """ primary_new = self.primary.add_child_comment(comment, position) secondary_new = self.secondary.add_child_comment(comment, position) assertions.assertEqual(primary_new, secondary_new) new_comment = DualCommentNode(primary=primary_new, secondary=secondary_new) return new_comment
def add_child_block(self, name, parameters=None, position=None): """ Creates a new child BlockNode, asserts that both implementations did it in a similar way, and returns a newly created DualBlockNode object encapsulating both of the newly created objects """ primary_new = self.primary.add_child_block(name, parameters, position) secondary_new = self.secondary.add_child_block(name, parameters, position) assertions.assertEqual(primary_new, secondary_new) new_block = DualBlockNode(primary=primary_new, secondary=secondary_new) return new_block