<commit_before>import subprocess
from browserstack.local_binary import LocalBinary
from browserstack.bserrors import BrowserStackLocalError

class Local:
    def __init__(self, key, binary_path=None):
        if binary_path is None:
            self.binary_path = LocalBinary().get_binary()
        else:
            self.binary_path = binary_path
        self.options = {
            'key': key
        }
        self.local_folder_path = None

    def __xstr(self, obj):
        if obj is None:
            return ''
        return str(obj)

    def _generate_cmd(self):
        options_order = ['folder_flag', 'key', 'folder_path', 'forcelocal', 'local_identifier', 'only', 'only_automate', 'proxy_host', 'proxy_port', 'proxy_user', 'proxy_pass', 'force', 'verbose', 'hosts']
        cmd = [self.__xstr(self.options.get(o)) for o in options_order if self.options.get(o) is not None]
        return [self.binary_path] + cmd

    def start(self, **kwargs):
        for key, value in kwargs.iteritems():
            self.__add_arg(key, value)
        self.proc = subprocess.Popen(self._generate_cmd(), stdout=subprocess.PIPE)
        self.stdout = self.proc.stdout
        self.stderr = self.proc.stderr
        while True:
            line = self.proc.stdout.readline()
            if 'Error:' in line.strip():
                raise BrowserStackLocalError(line)
            elif line.strip() == 'Press Ctrl-C to exit':
                break

    def is_running(self):
        if (hasattr(self, 'proc')):
            return True if self.proc.poll() is None else False
        return False

    def __add_arg(self, key, value):
        if key == 'verbose' and value:
            self.options['verbose'] = '-v'
        elif key == 'force' and value:
            self.options['force'] = '-force'
        elif key == 'only' and value:
            self.options['only'] = '-only'
        elif key == 'only_automate' and value:
            self.options['only_automate'] = '-onlyAutomate'
        elif key == 'forcelocal' and value:
            self.options['forcelocal'] = '-forcelocal'
        elif key == 'local_identifier':
            self.options['local_identifier'] = '-localIdentifier ' + str(value)
        elif key == 'f':
            self.options['folder_flag'] = '-f'
            self.options['folder_path'] = str(value)
        elif key == 'proxy_host':
            self.options['proxy_host'] = '-proxyHost ' + str(value)
        elif key == 'proxy_port':
            self.options['proxy_port'] = '-proxyPort ' + str(value)
        elif key == 'proxy_user':
            self.options['proxy_user'] = '-proxyUser ' + str(value)
        elif key == 'proxy_pass':
            self.options['proxy_pass'] = '-proxyPass ' + str(value)
        elif key == 'hosts':
            self.options['hosts'] = str(value)
        else:
            raise BrowserStackLocalError('Attempted to pass invalid option to binary')

    def stop(self):
        if self.is_running():
            self.proc.terminate()
<commit_msg>key optional as per specs<commit_after>import subprocess
from browserstack.local_binary import LocalBinary
from browserstack.bserrors import BrowserStackLocalError

class Local:
    def __init__(self, key=None, binary_path=None):
        if binary_path is None:
            self.binary_path = LocalBinary().get_binary()
        else:
            self.binary_path = binary_path
        self.options = {
            'key': key
        }
        self.local_folder_path = None

    def __xstr(self, obj):
        if obj is None:
            return ''
        return str(obj)

    def _generate_cmd(self):
        options_order = ['folder_flag', 'key', 'folder_path', 'forcelocal', 'local_identifier', 'only', 'only_automate', 'proxy_host', 'proxy_port', 'proxy_user', 'proxy_pass', 'force', 'verbose', 'hosts']
        cmd = [self.__xstr(self.options.get(o)) for o in options_order if self.options.get(o) is not None]
        return [self.binary_path] + cmd

    def start(self, **kwargs):
        for key, value in kwargs.iteritems():
            self.__add_arg(key, value)
        self.proc = subprocess.Popen(self._generate_cmd(), stdout=subprocess.PIPE)
        self.stdout = self.proc.stdout
        self.stderr = self.proc.stderr
        while True:
            line = self.proc.stdout.readline()
            if 'Error:' in line.strip():
                raise BrowserStackLocalError(line)
            elif line.strip() == 'Press Ctrl-C to exit':
                break

    def is_running(self):
        if (hasattr(self, 'proc')):
            return True if self.proc.poll() is None else False
        return False

    def __add_arg(self, key, value):
        if key == 'verbose' and value:
            self.options['verbose'] = '-v'
        elif key == 'force' and value:
            self.options['force'] = '-force'
        elif key == 'only' and value:
            self.options['only'] = '-only'
        elif key == 'only_automate' and value:
            self.options['only_automate'] = '-onlyAutomate'
        elif key == 'forcelocal' and value:
            self.options['forcelocal'] = '-forcelocal'
        elif key == 'local_identifier':
            self.options['local_identifier'] = '-localIdentifier ' + str(value)
        elif key == 'f':
            self.options['folder_flag'] = '-f'
            self.options['folder_path'] = str(value)
        elif key == 'proxy_host':
            self.options['proxy_host'] = '-proxyHost ' + str(value)
        elif key == 'proxy_port':
            self.options['proxy_port'] = '-proxyPort ' + str(value)
        elif key == 'proxy_user':
            self.options['proxy_user'] = '-proxyUser ' + str(value)
        elif key == 'proxy_pass':
            self.options['proxy_pass'] = '-proxyPass ' + str(value)
        elif key == 'hosts':
            self.options['hosts'] = str(value)
        else:
            raise BrowserStackLocalError('Attempted to pass invalid option to binary')

    def stop(self):
        if self.is_running():
            self.proc.terminate()

{'fraction_non_alphanumeric': 0.12987228396085587, 'fraction_numerical': 0.0, 'mean_word_length': 2.2401934443847393, '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': '313976', 'n_tokens_mistral': 1754, 'n_tokens_neox': 1720, 'n_words': 434}