Exemplo n.º 1
0
def resolve_xinclude(source_folder, source_ext):
    """"""
    # Process all files in the source folder with the give extension
    pattern = "{}/**/*{}".format(source_folder, source_ext)
    matches = glob.glob(pattern, recursive=True)
    for filepath in matches:
        # print("Parsing", filepath)
        tree = ET.parse(filepath)
        root = tree.getroot()
        EI.include(root, loader=loader)  # resolves xi:include elements

        # Write output file
        filename = os.path.basename(filepath)
        basename, ext = os.path.splitext(filename)
        out_filename = "{}.sbt".format(basename)
        output_folder = os.path.dirname(filepath)
        out_filepath = os.path.join(output_folder, out_filename)
        with open(out_filepath, "wb") as f:
            s = ET.tostring(root)
            # TODO: would be good to clean up trailing whitespace
            #       however, we'd have to do some str-bytes conversion
            # s = "\n".join([ln.strip() for ln in s.splitlines()])
            f.write(s)
        print("Wrote", out_filepath)
    return
def parseAndroidAudioPolicyEngineConfigurationFile(
        audiopolicyengineconfigurationfile):

    logging.info("Checking Audio Policy Engine Configuration file {}".format(
        audiopolicyengineconfigurationfile))
    #
    # extract all product strategies name from audio policy engine configuration file
    #
    strategy_names = []

    oldWorkingDir = os.getcwd()
    print "Current working directory %s" % oldWorkingDir

    newDir = os.path.join(oldWorkingDir,
                          audiopolicyengineconfigurationfile.name)

    policy_engine_in_tree = ET.parse(audiopolicyengineconfigurationfile)
    os.chdir(os.path.dirname(os.path.normpath(newDir)))

    print "new working directory %s" % os.getcwd()

    policy_engine_root = policy_engine_in_tree.getroot()
    EI.include(policy_engine_root)

    os.chdir(oldWorkingDir)

    for strategy in policy_engine_root.iter('ProductStrategy'):
        strategy_names.append(strategy.get('name'))

    return strategy_names
Exemplo n.º 3
0
    def parse(self, file, baseUrl="", ownerDoc=None):
        absUrl = convertToAbsUrl(file, baseUrl)
        fp = urllib.request.urlopen(absUrl)
        try:
            tree = ElementTreeExtension()
            treeWrapper = self.treeWrapperClass(self, tree, self.useCaching)
            parser = ExtXMLTreeBuilder(file, absUrl, self, treeWrapper)
            treeWrapper.getTree().parse(fp, parser)
            fp.close()

            # XInclude support
            if self.processXInclude:
                loaderInst = ExtXIncludeLoader(self.parse, absUrl, ownerDoc)
                try:
                    ElementInclude.include(
                        treeWrapper.getTree().getroot(),
                        loaderInst.loader)
                except IOError as errInst:
                    raise GenXmlIfError(
                        "%s: IOError: %s" %
                        (file, str(errInst)))

        except ExpatError as errstr:
            fp.close()
            raise GenXmlIfError("%s: ExpatError: %s" % (file, str(errstr)))
        except ElementInclude.FatalIncludeError as errInst:
            fp.close()
            raise GenXmlIfError("%s: XIncludeError: %s" % (file, str(errInst)))

        return treeWrapper
Exemplo n.º 4
0
def main():
    parser = argparse.ArgumentParser(description='Create text with wrapping directives from rtt.')
    parser.add_argument("rtt_file", help="RTT file.")
    parser.add_argument("-r", "--heading_regexp", default="", help="Regexp to identify a chapter heading")
    parser.add_argument("-b", "--heading_blocks", type=int, choices=[1, 2],
                        default=1, help="The number of blocks in a chapter heading.")
    args = parser.parse_args()
    rtt_file = os.path.abspath(args.rtt_file)
    if not os.path.isfile(rtt_file):
        print(rtt_file, "is not a file\n", file="stderr")
        parser.print_help()
        sys.exit(-1)
    rtt = et.parse(rtt_file)
    pages = rtt.find("pages")
    os.chdir(os.path.dirname(rtt_file))
    ei.include(pages)
    open_mf = pages.attrib["microformatting-open"]
    close_mf = pages.attrib["microformatting-close"]
    pages_text = []
    first_page_text = pages.find("page/text")
    if first_page_text.text[0] != "\n":
        first_page_text.text = "\n" + first_page_text.text
    for p in pages.findall("page"):
        pages_text.append(process_page(p, open_mf, close_mf,
                                       args.heading_regexp, args.heading_blocks))
    sys.stdout.write("\n".join(pages_text))
