<commit_before>from . driver_base import DriverBase, ChannelOrder
import time

import os
from .. import log


class DriverSPIBase(DriverBase):
    """Base driver for controling SPI devices on systems like the Raspberry Pi and BeagleBone"""

    def __init__(self, num, c_order=ChannelOrder.GRB, use_py_spi=True,
                 dev="/dev/spidev0.0", SPISpeed=2, open=open, gamma=None):
        super().__init__(num, c_order=c_order, gamma=gamma)

        self.dev = dev
        self.use_py_spi = use_py_spi
        self._spiSpeed = SPISpeed

        # File based SPI requires a bytearray so we have to overwrite _buf
        if self.use_py_spi:
            a, b = -1, -1
            d = self.dev.replace("/dev/spidev", "")
            s = d.split('.')
            if len(s) == 2:
                a = int(s[0])
                b = int(s[1])

            if a < 0 or b < 0:
                error = "When using py-spidev, the given device must be in the format /dev/spidev*.*"
                log.error(error)
                raise ValueError(error)

            self._bootstrapSPIDev()
            import spidev
            self.spi = spidev.SpiDev()
            self.spi.open(a, b)
            self.spi.max_speed_hz = int(self._spiSpeed * 1000000.0)
            log.info('py-spidev speed @ %.1f MHz',
                     (float(self.spi.max_speed_hz) / 1000000.0))
        else:
            self._buf = bytearray(self._buf)
            self.spi = open(self.dev, "wb")

    def _bootstrapSPIDev(self):
        import os.path
        import sys
        if self.use_py_spi:
            try:
                import spidev
            except:
                error = "Unable to import spidev. Please install. pip install spidev"
                log.error(error)
                raise ImportError(error)

        if not os.path.exists(self.dev):
            error = "Cannot find SPI device. Please see https://github.com/maniacallabs/bibliopixel/wiki/SPI-Setup for details."
            log.error(error)
            raise IOError(error)

        # permissions check
        try:
            open(self.dev)
        except IOError as e:
            if e.errno == 13:
                error = "Cannot find SPI device. Please see https://github.com/maniacallabs/bibliopixel/wiki/SPI-Setup for details."
                log.error(error)
                raise IOError(error)
            else:
                raise e

    def _send_packet(self):
        if self.use_py_spi:
            self.spi.xfer2(self._packet)
        else:
            self.spi.write(self._packet)
            self.spi.flush()

    def _compute_packet(self):
        self._render()
        self._packet = self._buf<commit_msg>Tiny cleanup to spi_driver_base.py.<commit_after>import os, sys, time
from . driver_base import DriverBase, ChannelOrder
from .. import log


class DriverSPIBase(DriverBase):
    """
    Base driver for controling SPI devices on systems like the Raspberry Pi and
    BeagleBone.
    """
    def __init__(self, num, c_order=ChannelOrder.GRB, use_py_spi=True,
                 dev='/dev/spidev0.0', SPISpeed=2, open=open, gamma=None):
        super().__init__(num, c_order=c_order, gamma=gamma)

        self.dev = dev
        self.use_py_spi = use_py_spi
        self._spiSpeed = SPISpeed
        self._open = open

        # File based SPI requires a bytearray so we have to overwrite _buf
        if self.use_py_spi:
            a, b = -1, -1
            d = self.dev.replace('/dev/spidev', '')
            s = d.split('.')
            if len(s) == 2:
                a = int(s[0])
                b = int(s[1])

            if a < 0 or b < 0:
                error(BAD_FORMAT_ERROR)

            self._bootstrapSPIDev()
            import spidev
            self.spi = spidev.SpiDev()
            self.spi.open(a, b)
            self.spi.max_speed_hz = int(self._spiSpeed * 1000000.0)
            log.info('py-spidev speed @ %.1f MHz',
                     (float(self.spi.max_speed_hz) / 1000000.0))
        else:
            self._buf = bytearray(self._buf)
            self.spi = self._open(self.dev, 'wb')

    def _bootstrapSPIDev(self):
        if self.use_py_spi:
            try:
                import spidev
            except:
                error(CANT_IMPORT_SPIDEV_ERROR)

        if not os.path.exists(self.dev):
            error(CANT_FIND_ERROR)

        # permissions check
        try:
            self.open(self.dev)
        except IOError as e:
            if e.errno == 13:
                error(CANT_FIND_ERROR)
            raise e

    def _send_packet(self):
        if self.use_py_spi:
            self.spi.xfer2(self._packet)
        else:
            self.spi.write(self._packet)
            self.spi.flush()

    def _compute_packet(self):
        self._render()
        self._packet = self._buf


CANT_FIND_ERROR = """Cannot find SPI device.
Please see

    https://github.com/maniacallabs/bibliopixel/wiki/SPI-Setup

for details.
"""

BAD_FORMAT_ERROR = (
    'When using py-spidev, `dev` must be in the format /dev/spidev*.*')

CANT_IMPORT_SPIDEV_ERROR = """
Unable to import spidev. Please install:

    pip install spidev
"""


def error(text):
    log.error(text)
    raise IOError(text)
{'fraction_non_alphanumeric': 0.10307101727447217, 'fraction_numerical': 0.011516314779270634, 'mean_word_length': 1.6944157187176836, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 3, 'https://': 3, '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': '6329692', 'n_tokens_mistral': 1688, 'n_tokens_neox': 1603, 'n_words': 387}