Ejemplo n.º 1
0
 def _get_xml(self, path):
     try:
         return self.open_xml_file(path)
     except IOError:
         raise IOError("Cannot find {}".format(path))
     except ExpatError as ex:
         raise ExpatError("{} is invalid xml '{}'".format(path, ex))
Ejemplo n.º 2
0
    def _render_xml(self, xml_document, **kwargs):
        # Prepare the xml object to be processed by jinja2
        self.log.debug('Rendering XML object')
        template_string = ""

        try:
            self.template_images = dict()
            self._prepare_document_tags(xml_document)
            xml_source = xml_document.toxml()
            xml_source = xml_source.encode('ascii', 'xmlcharrefreplace')
            jinja_template = self.environment.from_string(
                self._unescape_entities(xml_source.decode('utf-8'))
            )

            result = jinja_template.render(**kwargs)
            result = self._encode_escape_chars(result)

            final_xml = parseString(result.encode('ascii', 'xmlcharrefreplace'))
            if self.template_images:
                self.replace_images(final_xml)

            return final_xml
        except ExpatError as e:
            near = result.split('\n')[e.lineno -1][e.offset-200:e.offset+200]
            raise ExpatError('ExpatError "%s" at line %d, column %d\nNear of: "[...]%s[...]"' % \
                             (ErrorString(e.code), e.lineno, e.offset, near))
        except:
            self.log.error('Error rendering template:\n%s',
                           xml_document.toprettyxml(), exc_info=True)

            self.log.error('Unescaped template was:\n{0}'.format(template_string))
            raise
        finally:
            self.log.debug('Rendering xml object finished')
Ejemplo n.º 3
0
def _get_metadata(metadata, element, tag):
    values = []

    try:
        metadatadom = minidom.parse(metadata)
    except ExpatError as e:
        raise ExpatError("%s: %s" % (
            metadata,
            e,
        ))

    try:
        elements = metadatadom.getElementsByTagName(element)
        if not elements:
            return values
    except NotFoundErr:
        return values

    try:
        for _element in elements:
            node = _element.getElementsByTagName(tag)

            try:
                values.append(node[0].childNodes[0].data)
            except IndexError:
                pass
    except NotFoundErr:
        raise NotFoundErr("%s: Malformed input: missing 'flag' tag(s)" %
                          (metadata))

    metadatadom.unlink()
    return values
Ejemplo n.º 4
0
 def testLoad(self):
     """ Tests the initialization of the data store handler. """
     
     self._fileStorerMock.methodNameResultMap = {"exists": (True, None), "readData": (SimpleMock(""), None)}
     self._parseStringMock.value = datastores.datastores([datastores.default(name="name1", storeType=constants.DEFAULT_STORE),
                                                          datastores.default(name="name2", storeType=constants.DEFAULT_STORE)])
     self._datastoreHandler.load()
     self.assertEquals(len(self._datastoreHandler.datastores), 2)
 
     self._fileStorerMock.methodNameResultMap = {"exists": (False, None)}
     self.assertRaises(ConfigurationError, self._datastoreHandler.load)
     
     self._fileStorerMock.methodNameResultMap = {"exists": (None, PersistenceError(""))}
     self.assertRaises(ConfigurationError, self._datastoreHandler.load)
     
     self._fileStorerMock.methodNameResultMap = {"exists": (True, None), "readData": (None, PersistenceError(""))}
     self.assertRaises(ConfigurationError, self._datastoreHandler.load)
     
     self._fileStorerMock.methodNameResultMap = {"exists": (True, None), "readData": (SimpleMock(""), None)}
     self._parseStringMock.error = ExpatError("")
     self.assertRaises(ConfigurationError, self._datastoreHandler.load)
     
     self._fileStorerMock.methodNameResultMap = {"exists": (True, None), "readData": (SimpleMock(""), None)}
     self._parseStringMock.error = ValueError("")
     self.assertRaises(ConfigurationError, self._datastoreHandler.load)