Exemplo n.º 5
0
def loader(href, parse, encoding=None):
    if parse == "xml":
        with open(href, "rb") as file:
            data = ET.parse(file).getroot()
            EI.include(data, loader=loader)
    else:
        raise RuntimeError("Unable to handle recursive include.")
    return data
Exemplo n.º 6
0
def create_project(project_dir, rtt_file):
    project_file = project_dir + "/project"
    data = project_data.ProjectData(project_file, True)
    rtt = et.parse(rtt_file).getroot()
    ei.include(rtt)
    process_metadata(rtt, data)
    process_pages(rtt, data, project_dir)
    data.save() #also unlocks
    os.chmod(project_file, 0660)
Exemplo n.º 7
0
def get_bocca_objects(element):
    objs = []
    ElementInclude.include(element)
    for child in element.getchildren():
        if child.tag == 'project':
            objs.extend(get_bocca_objects(child))
        elif child.tag in bocca_object_names:
            objs.append(child)
    return objs
Exemplo n.º 8
0
def parse_xml( fname ):
    """Returns a parsed xml tree"""
    # handle deprecation warning for XMLParsing a file with DOCTYPE
    class DoctypeSafeCallbackTarget( ElementTree.TreeBuilder ):
        def doctype( *args ):
            pass
    tree = ElementTree.ElementTree()
    root = tree.parse( fname, parser=ElementTree.XMLParser( target=DoctypeSafeCallbackTarget() ) )
    ElementInclude.include( root )
    return tree
Exemplo n.º 9
0
def parse_xml( fname ):
    """Returns a parsed xml tree"""
    # handle deprecation warning for XMLParsing a file with DOCTYPE
    class DoctypeSafeCallbackTarget( ElementTree.TreeBuilder ):
        def doctype( *args ):
            pass
    tree = ElementTree.ElementTree()
    root = tree.parse( fname, parser=ElementTree.XMLParser( target=DoctypeSafeCallbackTarget() ) )
    ElementInclude.include( root )
    return tree
Exemplo n.º 10
0
	def testURLInvocation_XInclude_ETINCLUDE(self):
		#Reset the server back to "0"                                           
		r = requests.get(self._URL_+"/reset")                                   
		r = requests.get(self._URL_ +"/getCounter")                             
		request_content = r.text.replace("\r\n","")                             
		self.assertEqual("0", request_content)   

		with self.assertRaises(IOError):
			root = self.helperParse('../../xml_files_windows/url_invocation_xinclude.xml')                      
			# Resolve xi:include references
			_ETINCLUDE.include(root)
Exemplo n.º 11
0
    def testXInclude_ETINCLUDE(self):
        root = self.helperParse('../../xml_files_windows/xinclude.xml')
        # Resolve xi:include references
        _ETINCLUDE.include(root)

        # The Inclusion of xi:include should now have happened.
        # Therefore we can get an iterator over the newly included element
        # 'content'
        elem_data = root.iter('content')
        for elem in elem_data:
            self.assertEqual("it_works", elem.text)
            self.assertEqual("content", elem.tag)
Exemplo n.º 12
0
	def testXInclude_ETINCLUDE(self):
		root = self.helperParse('../../xml_files_windows/xinclude.xml')
		# Resolve xi:include references
		_ETINCLUDE.include(root)

		# The Inclusion of xi:include should now have happened.           
		# Therefore we can get an iterator over the newly included element
		# 'content'
		elem_data = root.iter('content')
		for elem in elem_data:
			self.assertEqual("it_works", elem.text)
			self.assertEqual("content", elem.tag)
Exemplo n.º 13
0
    def testURLInvocation_XInclude_ETINCLUDE(self):
        #Reset the server back to "0"
        r = requests.get(self._URL_ + "/reset")
        r = requests.get(self._URL_ + "/getCounter")
        request_content = r.text.replace("\r\n", "")
        self.assertEqual("0", request_content)

        with self.assertRaises(IOError):
            root = self.helperParse(
                '../../xml_files_windows/ssrf/url_invocation_xinclude.xml')
            # Resolve xi:include references
            _ETINCLUDE.include(root)
Exemplo n.º 14
0
 def test_include_xml(self):
     from xml.etree import ElementTree, ElementInclude
     tree = ElementTree.parse("files/document.xml")
     root = tree.getroot()
     ElementInclude.include(root)
     tree.write("files/tree_created.xml")
     fo = open("files/tree_created.xml", "r+")
     lines = fo.readlines()
     self.assertEqual([
         '<document>\n', '  <document>\n',
         '  <para>This is a paragraph.</para>\n', '</document>\n',
         '</document>'
     ], lines)
