예제 #1
0
def addNewPath(trees, target, value):
    """
    Adds a path and value to existing trees. Creates starting only at missing portion.
    @ In, trees, list(TreeStructure.InputTree), existing information
    @ In, target, list(string), nodal path to desired target as [Block1, Block2, entry]
    @ In, value, object, desired value for entry
    @ Ou, trees, list(TreeStructure.InputTree), modified list of trees
  """
    foundSoFar = None
    for t, targ in enumerate(target):
        found = findInGetpot(trees, target[:t + 1])
        if found is not None:
            foundSoFar = found
        else:
            break
    # if starting from scratch ...
    if foundSoFar is None:
        base = TreeStructure.InputNode(target[0])
        tree = TreeStructure.InputTree(base)
        trees.append(tree)
        foundSoFar = [base]
        t = 1
    # add missing nodes
    current = foundSoFar[-1]
    for a, addTarg in enumerate(target[t:]):
        new = TreeStructure.InputNode(addTarg)
        current.append(new)
        if t + a == len(target) - 1:
            new.text = str(value)
        current = new
    return trees
예제 #2
0
def getpotToInputTree(getpot):
    """
    Converts getpot input to RAVEN InputTree structure
    @ In, getpot, string, string to read in (with newlines, could be open file object)
    @ Out, tree, TreeStructure.NodeTree, tree with information
  """
    getpot = preprocessGetpot(getpot)
    roots = []
    parentNodes = [
    ]  # stack trace of parent nodes, as [First, Second, Third] nested
    currentNode = None  # current node having objects added to it
    # track multiline vectors
    multilineKey = None  # attribute name
    multilineValue = None  # multiline value
    multilineIndicator = None  # indicator to open/close, either ' or "
    for line in getpot:
        if multilineValue is not None:
            if multilineIndicator in line:
                # the multiline is done, so close and record it
                value, leftover = line.split(multilineIndicator, maxsplit=1)
                addSpace = ' ' if value.strip() else ''
                multilineValue += addSpace + value.strip() + multilineIndicator
                # set up to close entry
                attribute = multilineKey
                value = multilineValue
                closeEntry = True
                # reset
                multilineKey = None
                multilineValue = None
                multilineIndicator = None
            else:
                # still open and not closed, so keep appending
                addSpace = ' ' if multilineValue[
                    -1] != multilineIndicator else ''
                multilineValue += addSpace + line.strip()
                closeEntry = False
        else:
            line = line.strip()
            #------------------
            # starting new node
            if line.startswith(
                ('[./', '[')) and line.endswith(']') and line not in ('[../]',
                                                                      '[]'):
                #if child node, stash the parent for now
                if currentNode is not None:
                    parentNodes.append(currentNode)
                currentNode = TreeStructure.InputNode(tag=line.strip('[]./'))
                closeEntry = False
            #------------------
            # closing node
            elif line.startswith(('[../]', '[]')):
                if parentNodes:
                    parentNodes[-1].append(currentNode)
                    #make parent the active node
                    currentNode = parentNodes.pop()
                else:
                    #this is a root
                    roots.append(currentNode)
                    currentNode = None
                closeEntry = False
            #------------------
            # attributes and values
            elif '=' in line:
                attribute, value = (x.strip()
                                    for x in line.split('=', maxsplit=1))
                # TODO multilline, if "'" or '"' in line
                # ASSUME: both " and ' aren't used in the same line
                if any(x in line for x in multiIndicators):
                    indicator = '\'' if '\'' in line else '\"'
                    if line.count(
                            indicator
                    ) % 2 == 0:  # NOTE this may be more complex than needed, can you really use 4, 6?
                        # closes within line
                        value = value.strip('\'\"')
                        closeEntry = True
                    else:
                        # multiline
                        multilineKey = attribute
                        multilineValue = value
                        multilineIndicator = indicator
                        closeEntry = False
                        # DO NOT continue, keep going until multiline is closed
                        continue
                else:
                    # single line entry with no vector representation
                    value = value.strip()
                    closeEntry = True
            #------------------
            # we don't know what's going on here
            else:
                raise IOError('Unrecognized line syntax: "{}"'.format(line))
        #------------------
        # if the "value" if the "attribute" is closed, record the entry
        if closeEntry:
            if attribute in (
                    c.tag for c in currentNode.children):  #currentNode.attrib:
                raise IOError(
                    'Multiple entries defined with same name "{a}"!'.format(
                        a=attribute))
            else:
                new = TreeStructure.InputNode(tag=attribute, text=value)
                currentNode.append(new)

    if multilineValue:
        raise IOError(
            'There was a parsing error reading MOOSE input! Multiline attribute "{n}" opened by {i} but never closed!'
            .format(i=multilineIndicator, n=multilineKey))

    return [TreeStructure.InputTree(root) for root in roots]