Ejemplo n.º 5
0
    def wrapper(*args, **kwargs):
        rtc_obj = args[0].get_rtc_obj()

        if not hasattr(rtc_obj, "headers") or rtc_obj.headers is None:
            # still in the initialization or relogin
            # directly call the method
            return func(*args, **kwargs)
        else:
            # check whether token expires
            try:
                resp = func(*args, **kwargs)
                xmltodict.parse(resp.content)
                return resp
            except ExpatError as excp:
                if "invalid token" in str(excp):
                    # expires
                    try:
                        rtc_obj.relogin()
                    except RTCException:
                        raise RTCException("Relogin Failed: "
                                           "Invalid username or password")
                    kwargs["headers"]["Cookie"] = rtc_obj.headers["Cookie"]
                    return func(*args, **kwargs)
                else:
                    # not expires
                    # raise the actual exception
                    raise ExpatError(excp)
Ejemplo n.º 6
0
def _get_metadata(metadata, element, tag):
	values = []

	try:
		metadatadom = minidom.parse(metadata)
	except ExpatError as e:
		raise ExpatError("%s: %s" % (metadata, e,))

	try:
		elements = metadatadom.getElementsByTagName(element)
		if not elements:
			return values
	except NotFoundErr:
		return values

	try:
		for _element in elements:
			node = _element.getElementsByTagName(tag)

			if tag == "herd" and (not node or not node[0].childNodes):
#				print >> stderr, "'%s' is missing a <herd> tag or it is empty," % metadata
#				print >> stderr, "please file a bug at https://bugs.gentoo.org and refer to http://www.gentoo.org/proj/en/devrel/handbook/handbook.xml?part=2&chap=4"
				values.append("no-herd")
				continue

			try:
				values.append(node[0].childNodes[0].data)
			except IndexError:
				pass
	except NotFoundErr:
		raise NotFoundErr("%s: Malformed input: missing 'flag' tag(s)" % (metadata))

	metadatadom.unlink()
	return values
Ejemplo n.º 7
0
    def get_response(self, url, payload):
        self.headers['Content-Length'] = str(len(payload))

        status_code = 0
        i = 0
        while status_code != 200 and i < 10:
            response = requests.post(url, data=payload, headers=self.headers)
            status_code = response.status_code
            i += 1

        if i >= 10 and status_code != 200:
            raise TimeoutError(
                'Aparentemente o webservice dos Correios está indisponível no momento'
                'Tente novamente mais tarde.')
        try:
            response = xmltodict.parse(
                response.text)['cResultado']['Servicos']['cServico']
            return response
        except KeyError:
            response = xmltodict.parse(
                response.text
            )['cResultadoServicos']['ServicosCalculo']['cServicosCalculo']
            return response
        except ExpatError:
            raise ExpatError(
                'Não foi possível parsear o XML de resposta do webservice dos Correios'
            )
Ejemplo n.º 8
0
 def _importBody(self, body):
     """Import the object from the file body.
     """
     try:
         dom = parseString(body)
     except ExpatError, e:
         filename = (self.filename
                     or '/'.join(self.context.getPhysicalPath()))
         raise ExpatError('%s: %s' % (filename, e))
    def test_GIVEN_invalid_xml_WHEN_adding_ioc_THEN_error(self):
        error_str = "No configs Exist"
        self.file_access.open_xml_file = Mock(side_effect=ExpatError(error_str))
        adder = AddToBaseIOCs(None, None, None)

        result = adder.perform(self.file_access, self.logger)

        assert_that(result, is_(-1), "result")
        assert_that(self.logger.log_err, has_item("IOC file appears not be valid XML, error '{}'".format(error_str)))
Ejemplo n.º 10
0
def xml_to_json(body):
    try:
        dict = xmltodict.parse(body)
    except ExpatError as e:
        # Something went wrong--probably malformed XML. Pass the
        # error back up to the caller.
        raise ExpatError('Malformed XML - ' + str(e))

    # TODO: Error handling here? Can this go wrong?
    return json.dumps(dict[DATA_ELEMENT])
