Esempio n. 1
0
def raw_xml_to_dict(raw_xml, postprocessor=None, force_list=None):
    """Transform a raw xml to dict. Returns an empty dict if the parsing failed.

    Args:
        raw_xml:
        postprocessor:
        force_list:

    Returns:

    """
    try:
        if postprocessor:
            # set postprocessor function if found in the list (XML_POST_PROCESSORS)
            postprocessor = (XML_POST_PROCESSORS[postprocessor]
                             if postprocessor in XML_POST_PROCESSORS else
                             postprocessor)
            # check if postprocessor is callable
            if not callable(postprocessor):
                raise exceptions.CoreError("postprocessor is not callable")

        # convert xml to dict
        return xmltodict.parse(raw_xml,
                               postprocessor=postprocessor,
                               force_list=force_list)
    except xmltodict.expat.ExpatError:
        raise exceptions.XMLError(
            "An unexpected error happened during the XML parsing.")
Esempio n. 2
0
    def get_collection(db, collection_name):
        """ Return cursor of collection name in parameters.

        Args:
            db:
            collection_name:

        Returns:

        """
        try:
            data_list = db[collection_name]  # get the data collection
            return data_list  # return collection
        except:  # or raise an exception
            raise exceptions.CoreError("Collection in database does not exist")
Esempio n. 3
0
    def connect(self, db_uri, db_name, doc_class=dict):
        """ Connect to the database from settings.py.

        Args:
            db_uri:
            db_name:
            doc_class:

        Returns:

        """
        # create a connection
        self.client = MongoClient(db_uri, document_class=doc_class)
        db = self.client[db_name]    # connect to the database
        if db is not None:
            return db           # return db connection
        else:                   # or raise an exception
            raise exceptions.CoreError("Database connection error")
Esempio n. 4
0
def is_schema_valid(xsd_string, *args, **kwargs):
    """Test if the schema is valid to be uploaded.

    Args:
        xsd_string:

    Returns:

    """
    if not is_well_formed_xml(xsd_string):
        raise exceptions.XMLError("Uploaded file is not well formatted XML.")

    # Check schema support by the core
    errors = _check_core_support(xsd_string)
    if len(errors) > 0:
        errors_str = ", ".join(errors)
        raise exceptions.CoreError(errors_str)

    error = validate_xml_schema(XSDTree.build_tree(xsd_string), *args, **kwargs)
    if error is not None:
        raise exceptions.XSDError(error)
Esempio n. 5
0
def xsl_transform(xml_string, xslt_string):
    """Apply transformation to xml.

    Args:
        xml_string:
        xslt_string:

    Returns:

    """
    try:
        # Build the XSD and XSLT tree
        xslt_tree = XSDTree.build_tree(xslt_string)
        xsd_tree = XSDTree.build_tree(xml_string)

        # Get the XSLT transformation and transform the XSD
        transform = XSDTree.transform_to_xslt(xslt_tree)
        transformed_tree = transform(xsd_tree)
        return str(transformed_tree)
    except Exception:
        raise exceptions.CoreError(
            "An unexpected exception happened while transforming the XML")