<commit_before>import json, time, subprocess, os, logging

FIFO_PATH = '/tmp/sshtman_daemon_in'
CHECK_INTERVAL = 0.5

def command(function):
    def decorator(*args, **kwargs):
        stripped_kwargs = _strip_empty_values(kwargs)
        stripped_args = [a for a in args if a is not None]
        return function(*stripped_args, **stripped_kwargs)
    return decorator

def value_or_error(value, message=None):
    if value is None:
        raise ValueError(message)
    return value

def _strip_empty_values(dictionary):
    return dict(((k, v) for k, v in dictionary.items() if v is not None))

def _create_fifo(path):
    if os.path.exists(path):
        os.unlink(path)
    os.mkfifo(path)

def create_tunnel(**kwargs):
    return Tunnel(**kwargs)


class Client:

    def __init__(self, path):
        self.path = path
        self._actions = {
            'kill': self.kill_daemon,
            'open': self.open_tunnel,
            'close': self.close_tunnel,
        }

    def perform_action(self, action, name, **kwargs):
        self._actions[action](name, **kwargs)

    @command
    def open_tunnel(self, name, **kwargs):
        self.send({
            'name': 'topen',
            'args': {
                'name': name,
                'kwargs': kwargs,
            },
        })

    @command
    def debug(self):
        self.send({
            'name': 'debug',
            'args': {'debug': True},
        })

    @command
    def kill_daemon(self):
        self.send({
            'name': 'die',
        })

    @command
    def close_tunnel(self, name):
        self.send({
            'name': 'tclose',
            'args': {
                'name': name,
            },
        })

    def send(self, cmd):
        with open(self.path, 'w') as fifo:
            cmd_text = json.dumps(cmd)
            print('Sending ' + cmd_text)
            fifo.write(cmd_text + '\n')


class Daemon:

    def __init__(self):
        self._manager = TunnelManager(logging)
        commands = {
            'debug': self.debug,
            'die': self.die,
            'topen': self.topen,
            'tclose': self.tclose,
        }
        self._listener = Listener(commands)
        self._pipe = None

    def run(self):
        with open(FIFO_PATH, 'r') as fifo:
            self._pipe = Pipe(fifo)
            self._listener.listen(self._pipe)

    @command
    def debug(self, args):
        print('Debug command called with\n' + str(args))

    @command
    def die(self):
        self._pipe.close()

    @command
    def topen(self, args):
        self._manager.open(args['name'], **args['kwargs'])

    @command
    def tclose(self, args):
        self._manager.close(args['name'])<commit_msg>daemon creates fifo on run()<commit_after>import json, time, subprocess, os, logging

FIFO_PATH = '/tmp/sshtman_daemon_in'
CHECK_INTERVAL = 0.5

def command(function):
    def decorator(*args, **kwargs):
        stripped_kwargs = _strip_empty_values(kwargs)
        stripped_args = [a for a in args if a is not None]
        return function(*stripped_args, **stripped_kwargs)
    return decorator

def value_or_error(value, message=None):
    if value is None:
        raise ValueError(message)
    return value

def _strip_empty_values(dictionary):
    return dict(((k, v) for k, v in dictionary.items() if v is not None))

def _create_fifo(path):
    if os.path.exists(path):
        os.unlink(path)
    os.mkfifo(path)

def create_tunnel(**kwargs):
    return Tunnel(**kwargs)


class Client:

    def __init__(self, path):
        self.path = path
        self._actions = {
            'kill': self.kill_daemon,
            'open': self.open_tunnel,
            'close': self.close_tunnel,
        }

    def perform_action(self, action, name, **kwargs):
        self._actions[action](name, **kwargs)

    @command
    def open_tunnel(self, name, **kwargs):
        self.send({
            'name': 'topen',
            'args': {
                'name': name,
                'kwargs': kwargs,
            },
        })

    @command
    def debug(self):
        self.send({
            'name': 'debug',
            'args': {'debug': True},
        })

    @command
    def kill_daemon(self):
        self.send({
            'name': 'die',
        })

    @command
    def close_tunnel(self, name):
        self.send({
            'name': 'tclose',
            'args': {
                'name': name,
            },
        })

    def send(self, cmd):
        with open(self.path, 'w') as fifo:
            cmd_text = json.dumps(cmd)
            print('Sending ' + cmd_text)
            fifo.write(cmd_text + '\n')


class Daemon:

    def __init__(self):
        self._manager = TunnelManager(logging)
        commands = {
            'debug': self.debug,
            'die': self.die,
            'topen': self.topen,
            'tclose': self.tclose,
        }
        self._listener = Listener(commands)
        self._pipe = None

    def run(self):
        _create_fifo(FIFO_PATH)
        with open(FIFO_PATH, 'r') as fifo:
            self._pipe = Pipe(fifo)
            self._listener.listen(self._pipe)
        os.unlink(FIFO_PATH)

    @command
    def debug(self, args):
        print('Debug command called with\n' + str(args))

    @command
    def die(self):
        self._pipe.close()

    @command
    def topen(self, args):
        self._manager.open(args['name'], **args['kwargs'])

    @command
    def tclose(self, args):
        self._manager.close(args['name'])
{'fraction_non_alphanumeric': 0.1312340735347652, 'fraction_numerical': 0.0007280669821623589, 'mean_word_length': 1.932230522945571, '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': '4762238', 'n_tokens_mistral': 1741, 'n_tokens_neox': 1702, 'n_words': 395}