Ejemplo n.º 11
0
    def __call__(self):

        full_path = os.path.join(self._path, METADATA_XML)
        if not os.path.exists(full_path):
            return {}

        with open(full_path, 'r') as fp:
            try:
                return self.parseXML(fp.read())
            except ExpatError as e:
                raise ExpatError('%s: %s' % (full_path, e))
Ejemplo n.º 12
0
def xml_pickler(json_conf, filename, default_header,
                cache=None,
                max_target_seqs=10):
    valid, _, exc = BlastOpener(filename).sniff(default_header=default_header)
    engine = connect(json_conf, strategy="threadlocal")
    session = Session(bind=engine)

    if not valid:
        err = "Invalid BLAST file: %s" % filename
        raise TypeError(err)
    dbname, conn, cursor = _create_xml_db(filename)
    if not isinstance(cache, dict) or set(cache.keys()) != {"query", "target"}:
        cache = dict()
        cache["query"] = dict((item.query_name, item.query_id) for item in session.query(Query))
        cache["target"] = dict((item.target_name, item.target_id) for item in session.query(Target))
    try:
        with BlastOpener(filename) as opened:
            try:
                qmult, tmult = None, None
                for query_counter, record in enumerate(opened, start=1):
                    if qmult is None:
                        qmult, tmult = get_multipliers(record)
                        off_by_one = get_off_by_one(record)
                    hits, hsps, cache = objectify_record(
                        record, [], [], cache, max_target_seqs=max_target_seqs,
                        qmult=qmult, tmult=tmult, off_by_one=off_by_one)

                    try:
                        jhits = json.dumps(hits, number_mode=json.NM_NATIVE)
                        jhsps = json.dumps(hsps, number_mode=json.NM_NATIVE)
                    except ValueError:
                        jhits = json.dumps(hits)
                        jhsps = json.dumps(hsps)

                    cursor.execute("INSERT INTO dump VALUES (?, ?, ?)",
                                   (query_counter,
                                    jhits,
                                    jhsps))
            except ExpatError as err:
                raise ExpatError("{} is an invalid BLAST file, sending back anything salvageable.\n{}".format(filename,
                                                                                                              err))
    except xml.etree.ElementTree.ParseError as err:
        # logger.error("%s is an invalid BLAST file, sending back anything salvageable", filename)
        raise xml.etree.ElementTree.ParseError(
            "{} is an invalid BLAST file, sending back anything salvageable.\n{}".format(filename, err))
    except ValueError as err:
        # logger.error("Invalid BLAST entry")
        raise ValueError(
            "{} is an invalid BLAST file, sending back anything salvageable.\n{}".format(filename, err))

    cursor.close()
    conn.commit()
    conn.close()
    return dbname
Ejemplo n.º 13
0
 def _context(self):
     self.p = ParserCreate(namespace_separator='}')
     self.p.StartElementHandler = self._start_element
     self.p.EndElementHandler = self._end_element
     self.p.CharacterDataHandler = self._char_data
     self._ancestors = []
     self._root = None
     try:
         yield
     except ExpatError as err:
         e = ExpatError(ErrorString(err.code))
         e.lineno = err.lineno
         e.offset = err.offset
         e.filename = self.filename
         raise e
     except ValueError as err:
         err.lineno = self.p.CurrentLineNumber
         err.offset = self.p.CurrentColumnNumber
         err.filename = self.filename
         raise err
     except self.Exit:
         pass
Ejemplo n.º 14
0
    def test_parse_when_minidom_fails_without_apk_path(self, mock_file,
                                                       mock_file_parser,
                                                       mock_minidom):
        file = any_file(filename="AndroidManifest.xml")
        mock_parser_instance = any_file_parser(file=file)
        mock_file_parser.return_value = mock_parser_instance
        mock_minidom.parse.side_effect = ExpatError()

        with self.assertRaises(AndroidManifestParsingError):
            self.sut.parse(filepath="any-file-path",
                           binary=False,
                           apk_path=None,
                           extended_processing=False)