Exemplo n.º 15
0
 def load(self, filename):
     try:
         doc = etree.parse(filename)
         root = doc.getroot()
         einclude.include(root)
         self.logger.info("Parsing sources.")
         self.__parse_sources(root)
         self.logger.info("Parsing servers.")
         self.__parse_servers(root)
         self.logger.info("Parsing rules.")
         self.__parse_rules(root)
     except ExpatError, e:
         self.logger.error("The XML file is invalid; %s." % (e))
         self.errors += 1
Exemplo n.º 16
0
    def parseString (self, text, baseUrl="", ownerDoc=None):
        absUrl = convertToAbsUrl ("", baseUrl)
        tree        = ElementTreeExtension()
        treeWrapper = self.treeWrapperClass(self, tree, self.useCaching)
        parser = ExtXMLTreeBuilder("", absUrl, self, treeWrapper)
        parser.feed(text)
        treeWrapper.getTree()._setroot(parser.close())

        # XInclude support
        if self.processXInclude:
            loaderInst = ExtXIncludeLoader (self.parse, absUrl, ownerDoc)
            ElementInclude.include(treeWrapper.getTree().getroot(), loaderInst.loader)

        return treeWrapper
Exemplo n.º 17
0
def main():
    if len(sys.argv) != 2 or not os.path.isfile(sys.argv[1]):
        print("Usage: ", sys.argv[0], "rtt_file")
        sys.exit(-1)
    rtt = et.parse(sys.argv[1])
    pages = rtt.find("pages")
    os.chdir(os.path.dirname(sys.argv[1]))
    ei.include(pages)
    open_mf = pages.attrib["microformatting-open"]
    close_mf = pages.attrib["microformatting-close"]
    pages_text = []
    first_page_text = pages.find("page/text")
    if first_page_text.text[0] != "\n":
        first_page_text.text = "\n" + first_page_text.text
    for p in pages.findall("page"):
        pages_text.append(process_page(p, open_mf, close_mf, heading_regexp))
    sys.stdout.write("\n".join(pages_text))
Exemplo n.º 18
0
    def __load_target_xml(archtype: QL_ARCH,
                          ostype: QL_OS) -> ElementTree.ElementTree:
        filename, base_url = QlGdbFeatures.__get_xml_path(archtype)

        tree = ElementTree.parse(filename)

        # NOTE: this is needed to load xinclude hrefs relative to the main xml file. starting
        # from python 3.9 ElementInclude.include has an argument for that called 'base_url'.
        # this is a workaround for earlier python versions such as 3.8

        # <WORKAROUND>
        def my_loader(base: PurePath):
            def __wrapped(href: str, parse, encoding=None):
                abshref = base / href

                return ElementInclude.default_loader(str(abshref), parse,
                                                     encoding)

            return __wrapped

        # </WORKAROUND>

        # inline all xi:include elements
        ElementInclude.include(tree.getroot(), loader=my_loader(base_url))

        # patch xml osabi element with the appropriate abi tag
        osabi = tree.find('osabi')

        if osabi is not None:
            # NOTE: the 'Windows' abi tag is supported starting from gdb 10.
            # earlier gdb versions use 'Cygwin' instead

            abitag = {
                QL_OS.LINUX: 'GNU/Linux',
                QL_OS.FREEBSD: 'FreeBSD',
                QL_OS.MACOS: 'Darwin',
                QL_OS.WINDOWS: 'Windows',
                QL_OS.UEFI: 'Windows',
                QL_OS.DOS: 'Windows',
                QL_OS.QNX: 'QNX-Neutrino'
            }.get(ostype, 'unknown')

            osabi.text = abitag

        return tree
Exemplo n.º 19
0
def parseAndroidAudioPolicyConfigurationFile(audiopolicyconfigurationfile):

    logging.info("Checking Audio Policy Configuration file {}".format(
        audiopolicyconfigurationfile))
    #
    # extract all devices addresses from audio policy configuration file
    #
    address_criteria_mapping_table = {
        'sink': "OutputDevicesAddressesType",
        'source': "InputDevicesAddressesType"
    }

    address_criteria = {
        'OutputDevicesAddressesType': [],
        'InputDevicesAddressesType': []
    }

    old_working_dir = os.getcwd()
    print("Current working directory %s" % old_working_dir)

    new_dir = os.path.join(old_working_dir, audiopolicyconfigurationfile.name)

    policy_in_tree = ET.parse(audiopolicyconfigurationfile)
    os.chdir(os.path.dirname(os.path.normpath(new_dir)))

    print("new working directory %s" % os.getcwd())

    policy_root = policy_in_tree.getroot()
    EI.include(policy_root)

    os.chdir(old_working_dir)

    for device in policy_root.iter('devicePort'):
        for key in address_criteria_mapping_table.keys():
            if device.get('role') == key and device.get('address'):
                logging.info("{}: <{}>".format(key, device.get('address')))
                address_criteria[address_criteria_mapping_table[key]].append(
                    device.get('address'))

    for criteria in address_criteria:
        values = ','.join(address_criteria[criteria])
        logging.info("{}: <{}>".format(criteria, values))

    return address_criteria
