def testFindAll(self): root = build_tree() nodes = list(moosetree.findall(root, lambda n: n.name.endswith('AB'), method=moosetree.IterMethod.PRE_ORDER)) self.assertEqual(len(nodes), 3) self.assertEqual(nodes[0].name, 'AB') self.assertEqual(nodes[1].name, 'ABCAB') self.assertEqual(nodes[2].name, 'BAB') nodes = list(moosetree.search.findall(root, lambda n: 'year' in n, method=moosetree.IterMethod.PRE_ORDER)) self.assertEqual(len(nodes), 6) self.assertEqual(nodes[0].name, 'ABC') self.assertEqual(nodes[1].name, 'ABCAB') self.assertEqual(nodes[2].name, 'BAB') self.assertEqual(nodes[3].name, 'BB') self.assertEqual(nodes[4].name, 'C') self.assertEqual(nodes[5].name, 'CB') self.assertEqual(nodes[0]['year'], 1980) self.assertEqual(nodes[1]['year'], 2013) self.assertEqual(nodes[2]['year'], 1954) self.assertEqual(nodes[3]['year'], 1949) self.assertEqual(nodes[4]['year'], 2011) self.assertEqual(nodes[5]['year'], 1980) nodes = list(moosetree.findall(root, lambda n: n.get('year') == 1980, method=moosetree.IterMethod.PRE_ORDER)) self.assertEqual(len(nodes), 2) self.assertIs(nodes[0].name, 'ABC') self.assertIs(nodes[1].name, 'CB') self.assertEqual(nodes[0]['year'], 1980) self.assertEqual(nodes[1]['year'], 1980)
def postTokenize(self, page, ast): """Remove empty SQARequirementMatrix tokens""" for node in moosetree.findall( ast.root, func=lambda n: n.name == 'SQARequirementMatrix'): if not any(n.name == 'SQARequirementMatrixItem' for n in node.children): node.parent = None
def findall(self, name, fuzzy=True): """ Locate all nodes withing the tree starting from this node. Inputs: name[str]: The name to search for within the tree. fuzzy[bool]: When True (the default) a "fuzzy" search is performed, meaning that the provide name must be in the node name. If this is set to False the names must match exact. """ func = lambda n: (fuzzy and name in n.fullpath) or (not fuzzy and name == n.fullpath) return moosetree.findall(self, func, method=moosetree.IterMethod.PRE_ORDER)
def postTokenize(self, page, ast): index = [] title = None for head in moosetree.findall(ast, lambda n: n.name == 'Heading'): if (head['level'] == 1) and (title is None): title = head.text() id_ = head.get('id') if id_ == '': id_ = head.text('-').lower() index.append(dict(title=title, text=head.text(), bookmark=id_)) page['search'].extend(index)
def preRender(self, page, ast, result): """Cache Shortcut tokens in the page attributes.""" page['shortcuts'].clear() # in case this page is being live reloaded for node in moosetree.findall(ast, lambda n: (n.name == 'Shortcut')): key = node['key'] if key not in page['shortcuts']: page['shortcuts'][key] = node.copy() else: msg = "The shortcut link key '{}' has already been used." LOG.error( report_error( msg.format(key), page.source, node.info.line if node.info else None, node.info[0] if node.info and node.info[0].strip() else ''))
def createHTML(self, parent, token, page): hide = token['hide'] levels = token['levels'] func = lambda n: (n.name == 'Heading') and (n['level'] in levels) and (n is not token) \ and (n['id'] not in hide) toks = moosetree.findall(token.root, func) div = html.Tag(parent, 'div', class_='moose-table-of-contents') div.addStyle('column-count:{}'.format(token['columns'])) for tok in toks: id_ = tok['id'] bookmark = id_ if id_ else tok.text('-').lower() link = core.Link(None, url='#{}'.format(bookmark)) tok.copyToToken(link) core.LineBreak(link) self.renderer.render(div, link, page)
def postTokenize(self, ast, page, meta, reader): """Capture the search results.""" if not self.get('search', False): return index = [] title = None for head in moosetree.findall(ast, lambda n: n.name == 'Heading'): if (head['level'] == 1) and (title is None): title = head.text() id_ = head.get('id') if id_ == '': id_ = head.text('-').lower() index.append(dict(title=title, text=head.text(), bookmark=id_)) meta.getData('search').extend(index)
def __nodeFinder(self, node_type, syntax='', group=None, recursive=False): """ A helper method for finding nodes of a given type, syntax, and group. Inputs: node_type[NodeCore]: The type of node to consider. syntax: (optional) The syntax that must be within the object 'fullpath' property. group: (optional) The group to limit the search. recursive: When True the search will look through all nodes in the entire tree, when False only the children of the node are considered. """ if recursive: filter_ = lambda node: (syntax in node.fullpath()) and \ isinstance(node, node_type) and \ (group is None or group in node.groups()) return moosetree.findall(self, filter_) else: return [node for node in self.children if (syntax in node.fullpath()) and \ isinstance(node, node_type) and \ (group is None or group in node.groups())]
def check(self, location): # List of errors messages = [] # Load the test spec and create a list of PythonUnitTest files tested = set() spec = os.path.join(location, 'tests') if not os.path.exists(spec): if glob.glob(os.path.join(spec, '*.py')): messages.append("Missing a test spec file in '{}'".format(os.path.dirname(spec))) else: node = pyhit.load(os.path.join(location, 'tests')) # check for PythonUnitTest blocks in [Tests] block = moosetree.find(node, lambda n: n.name=='Tests') for subblock in moosetree.findall(block, lambda n: n): if subblock['type'] == 'PythonUnitTest': tested.add(subblock['input']) # Loop through python files in this directory for filename in glob.glob(os.path.join(location, '*.py')): # Local filename base = os.path.basename(filename) # Load the module (tried this with os.chdir, but that didn't work) sys.path.append(location) mod = __import__(base[:-3]) sys.path.remove(location) # Get a lit of unittest.TestCase objects, if they exist this file should be in spec tests = get_parent_objects(mod, unittest.TestCase) if tests and (base not in tested): msg = "The test script '{}' is not included in the tests spec '{}'." messages.append(msg.format(base, spec)) return messages
def testFindAttr(self): root = build_tree() nodes = list(moosetree.findall(root, year=1980)) self.assertEqual(len(nodes), 2) self.assertEqual(nodes[0].name, 'CB') self.assertEqual(nodes[1].name, 'ABC')