<commit_before>"""For running command line executables with a timeout"""

import subprocess
import threading
import salt.exceptions


class TimedProc(object):
    '''
    Create a TimedProc object, calls subprocess.Popen with passed args and **kwargs
    '''
    def __init__(self, args, **kwargs):

        self.command = args
        self.stdin = kwargs.pop('stdin', None)
        if self.stdin is not None:
            # Translate a newline submitted as '\n' on the CLI to an actual
            # newline character.
            self.stdin = self.stdin.replace('\\n', '\n')
        self.with_communicate = True
        if 'with_communicate' in kwargs:
            self.with_communicate = kwargs['with_communicate']
            del kwargs['with_communicate']

        self.process = subprocess.Popen(args, **kwargs)

    def wait(self, timeout=None):
        '''
        wait for subprocess to terminate and return subprocess' return code.
        If timeout is reached, throw TimedProcTimeoutError
        '''
        def receive():
            if self.with_communicate:
                (self.stdout, self.stderr) = self.process.communicate(input=self.stdin)
            else:
                self.process.wait()
                (self.stdout, self.stderr) = (None, None)

        if timeout:
            if not isinstance(timeout, (int, float)):
                raise salt.exceptions.TimedProcTimeoutError('Error: timeout must be a number')
            rt = threading.Thread(target=receive)
            rt.start()
            rt.join(timeout)
            if rt.isAlive():
                # Subprocess cleanup (best effort)
                self.process.kill()

                def terminate():
                    if rt.isAlive():
                        self.process.terminate()
                threading.Timer(10, terminate).start()
                raise salt.exceptions.TimedProcTimeoutError('%s : Timed out after %s seconds' % (
                    self.command,
                    str(timeout),
                ))
        else:
            receive()
        return self.process.returncode
<commit_msg>Collect with_communicate value in consistent and concise way<commit_after>"""For running command line executables with a timeout"""

import subprocess
import threading
import salt.exceptions


class TimedProc(object):
    '''
    Create a TimedProc object, calls subprocess.Popen with passed args and **kwargs
    '''
    def __init__(self, args, **kwargs):

        self.command = args
        self.stdin = kwargs.pop('stdin', None)
        if self.stdin is not None:
            # Translate a newline submitted as '\n' on the CLI to an actual
            # newline character.
            self.stdin = self.stdin.replace('\\n', '\n')
        self.with_communicate = kwargs.pop('with_communicate', True)

        self.process = subprocess.Popen(args, **kwargs)

    def wait(self, timeout=None):
        '''
        wait for subprocess to terminate and return subprocess' return code.
        If timeout is reached, throw TimedProcTimeoutError
        '''
        def receive():
            if self.with_communicate:
                (self.stdout, self.stderr) = self.process.communicate(input=self.stdin)
            else:
                self.process.wait()
                (self.stdout, self.stderr) = (None, None)

        if timeout:
            if not isinstance(timeout, (int, float)):
                raise salt.exceptions.TimedProcTimeoutError('Error: timeout must be a number')
            rt = threading.Thread(target=receive)
            rt.start()
            rt.join(timeout)
            if rt.isAlive():
                # Subprocess cleanup (best effort)
                self.process.kill()

                def terminate():
                    if rt.isAlive():
                        self.process.terminate()
                threading.Timer(10, terminate).start()
                raise salt.exceptions.TimedProcTimeoutError('%s : Timed out after %s seconds' % (
                    self.command,
                    str(timeout),
                ))
        else:
            receive()
        return self.process.returncode

{'fraction_non_alphanumeric': 0.09811957569913211, 'fraction_numerical': 0.0009643201542912247, 'mean_word_length': 1.8071718538565629, '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': '1660672', 'n_tokens_mistral': 1104, 'n_tokens_neox': 1056, 'n_words': 296}