Пример #1
0
    def __init__(self, inputFile):
        """
      Constructor
      @ In, inputFile, string, input file name
      @ Out, None
    """
        self.printTag = 'RAVEN_PARSER'  # print tag
        self.inputFile = inputFile  # input file name
        self.outStreamsNames = {
        }  # {'outStreamName':[DataObjectName,DataObjectType]}
        self.varGroups = {}  # variable groups, names and values
        if not os.path.exists(inputFile):
            raise IOError(self.printTag + ' ERROR: Not found RAVEN input file')
        try:
            tree = ET.parse(open(inputFile, 'r'))
        except IOError as e:
            raise IOError(self.printTag + ' ERROR: Input Parsing error!\n' +
                          str(e) + '\n')
        self.tree = tree.getroot()

        # expand the ExteranlXML nodes
        cwd = os.path.dirname(inputFile)
        xmlUtils.expandExternalXML(self.tree, cwd)

        # get the NAMES of the variable groups
        variableGroupNode = self.tree.find('VariableGroups')
        if variableGroupNode is not None:
            # make a messageHandler and messageUsesr to handle variable group creation
            ## if made generally available to this parser, this can be relocated and used generally
            messageHandler = MessageHandler.MessageHandler()
            messageHandler.initialize({'verbosity': 'quiet'})
            messageUser = MessageHandler.MessageUser()
            self.varGroups = xmlUtils.readVariableGroups(
                variableGroupNode, messageHandler, messageUser)

        # do some sanity checks
        sequence = [
            step.strip()
            for step in self.tree.find('.//RunInfo/Sequence').text.split(",")
        ]
        # firstly no multiple sublevels of RAVEN can be handled now
        for code in self.tree.findall('.//Models/Code'):
            if 'subType' not in code.attrib:
                raise IOError(
                    self.printTag +
                    ' ERROR: Not found subType attribute in <Code> XML blocks!'
                )
            if code.attrib['subType'].strip() == 'RAVEN':
                raise IOError(
                    self.printTag +
                    ' ERROR: Only one level of RAVEN runs are allowed (Not a chain of RAVEN runs). Found a <Code> of subType RAVEN!'
                )
        # find steps and check if there are active outstreams (Print)
        foundOutStreams = False
        for step in self.tree.find('.//Steps'):
            if step.attrib['name'] in sequence:
                for role in step:
                    if role.tag.strip() == 'Output':
                        mainClass, subType = role.attrib['class'].strip(
                        ), role.attrib['type'].strip()
                        if mainClass == 'OutStreams' and subType == 'Print':
                            outStream = self.tree.find(
                                './/OutStreams/Print[@name="' +
                                role.text.strip() + '"]' + '/source')
                            if outStream is None:
                                raise IOError(
                                    self.printTag +
                                    ' ERROR: The OutStream of type "Print" named "'
                                    + role.text.strip() +
                                    '" has not been found!')
                            dataObjectType = None
                            linkedDataObjectPointSet = self.tree.find(
                                './/DataObjects/PointSet[@name="' +
                                outStream.text.strip() + '"]')
                            if linkedDataObjectPointSet is None:
                                linkedDataObjectHistorySet = self.tree.find(
                                    './/DataObjects/HistorySet[@name="' +
                                    outStream.text.strip() + '"]')
                                if linkedDataObjectHistorySet is None:
                                    # try dataset
                                    linkedDataObjectHistorySet = self.tree.find(
                                        './/DataObjects/DataSet[@name="' +
                                        outStream.text.strip() + '"]')
                                    if linkedDataObjectHistorySet is None:
                                        raise IOError(
                                            self.printTag +
                                            ' ERROR: The OutStream of type "Print" named "'
                                            + role.text.strip() +
                                            '" is linked to not existing DataObject!'
                                        )
                                dataObjectType, xmlNode = "HistorySet", linkedDataObjectHistorySet
                            else:
                                dataObjectType, xmlNode = "PointSet", linkedDataObjectPointSet
                            self.outStreamsNames[role.text.strip()] = [
                                outStream.text.strip(), dataObjectType, xmlNode
                            ]
                            foundOutStreams = True
        if not foundOutStreams:
            raise IOError(
                self.printTag +
                ' ERROR: at least one <OutStreams> of type "Print" needs to be inputted in the active Steps!!'
            )
        # Now we grep the paths of all the inputs the SLAVE RAVEN contains in the workind directory.
        self.workingDir = self.tree.find('.//RunInfo/WorkingDir').text.strip()
        # Find the Files
        self.slaveInputFiles = []
        filesNode = self.tree.find('.//Files')
        if filesNode is not None:
            for child in self.tree.find('.//Files'):
                subDirectory = child.attrib.get('subDirectory', '')
                self.slaveInputFiles.append(
                    os.path.expanduser(
                        os.path.join(self.workingDir, subDirectory,
                                     child.text.strip())))

        externalModels = self.tree.findall('.//Models/ExternalModel')
        if len(externalModels) > 0:
            for extModel in externalModels:
                if 'ModuleToLoad' in extModel.attrib:
                    moduleToLoad = extModel.attrib['ModuleToLoad']
                    if not moduleToLoad.endswith("py"):
                        moduleToLoad += ".py"
                    if self.workingDir not in moduleToLoad:
                        self.slaveInputFiles.append(
                            os.path.expanduser(
                                os.path.join(self.workingDir, moduleToLoad)))
                    else:
                        self.slaveInputFiles.append(
                            os.path.expanduser(moduleToLoad))
                else:
                    if 'subType' not in extModel.attrib or len(
                            extModel.attrib['subType']) == 0:
                        raise IOError(
                            self.printTag + ' ERROR: ExternalModel "' +
                            extModel.attrib['name'] +
                            '" does not have any attribute named "ModuleToLoad" or "subType" with an available plugin name!'
                        )

        externalFunctions = self.tree.findall('.//Functions/External')
        if len(externalFunctions) > 0:
            for extFunct in externalFunctions:
                if 'file' in extFunct.attrib:
                    moduleToLoad = extFunct.attrib['file']
                    if not moduleToLoad.endswith("py"):
                        moduleToLoad += ".py"
                    if self.workingDir not in moduleToLoad:
                        self.slaveInputFiles.append(
                            os.path.expanduser(
                                os.path.join(self.workingDir, moduleToLoad)))
                    else:
                        self.slaveInputFiles.append(
                            os.path.expanduser(moduleToLoad))
                else:
                    raise IOError(
                        self.printTag + ' ERROR: Functions/External ' +
                        extFunct.attrib['name'] +
                        ' does not have any attribute named "file"!!')