Exemplo n.º 20
0
 def parse (self, file, baseUrl="", ownerDoc=None):
     absUrl = convertToAbsUrl (file, baseUrl)
     fp     = urllib.urlopen (absUrl)
     try:
         tree        = ElementTreeExtension()
         treeWrapper = self.treeWrapperClass(self, tree, self.useCaching)
         parser = ExtXMLTreeBuilder(file, absUrl, self, treeWrapper)
         treeWrapper.getTree().parse(fp, parser)
         fp.close()
         
         # XInclude support
         if self.processXInclude:
             loaderInst = ExtXIncludeLoader (self.parse, absUrl, ownerDoc)
             try:
                 ElementInclude.include(treeWrapper.getTree().getroot(), loaderInst.loader)
             except IOError, errInst:
                 raise GenXmlIfError, "%s: IOError: %s" %(file, str(errInst))
         
     except ExpatError, errstr:
         fp.close()
         raise GenXmlIfError, "%s: ExpatError: %s" %(file, str(errstr))
Exemplo n.º 21
0
    def fromFile(self, filename):
        r"""
        Following is the header as given in zend-config::
        
            /**
             * fromFile(): defined by Reader interface.
             *
             * @see    ReaderInterface::fromFile()
             * @param  string $filename
             * @return array
             * @throws Exception\RuntimeException
             */
        """

        if not os.path.isfile(filename) and not os.access(filename, os.R_OK):
            raise RuntimeException(
                "Xml: File %s Doesn't Exist or Not Readable" % filename)

        self.directory = os.path.dirname(filename.rstrip(os.sep)) or '.'

        try:
            xmlcontent = ''
            with open(filename, "r") as file:
                for line in file:
                    if "@include" in line:
                        include = line.split(":")[1].strip()
                        with open(os.path.join(self.directory, include),
                                  "r") as includedfile:
                            for includedline in includedfile:
                                xmlcontent += includedline
                    else:
                        xmlcontent += line

            self.root = ElementTree.fromstring(xmlcontent)
            ElementInclude.include(self.root, self.loader)
        except ElementTree.ParseError as e:
            raise RuntimeException("Xml: Error Reading XML file \"%s\": %s" %
                                   (filename, e))

        return self.process(self.root)
Exemplo n.º 22
0
def orca_et_loader(uFile, parse, encoding=None,aReplacementVars={}):
    '''Custom Loader for ElementTree Include to add definition path to includes
        and to make make bulk include using placeholder possible
    '''
    if uFile.endswith('*'):
        uRet=[]
        uFile2=ReplaceVars(uFile)
        uDir=AdjustPathToOs(os.path.dirname(uFile2))
        uPattern=uFile2[len(uDir):]
        uPattern=uPattern[1:-1]
        aFiles=GetFileList(uDir)
        for uFile in aFiles:
            if uFile.startswith(uPattern):
                uLoadFile=AdjustPathToOs(uDir+u'/'+uFile)
                uLoadFileRedirect=oORCA.oTheScreen.aSkinRedirects.get(uLoadFile)
                if uLoadFileRedirect:
                    uLoadFile=AdjustPathToOs(ReplaceVars(uLoadFileRedirect))
                Logger.debug (u'XML: Bulk-Loading XML Include:'+uLoadFile)
                uRaw= ElementInclude.default_loader(uLoadFile, "text", encoding)
                uRaw2 = ReplaceDefVars(uRaw,aReplacementVars)
                uRet.append(fromstring(uRaw2))
                #uRet.append(ElementInclude.default_loader(uLoadFile, parse, encoding))
        if len(uRet)==0:
            return None
        else:
            return uRet
    else:
        uFile2=uFile
        if FileExists(uFile2):
            Logger.debug (u'XML: Loading XML Include:'+uFile2)
            uRaw= ElementInclude.default_loader(uFile2, "text", encoding)
            uRaw2 = ReplaceDefVars(uRaw,aReplacementVars)
            bRet = fromstring(uRaw2)
            #bRet= ElementInclude.default_loader(uFile2, parse, encoding)
        else:
            Logger.warning (u'XML: Skipping XML Include (File not found):'+uFile2)
            bRet=None
        return bRet
