Beispiel #1
0
    def read_meta(self, document_id=None):
        """
        Load metadata associated with the document

        .. note::
           This method is called automatically if needed when a property is first accessed. You will not normally have
           to use this method manually.

        :param document_id: (optional) set the document id if this is an :py:meth:`abstract` document
        :return: self
        """
        if document_id:
            if not self.abstract:
                raise ImmutableDocumentException()
            self._id = document_id

        if self.abstract:
            raise AbstractDocumentException()

        metadata = self._api.get_document_meta(self.id)

        self._name = metadata['document_name']
        self._public = metadata['public']
        self._owner = metadata['owner']
        self._created_at = parse_xsd_datetime(metadata['created_at'])
        self._views = metadata['views_count']

        self._bundles = BundleManager(self._api, self)

        return self
Beispiel #2
0
    def read_meta(self, document_id=None):
        """
        Load metadata associated with the document

        .. note::
           This method is called automatically if needed when a property is first accessed. You will not normally have
           to use this method manually.

        :param document_id: (optional) set the document id if this is an :py:meth:`abstract` document
        :return: self
        """
        if document_id:
            if not self.abstract:
                raise ImmutableDocumentException()
            self._id = document_id

        if self.abstract:
            raise AbstractDocumentException()

        metadata = self._api.get_document_meta(self.id)

        self._name = metadata['document_name']
        self._public = metadata['public']
        self._owner = metadata['owner']
        self._created_at = parse_xsd_datetime(metadata['created_at'])
        self._views = metadata['views_count']

        self._bundles = BundleManager(self._api, self)

        return self
Beispiel #3
0
    def __init__(self, api, document, bundle):
        self._api = api

        self._id = bundle['id']
        self._created_at = parse_xsd_datetime(bundle['created_at'])
        self._identifier = bundle['identifier']
        self._document = document

        self._prov = None
Beispiel #4
0
    def __init__(self, api, document, bundle):
        self._api = api

        self._id = bundle['id']
        self._created_at = parse_xsd_datetime(bundle['created_at'])
        self._identifier = bundle['identifier']
        self._document = document

        self._prov = None
Beispiel #5
0
def decode_json_container(jc, bundle):
    if 'prefix' in jc:
        prefixes = jc['prefix']
        for prefix, uri in prefixes.items():
            if prefix != 'default':
                bundle.add_namespace(Namespace(prefix, uri))
            else:
                bundle.set_default_namespace(uri)
        del jc['prefix']

    for rec_type_str in jc:
        rec_type = PROV_RECORD_IDS_MAP[rec_type_str]
        for rec_id, content in jc[rec_type_str].items():
            if hasattr(content, 'items'):  # it is a dict
                #  There is only one element, create a singleton list
                elements = [content]
            else:
                # expect it to be a list of dictionaries
                elements = content

            for element in elements:
                attributes = dict()
                other_attributes = []
                # this is for the multiple-entity membership hack to come
                membership_extra_members = None
                for attr_name, values in element.items():
                    attr = (PROV_ATTRIBUTES_ID_MAP[attr_name]
                            if attr_name in PROV_ATTRIBUTES_ID_MAP else
                            valid_qualified_name(bundle, attr_name))
                    if attr in PROV_ATTRIBUTES:
                        if isinstance(values, list):
                            # only one value is allowed
                            if len(values) > 1:
                                # unless it is the membership hack
                                if rec_type == PROV_MEMBERSHIP and \
                                   attr == PROV_ATTR_ENTITY:
                                    # This is a membership relation with
                                    # multiple entities
                                    # HACK: create multiple membership
                                    # relations, one for each entity

                                    # Store all the extra entities
                                    membership_extra_members = values[1:]
                                    # Create the first membership relation as
                                    # normal for the first entity
                                    value = values[0]
                                else:
                                    error_msg = (
                                        'The prov package does not support PROV'
                                        ' attributes having multiple values.')
                                    logger.error(error_msg)
                                    raise ProvJSONException(error_msg)
                            else:
                                value = values[0]
                        else:
                            value = values
                        value = (valid_qualified_name(bundle, value)
                                 if attr in PROV_ATTRIBUTE_QNAMES else
                                 parse_xsd_datetime(value))
                        attributes[attr] = value
                    else:
                        if isinstance(values, list):
                            other_attributes.extend(
                                (attr,
                                 decode_json_representation(value, bundle))
                                for value in values)
                        else:
                            # single value
                            other_attributes.append(
                                (attr,
                                 decode_json_representation(values, bundle)))
                bundle.new_record(rec_type, rec_id, attributes,
                                  other_attributes)
                # HACK: creating extra (unidentified) membership relations
                if membership_extra_members:
                    collection = attributes[PROV_ATTR_COLLECTION]
                    for member in membership_extra_members:
                        bundle.membership(collection,
                                          valid_qualified_name(bundle, member))
