当前位置: 首页>>代码示例>>Python>>正文


Python etree.XMLSyntaxError方法代码示例

本文整理汇总了Python中lxml.etree.XMLSyntaxError方法的典型用法代码示例。如果您正苦于以下问题:Python etree.XMLSyntaxError方法的具体用法?Python etree.XMLSyntaxError怎么用?Python etree.XMLSyntaxError使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在lxml.etree的用法示例。


在下文中一共展示了etree.XMLSyntaxError方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: Open

# 需要导入模块: from lxml import etree [as 别名]
# 或者: from lxml.etree import XMLSyntaxError [as 别名]
def Open(self):
        """
        Opens a session to the nexpose appliance by logging in.
        This function with raise an exception on error or if the session is already open.
        """
        if self._session_id:
            raise SessionIsNotClosedException("Please close the session first!")
        try:
            response = self._Execute_APIv1d1(self._login_request)
        except NexposeConnectionException as ex:
            if isinstance(ex.inner_exception, etree.XMLSyntaxError):
                raise NexposeException("Unexpected error! Is the Nexpose appliance activated?")
            raise ex
        if response.tag == "LoginResponse":
            if response.attrib["success"] == "1":
                self._session_id = response.attrib["session-id"]
        if not self._session_id:
            raise NexposeFailureException("Login failure!") 
开发者ID:rapid7,项目名称:nexpose-client-python,代码行数:20,代码来源:nexpose.py

示例2: import_config

# 需要导入模块: from lxml import etree [as 别名]
# 或者: from lxml.etree import XMLSyntaxError [as 别名]
def import_config(self, config: str) -> Any:
        """Import a scan config from XML

        Arguments:
            config: Scan Config XML as string to import. This XML must
                contain a :code:`<get_configs_response>` root element.

        Returns:
            The response. See :py:meth:`send_command` for details.
        """
        if not config:
            raise RequiredArgument(
                function=self.import_config.__name__, argument='config'
            )

        cmd = XmlCommand("create_config")

        try:
            cmd.append_xml_str(config)
        except etree.XMLSyntaxError:
            raise InvalidArgument(
                function=self.import_config.__name__, argument='config'
            )

        return self._send_xml_command(cmd) 
开发者ID:greenbone,项目名称:python-gvm,代码行数:27,代码来源:__init__.py

示例3: _validate_document

# 需要导入模块: from lxml import etree [as 别名]
# 或者: from lxml.etree import XMLSyntaxError [as 别名]
def _validate_document(self, document_string, harvest_object, validator=None):
        '''
        Validates an XML document with the default, or if present, the
        provided validators.

        It will create a HarvestObjectError for each validation error found,
        so they can be shown properly on the frontend.

        Returns a tuple, with a boolean showing whether the validation passed
        or not, the profile used and a list of errors (tuples with error
        message and error lines if present).
        '''
        if not validator:
            validator = self._get_validator()

        document_string = re.sub('<\?xml(.*)\?>', '', document_string)

        try:
            xml = etree.fromstring(document_string)
        except etree.XMLSyntaxError, e:
            self._save_object_error('Could not parse XML file: {0}'.format(str(e)), harvest_object, 'Import')
            return False, None, [] 
开发者ID:italia,项目名称:daf-recipes,代码行数:24,代码来源:base.py

示例4: __call__

# 需要导入模块: from lxml import etree [as 别名]
# 或者: from lxml.etree import XMLSyntaxError [as 别名]
def __call__(self, parser, namespace, string, option_string):
        try:
            with open(string, 'r') as f:
                tree = etree.parse(f)
                xml = tree.getroot()
        except (OSError, IOError):
            xml = None
        except etree.XMLSyntaxError as e:
            if 'Document is empty' not in str(e):
                raise argparse.ArgumentError(self,
                                             "%s does not seem to be a valid xml file"
                                             % string)
            xml = None

        setattr(namespace, self.dest, string)
        setattr(namespace, 'xml', xml) 
开发者ID:datamade,项目名称:parserator,代码行数:18,代码来源:main.py