Exemplo n.º 23
0
import defusedxml.ElementTree as _ET
import xml.etree.ElementInclude as _ETINCLUDE

tree = _ET.parse('../../xml_files_windows/standard.xml')
root = tree.getroot()
print root.tag
print root.text


'''
# for using XInclude
# This code helped a lot with troubeshooting ElementInclude.include:
# http://stackoverflow.com/questions/23684996/using-python-elementtree-elementinclude-and-xpointer-to-access-included-xml-file

import xml.etree.ElementTree as _ET
import xml.etree.ElementInclude as _ETINCLUDE

tree = _ET.parse('../../xml_files_windows/xinclude.xml')
root = tree.getroot()

_ETINCLUDE.include(root)
elem_data = root.iter('content')
for elem in elem_data:
	print elem.tag
	print elem.text
'''
Exemplo n.º 24
0
def runCmds():
    #1517 is VDSLR2 EqptType. This is demo. We should first get the EqptType from BID
    if tstItem_name is not None:
        if tstItem_name.upper() == "ALL":
            print g_ERR_TBD
            exit(0)

    cmdMapFile = None
    pCmdMapping = None
    boardInfo = getBidEx()
    if boardInfo is not None:
        if arg_plat_sw_automation:
            setPlatSwAutomation()

        cmdMapFile = getCmdMapFile(boardInfo['EqptType'])
        if cmdMapFile is not None:
            pCmdMapping = ET.parse(cmdMapFile)
            # ESA-760
            depth = 0
            while (depth < m_XINCLUDE_DEPTH):
                EI.include(pCmdMapping.getroot())
                depth += 1
            # debug
            #ET.dump(pCmdMapping.getroot())
        else:
            print g_ERR_CMD_MAPPING_FILE_NOT_FOUND
            exit(-1)
    else:
        print g_ERR_USE_DEFAULT_CMD_MAP
        cmdMapFile = getCmdMapFile('1517')
        if cmdMapFile is not None:
            pCmdMapping = ET.parse(cmdMapFile)
        else:
            print g_ERR_CMD_MAPPING_FILE_NOT_FOUND
            exit(-1)

    bTstItem = False
    for tstKey, tstVal in getTstItemMapping(boardInfo['EqptType']):
        testCmd = tstKey.upper()
        if testCmd == tstItem_name:
            bTstItem = True
            cmdParse = None
            testCmd = None
            childNotEmpty = False
            cmdMappingList = pCmdMapping.findall(tstVal)
            # ESA-760
            if not cmdMappingList:
                itemXPath = m_XPATH_PATTERN + tstVal
                cmdMappingList = pCmdMapping.findall(itemXPath)

            # ESAA-523
            milog = miLog()
            passCnt = 0
            failCnt = 0
            summaryMsg = ""
            detailMsg = ""
            debugMsg = ""
            tstNameFmtStr = "\nTestName:[{}] \n".format(tstItem_name)
            tstNameEndFmtStr = "\nTestName:[/{}] \n".format(tstItem_name)
            summaryMsg = tstNameFmtStr + summaryMsg
            summaryMsg = summaryMsg + g_HEADERFMTSTR
            startTime = time.strftime("%a, %d %b %Y %H:%M:%S",
                                      time.localtime(time.time()))
            tmpDebugStr = ""
            tmpDetailStr = ""

            if arg_dump_single_dbg:
                milog.dumpTest(tstItem_name)
                continue

            if arg_dump_single_detail:
                milog.dumpTest(tstItem_name, g_DUMP_DETAIL_STR)
                continue

            # ESA-1227
            debugMsg = tstNameFmtStr
            milog.prt_debug(debugMsg)
            # ESA-747
            if not cmdMappingList:
                testResult = g_cmp_FAIL
                tmpDebugStr = g_ERR_ITEM_NOT_FOUND_F(cmdMapFile)
            else:
                # # ESA-1227
                detailMsg = tstNameFmtStr
                milog.prt_detail(detailMsg)
                # debugMsg = tstNameFmtStr
                # milog.prt_debug(debugMsg)

                testResult = g_cmp_PASS
                # ESA-1476. Only get the last item as the findall returns a list
                # containing all matching elements in *document* order.
                cmdMappingList = cmdMappingList[-1:]
                for cmdMappingElem in cmdMappingList:
                    for child in cmdMappingElem.getchildren():
                        #print child.attrib['name'],':',child.text.strip()
                        if cmdParse is None:
                            childNotEmpty = True
                            cmdParse = testCmdParse(child)
                        else:
                            cmdParse.add(child)

                        if cmdParse:
                            if cmdParse.ready():
                                testCmdHandler = cmdParse.getCmdHandler()
                                if testCmdHandler is not None:
                                    testCmdHandler.preProcess(verbose=arg_verbose, \
                                            rstshown=arg_rstShown, killcard=arg_forced,\
                                            repeated=arg_repeated)
                                    testCmdHandler.process()
                                    # ESAA-480
                                    tmpResult = testCmdHandler.postProcess()
                                    # ESA-1227
                                    #tmpDetailStr = testCmdHandler.getDetailMsg()
                                    # ESA-1283. Move log from the end to here since there
                                    # is one case of hybrid(e.g. SGMIIKA2CPU test which is
                                    # the combination of BCM and linux commands).

                                    # EX-25985 does the same as debug log(add ending label)
                                    # to get the detail information for the phyconn test
                                    # if this test pass.
                                    #tmpDetailStr = tmpDetailStr + tstNameEndFmtStr
                                    #milog.prt_detail(tmpDetailStr)

                                    if tmpResult == g_cmp_FAIL:
                                        testResult = g_cmp_FAIL
                                        # ESAA-523
                                        #if testCmdHandler.getDebugMsg() is not None:
                                        #   tmpDebugStr = tmpDebugStr + testCmdHandler.getDebugMsg()
                                        # EX-25985
                                        #tmpDebugStr = tmpDebugStr + tstNameEndFmtStr
                                        # ESA-1283 Put log there since there is one hybrid case.
                                        #milog.prt_debug(tmpDebugStr)

                                    cmdParse = None
                                else:
                                    print g_ERR_HANDLER_NOT_FOUND
                        else:
                            print g_ERR_TEST_CMD_EMPTY

                #The cmdParse should be None. If it's not, there is one possibility
                #that the value of next in last command of config xml file(config/xxx.xml)
                #is still yes. To cover this inadvertent incorrectness, add code below
                if cmdParse is not None:
                    #No ready() invoked. Forced to run the handler
                    testCmdHandler = cmdParse.getCmdHandler()
                    if testCmdHandler is not None:
                        testCmdHandler.preProcess(verbose=arg_verbose, \
                                rstshown=arg_rstShown, killcard=arg_forced,\
                                repeated=arg_repeated)
                        testCmdHandler.process()
                        # ESAA-480
                        tmpResult = testCmdHandler.postProcess()
                        #tmpDetailStr = testCmdHandler.getDetailMsg()
                        # ESA-1283

                        # EX-25985 does the same as debug log(add ending label)
                        #tmpDetailStr = tmpDetailStr + tstNameEndFmtStr
                        #milog.prt_detail(tmpDetailStr)

                        if tmpResult == g_cmp_FAIL:
                            testResult = g_cmp_FAIL
                            # ESAA-523
                            #tmpDebugStr = tmpDebugStr + testCmdHandler.getDebugMsg()
                            # EX-25985
                            #tmpDebugStr = tmpDebugStr + tstNameEndFmtStr
                            # ESA-1283
                            #milog.prt_debug(tmpDebugStr)

                        cmdParse = None
                    else:
                        print g_ERR_HANDLER_NOT_FOUND

                if childNotEmpty:
                    tmpDetailStr = testCmdHandler.getDetailMsg()
                    # ESA-1283. Move log from the end to here since there
                    # is one case of hybrid(e.g. SGMIIKA2CPU test which is
                    # the combination of BCM and linux commands).

                    # EX-25985 does the same as debug log(add ending label)
                    # to get the detail information for the phyconn test
                    # if this test pass.
                    tmpDetailStr = tmpDetailStr + tstNameEndFmtStr
                    milog.prt_detail(tmpDetailStr)
                    if testResult == g_cmp_FAIL:
                        tmpDebugStr = tmpDebugStr + testCmdHandler.getDebugMsg(
                        )

            if testResult == g_cmp_FAIL:

                # ESAA-523
                #if testCmdHandler.getDebugMsg() is not None:
                #tmpDebugStr = tmpDebugStr + testCmdHandler.getDebugMsg()
                # EX-25985
                tmpDebugStr = tmpDebugStr + tstNameEndFmtStr
                # ESA-1283 Put log there since there is one hybrid case.
                milog.prt_debug(tmpDebugStr)
            # ESAA-523
            endTime = time.strftime("%a, %d %b %Y %H:%M:%S",
                                    time.localtime(time.time()))
            if testResult == g_cmp_PASS:
                passCnt += 1
            else:
                failCnt += 1

            tmpStr = "| {:>4} | {:>4} | {} | {} |\n".format(
                str(passCnt), str(failCnt), startTime, endTime)
            summaryMsg = summaryMsg + tmpStr
            summaryMsg = summaryMsg + g_FOOTERFMRSTR
            milog.prt_summary(summaryMsg)

            # ESAA-480
            if arg_rstShown:
                print testResult

    if bTstItem == False:
        if tstItem_name is None:
            usage()
        else:
            print g_ERR_TEST_ITEM_NOT_FOUND