Ejemplo n.º 15
0
 def _context(self):
     self.p = ParserCreate(namespace_separator='}')
     self.p.StartElementHandler = self._start_element
     self.p.EndElementHandler = self._end_element
     self.p.CharacterDataHandler = self._char_data
     self._ancestors = []
     self._root = None
     try:
         yield
     except ExpatError as err:
         e = ExpatError(ErrorString(err.code))
         e.lineno = err.lineno
         e.offset = err.offset
         e.filename = self.filename
         raise e
     except ValueError as err:
         err.lineno = self.p.CurrentLineNumber
         err.offset = self.p.CurrentColumnNumber
         err.filename = self.filename
         raise err
     except self.Exit:
         pass
Ejemplo n.º 16
0
def xml_pickler(json_conf, filename, default_header, max_target_seqs=10):
    valid, _, exc = BlastOpener(filename).sniff(default_header=default_header)
    engine = connect(json_conf, strategy="threadlocal")
    session = Session(bind=engine)

    if not valid:
        err = "Invalid BLAST file: %s" % filename
        raise TypeError(err)
    dbname, conn, cursor = _create_xml_db(filename)
    cache = {"query": dict(), "target": dict()}
    try:
        with BlastOpener(filename) as opened:
            try:
                for query_counter, record in enumerate(opened, start=1):
                    hits, hsps, cache = objectify_record(
                        session,
                        record, [], [],
                        cache,
                        max_target_seqs=max_target_seqs)

                    try:
                        jhits = json.dumps(hits, number_mode=json.NM_NATIVE)
                        jhsps = json.dumps(hsps, number_mode=json.NM_NATIVE)
                    except ValueError:
                        jhits = json.dumps(hits)
                        jhsps = json.dumps(hsps)

                    cursor.execute("INSERT INTO dump VALUES (?, ?, ?)",
                                   (query_counter, jhits, jhsps))
            except ExpatError as err:
                # logger.error("%s is an invalid BLAST file, sending back anything salvageable", filename)
                raise ExpatError(
                    "{} is an invalid BLAST file, sending back anything salvageable.\n{}"
                    .format(filename, err))
    except xml.etree.ElementTree.ParseError as err:
        # logger.error("%s is an invalid BLAST file, sending back anything salvageable", filename)
        raise xml.etree.ElementTree.ParseError(
            "{} is an invalid BLAST file, sending back anything salvageable.\n{}"
            .format(filename, err))
    except ValueError as err:
        # logger.error("Invalid BLAST entry")
        raise ValueError(
            "{} is an invalid BLAST file, sending back anything salvageable.\n{}"
            .format(filename, err))

    cursor.close()
    conn.commit()
    conn.close()
    return dbname
Ejemplo n.º 17
0
    def test_parse_when_minidom_fails_with_apk_path_and_extended_processing(
            self, mock_file, mock_file_parser, mock_minidom, mock_aapt):
        file = any_file(filename="AndroidManifest.xml")
        mock_parser_instance = any_file_parser(file=file)
        mock_file_parser.return_value = mock_parser_instance
        mock_minidom.parse.side_effect = ExpatError()
        mock_aapt.get_apk_info.return_value = self.any_aapt_apk_info(
            package_name="any-package-name",
            version_code=1,
            version_name="any-version-name",
            sdk_max="20",
            sdk_min="10",
            sdk_target="15")
        mock_aapt.get_app_permissions.return_value = [
            "any-permission-0", "any-permission-1", "any-permission-2"
        ]
        mock_aapt.get_manifest_info.return_value = self.any_aapt_manifest_info(
            activities=["any-activity-name"],
            services=["any-service-name"],
            receivers=["any-broadcast-receiver-name"])

        manifest = self.sut.parse(filepath="any-file-path",
                                  binary=False,
                                  apk_path="any_apk_path",
                                  extended_processing=True)

        assert_file_parser_called_once_with(mock_parser_instance,
                                            filepath="any-file-path",
                                            filename="AndroidManifest.xml")
        mock_file.assert_called_with("any-file-path", "rb")
        mock_minidom.parse.assert_called_with("any-file-path")
        mock_aapt.get_apk_info.assert_called_with("any_apk_path")
        mock_aapt.get_app_permissions.assert_called_with("any_apk_path")
        mock_aapt.get_manifest_info.assert_called_with("any_apk_path")
        assert_file_equal(self, expected=file, actual=manifest)
        self.assert_manifest_equal(
            manifest=manifest,
            package_name="any-package-name",
            version=AppVersion(code=1, name="any-version-name"),
            sdk=AppSdk(min_version="10", target_version="15",
                       max_version="20"),
            permissions=[
                "any-permission-0", "any-permission-1", "any-permission-2"
            ],
            activities=[AppActivity(name="any-activity-name")],
            services=[AppService(name="any-service-name")],
            receivers=[
                AppBroadcastReceiver(name="any-broadcast-receiver-name")
            ])
