<commit_before>############################################################
#
#    Copyright (c) 2008, Southpaw Technology
#                        All Rights Reserved
#
#    PROPRIETARY INFORMATION.  This software is proprietary to
#    Southpaw Technology, and is not to be reproduced, transmitted,
#    or disclosed in any way without written permission.
#
#

__all__ = ['SubprocessTrigger', 'QueueTrigger', 'ScriptTrigger']

import tacticenv

import sys
import subprocess
from subprocess import Popen

from pyasm.common import Config, Common, jsonloads, jsondumps
from pyasm.security import Site
from tactic_client_lib import TacticServerStub
from tactic_client_lib.interpreter import Handler

from trigger import Trigger
from command import Command




class SubprocessTrigger(Handler):
    '''Utility class that calls a trigger by external process'''
    def __init__(self):
        self.mode = "same process,new transaction"
        self.info = {}
        super(SubprocessTrigger,self).__init__()

    def set_data(self, data):
        self.data = data
        self.class_name = data.get("class_name")

        # Since the trigger will run separate somewhere else, we do not
        # know if the the workflow should stop of not.  The trigger
        # can define a result in the data
        kwargs = data.get("kwargs")
        if kwargs and kwargs.get("result"):
            self.info['result'] = kwargs.get("result")




    def get_info(self):
        return self.info


    def get_class_name(self):
        return self.class_name

    def set_mode(self, mode):
        self.mode = mode

    def execute(self):
      
        input_data = self.get_input_data()
        data = self.data


        # add site info to the data object
        site = Site.get_site()
        if site and not data.get("site"):
            data['site'] = site

        result = True


        # input data for the handler
        if self.mode == 'separate process,blocking':
            input_data_str = jsondumps(input_data)
            data_str = jsondumps(data)

            file = __file__
            py_exec = Config.get_value("services", "python")
            if not py_exec:
                py_exec = "python"


            retcode = subprocess.call([py_exec, file, data_str, input_data_str])



        elif self.mode == 'separate process,non-blocking':
            input_data_str = jsondumps(input_data)
            data_str = jsondumps(data)

            file = __file__
            py_exec = Config.get_value("services", "python")
            if not py_exec:
                py_exec = "python"

            retcode = subprocess.Popen([py_exec, file, data_str, input_data_str])

            result = "wait"

        elif self.mode == 'separate process,queued':

            kwargs = data.get("kwargs")
            priority = kwargs.get("priority") or 99999
            description = kwargs.get("description") or "Trigger"
            queue_type = kwargs.get("trigger") or "trigger"

            class_name = "pyasm.command.QueueTrigger"
            kwargs = {
                'input_data': input_data,
                'data': data,
            }

            from tactic.command import Queue
            queue_item = Queue.add(class_name, kwargs, queue_type=queue_type, priority=priority, description=description)

            result = "wait"


        elif self.mode == 'same process,new transaction':
            # run it inline
            trigger = ScriptTrigger()
            trigger.set_data(data)
            trigger.set_input(input_data)
            trigger.execute()


        # if it was not overridden by the trigger, the return default for the mode
        if not self.info.has_key("result"):
            self.info['result'] = result


 <commit_msg>Update subprocess_trigger.py<commit_after>############################################################
#
#    Copyright (c) 2008, Southpaw Technology
#                        All Rights Reserved
#
#    PROPRIETARY INFORMATION.  This software is proprietary to
#    Southpaw Technology, and is not to be reproduced, transmitted,
#    or disclosed in any way without written permission.
#
#

__all__ = ['SubprocessTrigger', 'QueueTrigger', 'ScriptTrigger']

import tacticenv

import sys
import subprocess
from subprocess import Popen

from pyasm.common import Config, Common, jsonloads, jsondumps
from pyasm.security import Site
from tactic_client_lib import TacticServerStub
from tactic_client_lib.interpreter import Handler

from trigger import Trigger
from command import Command




class SubprocessTrigger(Handler):
    '''Utility class that calls a trigger by external process'''
    def __init__(self):
        self.mode = "same process,new transaction"
        self.info = {}
        super(SubprocessTrigger,self).__init__()

    def set_data(self, data):
        self.data = data
        self.class_name = data.get("class_name")

        # Since the trigger will run separate somewhere else, we do not
        # know if the the workflow should stop of not.  The trigger
        # can define a result in the data
        kwargs = data.get("kwargs")
        if kwargs and kwargs.get("result"):
            self.info['result'] = kwargs.get("result")




    def get_info(self):
        return self.info


    def get_class_name(self):
        return self.class_name

    def set_mode(self, mode):
        self.mode = mode

    def execute(self):
      
        input_data = self.get_input_data()
        data = self.data


        # add site info to the data object
        site = Site.get_site()
        if site and not data.get("site"):
            data['site'] = site

        result = True


        # input data for the handler
        if self.mode == 'separate process,blocking':
            input_data_str = jsondumps(input_data)
            data_str = jsondumps(data)

            file = __file__
            py_exec = Config.get_value("services", "python")
            if not py_exec:
                py_exec = "python"


            retcode = subprocess.call([py_exec, file, data_str, input_data_str])



        elif self.mode == 'separate process,non-blocking':
            input_data_str = jsondumps(input_data)
            data_str = jsondumps(data)

            file = __file__
            py_exec = Config.get_value("services", "python")
            if not py_exec:
                py_exec = "python"

            retcode = subprocess.Popen([py_exec, file, data_str, input_data_str])

            result = "wait"

        elif self.mode == 'separate process,queued':

            kwargs = data.get("kwargs")
            priority = kwargs.get("priority") or 99999
            description = kwargs.get("description") or self.description or "Trigger"
            queue_type = kwargs.get("trigger") or "trigger"

            class_name = "pyasm.command.QueueTrigger"
            kwargs = {
                'input_data': input_data,
                'data': data,
            }

            from tactic.command import Queue
            queue_item = Queue.add(class_name, kwargs, queue_type=queue_type, priority=priority, description=description)

            result = "wait"


        elif self.mode == 'same process,new transaction':
            # run it inline
            trigger = ScriptTrigger()
            trigger.set_data(data)
            trigger.set_input(input_data)
            trigger.execute()


        # if it was not overridden by the trigger, the return default for the mode
        if not self.info.has_key("result"):
            self.info['result'] = result


 
{'fraction_non_alphanumeric': 0.10165453342157511, 'fraction_numerical': 0.002382528127068167, 'mean_word_length': 2.2429184549356225, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, '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': '1997104', 'n_tokens_mistral': 2104, 'n_tokens_neox': 1998, 'n_words': 595}