Exemplo n.º 25
0
def XInclude_Tutorial(file):
    tree = ET.parse(file)
    root = tree.getroot()
    EI.include(root)

    tree.write('include.xml')
Exemplo n.º 26
0
def parse_xml(fname):
    """Returns a parsed xml tree"""
    tree = ElementTree.parse(fname)
    root = tree.getroot()
    ElementInclude.include(root)
    return tree
Exemplo n.º 27
0
def load_menus(path):
    """Load the file path."""

    menus = ElementTree.parse(path)
    ElementInclude.include(menus.getroot(), loader=Loader(path))
    return menus
Exemplo n.º 28
0
            def __wrapped(href: str, parse, encoding=None):
                abshref = base / href

                return ElementInclude.default_loader(str(abshref), parse,
                                                     encoding)
Exemplo n.º 29
0
 def _loader(self, href, parse, encoding=None):
     return ElementInclude.default_loader(self._path_map[href], parse,
                                          encoding)
Exemplo n.º 30
0
 def LoadDocument(self, path: str) -> document.Document:
     tree = ET.parse(path)
     root = tree.getroot()
     ElementInclude.include(root, self._loader)
     return parser.ParseDocument(root)
Exemplo n.º 31
0
 def LoadNav(self, path: str) -> List[nav.NavItem]:
     tree = ET.parse(path)
     root = tree.getroot()
     ElementInclude.include(root, self._loader)
     return parser.ParseNav(root)
