def serialize_prop(self, prop, value):
        """Serializes `Relation` and `OrderedRelation` references as a prefix.

        For all other properties the super method is called as a fallback.
        """

        if type(prop) == NestedProperty:
            for _, item in iterate(value):
                return self.serialize(item)

        serialized = super(JsonSerializer, self).serialize_prop(prop, value)

        if type(prop) == Relation or type(prop) == OrderedRelation:
            props = list()
            for _, item in iterate(value):
                from .model import Relationship
                if isinstance(item, Relationship):
                    item = item.model

                props.append(self.ori_uri(item))

            if len(props) == 1:
                return props[0]
            return props

        return serialized
Beispiel #2
0
        def inner(model):
            # Prevent circular recursion
            if model in rels_list:
                return

            rels_list.append(model)

            for _, prop in iterate(model.values.items()):
                if isinstance(prop, Model) or isinstance(prop, Relationship):
                    inner(prop)
Beispiel #3
0
        def inner(model):
            identifier = model.source_iri

            # Prevent circular recursion
            if identifier in rels.keys():
                return

            rels[identifier] = model

            for _, prop in iterate(model.values.items()):
                if isinstance(prop, Model) or isinstance(prop, Relationship):
                    inner(prop)
    def start(self, *args, **kwargs):
        """Start loading of a single item.

        This method is called by the transformer and expects args to
        contain the output of the transformer as a tuple.
        Kwargs should contain the ``source_definition`` dict.

        :returns: the output of :py:meth:`~BaseTransformer.transform_item`
        """
        self.source_definition = kwargs['source_definition']

        for _, item in iterate(args):
            self.post_processing(item)
            self.load_item(item)
    def run(self, *args, **kwargs):
        """Start loading of a single item.

        This method is called by the transformer and expects args to
        contain the output of the transformer as a tuple.
        Kwargs should contain the ``source_definition`` dict.

        :returns: the output of :py:meth:`~BaseTransformer.transform_item`
        """
        self.source_definition = kwargs['source_definition']

        for _, item in iterate(args):
            self.post_processing(item)
            self.load_item(item)
    def properties(self, props=True, rels=True, parent=False):
        """ Returns namespaced properties with their inflated values """
        props_list = list()
        for name, prop in iterate({k: v for k, v in self.values.items() if k[0:1] != '_'}):
            if not parent and name == 'parent':
                continue

            definition = self.definition(name)
            if not definition:
                continue

            if (props and not isinstance(prop, Model)) or \
                    (rels and (isinstance(prop, Model) or isinstance(prop, Relationship))):
                props_list.append((self.serializer.uri_format(definition), prop,))  # pylint: disable=no-member
        return props_list
Beispiel #7
0
 def properties(self, props=True, rels=True):
     """ Returns namespaced properties with their inflated values """
     props_list = list()
     for name, prop in iterate(
         {k: v
          for k, v in self.values.items() if k[0:1] != '_'}):
         definition = self.definition(name)
         if not definition:
             continue
         if (props and not isinstance(prop, Model)) or \
                 (rels and (isinstance(prop, Model) or isinstance(prop, Relationship))):
             props_list.append((
                 self.serializer.uri_format(definition),
                 prop,
             ))  # pylint: disable=no-member
     return props_list
    def serialize_prop(self, prop, value):
        """Serializes `Relation` and `OrderedRelation` as a short identifier when they are Relationships.
        For Individuals, the compact or absolute IRI of the label is returned.

        For all other properties the super method is called as a fallback.
        """
        if type(prop) == Relation or type(prop) == OrderedRelation:
            props = list()
            for _, item in iterate(value):
                from .model import Relationship
                if isinstance(item, Relationship):
                    item = item.model

                props.append(self.ori_uri(item))

            if len(props) == 1:
                return props[0]
            return props

        return super(PostgresSerializer, self).serialize_prop(prop, value)
Beispiel #9
0
    def start(self, *args, **kwargs):
        """Start enrichment of a single item.

        This method is called by the transformer or by another enricher
        and expects args to contain a transformed (and possibly enriched)
        item. Kwargs should contain the ``source_definition`` dict.

        :returns: the output of :py:meth:`~BaseEnricher.enrich_item`
        """

        self.source_definition = kwargs['source_definition']
        self.enricher_settings = kwargs['enricher_settings']

        for _, doc in iterate(args):
            for model in doc.traverse():
                try:
                    if not hasattr(model, 'enricher_task'):
                        continue
                except AttributeError:
                    continue

                try:
                    self.enrich_item(model)
                except SkipEnrichment as e:
                    bugsnag.notify(e, severity="info")
                    log.info('Skipping %s, reason: %s' %
                             (self.__class__.__name__, e.message))
                except IOError as e:
                    # In the case of an IOError, disk space or some other
                    # serious problem might occur.
                    bugsnag.notify(e, severity="error")
                    log.critical(e)
                    raise
                except Exception as e:
                    bugsnag.notify(e, severity="warning")
                    log.warning('Unexpected error: %s, reason: %s' %
                                (self.__class__.__name__, e))
                    raise

        return args
    def serialize_prop(self, prop, value):
        """Calls the super method and applies rdflib specific logic on it.

        Most properties will be returned as a rdflib `Literal`. Relations will be
        iterated and returned as `URIRef`.
        """
        serialized = super(RdfSerializer, self).serialize_prop(prop, value)

        if type(prop) == StringProperty:
            return Literal(serialized, datatype=XSD.string)

        elif type(prop) == IntegerProperty:
            return Literal(serialized, datatype=XSD.integer)

        elif type(prop) == DateTimeProperty:
            return Literal(serialized, datatype=XSD.dateTime)

        elif type(prop) == ArrayProperty:
            return Literal(serialized)  # todo tests?

        elif type(prop) == Relation or type(prop) == OrderedRelation:
            props = list()
            o = BNode()
            for _, item in iterate(value):
                from .model import Relationship
                if isinstance(item, Relationship):
                    item = item.model
                self.deflate(item, props=True, rels=True)
                props.append(URIRef('{}{}'.format(Ori.uri,
                                                  item.get_ori_identifier())))

            if type(prop) == OrderedRelation:
                Collection(self.g, o, props)
                return o
            return props
        else:
            raise SerializerError(
                "Cannot serialize the property of type '{}'".format(type(prop))
            )
    def run(self, *args, **kwargs):
        """Start enrichment of a single item.

        This method is called by the transformer or by another enricher
        and expects args to contain a transformed (and possibly enriched)
        item. Kwargs should contain the ``source_definition`` dict.

        :returns: the output of :py:meth:`~BaseEnricher.enrich_item`
        """

        self.source_definition = kwargs['source_definition']
        self.enricher_settings = kwargs['enricher_settings']

        for _, doc in iterate(args):
            try:
                for prop, value in doc.properties(props=True, rels=True):
                    try:
                        if not hasattr(value, 'enricher_task'):
                            continue
                    except AttributeError:
                        continue

                    self.enrich_item(value)

            except SkipEnrichment as e:
                bugsnag.notify(e, severity="info")
                log.info('Skipping %s, reason: %s'
                         % (self.__class__.__name__, e.message))
            except IOError as e:
                # In the case of an IOError, disk space or some other
                # serious problem might occur.
                bugsnag.notify(e, severity="error")
                log.critical(e)
            except Exception, e:
                bugsnag.notify(e, severity="warning")
                log.warning('Unexpected error: %s, reason: %s'
                         % (self.__class__.__name__, e))