Exemplo n.º 1
0
def getNode(fname, nodepath):
    """
    Searches file for particular node.  Note that this takes the first matching node path in the file.
    @ In, fname, string, name of file with XML tree to search
    @ In, nodepath, string, "."-separated string with xml node name path, i.e., Simulation.RunInfo.Sequence
    @ Out, getNode, ET.Element, prettified element
  """
    #TODO add option to include parent nodes with ellipses
    root = ET.parse(fname).getroot()
    #format nodepath
    nodepath = nodepath.replace('.', '/')
    #check if root is desired node
    if root.tag == nodepath:
        node = root
        docLevel = 0
    else:
        docLevel = len(nodepath.split('/'))
        node = xmlUtils.findPathEllipsesParents(root, nodepath, docLevel)
        if node is None:
            raise IOError('Unable to find ' + nodepath + ' in ' + fname)
    return xmlUtils.prettify(node, doc=True, docLevel=docLevel)
Exemplo n.º 2
0
    print(
        'ERROR: Test of "findPath" failed!  No element should have been found, but found',
        found)
    results['fail'] += 1
else:
    results['pass'] += 1

for f in toRemove:
    if os.path.exists(f):
        try:
            os.remove(f)
        except OSError:
            print('WARNING: In cleaning up, could not remove file', f)

#test findPathEllipsesParents
found = xmlUtils.findPathEllipsesParents(xmlTree.getroot(), 'child/cchild')
print('ellipses')
print(xmlUtils.prettify(found, doc=True))
# TODO is there supposed to be a test here?

#test bad XML tags
# rule 1: only start with letter or underscore, can't start with xml
bads = ['xmlstart', '0startnum', '.startspec']
for bad in bads:
    fixed = xmlUtils.fixXmlTag(bad)
    if fixed == '_' + bad:
        results['pass'] += 1
    else:
        print('ERROR: Fixing illegal XML tag "' + bad + '" FAILED:', fixed,
              'should be', '_' + bad)
        results['fail'] += 1