Exemplo n.º 32
0
#!/usr/bin/python3

import xml.etree.ElementTree as et
import xml.etree.ElementInclude as ei
import sys

rtt = et.parse(sys.argv[1])
ei.include(rtt.getroot())
error_found = False
for page in rtt.findall(".//page"):
    messages = []
    img_lines = len(page.find("image/lines").text.split(","))
    text = page.find("text").text
    text_lines = len([X for X in text.splitlines() if len(X)])
    if(img_lines != text_lines):
        messages.append("Line count mismatch: image %d text %d" % (img_lines, text_lines))
    if text.find("'") != -1:
        messages.append("Single straight quote found")
    if text.find('"') != -1:
        messages.append("Double straight quote found")
    if text.find("--") != -1:
        messages.append("Double hyphen found")
    if messages:
        error_found = True
        print(page.attrib["id"])
        for m in messages:
            print("   ", m)
if not error_found:
    print("No errors found")
Exemplo n.º 33
0
# -*- coding: utf-8 -*-
# Author: hkey
import xml.etree.ElementInclude as ET

tree = ET.parse('test.xml')
root = tree.getroot()
print(root.tag)

Exemplo n.º 34
0
import defusedxml.ElementTree as _ET
import xml.etree.ElementInclude as _ETINCLUDE

tree = _ET.parse('../../xml_files_windows/standard.xml')
root = tree.getroot()
print root.tag
print root.text
'''
# for using XInclude
# This code helped a lot with troubeshooting ElementInclude.include:
# http://stackoverflow.com/questions/23684996/using-python-elementtree-elementinclude-and-xpointer-to-access-included-xml-file

import xml.etree.ElementTree as _ET
import xml.etree.ElementInclude as _ETINCLUDE

tree = _ET.parse('../../xml_files_windows/xinclude.xml')
root = tree.getroot()

_ETINCLUDE.include(root)
elem_data = root.iter('content')
for elem in elem_data:
	print elem.tag
	print elem.text
'''
Exemplo n.º 35
0

class CommentedTreeBuilder(XmlET.TreeBuilder):
    # Python 2.7 uses ElementTree 1.3.x.

    def comment(self, data):
        self.start(XmlET.Comment, {})
        self.data(data)
        self.end(XmlET.Comment)


def parse_xml(file_name, lh, include_comments=False):
    """Returns a parsed xml tree with comments intact."""
    fh = open(file_name, 'r')
    try:
        if include_comments:
            tree = XmlET.parse(
                fh, parser=XmlET.XMLParser(target=CommentedTreeBuilder()))
        else:
            tree = XmlET.parse(
                fh, parser=XmlET.XMLParser(target=DoctypeSafeCallbackTarget()))
    except Exception, e:
        fh.close()
        lh.write("Exception attempting to parse %s:\n%s\n\n" %
                 (file_name, str(e)))
        return None
    fh.close()
    root = tree.getroot()
    ElementInclude.include(root)
    return tree