Пример #2
0
  def __init__(self, inputFile):
    """
      Constructor
      @ In, inputFile, string, input file name
      @ Out, None
    """
    self.printTag  = 'RAVEN_PARSER' # print tag
    self.inputFile = inputFile      # input file name
    self.outStreamsNames = {}       # {'outStreamName':[DataObjectName,DataObjectType]}
    self.databases = {}             # {name: full rel path to file with filename}
    self.varGroups = {}             # variable groups, names and values
    if not os.path.exists(inputFile):
      raise IOError(self.printTag+' ERROR: Not found RAVEN input file')
    try:
      tree = ET.parse(open(inputFile,'r'))
    except IOError as e:
      raise IOError(self.printTag+' ERROR: Input Parsing error!\n' +str(e)+'\n')
    self.tree = tree.getroot()

    # expand the ExteranlXML nodes
    cwd = os.path.dirname(inputFile)
    xmlUtils.expandExternalXML(self.tree,cwd)

    # get the NAMES of the variable groups
    variableGroupNode = self.tree.find('VariableGroups')
    if variableGroupNode is not None:
      # make a messageHandler and messageUsesr to handle variable group creation
      ## if made generally available to this parser, this can be relocated and used generally
      messageHandler = MessageHandler.MessageHandler()
      messageHandler.initialize({'verbosity':'quiet'})
      messageUser = MessageHandler.MessageUser()
      self.varGroups = mathUtils.readVariableGroups(variableGroupNode,messageHandler,messageUser)

    # do some sanity checks
    sequence = [step.strip() for step in self.tree.find('.//RunInfo/Sequence').text.split(",")]
    # firstly no multiple sublevels of RAVEN can be handled now
    for code in self.tree.findall('.//Models/Code'):
      if 'subType' not in code.attrib:
        raise IOError(self.printTag+' ERROR: Not found subType attribute in <Code> XML blocks!')
      if code.attrib['subType'].strip() == 'RAVEN':
        raise IOError(self.printTag+' ERROR: Only one level of RAVEN runs are allowed (Not a chain of RAVEN runs). Found a <Code> of subType RAVEN!')
    # find steps and check if there are active outstreams (Print)
    foundOutStreams = False
    foundDatabases = False
    for step in self.tree.find('.//Steps'):
      if step.attrib['name'] in sequence:
        for role in step:
          if role.tag.strip() == 'Output':
            mainClass, subType = role.attrib['class'].strip(), role.attrib['type'].strip()
            if mainClass == 'OutStreams' and subType == 'Print':
              outStream = self.tree.find('.//OutStreams/Print[@name="'+role.text.strip()+ '"]'+'/source')
              if outStream is None:
                continue # can have an outstream in inner but still use database return
              dataObjectType = None
              linkedDataObjectPointSet = self.tree.find('.//DataObjects/PointSet[@name="'+outStream.text.strip()+ '"]')
              if linkedDataObjectPointSet is None:
                linkedDataObjectHistorySet = self.tree.find('.//DataObjects/HistorySet[@name="'+outStream.text.strip()+ '"]')
                if linkedDataObjectHistorySet is None:
                  # try dataset
                  linkedDataObjectHistorySet = self.tree.find('.//DataObjects/DataSet[@name="'+outStream.text.strip()+ '"]')
                  if linkedDataObjectHistorySet is None:
                    raise IOError(self.printTag+' ERROR: The OutStream of type "Print" named "'+role.text.strip()+'" is linked to not existing DataObject!')
                dataObjectType, xmlNode = "HistorySet", linkedDataObjectHistorySet
              else:
                dataObjectType, xmlNode = "PointSet", linkedDataObjectPointSet
              self.outStreamsNames[role.text.strip()] = [outStream.text.strip(),dataObjectType,xmlNode]
              foundOutStreams = True
            elif mainClass == 'Databases' and subType == 'NetCDF':
              rName = role.text.strip()
              db = self.tree.find(f'.//Databases/NetCDF[@name="{rName}"]')
              if db is None:
                continue # can have a database in inner but still use outsream return
              if db.attrib['readMode'] == 'overwrite':
                dirs = db.attrib.get('directory', 'DatabaseStorage')
                name = db.attrib.get('filename', db.attrib['name']+'.nc')
                full = os.path.join(dirs, name)
                self.databases[rName] = full
                foundDatabases = True

    if not foundOutStreams and not foundDatabases:
      raise IOError(self.printTag+' ERROR: No <OutStreams><Print> or <Databases><NetCDF readMode="overwrite"> found in the active <Steps> of inner RAVEN!')

    # Now we grep the paths of all the inputs the SLAVE RAVEN contains in the workind directory.
    self.workingDir = self.tree.find('.//RunInfo/WorkingDir').text.strip()
    # Find the Files
    self.slaveInputFiles = self.findSlaveFiles(self.tree, self.workingDir)
Пример #3
0
# See the License for the specific language governing permissions and
# limitations under the License.
import sys
import os

#establish required paths for importing MessageHandler
frameworkDir = os.path.abspath(
    os.path.join(os.path.abspath(os.path.dirname(__file__)), '..', '..', '..',
                 'framework'))
sys.path.append(frameworkDir)
sys.path.append(os.path.join(frameworkDir, 'utils'))

import MessageHandler

#establish a basic message user
user = MessageHandler.MessageUser()
user.printTag = 'MessageUser'
user.messageHandler = MessageHandler.MessageHandler()
user.messageHandler.initialize({'verbosity': 'all'})

#test that exceptions raised through raiseAnError can be caught
try:
    user.raiseAnError(RuntimeError, 'An example error')
except RuntimeError:
    user.raiseAMessage('Error catching works as expected.')
"""
  <TestInfo>
    <name>framework.test_trycatch</name>
    <author>talbpaul</author>
    <created>2016-02-26</created>
    <classesTested>MessageHandler</classesTested>