Esempio n. 1
0
    def __call__(self, message):
        """Process the RPC Payload.

        :param Message message:
        :return:
        """
        try:
            processed_message = self.handle_message(message)
        except Exception as e:
            app_logger.error('Something went wrong: {0}'.format(e))
            properties = {'correlation_id': message.correlation_id}
            error_message = "Error Type: {0}, with message: {1}".format(
                e.__class__.__name__, e.message)
            message_data = json.loads(message.body)
            processed_message = response_message(message_data["provenance"],
                                                 status="error",
                                                 status_messsage=error_message)
            response = Message.create(
                message.channel,
                str(
                    json.dumps(processed_message,
                               indent=4,
                               separators=(',', ': '))), properties)
            response.publish(message.reply_to)
            message.reject(requeue=False)
        else:
            properties = {'correlation_id': message.correlation_id}
            response = Message.create(message.channel, processed_message,
                                      properties)
            response.publish(message.reply_to)
            message.ack()
Esempio n. 2
0
def create_jsonLD(graph_data, filter_frame):
    """Create JSON-LD output for the given subject."""
    graph = ConjunctiveGraph()
    graph.parse(data=graph_data, format="turtle")
    try:
        # pyld likes nquads, by default
        expanded = jsonld.from_rdf(graph.serialize(format="nquads"))
        framed = jsonld.frame(expanded, json.loads(filter_frame))
        result = json.dumps(framed, indent=1, sort_keys=True)
        app_logger.info('Serialized as JSON-LD compact with the frame.')
        return result
    except Exception as error:
        app_logger.error('JSON-LD frame failed with error: {0}'.format(error))
        return error
Esempio n. 3
0
 def _create_ld(self):
     """Create JSON-LD output for the given subject."""
     graph = self._merge_graphs()
     # result = None
     try:
         # pyld likes nquads, by default
         expanded = pyld_jsonld_from_rdflib_graph(graph)
         framed = jsonld.frame(expanded, json.loads(self.ld_frame))
         result = json.dumps(framed, indent=1, sort_keys=True)
         app_logger.info('Serialized as JSON-LD compact with the frame.')
     except Exception as error:
         app_logger.error(
             'JSON-LD frame failed with error: {0}'.format(error))
         raise
     finally:
         return result
Esempio n. 4
0
def results_path(content, extension):
    """Write results to specific file."""
    try:
        path = "{0}/graphframing/{1}".format(data["directory"],
                                             uuid.uuid4().hex)
        if not os.path.exists(path):
            os.makedirs(path)
        full_path = "{0}/{1}.{2}".format(path, uuid.uuid1().hex, extension)
        f = open(full_path, "a+")
        f.write(content)
        app_logger.info('Content available in path: {0}'.format(full_path))
        return "file://{0}".format(full_path)
    except Exception as error:
        app_logger.error('Something is wrong: {0}'.format(error))
        raise
    finally:
        f.close()
Esempio n. 5
0
    def start_server(self):
        """Start the RPC Server.

        :return:
        """
        self._stopped.clear()
        if not self._connection or self._connection.is_closed:
            self._create_connection()
        while not self._stopped.is_set():
            try:
                # Check our connection for errors.
                self._connection.check_for_errors()
                self._update_consumers()
            except amqpstorm.AMQPError as why:
                # If an error occurs, re-connect and let update_consumers
                # re-open the channels.
                app_logger.error(why)
                self._stop_consumers()
                self._create_connection()
            time.sleep(1)
Esempio n. 6
0
def ld_message(message_data):
    """Replace an old index with a new index for a given alias list."""
    startTime = datetime.now().strftime('%Y-%m-%dT%H:%M:%S')
    ld_frame = message_data["payload"]["framingServiceInput"]["ldFrame"]
    doc_type = None
    if message_data["payload"]["framingServiceInput"].get('docType'):
        doc_type = message_data["payload"]["framingServiceInput"]["docType"]
    source_data = message_data["payload"]["framingServiceInput"]["sourceData"]
    PUBLISHER = Publisher(broker['host'], broker['user'], broker['pass'],
                          broker['provqueue'])
    frame = Frame(ld_frame, source_data, doc_type)
    try:
        output_data = frame._bulk_data()
        output_uri = results_path(output_data, "json")
        endTime = datetime.now().strftime('%Y-%m-%dT%H:%M:%S')
        if bool(message_data["provenance"]):
            PUBLISHER.push(
                prov_message(message_data, "success", startTime, endTime,
                             output_uri))
        output_obj = {
            "contentType": "elasticbulkfile",
            "outputType": "URI",
            "output": output_uri
        }
        app_logger.info(
            'Generated an JSON-LD frame output at: {0}.'.format(output_uri))
        return json.dumps(response_message(message_data["provenance"],
                                           status="success",
                                           output=output_obj),
                          indent=4,
                          separators=(',', ': '))
    except Exception as error:
        endTime = datetime.now().strftime('%Y-%m-%dT%H:%M:%S')
        PUBLISHER.push(
            prov_message(message_data, "error", startTime, endTime,
                         output_uri))
        app_logger.error('Something is wrong: {0}'.format(error))
        raise
Esempio n. 7
0
    def _create_connection(self):
        """Create a connection.

        :return:
        """
        attempts = 0
        while True:
            attempts += 1
            if self._stopped.is_set():
                break
            try:
                self._connection = Connection(self.hostname, self.username,
                                              self.password)
                app_logger.info(
                    'Established connection with AMQP server {0}'.format(
                        self._connection))
                break
            except amqpstorm.AMQPError as why:
                app_logger.error(why)
                if self.max_retries and attempts > self.max_retries:
                    raise Exception('max number of retries reached')
                time.sleep(min(attempts * 2, 30))
            except KeyboardInterrupt:
                break
Esempio n. 8
0
 def _merge_graphs(self):
     """Merge graphs received for framing."""
     graph = ConjunctiveGraph()
     try:
         for key, unit in enumerate(self.source_data):
             if "contentType" in unit:
                 content_type = unit["contentType"]
             else:
                 content_type = "turtle"
             if "inputType" in unit and unit["inputType"] == "Graph":
                 request_url = 'http://{0}:{1}/{2}/graph?uri={3}'.format(
                     gm['host'], gm['port'], gm['api'], unit["input"])
                 graph.parse(request_url, format=content_type)
             elif unit["inputType"] == "URI":
                 graph.parse(unit["input"], format=content_type)
             elif unit["inputType"] == "Data":
                 graph.parse(data=unit["input"], format=content_type)
             else:
                 raise IOError("Cannot read input source data.")
     except Exception as error:
         app_logger.error('Merging graphs failed with: {0}'.format(error))
         raise
     finally:
         return graph