Source code for mojo.pavayo.testcase.validationSuiteParser

"""
validationSuiteParser
=====================
The validationSuiteParser module provides methods to read and write XML files, according to the structure in testcase_generated.py.

The parsePavayo method can be used to parse an XML file which fulfills this structure.
The method writeXmlFile a list of test cases can be packed into a suite.
"""

import os
import sys

from lxml import etree as etree_

from .abstractTestCase import AbstractTestCase
from . import testcase_generated as supermod

from ...bricabrac.fileIO import workingDirectory
from .generalTestCase import GeneralTestCaseSub
from .validationTestCase import ValidationTestCaseSub
from .validationTestCaseSpeedline import ValidationTestCaseSpeedlineSub
from .speedlineTestCase import SpeedlineTestCaseSub
from .parallelTestCase import ParallelTestCaseSub
from .testSuiteTestCase import TestSuiteTestCaseSub
from .testSuiteComponents import SuiteSub, TclistSub, JobListSub, JobSub, SimulationJobSub, AnalysisJobSub, SetupSub, RestartSetupSub, \
    RestartValidationOpListSub, RestartValidationOpSub, ValidationOpListSub, ValidationOpSub, \
    OpInitialisationGlobalPerRowSub, OpInitialisationGlobalSub, OpListSub, OpSub, CommandsSub, RowListSub, RowSub, \
    DependencySub, SliceSub
from .suiteComponents import ReportSub
from ...plotting.plotComponents import FileAxisAttributesSub, LabelAttributesSub, GeneralPlotSub, XYPlotSub


# Tell the parent class about its sub classes to make the factory methods work.
supermod.Suite.subclass = SuiteSub
supermod.Report.subclass = ReportSub
supermod.Tclist.subclass = TclistSub
supermod.GeneralTestCase.subclass = GeneralTestCaseSub
supermod.ValidationTestCase.subclass = ValidationTestCaseSub
supermod.ValidationTestCaseSpeedline.subclass = ValidationTestCaseSpeedlineSub
supermod.SpeedlineTestCase.subclass = SpeedlineTestCaseSub
supermod.ParallelTestCase.subclass = ParallelTestCaseSub
supermod.TestSuiteTestCase.subclass = TestSuiteTestCaseSub
supermod.JobList.subclass = JobListSub
supermod.Job.subclass = JobSub
supermod.SimulationJob.subclass = SimulationJobSub
supermod.AnalysisJob.subclass = AnalysisJobSub
supermod.Setup.subclass = SetupSub
supermod.RestartSetup.subclass = RestartSetupSub
supermod.OpList.subclass = OpListSub
supermod.ValidationOpList.subclass = ValidationOpListSub
supermod.RestartValidationOpList.subclass = RestartValidationOpListSub
supermod.OpInitialisationGlobalPerRow.subclass = OpInitialisationGlobalPerRowSub
supermod.OpInitialisationGlobal.subclass = OpInitialisationGlobalSub
supermod.Op.subclass = OpSub
supermod.ValidationOp.subclass = ValidationOpSub
supermod.RestartValidationOp.subclass = RestartValidationOpSub
supermod.Commands.subclass = CommandsSub
supermod.RowList.subclass = RowListSub
supermod.Row.subclass = RowSub

supermod.Dependency.subclass = DependencySub
supermod.fileAxisAttributes.subclass = FileAxisAttributesSub
supermod.LabelAttributes.subclass = LabelAttributesSub
supermod.GeneralPlot.subclass = GeneralPlotSub
supermod.XYPlot.subclass = XYPlotSub
supermod.Slice.subclass = SliceSub


def parsexml_(*args, **kwargs):

    if 'parser' not in kwargs:
        # Use the lxml ElementTree compatible parser so that, e.g.,
        #   we ignore comments.
        kwargs['parser'] = etree_.ETCompatXMLParser()

    doc = etree_.parse(*args, **kwargs)

    for element in doc.iter():

        if element.text:
            element.text = element.text.strip()

    return doc


def get_root_tag(node):
    tag = supermod.Tag_pattern_.match(node.tag).groups()[-1]
    rootClass = supermod.GDSClassesMapping.get(tag)
    if rootClass is None and hasattr(supermod, tag):
        rootClass = getattr(supermod, tag)
    return tag, rootClass