示例5: test_download_default_transcript

# 需要导入模块: from lxml import etree [as 别名]
# 或者: from lxml.etree import XMLSyntaxError [as 别名]
def test_download_default_transcript(self, backend, download_transcript_mock, params):
        """
        Check default transcript is downloaded from a video platform API.
        """
        player = self.player[backend]
        for index, event in enumerate(download_transcript_mock.get_outcomes()):
            mock = download_transcript_mock(event=event)
            self.mocked_objects = mock.apply_mock(self.mocked_objects)
            try:
                res = player(self.xblock).download_default_transcript(**params[index])
                message = ''
                expected_default_transcript = mock.expected_value[0]
                self.assertIsInstance(res, unicode)
                self.assertEqual(res, expected_default_transcript)
            except VideoXBlockException as ex:
                message = ex.message
            except etree.XMLSyntaxError:
                message = 'XMLSyntaxError exception'
            expected_message = mock.expected_value[-1]
            self.assertIn(expected_message, message)
            self.restore_mocked() 
开发者ID:appsembler,项目名称:xblock-video,代码行数:23,代码来源:test_backends.py

示例6: check_output

# 需要导入模块: from lxml import etree [as 别名]
# 或者: from lxml.etree import XMLSyntaxError [as 别名]
def check_output(self, want, got, optionflags):
        alt_self = getattr(self, '_temp_override_self', None)
        if alt_self is not None:
            super_method = self._temp_call_super_check_output
            self = alt_self
        else:
            super_method = OutputChecker.check_output
        parser = self.get_parser(want, got, optionflags)
        if not parser:
            return super_method(
                self, want, got, optionflags)
        try:
            want_doc = parser(want)
        except etree.XMLSyntaxError:
            return False
        try:
            got_doc = parser(got)
        except etree.XMLSyntaxError:
            return False
        return self.compare_docs(want_doc, got_doc) 
开发者ID:JFox,项目名称:aws-lambda-lxml,代码行数:22,代码来源:doctestcompare.py

示例7: transform_with_xsl

# 需要导入模块: from lxml import etree [as 别名]
# 或者: from lxml.etree import XMLSyntaxError [as 别名]
def transform_with_xsl(xml_path, xsl_path, recover=False):
    try:
        xml_dom = etree.parse(xml_path)
    except etree.XMLSyntaxError as e:
        if recover:
            logger.error(e)
            parser = etree.XMLParser(recover=True)
            xml_dom = etree.parse(xml_path, parser=parser)
        else:
            raise
    xsl_transform = etree.XSLT(etree.parse(xsl_path))
    try:
        transformed_dom = xsl_transform(xml_dom)
    except Exception as err:
        logger.error(err)
        for xsl_error in xsl_transform.error_log:
            logger.error(xsl_error)
        if not recover:
            raise

    return transformed_dom 
开发者ID:BirkbeckCTP,项目名称:janeway,代码行数:23,代码来源:files.py

示例8: get_uis_by_xpath

# 需要导入模块: from lxml import etree [as 别名]
# 或者: from lxml.etree import XMLSyntaxError [as 别名]
def get_uis_by_xpath(self, xpath, is_update=True):
        """
        通过xpath查找节点
        :param xpath: 
        :param is_update: 
        :return: 
        """
        if is_update:
            xml_str = None
            for _ in range(5):
                try:
                    xml_str = self.adb_ext.dump()  # 获取xml文件
                    self.__init_xml(xml_str)
                    break
                except etree.XMLSyntaxError:
                    logging.error('etree.XMLSyntaxError:\n')
                    if xml_str:
                        logging.error('xml str:{}'.format(xml_str))
        xpath = xpath.decode('utf-8') if sys.version_info[0] < 3 else xpath
        elements = self.xml.xpath(xpath)
        uis = []
        for element in elements:
            uis.append(self.get_ui_by_element(element))
        return uis 
开发者ID:hao1032,项目名称:adbui,代码行数:26,代码来源:get_ui.py

示例9: parse_xml

