<commit_before>import os, sys, time, traceback

from . codes import CMDTYPE, LEDTYPE, SPIChipsets, BufferChipsets
from . devices import Devices, serial
from .. driver_base import DriverBase, ChannelOrder
from ... import log, util
from ... return_codes import RETURN_CODES, print_error, BiblioSerialError


class DriverSerial(DriverBase):
    """Main driver for Serial based LED strips"""

    def __init__(self, type, num, dev="",
                 c_order=ChannelOrder.RGB, SPISpeed=2,
                 gamma=None, restart_timeout=3,
                 deviceID=None, hardwareID="1D50:60AB",
                 baudrate=921600):
        super().__init__(num, c_order=c_order, gamma=gamma)
        self.devices = Devices(hardwareID, baudrate)

        if SPISpeed < 1 or SPISpeed > 24 or not (type in SPIChipsets):
            SPISpeed = 1

        self._SPISpeed = SPISpeed
        self._com = None
        self._type = type
        self._bufPad = 0
        self.dev = dev
        self.device_version = 0
        self.deviceID = deviceID
        self._sync_packet = util.generate_header(CMDTYPE.SYNC, 0)

        if self.deviceID is not None and (self.deviceID < 0 or self.deviceID > 255):
            raise ValueError("deviceID must be between 0 and 255")

        resp = self._connect()
        if resp == RETURN_CODES.REBOOT:  # reboot needed
            log.info(
                "Reconfigure and reboot needed, waiting for controller to restart...")
            self._com.close()
            time.sleep(restart_timeout)
            resp = self._connect()
            if resp != RETURN_CODES.SUCCESS:
                print_error(resp)
            else:
                log.info("Reconfigure success!")
        elif resp != RETURN_CODES.SUCCESS:
            print_error(resp)

        if type in SPIChipsets:
            log.info("Using SPI Speed: %sMHz", self._SPISpeed)

    def cleanup(self):
        if self._com:
            log.info("Closing connection to: %s", self.dev)
            self._com.close()

    def _connect(self):
        try:
            if(self.dev == "" or self.dev is None):
                self.devices.find_serial_devices()

                if self.deviceID is not None:
                    if self.deviceID in self.devices.device_ids:
                        self.dev = self.devices.device_ids[self.deviceID]
                        self.device_version = 0
                        try:
                            i = self.devices.found_devices.index(self.dev)
                            self.device_version = self.devices.device_versions[i]
                        except:
                            pass
                        log.info("Using COM Port: %s, Device ID: %s, Device Ver: %s",
                                 self.dev, self.deviceID, self.device_version)

                    if self.dev == "" or self.dev is None:
                        error = "Unable to find device with ID: {}".format(
                            self.deviceID)
                        log.error(error)
                        raise ValueError(error)
                elif len(self.devices.found_devices) > 0:
                    self.dev = self.devices.found_devices[0]
                    self.device_version = 0
                    try:
                        i = self.devices.found_devices.index(self.dev)
                        self.device_version = self.devices.device_versions[i]
                    except:
                        pass
                    devID = -1
                    for id in self.devices.device_ids:
                        if self.devices.device_ids[id] == self.dev:
                            devID = id

                    log.info("Using COM Port: %s, Device ID: %s, Device Ver: %s",
                             self.dev, devID, self.device_version)

            try:
                self._com = serial.Serial(self.dev, baudrate=self.devices.baudrate, timeout=5)
            except serial.SerialException as e:
                ports = self.devices.find_serial_devices()
                error = "Invalid port specified. No COM ports available."
                if len(ports) > 0:
                    error = "Invalid port specified. Try using one of: \n" + \
                        "\n".join(ports)
                log.info(error)
                raise BiblioSerialError(error)

            packet = util.generate_header(CMDTYPE.SETUP_DATA, 4)
            packet.append(self._type)  # set strip type
            byteCount = self.bufByteCount()
            if self._type in BufferChipsets:
                if self._type == LEDTYPE.APA102 and self.device_version >= 2:
                    pass
                else:
                    self._bufPad = BufferChipsets[self._type](self.numLEDs) * 3
                    byteCount += self._bufPad

            packet.append(byteCount & 0xFF)  # set 1st byte of byteCount
            packet.append(byteCount >> 8)  # set 2nd byte of byteCount
            packet.append(self._SPISpeed)
            self._com.write(packet)

            resp = self._com.read(1)
            if len(resp) == 0:
                self.devices.error()
<commit_msg>Get rid of if statement that's always true.<commit_after>import os, sys, time, traceback

