<commit_before>import os
import socket
from functools import partial
from subprocess import Popen, PIPE, STDOUT

import lirc
import serial

class LircCodeReaderMixin(object):

    def __init__(self, name='PyLirc', config='~/.lircrc', **kwargs):
        self.name = name
        self.config = config
        lirc.init(self.name, self.config)
        super(LircCodeReaderMixin, self).__init__(**kwargs)

    def lirc_commands(self):
        while True:
            for each in lirc.nextcode():
                yield each


class TcpControlMixin(object):

    def __init__(self, ip_address=None, port=8102, **kwargs):
        if not ip_address:
            raise TypeError
        self.tcp_state = {}
        self.connected = False
        self.ip_address = ip_address
        self.port = port
        self.connect_tcp()
        self.update_command_map(self.execute_tcp_command, self.tcp_command_map)
        super(TcpControlMixin, self).__init__(**kwargs)

    tcp_command_map = {
        'KEY_VOLUMEUP': 'VU',
        'KEY_VOLUMEDOWN': 'VD',
        'KEY_MUTE': 'MUTE',
    }

    tcp_toggle_commands = {
        'MUTE': {
            False: 'MO',
            True: 'MF',
        }
    }

    def get_tcp_toggle_state(self, command):
        state = self.tcp_state.get(command, False)
        self.tcp_state[command] = not state
        return self.tcp_toggle_commands[command][state]

    def connect_tcp(self):
        self._tcpsock = socket.socket()
        self._tcpsock.connect((self.ip_address, self.port))
        self.connected = True

    def send_tcp_command(self, command):
        if not self.connected:
            self.connect_tcp()
        try:
            self._tcpsock.send(command)
        except BrokenPipeError:
            self.connected = False
            self.send_tcp_command(command)

    def execute_tcp_command(self, command):
        if command in self.tcp_toggle_commands:
            command = self.get_tcp_toggle_state(command)
        command = command + '\r\n'
        encoded = command.encode('ascii')
        self.send_tcp_command(encoded)


class SerialControlMixin(object):

    def __init__(self, serial_port=None, **kwargs):
        if not serial_port:
            raise TypeError
        self.ser = serial.Serial(serial_port, baudrate=9600, bytesize=8, timeout=3)
        self.update_command_map(self.send_serial_command, self.serial_command_map)
        super(SerialControlMixin, self).__init__(**kwargs)

    serial_command_map = {
        'KEY_HOME': [0x08, 0x22, 0x00, 0x00, 0x00, 0x00, 0xd6]
    }

    def send_serial_command(self, command):
        self.ser.write(command)


class CecControlMixin(object):

    cec_command_map = {
        'KEY_UP': '01',
        'KEY_DOWN': '02',
        'KEY_RIGHT': '04',
        'KEY_LEFT': '03',
        'KEY_SELECT': '00',
    }

    def __init__(self, source_id=1, sink_id=None, **kwargs):
        if not sink_id:
            raise TypeError
        self.cec_source_id = source_id
        self.cec_sink_id = sink_id
        self.cec_client = Popen(['cec-client'], stdin=PIPE, stdout=PIPE, stderr=PIPE, shell=True)
        self.update_command_map(self.send_cec_command, self.cec_command_map)
        super(CecControlMixin, self).__init__(**kwargs)

    def send_cec_command(self, command):
        full_command = 'tx {source}{sink}:44:{command} \n tx {source}{sink}:45 \n'.format(
            source=self.cec_source_id,
            sink=self.cec_sink_id,
            command=command
        ).encode('ascii')
        self.cec_client.stdin.write(full_command)


class RemoteCommand(LircCodeReaderMixin, TcpControlMixin, SerialControlMixin, CecControlMixin, object):

    def __init__(self, **kwargs):
        self.command_map = {}
        self.state = {}
        super(RemoteCommand, self).__init__(**kwargs)
<commit_msg>Buffer by line<commit_after>import os
import socket
from functools import partial
from subprocess import Popen, PIPE, STDOUT

import lirc
import serial