# 需要导入模块: from lxml import etree [as 别名]
# 或者: from lxml.etree import XMLSyntaxError [as 别名]
def parse_xml(filename):
    """
    Parses either a PageXML or ALTO file with autodetermination of the file
    format.

    Args:
        filename (str): path to an XML file.

    Returns:
        A dict {'image': impath, lines: [{'boundary': [[x0, y0], ...],
        'baseline': [[x0, y0], ...]}, {...], 'text': 'apdjfqpf', 'script':
        'script_type'}, regions: {'region_type_0': [[[x0, y0], ...], ...],
        ...}}
    """
    with open(filename, 'rb') as fp:
        try:
            doc = etree.parse(fp)
        except etree.XMLSyntaxError as e:
            raise KrakenInputException(f'Parsing {filename} failed: {e}')
    if doc.getroot().tag.endswith('alto'):
        return parse_alto(filename)
    elif doc.getroot().tag.endswith('PcGts'):
        return parse_page(filename)
    else:
        raise KrakenInputException(f'Unknown XML format in {filename}') 
开发者ID:mittagessen,项目名称:kraken,代码行数:27,代码来源:xml.py

示例10: test_capi_multiple

# 需要导入模块: from lxml import etree [as 别名]
# 或者: from lxml.etree import XMLSyntaxError [as 别名]
def test_capi_multiple(self):
        """
        Test that tesseract CAPI calls create hocr output for multiple
        languages.
        """
        try:
            t = ctypes.cdll.LoadLibrary('libtesseract.so.3')
        except:
            raise unittest.SkipTest
        self.tesseract.setup(tessdata=tessdata, implementation='capi')
        ocr = self.tesseract.ocr_tesseract.run(('test', 'segmentation_tiff.xml'),
                                           languages=['grc', 'eng'],
                                           extended=False)
        outpath = os.path.join(self.storage_path, *ocr)
        self.assertTrue(os.path.isfile(outpath), msg='Tesseract did not '
                        'output a file!')
        try:
            doc = etree.parse(open(os.path.join(self.storage_path, *ocr)))
        except etree.XMLSyntaxError:
            self.fail(msg='The output was not valid html/xml!') 
开发者ID:OpenPhilology,项目名称:nidaba,代码行数:22,代码来源:test_tesseract.py

示例11: test_direct_multiple

# 需要导入模块: from lxml import etree [as 别名]
# 或者: from lxml.etree import XMLSyntaxError [as 别名]
def test_direct_multiple(self):
        """
        Test that direct tesseract calls create hocr output for multiple
        languages.
        """
        if not spawn.find_executable('tesseract'):
            raise unittest.SkipTest

        self.tesseract.setup(tessdata=tessdata, implementation='direct')
        ocr = self.tesseract.ocr_tesseract.run(('test', 'segmentation_tiff.xml'),
                                           languages=['grc', 'eng'],
                                           extended=False)
        outpath = os.path.join(self.storage_path, *ocr)
        self.assertTrue(os.path.isfile(outpath), msg='Tesseract did not '
                        'output a file!')
        try:
            etree.parse(open(os.path.join(self.storage_path, *ocr)))
        except etree.XMLSyntaxError:
            self.fail(msg='The output was not valid html/xml!') 
开发者ID:OpenPhilology,项目名称:nidaba,代码行数:21,代码来源:test_tesseract.py

示例12: test_capi_extended

# 需要导入模块: from lxml import etree [as 别名]
# 或者: from lxml.etree import XMLSyntaxError [as 别名]
def test_capi_extended(self):
        """
        Test that the CAPI extended output contains character cuts in each
        ocr_line and character confidences in each ocrx_word.
        """

        try:
            ctypes.cdll.LoadLibrary('libtesseract.so.3')
        except:
            raise unittest.SkipTest
        self.tesseract.setup(tessdata=tessdata, implementation='capi')
        ocr = self.tesseract.ocr_tesseract.run(('test', 'segmentation_tiff.xml'),
                                           languages=['eng'],
                                           extended=True)
        outpath = os.path.join(self.storage_path, *ocr)
        self.assertTrue(os.path.isfile(outpath), msg='Tesseract did not '
                        'output a file!')

        try:
            h = etree.parse(open(os.path.join(self.storage_path, *ocr)))
        except etree.XMLSyntaxError:
            self.fail(msg='The output was not valid html/xml!')
        self.assertIsNotNone(h.findall(".//line"), msg='Tesseract did not write lines.')
        self.assertIsNotNone(h.findall(".//seg"), msg='Tesseract did not write segments.')
        self.assertIsNotNone(h.findall(".//g"), msg='Tesseract did not write graphemes.') 