Ejemplo n.º 18
0
    def testStore(self):
        """ Tests the storing of the preferences. """

        self._handler.load()
        writeDataMock = _WriteDataMock()
        self._fileStorerMock.writeData = writeDataMock
        self._handler.showDottedFilesLocal = True
        self._handler.showDottedFilesRemote = False
        self._handler.addConnection(
            "http://192.168.125.130/repos/config/test2", "test", "test", False,
            "ldap://", "OU=DLR,DC=intra,DC=dlr,DC=de", False, None, "ds1",
            "ds2")
        self._handler.addConnection(
            "http://192.168.125.130/repos/config/test1", None, None)
        self._handler.addScriptUri("path")
        self._handler.addScriptUri("path2")
        self._handler.addSearchQuery("query", "query")
        self._handler.addSearchQuery("query2", "query2")
        self._handler.store()

        self.assertEquals(writeDataMock.content, _VALID_CONFIGURATION)
        del self._fileStorerMock.writeData
        self._handler.load()

        self._fileStorerMock.methodNameResultMap = {
            "exists": (None, PersistenceError())
        }
        self.assertRaises(ConfigurationError, self._handler.store)

        self._fileStorerMock.methodNameResultMap = {
            "exists": (False, None),
            "createResource": (None, PersistenceError())
        }
        self.assertRaises(ConfigurationError, self._handler.store)

        self._fileStorerMock.methodNameResultMap = {
            "exists": (True, None),
            "writeData": (None, PersistenceError())
        }
        self.assertRaises(ConfigurationError, self._handler.store)

        self._handler._preferences = SimpleMock(error=ExpatError())
        self._fileStorerMock.methodNameResultMap = {"exists": (True, None)}
        self.assertRaises(ConfigurationError, self._handler.store)
Ejemplo n.º 19
0
def fix_xml(needle):
    needle = replace_escape_chars(needle)
    # first, we need to make sure the needle is valid html
    """
    try:
        validate_html(needle)
    except HTMLParser.HTMLParseError:
        print "error with: %s"%needle
        raise
    """
    # then we close all the open-ended tags to make sure it will compare
    # properly
    #needle = bs(needle).prettify()
    needle = xhtmlify(needle)
    try:
        needle_node = etree.fromstring(needle)
    except ExpatError, e:
        raise ExpatError('Could not parse %s into xml. %s' % (
            needle, e.args[0]
        ))
Ejemplo n.º 20
0
    def test_ncbi_fails(self):
        exceptions = [ChunkedEncodingError(), ConnectionError(), ReadTimeout(),
                      ExpatError(), RuntimeError('bad record')]
        for code in [400, 429]:
            http_exception = HTTPError()
            http_exception.response = Response()
            http_exception.response.status_code = code
            exceptions.append(http_exception)

        for exception in exceptions:
            self.ncbi_exception = exception
            with self.assertLogs(level='DEBUG') as log:
                seq, tax = self.get_ncbi_data(query='MT345279.1')
                tax = tax.view(DataFrame)
                self.assertEqual(
                    tax['Taxon']['MT345279.1'],
                    'k__Fungi; p__Basidiomycota; c__Agaricomycetes; '
                    'o__Boletales; f__Boletaceae; g__Boletus; s__edulis'
                )
            self.assertTrue('Retrying' in log.output[0])
