<commit_before>"""
#-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=#
  This file is part of the Smart Developer Hub Project:
    http://www.smartdeveloperhub.org

  Center for Open Middleware
        http://www.centeropenmiddleware.com/
#-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=#
  Copyright (C) 2015 Center for Open Middleware.
#-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=#
  Licensed under the Apache License, Version 2.0 (the "License");
  you may not use this file except in compliance with the License.
  You may obtain a copy of the License at 

            http://www.apache.org/licenses/LICENSE-2.0

  Unless required by applicable law or agreed to in writing, software
  distributed under the License is distributed on an "AS IS" BASIS,
  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  See the License for the specific language governing permissions and
  limitations under the License.
#-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=#
"""

from sdh.curator.actions import Action
import logging
from rdflib import RDF
from rdflib.namespace import Namespace

__author__ = '<NAME>'

log = logging.getLogger('sdh.curator.actions.enrichment')
CURATOR = Namespace('http://www.smartdeveloperhub.org/vocabulary/curator#')


class EnrichmentAction(Action):
    def run(self):
        log.info('Received a potential enrichment request')
        request_nodes = list(self._request_graph.subjects(RDF.type, CURATOR.EnrichmentRequest))
        if not (0 < len(request_nodes) < 2):
            raise SyntaxError('There is no enrichment request specified in the message')

        request_node = request_nodes.pop()
        target_resource_nodes = list(self._request_graph.objects(request_node, CURATOR.targetResource))
        if not (0 < len(target_resource_nodes) < 2):
            raise SyntaxError('Invalid number of target resources: {}'.format(len(target_resource_nodes)))

        target_resource = target_resource_nodes.pop()
        log.debug('Found a request to enrich {}!'.format(target_resource))
<commit_msg>Extracting delivery channel info from message<commit_after>"""
#-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=#
  This file is part of the Smart Developer Hub Project:
    http://www.smartdeveloperhub.org

  Center for Open Middleware
        http://www.centeropenmiddleware.com/
#-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=#
  Copyright (C) 2015 Center for Open Middleware.
#-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=#
  Licensed under the Apache License, Version 2.0 (the "License");
  you may not use this file except in compliance with the License.
  You may obtain a copy of the License at 

            http://www.apache.org/licenses/LICENSE-2.0

  Unless required by applicable law or agreed to in writing, software
  distributed under the License is distributed on an "AS IS" BASIS,
  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  See the License for the specific language governing permissions and
  limitations under the License.
#-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=#
"""

from sdh.curator.actions import Action
import logging
from rdflib import RDF
from rdflib.namespace import Namespace

__author__ = '<NAME>'

log = logging.getLogger('sdh.curator.actions.enrichment')
CURATOR = Namespace('http://www.smartdeveloperhub.org/vocabulary/curator#')
AMQP = Namespace('http://www.smartdeveloperhub.org/vocabulary/amqp#')

class EnrichmentAction(Action):
    def run(self):
        log.debug('Received a potential enrichment request')
        try:
            request_node = self._request_graph.subjects(RDF.type, CURATOR.EnrichmentRequest, card=1)
        except ValueError:
            raise SyntaxError('There is not an only enrichment request specified in the message')

        try:
            target_resource = self._request_graph.objects(request_node, CURATOR.targetResource, card=1)
        except ValueError as e:
            raise SyntaxError('Invalid number of target resources: {}'.format(e.message))
        log.debug('Found a request to enrich {}!'.format(target_resource))

        try:
            message_id = self._request_graph.objects(request_node, CURATOR.messageId, card=1)
        except ValueError as e:
            raise SyntaxError('Invalid number of message identifiers: {}'.format(e.message))
        log.debug('Message identified as {}'.format(message_id))

        try:
            submitted_on = self._request_graph.objects(request_node, CURATOR.submittedOn, card=1)
        except ValueError as e:
            raise SyntaxError('Invalid number of submission dates: {}'.format(e.message))
        log.debug('Message submitted on {}'.format(submitted_on))

        try:
            submitter_node = self._request_graph.objects(request_node, CURATOR.submittedBy, card=1)
            submitted_by = self._request_graph.objects(submitter_node, CURATOR.agentId, card=1)
        except ValueError as e:
            raise SyntaxError('Invalid number of submitters: {}'.format(e.message))
        log.debug('Message submitted by {}'.format(submitted_by))

        dh_res = self._request_graph.query("""SELECT ?ex ?q ?rk ?h ?p ?v WHERE {
                                              [] a curator:EnrichmentRequest;
                                                 curator:replyTo [
                                                    a curator:DeliveryChannel;
                                                    amqp:exchangeName ?ex;
                                                    amqp:queueName ?q;
                                                    amqp:routingKey ?rk;
                                                    amqp:broker [
                                                       a amqp:Broker;
                                                       amqp:host ?h;
                                                       amqp:port ?p;
                                                       amqp:virtualHost ?v
                                                    ]
                                                 ]
                                              } """)
        dh_res = list(dh_res)
        if len(dh_res) != 1:
            raise SyntaxError('Invalid delivery channel specification')

        (exchange_name, queue_name, routing_key, host, port, virtual_host) = dh_res.pop()
        log.debug('Delivery change to reply to: {}')

{'fraction_non_alphanumeric': 0.16659108087679517, 'fraction_numerical': 0.004081632653061225, 'mean_word_length': 2.0404411764705883, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 9, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 30, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7200501', 'n_tokens_mistral': 1847, 'n_tokens_neox': 1638, 'n_words': 467}