Exemplo n.º 36
0
def _parse_xml(fname):
    tree = ElementTree.parse(fname)
    root = tree.getroot()
    ElementInclude.include(root)
    return tree
Exemplo n.º 37
0
    def doctype(*args):
        pass


class CommentedTreeBuilder(XmlET.TreeBuilder):
    # Python 2.7 uses ElementTree 1.3.x.

    def comment(self, data):
        self.start(XmlET.Comment, {})
        self.data(data)
        self.end(XmlET.Comment)


def parse_xml(file_name, lh, include_comments=False):
    """Returns a parsed xml tree with comments intact."""
    fh = open(file_name, 'r')
    try:
        if include_comments:
            tree = XmlET.parse(fh, parser=XmlET.XMLParser(target=CommentedTreeBuilder()))
        else:
            tree = XmlET.parse(fh, parser=XmlET.XMLParser(target=DoctypeSafeCallbackTarget()))
    except Exception, e:
        fh.close()
        lh.write("Exception attempting to parse %s:\n%s\n\n" % (file_name, str(e)))
        return None
    fh.close()
    root = tree.getroot()
    ElementInclude.include(root)
    return tree
Exemplo n.º 38
0
def _parse_xml(fname):
    tree = ElementTree.parse(fname)
    root = tree.getroot()
    ElementInclude.include(root)
    return tree
Exemplo n.º 39
0
def parse2tok(p, tmap, pel=False):
    tree = ET.parse(p)
    cwd=os.getcwd()
    os.chdir(os.path.dirname(p))
    root = tree.getroot()
    EI.include(root)
    parent_map = {c: p for p in root.iter() for c in p}
    ls = root.findall(f'.//{tei_xmlns}line')
    if len(ls) == 0:
        ls = root.findall(f'.//{tei_xmlns}seg')
    if len(ls) == 0:
        ls = root.findall(f'.//{tei_xmlns}l')
    if len(ls) == 0:
        ls = root.findall(f'.//{tei_xmlns}p')
    tok=[]
    divs=[]
    if ls[0].tag.endswith('p'):
        tl=[]
        for b in ls:
            res=[]
            p_content(b, res)
            tl.append(res)
        return tl, ["p"]
    for b in ls:
        p=parent_map[b]
        if pel:
            px = p.tag.replace(f'{tei_xmlns}', '') + "/"
        else:
            px = ""
        if (p.tag == '{http://www.tei-c.org/ns/1.0}head'):
            pdiv = parent_map[p]
            if f'{xml_xmlns}id' in pdiv.attrib:
                pdivid=pdiv.attrib[f'{xml_xmlns}id']
            else:
                pdivid=''
            divs.append((len(tok), b.text, pdivid))
        pm=[p]
        while p in parent_map:
            p=parent_map[p]
            pm.append(p)
        px="p"
        for p in pm:
            #print(p.tag)
            if p.tag in tmap:
                px = tmap[p.tag][1]
                break
        #tag = b.tag.replace(f'{tei_xmlns}', '')
        tag=""
        if "rend" in b.attrib:
            tag = ";" + b.attrib["rend"]
        if f'{xml_xmlns}id' in b.attrib:
            id=b.attrib[f'{xml_xmlns}id']
        else:
            id="noid"
        tx = linechildren(b)
        tx = tx.replace("\n", "")
        seq=line2arr(tx)
        cnt = 0
        for s in seq:
            cnt += 1
            tok.append((px + tag, id, cnt, s))
    os.chdir(cwd)
    return (tok, divs)
# 遍历xml文件
def traverseXml(element):
    # print (len(element))
    if len(element) > 0:
        for child in element:
            print(child.tag, "----", child.attrib)
            traverseXml(child)


if __name__ == "__main__":
    xmlFilePath = os.path.abspath(
        "F:/Github/DL-data-processing-methods/XML/test.xml")
    print(xmlFilePath)
    try:
        tree = ET.parse(xmlFilePath)
        print("tree type:", type(tree))

        # 获得根节点
        root = tree.getroot()
    except Exception as e:  # 捕获除与程序退出sys.exit()相关之外的所有异常
        print("parse test.xml fail!")
        sys.exit()
    print("root type:", type(root))
    print(root.tag, "----", root.attrib)

    # 遍历root的下一层
    for child in root:
        print("遍历root的下一层", child.tag, "----", child.attrib)

    # 使用下标访问
Exemplo n.º 41
0
    def __call__(self, href, typ, encoding=None):
        """Fix relative references and call the default loader."""

        return ElementInclude.default_loader(join(self.base, href), typ,
                                             encoding)