Ejemplo n.º 21
0
 def testImport(self):
     """ Tests the import of data store configurations. """
     
     self._createFileStorerMock.value = SimpleMock(SimpleMock())
     self._parseStringMock.value = datastores.datastores([datastores.default(name="name1", storeType=constants.DEFAULT_STORE),
                                                          datastores.default(name="name2", storeType=constants.DEFAULT_STORE)])
     self._datastoreHandler.importDataStores("/local/file/path")
     self.assertEquals(len(self._datastoreHandler.datastores), 2)
     
     self._parseStringMock.error = ExpatError("")
     self.assertRaises(ConfigurationError, self._datastoreHandler.importDataStores, "/local/file/path")
     self.assertEquals(len(self._datastoreHandler.datastores), 2)
     
     self._createFileStorerMock.value = SimpleMock(error=PersistenceError(""))
     self.assertRaises(ConfigurationError, self._datastoreHandler.importDataStores, "/local/file/path")
     self.assertEquals(len(self._datastoreHandler.datastores), 2)
     
     self._createFileStorerMock.error = PersistenceError("")
     self.assertRaises(ConfigurationError, self._datastoreHandler.importDataStores, "/local/file/path")
     self.assertEquals(len(self._datastoreHandler.datastores), 2)
Ejemplo n.º 22
0
    def testLoad(self):
        """ Tests the loading of the persisted data model. """

        self._fileStorerMock.methodNameResultMap = {
            "exists": (True, None),
            "readData": (SimpleMock(""), None)
        }
        self._parseStringMock.value = self._persistedDataModel
        self._dataModelHandler.load()
        self.assertEquals(self._dataModelHandler.datatypes, self._dataTypes)
        self.assertEquals(self._dataModelHandler.relations, self._relations)

        self._fileStorerMock.methodNameResultMap = None
        self._fileStorerMock.value = False
        self.assertRaises(ConfigurationError, self._dataModelHandler.load)

        self._fileStorerMock.error = PersistenceError("")
        self.assertRaises(ConfigurationError, self._dataModelHandler.load)

        self._parseStringMock.error = ExpatError("")
        self.assertRaises(ConfigurationError, self._dataModelHandler.load)

        self._parseStringMock.error = ValueError("")
        self.assertRaises(ConfigurationError, self._dataModelHandler.load)