Beispiel #6
0
def decode_json_container(jc, bundle):
    if 'prefix' in jc:
        prefixes = jc['prefix']
        for prefix, uri in prefixes.items():
            if prefix != 'default':
                bundle.add_namespace(Namespace(prefix, uri))
            else:
                bundle.set_default_namespace(uri)
        del jc['prefix']

    for rec_type_str in jc:
        rec_type = PROV_RECORD_IDS_MAP[rec_type_str]
        for rec_id, content in jc[rec_type_str].items():
            if hasattr(content, 'items'):  # it is a dict
                #  There is only one element, create a singleton list
                elements = [content]
            else:
                # expect it to be a list of dictionaries
                elements = content

            for element in elements:
                attributes = dict()
                other_attributes = []
                # this is for the multiple-entity membership hack to come
                membership_extra_members = None
                for attr_name, values in element.items():
                    attr = (
                        PROV_ATTRIBUTES_ID_MAP[attr_name]
                        if attr_name in PROV_ATTRIBUTES_ID_MAP
                        else valid_qualified_name(bundle, attr_name)
                    )
                    if attr in PROV_ATTRIBUTES:
                        if isinstance(values, list):
                            # only one value is allowed
                            if len(values) > 1:
                                # unless it is the membership hack
                                if rec_type == PROV_MEMBERSHIP and \
                                   attr == PROV_ATTR_ENTITY:
                                    # This is a membership relation with
                                    # multiple entities
                                    # HACK: create multiple membership
                                    # relations, one for each entity

                                    # Store all the extra entities
                                    membership_extra_members = values[1:]
                                    # Create the first membership relation as
                                    # normal for the first entity
                                    value = values[0]
                                else:
                                    error_msg = (
                                        'The prov package does not support PROV'
                                        ' attributes having multiple values.'
                                    )
                                    logger.error(error_msg)
                                    raise ProvJSONException(error_msg)
                            else:
                                value = values[0]
                        else:
                            value = values
                        value = (
                            valid_qualified_name(bundle, value)
                            if attr in PROV_ATTRIBUTE_QNAMES
                            else parse_xsd_datetime(value)
                        )
                        attributes[attr] = value
                    else:
                        if isinstance(values, list):
                            other_attributes.extend(
                                (
                                    attr,
                                    decode_json_representation(value, bundle)
                                )
                                for value in values
                            )
                        else:
                            # single value
                            other_attributes.append(
                                (
                                    attr,
                                    decode_json_representation(values, bundle)
                                )
                            )
                bundle.new_record(
                    rec_type, rec_id, attributes, other_attributes
                )
                # HACK: creating extra (unidentified) membership relations
                if membership_extra_members:
                    collection = attributes[PROV_ATTR_COLLECTION]
                    for member in membership_extra_members:
                        bundle.membership(
                            collection, valid_qualified_name(bundle, member)
                        )
def create_prov_record(bundle, prov_type, prov_id, properties, type_map):
    """

    :param bundle:
    :param prov_type: valid prov type like prov:Entry as string
    :param prov_id: valid id as string like <namespace>:<name>
    :param properties: dict{attr_name:attr_value} dict with all properties (prov and additional)
    :param type_map: dict{attr_name:type_str} Contains the type information for each property (only if type is necessary)
    :return: ProvRecord
    """
    # Parse attributes
    if isinstance(properties, dict):
        properties_list = properties.items()
    elif isinstance(properties, list):
        properties_list = properties
    else:
        raise SerializerException(
            "Please provide properties as list[(key,value)] or dict your provided: {}".format(
                properties.__class__.__name__))

    attributes = dict()
    other_attributes = []
    # this is for the multiple-entity membership hack to come
    membership_extra_members = None
    for attr_name, values in properties_list:

        attr = (
            PROV_ATTRIBUTES_ID_MAP[attr_name]
            if attr_name in PROV_ATTRIBUTES_ID_MAP
            else bundle.valid_qualified_name(attr_name)
        )
        if attr in PROV_ATTRIBUTES:
            if isinstance(values, list):
                # only one value is allowed
                if len(values) > 1:
                    # unless it is the membership hack
                    if prov_type == PROV_MEMBERSHIP and \
                                    attr == PROV_ATTR_ENTITY:
                        # This is a membership relation with
                        # multiple entities
                        # HACK: create multiple membership
                        # relations, one x each entity

                        # Store all the extra entities
                        membership_extra_members = values[1:]
                        # Create the first membership relation as
                        # normal for the first entity
                        value = values[0]
                    else:
                        error_msg = (
                            'The prov package does not support PROV'
                            ' attributes having multiple values.'
                        )
                        logger.error(error_msg)
                        raise SerializerException(error_msg)
                else:
                    value = values[0]
            else:
                value = values
            value = (
                bundle.valid_qualified_name(value)
                if attr in PROV_ATTRIBUTE_QNAMES
                else parse_xsd_datetime(value)
            )
            attributes[attr] = value
        else:
            value_type = None
            if type_map:
                value_type = type_map.get(attr_name)

            if isinstance(values, list):
                other_attributes.extend(
                    (
                        attr,
                        decode_json_representation(value, value_type, bundle)
                    )
                    for value in values
                )
            else:
                # single value
                other_attributes.append(
                    (
                        attr,
                        decode_json_representation(values, value_type, bundle)
                    )
                )
    record = bundle.new_record(
        prov_type, prov_id, attributes, other_attributes
    )
    # HACK: creating extra (unidentified) membership relations
    if membership_extra_members:
        collection = attributes[PROV_ATTR_COLLECTION]
        for member in membership_extra_members:
            bundle.membership(
                collection, bundle.valid_qualified_name(member)
            )
    return record