Exemplo n.º 1
0
    def _do(obj):
        # Create doc.
        doc, doc_type_info = _create_doc(obj, doc_type)

        # Set doc attributes:
        for _name, _type, _is_iterable in doc_type_info:
            # ... skip placeholders;
            if _name not in obj:
                continue

            # ... set incoming value;
            _val = obj[_name]

            # ... nulls;
            if _val is None:
                setattr(doc, _name, [] if _is_iterable else None)

            # ... complex types;
            elif _type in ontologies.get_types():
                setattr(doc, _name, _decode(_val, _type, _is_iterable))

            # ... simple types;
            else:
                setattr(doc, _name, _decode_simple(_val, _type, _is_iterable))

        return doc
Exemplo n.º 2
0
    def _do(obj):
        # Create doc.
        doc, doc_type_info = _create_doc(obj, doc_type)

        # Set doc attributes:
        for _name, _type, _is_iterable in doc_type_info:
            # ... skip placeholders;
            if _name not in obj:
                continue

            # ... set incoming value;
            _val = obj[_name]

            # ... nulls;
            if _val is None:
                setattr(doc, _name, [] if _is_iterable else None)

            # ... complex types;
            elif _type in ontologies.get_types():
                setattr(doc, _name, _decode(_val, _type, _is_iterable))

            # ... simple types;
            else:
                setattr(doc, _name, _decode_simple(_val, _type, _is_iterable))

        return doc
Exemplo n.º 3
0
def _assert_encode(target, encoding):
    """Asserts encode input parameters.

    """
    if target is None:
        raise ValueError("Cannot encode a null document.")

    if type(target) not in ontologies.get_types():
        raise TypeError("Unsupported document type: {0}.".format(type(target)))

    if not encoding in _CODECS:
        raise NotImplementedError('Unsupported document encoding :: {0}.'.format(encoding))
def _assert_encode(target, encoding):
    """Asserts encode input parameters.

    """
    if target is None:
        raise ValueError("Cannot encode a null document.")

    if type(target) not in ontologies.get_types():
        raise TypeError("Unsupported document type: {0}.".format(type(target)))

    if not encoding in _CODECS:
        raise NotImplementedError(
            'Unsupported document encoding :: {0}.'.format(encoding))
Exemplo n.º 5
0
    def _do(obj):
        # Create doc.
        doc, doc_type_info = _get_doc(obj, doc_type)

        # Set doc attributes.
        for _name, _type, _iterable in doc_type_info:
            # ... set placeholders
            if _name not in obj:
                continue
            # ... set nulls
            elif obj[_name] is None:
                setattr(doc, _name, [] if _iterable else None)
            # ... set simple / complex types
            else:
                decoder = _decode if _type in ontologies.get_types() else _decode_simple
                setattr(doc, _name, decoder(obj[_name], _type, _iterable))

        return doc
Exemplo n.º 6
0
def _is_pyesdoc_type(obj):
    """Returns flag indicating whether an object is a pyesdoc type.

    """
    return isinstance(obj, ontologies.get_types())
Exemplo n.º 7
0
   :license: GPL / CeCILL
   :platform: Unix, Windows
   :synopsis: Decodes a document from an XML text blob.

.. moduleauthor:: Earth System Documentation (ES-DOC) <*****@*****.**>

"""
import xml.etree.ElementTree as ET

from pyesdoc import ontologies
from pyesdoc.utils import convert


# Set of supported ontology types.
_TYPES = ontologies.get_types()


def _create_doc(elem, doc_type):
    """Creates & returns a document to be hydrated from an xml element.

    """
    type_key = elem.get("ontologyTypeKey")
    if type_key and ontologies.get_type_key(doc_type) != type_key:
        doc_type = ontologies.get_type_from_key(type_key)
    if doc_type is None:
        raise ValueError("Decoding type is unrecognized")

    return doc_type(), ontologies.get_decoder_info(doc_type)

Exemplo n.º 8
0
def _is_encodable(obj):
    """Returns flag indicating whether an object is encodable or not.

    """
    return type(obj) in ontologies.get_types()
Exemplo n.º 9
0
.. module:: xml.decoder.py

   :license: GPL / CeCILL
   :platform: Unix, Windows
   :synopsis: Decodes a document from an XML text blob.

.. moduleauthor:: Earth System Documentation (ES-DOC) <*****@*****.**>

"""
import xml.etree.ElementTree as ET

from pyesdoc import ontologies
from pyesdoc.utils import convert

# Set of supported ontology types.
_TYPES = ontologies.get_types()


def _create_doc(elem, doc_type):
    """Creates & returns a document to be hydrated from an xml element.

    """
    type_key = elem.get('ontologyTypeKey')
    if type_key and ontologies.get_type_key(doc_type) != type_key:
        doc_type = ontologies.get_type_from_key(type_key)
    if doc_type is None:
        raise ValueError('Decoding type is unrecognized')

    return doc_type(), ontologies.get_decoder_info(doc_type)

Exemplo n.º 10
0
def _is_pyesdoc_type(obj):
    """Returns flag indicating whether an object is a pyesdoc type.

    """
    return isinstance(obj, ontologies.get_types())