Ejemplo n.º 23
0
def read_label(filename,
               strip_extra_whitespace=True,
               enforce_default_prefixes=False,
               include_namespace_map=False,
               decode_py2=False):
    """ Reads a PDS4 XML Label into an ``ElementTree`` Element object.

        Parameters
        ----------
        filename : str or unicode
            The filename, including the path, of the XML label.
        strip_extra_whitespace : bool, optional
            If True, then for element text and attribute values, it collapses
            contiguous whitespaces (including space, tab and newlines) into a
            single space, and removes leading and trailing whitespace altogether.
            However, this only done if the value has a single line with
            non-whitespace characters. Defaults to False.
        enforce_default_prefixes : bool, optional
            If True, strips the default namespace, and ensures that
            default PDS4 prefixes are used for known namespaces (PDS4_NAMESPACES).
            Defaults to False.
        include_namespace_map : bool, optional
            If True, changes method return to a tuple, where the first
            value is the label ElementTree object as usual and the second
            is a ``dict`` with keys being the namespace URIs and values being
            the namespace prefixes in this label. Defaults to False.
        decode_py2 : bool, optional
            If True, decodes UTF-8 byte strings (``str``) into ``unicode``
            strings in Python 2. Option is ignored in Python 3. Defaults to False.

        Returns
        -------
        ``ElementTree`` Element
            Root element for the read-in PDS4 label

    """

    # Read-in XML tree
    try:
        xml_tree = ET.iterparse(filename, events=('start-ns', 'end'))
    except IOError:
        raise IOError('Unable to locate or read label file: ' + filename)

    # Adjust XML tree
    try:

        namespace_map = {}

        for event, elem in xml_tree:

            # Add namespace to the namespace map
            if event == 'start-ns':

                if enforce_default_prefixes:

                    for prefix, uri in six.iteritems(PDS4_NAMESPACES):

                        # Ensure the PDS4 namespace is the default prefix
                        if elem[1] == PDS4_NAMESPACES['pds']:
                            elem = ('', elem[1])

                        # Ensure that dictionaries which are referred to in code
                        # by prefix (such as disp and sp) have the expected prefix
                        elif uri == elem[1]:
                            elem = (prefix, elem[1])

                # Add namespace to map (different prefixes for an existing namespace URI are skipped)
                # Technical note: this map is stored dict[URI] = prefix for two reasons:
                #   (1) ElementTree itself internally stores the namespace map like this, despite taking
                #       it in opposite relation from user
                #   (2) It seems ever slightly more legitimate to remember a single prefix to referring to
                #       multiple URI (e.g. via local prefixes) than to remember two prefixes referring to
                #       the same URI.
                #   These are not necessarily good reasons.
                if elem[1] not in namespace_map:
                    namespace_map[elem[1]] = elem[0]

                continue

            # Strip PDS4 namespace tag (a continuation of ensuring default prefix is PDS4 namespace)
            if (enforce_default_prefixes) and (PDS4_NAMESPACES['pds']
                                               in elem.tag):
                elem.tag = elem.tag.split('{', 1)[0] + elem.tag.split('}',
                                                                      1)[1]

            # Strip whitespace in elements and attributes if requested
            if strip_extra_whitespace:

                subiter = compat.ET_Tree_iter(ET.ElementTree(elem))
                attribs = six.iteritems(elem.attrib)

                # Strip whitespaces at beginning and end of value in elements that do not have children
                if len(elem) == 0:
                    for elem_content in subiter:

                        if (elem_content.text) and (_non_blank_line_count(
                                elem_content.text) == 1):
                            elem_content.text = _normalize(elem_content.text)

                # Strip whitespaces at beginning and end of attribute values
                for name, value in attribs:
                    if _non_blank_line_count(value) == 1:
                        elem.attrib[name] = _normalize(value)

        label_xml_root = xml_tree.root

        # For Python 2, we can decode all ``str`` to ``unicode``, such that all meta data strings
        # are consistently unicode.
        if six.PY2 and decode_py2:
            label_xml_root = _decode_tree(label_xml_root)

    # Raise exception if XML cannot be parsed. In Python 3 we raise from None to avoid confusing re-raise
    except (ExpatError, compat.ET_ParseError):
        six.raise_from(
            ExpatError(
                'The requested PDS4 label file does not appear contain valid XML: '
                + filename), None)

    if include_namespace_map:
        return label_xml_root, namespace_map

    else:
        return label_xml_root
Ejemplo n.º 24
0
def xml_pickler(configuration,
                filename,
                default_header,
                cache=None,
                max_target_seqs=10):
    valid, _, exc = BlastOpener(filename).sniff(default_header=default_header)
    engine = connect(configuration, strategy="threadlocal")
    session = Session(bind=engine)

    if not valid:
        err = "Invalid BLAST file: %s" % filename
        raise TypeError(err)
    directory = os.path.dirname(filename)
    with tempfile.NamedTemporaryFile(suffix=".db",
                                     dir=directory,
                                     mode="wb",
                                     delete=False) as dbhandle:
        dbname = dbhandle.name
        if not isinstance(cache,
                          dict) or set(cache.keys()) != {"query", "target"}:
            cache = dict()
            cache["query"] = dict((item.query_name, item.query_id)
                                  for item in session.query(Query))
            cache["target"] = dict((item.target_name, item.target_id)
                                   for item in session.query(Target))

        rows = b""
        try:
            with BlastOpener(filename) as opened:
                try:
                    qmult, tmult = None, None
                    for query_counter, record in enumerate(opened, start=1):
                        if qmult is None:
                            qmult, tmult = get_multipliers(record)
                            off_by_one = get_off_by_one(record)
                        hits, hsps, cache = objectify_record(
                            record, [], [],
                            cache,
                            max_target_seqs=max_target_seqs,
                            qmult=qmult,
                            tmult=tmult,
                            off_by_one=off_by_one)

                        record = {"hits": hits, "hsps": hsps}
                        jrecord = dumper(record)
                        write_start = dbhandle.tell()
                        write_length = dbhandle.write(msgpack.dumps(jrecord))
                        rows += struct_row.pack(query_counter, write_start,
                                                write_length)

                except ExpatError as err:
                    raise ExpatError(
                        "{} is an invalid BLAST file, sending back anything salvageable.\n{}"
                        .format(filename, err))
        except xml.etree.ElementTree.ParseError as err:
            # logger.error("%s is an invalid BLAST file, sending back anything salvageable", filename)
            raise xml.etree.ElementTree.ParseError(
                "{} is an invalid BLAST file, sending back anything salvageable.\n{}"
                .format(filename, err))
        except ValueError as err:
            # logger.error("Invalid BLAST entry")
            raise ValueError(
                "{} is an invalid BLAST file, sending back anything salvageable.\n{}"
                .format(filename, err))

        return dbname, zlib.compress(rows)