class LircCodeReaderMixin(object):

    def __init__(self, name='PyLirc', config='~/.lircrc', **kwargs):
        self.name = name
        self.config = config
        lirc.init(self.name, self.config)
        super(LircCodeReaderMixin, self).__init__(**kwargs)

    def lirc_commands(self):
        while True:
            for each in lirc.nextcode():
                yield each


class TcpControlMixin(object):

    def __init__(self, ip_address=None, port=8102, **kwargs):
        if not ip_address:
            raise TypeError
        self.tcp_state = {}
        self.connected = False
        self.ip_address = ip_address
        self.port = port
        self.connect_tcp()
        self.update_command_map(self.execute_tcp_command, self.tcp_command_map)
        super(TcpControlMixin, self).__init__(**kwargs)

    tcp_command_map = {
        'KEY_VOLUMEUP': 'VU',
        'KEY_VOLUMEDOWN': 'VD',
        'KEY_MUTE': 'MUTE',
    }

    tcp_toggle_commands = {
        'MUTE': {
            False: 'MO',
            True: 'MF',
        }
    }

    def get_tcp_toggle_state(self, command):
        state = self.tcp_state.get(command, False)
        self.tcp_state[command] = not state
        return self.tcp_toggle_commands[command][state]

    def connect_tcp(self):
        self._tcpsock = socket.socket()
        self._tcpsock.connect((self.ip_address, self.port))
        self.connected = True

    def send_tcp_command(self, command):
        if not self.connected:
            self.connect_tcp()
        try:
            self._tcpsock.send(command)
        except BrokenPipeError:
            self.connected = False
            self.send_tcp_command(command)

    def execute_tcp_command(self, command):
        if command in self.tcp_toggle_commands:
            command = self.get_tcp_toggle_state(command)
        command = command + '\r\n'
        encoded = command.encode('ascii')
        self.send_tcp_command(encoded)


class SerialControlMixin(object):

    def __init__(self, serial_port=None, **kwargs):
        if not serial_port:
            raise TypeError
        self.ser = serial.Serial(serial_port, baudrate=9600, bytesize=8, timeout=3)
        self.update_command_map(self.send_serial_command, self.serial_command_map)
        super(SerialControlMixin, self).__init__(**kwargs)

    serial_command_map = {
        'KEY_HOME': [0x08, 0x22, 0x00, 0x00, 0x00, 0x00, 0xd6]
    }

    def send_serial_command(self, command):
        self.ser.write(command)


class CecControlMixin(object):

    cec_command_map = {
        'KEY_UP': '01',
        'KEY_DOWN': '02',
        'KEY_RIGHT': '04',
        'KEY_LEFT': '03',
        'KEY_SELECT': '00',
    }

    def __init__(self, source_id=1, sink_id=None, **kwargs):
        if not sink_id:
            raise TypeError
        self.cec_source_id = source_id
        self.cec_sink_id = sink_id
        self.cec_client = Popen(
            ['cec-client'],
            stdin=PIPE,
            stdout=PIPE,
            stderr=PIPE,
            buffsize=1,
            universal_newlines=True,
        )
        self.update_command_map(self.send_cec_command, self.cec_command_map)
        super(CecControlMixin, self).__init__(**kwargs)

    def send_cec_command(self, command):
        full_command = 'tx {source}{sink}:44:{command} \n tx {source}{sink}:45 \n'.format(
            source=self.cec_source_id,
            sink=self.cec_sink_id,
            command=command
        )
        self.cec_client.stdin.write(full_command)


class RemoteCommand(LircCodeReaderMixin, TcpControlMixin, SerialControlMixin, CecControlMixin, object):

    def __init__(self, **kwargs):
        self.command_map = {}
        self.state = {}
        super(RemoteCommand, self).__init__(**kwargs)

{'fraction_non_alphanumeric': 0.10979883192731993, 'fraction_numerical': 0.0118105126541207, 'mean_word_length': 2.507510241238052, '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': '1900373', 'n_tokens_mistral': 2564, 'n_tokens_neox': 2454, 'n_words': 455}