from . codes import CMDTYPE, LEDTYPE, SPIChipsets, BufferChipsets
from . devices import Devices, serial
from .. driver_base import DriverBase, ChannelOrder
from ... import log, util
from ... return_codes import RETURN_CODES, print_error, BiblioSerialError


class DriverSerial(DriverBase):
    """Main driver for Serial based LED strips"""

    def __init__(self, type, num, dev="",
                 c_order=ChannelOrder.RGB, SPISpeed=2,
                 gamma=None, restart_timeout=3,
                 deviceID=None, hardwareID="1D50:60AB",
                 baudrate=921600):
        super().__init__(num, c_order=c_order, gamma=gamma)
        self.devices = Devices(hardwareID, baudrate)

        if SPISpeed < 1 or SPISpeed > 24 or not (type in SPIChipsets):
            SPISpeed = 1

        self._SPISpeed = SPISpeed
        self._com = None
        self._type = type
        self._bufPad = 0
        self.dev = dev
        self.device_version = 0
        self.deviceID = deviceID
        self._sync_packet = util.generate_header(CMDTYPE.SYNC, 0)

        if self.deviceID is not None and (self.deviceID < 0 or self.deviceID > 255):
            raise ValueError("deviceID must be between 0 and 255")

        resp = self._connect()
        if resp == RETURN_CODES.REBOOT:  # reboot needed
            log.info(
                "Reconfigure and reboot needed, waiting for controller to restart...")
            self._com.close()
            time.sleep(restart_timeout)
            resp = self._connect()
            if resp != RETURN_CODES.SUCCESS:
                print_error(resp)
            else:
                log.info("Reconfigure success!")
        elif resp != RETURN_CODES.SUCCESS:
            print_error(resp)

        if type in SPIChipsets:
            log.info("Using SPI Speed: %sMHz", self._SPISpeed)

    def cleanup(self):
        if self._com:
            log.info("Closing connection to: %s", self.dev)
            self._com.close()

    def _connect(self):
        try:
            self.devices.find_serial_devices()

            if self.deviceID is not None:
                if self.deviceID in self.devices.device_ids:
                    self.dev = self.devices.device_ids[self.deviceID]
                    self.device_version = 0
                    try:
                        i = self.devices.found_devices.index(self.dev)
                        self.device_version = self.devices.device_versions[i]
                    except:
                        pass
                    log.info("Using COM Port: %s, Device ID: %s, Device Ver: %s",
                             self.dev, self.deviceID, self.device_version)

                if self.dev == "" or self.dev is None:
                    error = "Unable to find device with ID: {}".format(
                        self.deviceID)
                    log.error(error)
                    raise ValueError(error)
            elif len(self.devices.found_devices) > 0:
                self.dev = self.devices.found_devices[0]
                self.device_version = 0
                try:
                    i = self.devices.found_devices.index(self.dev)
                    self.device_version = self.devices.device_versions[i]
                except:
                    pass
                devID = -1
                for id in self.devices.device_ids:
                    if self.devices.device_ids[id] == self.dev:
                        devID = id

                log.info("Using COM Port: %s, Device ID: %s, Device Ver: %s",
                         self.dev, devID, self.device_version)

            try:
                self._com = serial.Serial(self.dev, baudrate=self.devices.baudrate, timeout=5)
            except serial.SerialException as e:
                ports = self.devices.find_serial_devices()
                error = "Invalid port specified. No COM ports available."
                if len(ports) > 0:
                    error = "Invalid port specified. Try using one of: \n" + \
                        "\n".join(ports)
                log.info(error)
                raise BiblioSerialError(error)

            packet = util.generate_header(CMDTYPE.SETUP_DATA, 4)
            packet.append(self._type)  # set strip type
            byteCount = self.bufByteCount()
            if self._type in BufferChipsets:
                if self._type == LEDTYPE.APA102 and self.device_version >= 2:
                    pass
                else:
                    self._bufPad = BufferChipsets[self._type](self.numLEDs) * 3
                    byteCount += self._bufPad

            packet.append(byteCount & 0xFF)  # set 1st byte of byteCount
            packet.append(byteCount >> 8)  # set 2nd byte of byteCount
            packet.append(self._SPISpeed)
            self._com.write(packet)

            resp = self._com.read(1)
            if len(resp) == 0:
                self.devices.error()

{'fraction_non_alphanumeric': 0.08960679707567674, 'fraction_numerical': 0.009286702232760324, 'mean_word_length': 1.4872235872235873, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 17, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 1, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7642133', 'n_tokens_mistral': 2845, 'n_tokens_neox': 2658, 'n_words': 682}