Ejemplo n.º 25
0
 def handle_invalid_xmpp_element(*args: Any) -> None:
     raise ExpatError('Found invalid xmpp stream element: %s' % str(args))
Ejemplo n.º 26
0
    def _render_xml(self, xml_document, **kwargs):
        # Prepare the xml object to be processed by jinja2
        self.log.debug('Rendering XML object')
        template_string = ""

        try:
            self.template_images = dict()
            self._prepare_document_tags(xml_document)
            xml_source = xml_document.toxml()
            xml_source = xml_source.encode('ascii', 'xmlcharrefreplace')
            jinja_template = self.environment.from_string(
                self._unescape_entities(xml_source.decode('utf-8')))

            result = jinja_template.render(**kwargs)

            final_xml = parseString(result.encode('ascii',
                                                  'xmlcharrefreplace'))

            # float convert
            if self.FLOAT_CONVERT != FLOAT_CONVERT_NONE:
                paras = final_xml.getElementsByTagName('text:p')
                for p in paras:
                    for ch in p.childNodes:
                        if ch.nodeType == ch.TEXT_NODE:
                            try:
                                if self.FLOAT_CONVERT == FLOAT_CONVERT_ONLY_FLOAT_STYLE:
                                    style = p.parentNode.getAttribute(
                                        'table:style-name')
                                    if 'float' not in style.lower():
                                        continue

                                float(ch.data)
                                p.parentNode.attributes[
                                    'office:value-type'] = 'float'
                                p.parentNode.attributes[
                                    'calcext:value-type'] = 'float'
                                p.parentNode.attributes[
                                    'office:value'] = ch.data
                            except Exception as e:
                                continue

            if self.template_images:
                self.replace_images(final_xml)

            return final_xml
        except ExpatError as e:
            if not 'result' in locals():
                result = xml_source
            near = result.split('\n')[e.lineno - 1][e.offset - 200:e.offset +
                                                    200]

            raise ExpatError('ExpatError "%s" at line %d, column %d\nNear of: "[...]%s[...]"' % \
                             (ErrorString(e.code), e.lineno, e.offset, near))
        except:
            self.log.error('Error rendering template:\n%s',
                           xml_document.toprettyxml(),
                           exc_info=True)

            self.log.error(
                'Unescaped template was:\n{0}'.format(template_string))
            raise
        finally:
            self.log.debug('Rendering xml object finished')
Ejemplo n.º 27
0
 def test_xmlsimpleparser_init_failed(self):
     """Ensure the simple XML parser fails correctly."""
     self.mod.ParserCreate = Mock()
     self.mod.ParserCreate.return_value.ParseFile.side_effect = ExpatError()
     with self.assertRaises(OSError):
         self.mod.XMLSimpleParser(self.normal_kml, 2, 3, 4, 5)
Ejemplo n.º 28
0
 def request(self, host, handler, request, verbose=None):
     raise ExpatError("mismatched tag")