开发者ID:OpenPhilology,项目名称:nidaba,代码行数:27,代码来源:test_tesseract.py

示例13: test_capi_file_output_png

# 需要导入模块: from lxml import etree [as 别名]
# 或者: from lxml.etree import XMLSyntaxError [as 别名]
def test_capi_file_output_png(self):
        """
        Test that tesseract CAPI calls create hocr output for pngs.
        """

        try:
            t = ctypes.cdll.LoadLibrary('libtesseract.so.3')
        except:
            raise unittest.SkipTest

        self.tesseract.setup(tessdata=tessdata, implementation='capi')
        ocr = self.tesseract.ocr_tesseract.run(('test', 'segmentation_png.xml'),
                                           languages=['eng'],
                                           extended=False)
        outpath = os.path.join(self.storage_path, *ocr)
        self.assertTrue(os.path.isfile(outpath), msg='Tesseract did not '
                        'output a file!')
        try:
            etree.parse(open(os.path.join(self.storage_path, *ocr)))
        except etree.XMLSyntaxError:
            self.fail(msg='The output was not valid html/xml!') 
开发者ID:OpenPhilology,项目名称:nidaba,代码行数:23,代码来源:test_tesseract.py

示例14: test_capi_file_output_jpg

# 需要导入模块: from lxml import etree [as 别名]
# 或者: from lxml.etree import XMLSyntaxError [as 别名]
def test_capi_file_output_jpg(self):
        """
        Test that tesseract CAPI calls create hocr output for jpgs.
        """

        try:
            t = ctypes.cdll.LoadLibrary('libtesseract.so.3')
        except:
            raise unittest.SkipTest

        self.tesseract.setup(tessdata=tessdata, implementation='capi')
        ocr = self.tesseract.ocr_tesseract.run(('test', 'segmentation_jpg.xml'),
                                           languages=['eng'],
                                           extended=False)
        outpath = os.path.join(self.storage_path, *ocr)
        self.assertTrue(os.path.isfile(outpath), msg='Tesseract did not '
                        'output a file!')
        try:
            etree.parse(open(os.path.join(self.storage_path, *ocr)))
        except etree.XMLSyntaxError:
            self.fail(msg='The output was not valid html/xml!') 
开发者ID:OpenPhilology,项目名称:nidaba,代码行数:23,代码来源:test_tesseract.py

示例15: test_direct_file_output_png

# 需要导入模块: from lxml import etree [as 别名]
# 或者: from lxml.etree import XMLSyntaxError [as 别名]
def test_direct_file_output_png(self):
        """
        Test that direct tesseract calls create hocr output for pngs.
        """

        if not spawn.find_executable('tesseract'):
            raise unittest.SkipTest

        self.tesseract.setup(tessdata=tessdata, implementation='direct')
        ocr = self.tesseract.ocr_tesseract.run(('test', 'segmentation_png.xml'),
                                           languages=['eng'],
                                           extended=False)
        outpath = os.path.join(self.storage_path, *ocr)
        self.assertTrue(os.path.isfile(outpath), msg='Tesseract did not '
                        'output a file!')
        try:
            etree.parse(open(os.path.join(self.storage_path, *ocr)))
        except etree.XMLSyntaxError:
            self.fail(msg='The output was not valid html/xml!') 
开发者ID:OpenPhilology,项目名称:nidaba,代码行数:21,代码来源:test_tesseract.py


注:本文中的lxml.etree.XMLSyntaxError方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。