def parse(inFilename, silence=False):

    body, tail = os.path.split(os.path.abspath(inFilename))
    with workingDirectory(body):
        doc = parsexml_(tail)
    rootNode = doc.getroot()
    rootTag, rootClass = get_root_tag(rootNode)
    if rootClass is None:
        rootTag = 'suite'
        rootClass = supermod.Suite
    rootObj = rootClass.factory()
    rootObj.build(rootNode)
    # Enable Python to collect the space used by the DOM.
    doc = None
    if not silence:
        sys.stdout.write('<?xml version="1.0" ?>\n')
        rootObj.export(sys.stdout, 0, name_=rootTag, namespacedef_='', pretty_print=True)
    return rootObj


def parseLiteral(inFilename, silence=False):
    doc = parsexml_(inFilename)
    rootNode = doc.getroot()
    rootTag, rootClass = get_root_tag(rootNode)
    if rootClass is None:
        rootTag = 'suite'
        rootClass = supermod.Suite
    rootObj = rootClass.factory()
    rootObj.build(rootNode)
    # Enable Python to collect the space used by the DOM.
    doc = None
    if not silence:
        sys.stdout.write('#from testcase_generated import *\n\n')
        sys.stdout.write('import testcase_generated as model_\n\n')
        sys.stdout.write('rootObj = model_.rootClass(\n')
        rootObj.exportLiteral(sys.stdout, 0, name_=rootTag)
        sys.stdout.write(')\n')
    return rootObj


[docs]def parsePavayo(inFilename, silence=True): """Parses the XML file, applies some operations necessary for PAVAYO and returns stuff in PAVAYO format. :param inFilename: name of the XML file to parse :param silence: prints the parsed XML file if true :type inFilename: str :type silence: bool :return: tuple containing a list of all read test cases and the parsed suite object :rtype: tuple(list(AbstractTestCase), Suite) """ suiteObject = parse(inFilename=inFilename, silence=silence) testCaseList = [] for tcList in suiteObject.tclist: testCaseList += tcList.speedlineTestCase + tcList.validationTestCaseSpeedline + tcList.validationTestCase + tcList.parallelTestCase + \ tcList.generalTestCase + tcList.testSuiteTestCase for testCase in testCaseList: testCase.path = os.path.abspath(os.path.join(suiteObject.suitename, testCase.name)) testCase.errorContainer.read(testCase.status_file_path) return testCaseList, suiteObject
[docs]def writeXmlFile(TClist=None, suiteName="unknown_suitename", mojoversion="-1", exit_file="MyTTCML"): """Writes the given test case list to the exit_file. :param TClist: list of test cases for the suite :param suiteName: name of the test suite :param mojoversion: current MOJO version (default: -1) :param exit_file: path to the XML file to be generated :type TClist: list(AbstractTestCase) :type suiteName: str :type mojoversion: str :type exit_file: str """ speedlineTestCases = [tc for tc in TClist if isinstance(tc, supermod.SpeedlineTestCase) and not isinstance(tc, supermod.ParallelTestCase)] validationTestCasesSpeedline = [tc for tc in TClist if isinstance(tc, supermod.ValidationTestCaseSpeedline)] validationTestCases = [tc for tc in TClist if isinstance(tc, supermod.ValidationTestCase)] parallelTestCases = [tc for tc in TClist if isinstance(tc, supermod.ParallelTestCase)] generalTestCases = [tc for tc in TClist if isinstance(tc, supermod.GeneralTestCase)] testSuiteTestCases = [tc for tc in TClist if isinstance(tc, supermod.TestSuiteTestCase)] testCaseList = supermod.Tclist.factory(generalTestCase=generalTestCases, speedlineTestCase=speedlineTestCases, validationTestCaseSpeedline=validationTestCasesSpeedline, validationTestCase=validationTestCases, parallelTestCase=parallelTestCases, testSuiteTestCase=testSuiteTestCases) suiteObject = supermod.Suite.factory(mojoversion=mojoversion, suitename=suiteName, tclist=[testCaseList]) with open(exit_file, "w") as fileHandle: suiteObject.export(fileHandle, 0, name_="suite")