<commit_before>#!/usr/bin/env python
'''
@author <NAME> <LCampbell at ASAScience dot com>
@date Tue Feb 12 09:54:27 EST 2013
@file ion/processes/data/transforms/transform_prime.py
'''

from ion.core.process.transform import TransformDataProcess
from coverage_model import ParameterDictionary
from interface.services.dm.ipubsub_management_service import PubsubManagementServiceProcessClient


class TransformPrime(TransformDataProcess):
    binding=['output']
    '''
    Transforms which have an incoming stream and an outgoing stream.

    Parameters:
      process.stream_id      Outgoing stream identifier.
      process.exchange_point Route's exchange point.
      process.routing_key    Route's routing key.
      process.queue_name     Name of the queue to listen on.
      
    Either the stream_id or both the exchange_point and routing_key need to be provided.
    '''    
    def on_start(self):
        TransformDataProcess.on_start(self)
        self.pubsub_management = PubsubManagementServiceProcessClient(process=self)

    
    def _get_pdict(self, stream_id):
        stream_def = self.pubsub_management.read_stream_definition(stream_id=stream_id)
        pdict = stream_def.parameter_dictionary
        return pdict

    def recv_packet(self, msg, stream_route, stream_id):
        incoming_pdict_dump = self._get_pdict(stream_id)
        outgoing_pdict_dump = self._get_pdict(self.CFG.process.stream_id)
       
        incoming_pdict = ParameterDictionary.load(incoming_pdict_dump)
        outgoing_pdict = ParameterDictionary.load(outgoing_pdict_dump)

        print "Incoming: "
        for key in incoming_pdict.keys():
            print '\t', key

        print "Outgoing: "
        for key in outgoing_pdict.keys():
            print '\t', key
<commit_msg>first attempt generic transform<commit_after>#!/usr/bin/env python
'''
@author <NAME> <LCampbell at ASAScience dot com>
@date Tue Feb 12 09:54:27 EST 2013
@file ion/processes/data/transforms/transform_prime.py
'''

from ion.core.process.transform import TransformDataProcess
from coverage_model import ParameterDictionary
from interface.services.dm.ipubsub_management_service import PubsubManagementServiceProcessClient
from ion.services.dm.utility.granule.record_dictionary import RecordDictionaryTool
from coverage_model import QuantityType, get_value_class
from coverage_model.parameter_types import ParameterFunctionType

class TransformPrime(TransformDataProcess):
    binding=['output']
    '''
    Transforms which have an incoming stream and an outgoing stream.

    Parameters:
      process.stream_id      Outgoing stream identifier.
      process.exchange_point Route's exchange point.
      process.routing_key    Route's routing key.
      process.queue_name     Name of the queue to listen on.
      
    Either the stream_id or both the exchange_point and routing_key need to be provided.
    '''    
    def on_start(self):
        TransformDataProcess.on_start(self)
        self.pubsub_management = PubsubManagementServiceProcessClient(process=self)
    
    def recv_packet(self, msg, stream_route, stream_id):
        stream_def_in = self.pubsub_management.read_stream_definition(stream_id=stream_id)
        incoming_pdict_dump = stream_def_in.parameter_dictionary
        
        stream_def_out = self.pubsub_management.read_stream_definition(stream_id=self.CFG.process.stream_id)
        outgoing_pdict_dump = stream_def_out.parameter_dictionary
        
        incoming_pdict = ParameterDictionary.load(incoming_pdict_dump)
        outgoing_pdict = ParameterDictionary.load(outgoing_pdict_dump)

        print "Incoming: "
        for key in incoming_pdict.keys():
            print '\t', key

        print "Outgoing: "
        for key in outgoing_pdict.keys():
            print '\t', key
        
        #merge incoming and outgoing parameter dictionaries
        #loop through the record dictionary output stream def
        #get the parameter context for foreach pdict associated with the pdict in the available fields
        #get the domain for the value class
        #get the Parameter value by using the parameter context

        merged_pdict = dict(incoming_pdict.items(), outgoing_pdict.items())
        for key in merged_pdict.keys():
            print '\t', key
        #rdt = RecordDictionaryTool(stream_definition_id=stream_def_out.id)
        rdt_in = RecordDictionaryTool.load_from_granule(msg)
        for key,pdict in merged_pdict.iteritems():
            if isinstance(pdict, ParameterFunctionType):
                #apply transform
                pcontext = pdict.get_context(key)
                pv = get_value_class(pcontext, rdt_in.domain)
                rdt_in._rd[key] = pv[:]
            
            
            #elif key in rdt._available_fields and isinstance(pdict, QuantityType):
                
    #def execute_transform(self, granule):
        
{'fraction_non_alphanumeric': 0.06846515430206418, 'fraction_numerical': 0.004904966278356836, 'mean_word_length': 3.1651063829787236, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3947489', 'n_tokens_mistral': 1399, 'n_tokens_neox': 1332, 'n_words': 340}