<commit_before>from __future__ import absolute_import

# Some utilities for speaking s3g
import struct
import array
import time
import serial

import makerbot_driver
import uuid


class s3g(object):
    """ Represents an interface to a s3g driven bot. Contains methods and functions to
    read and write data to the bot.  No data is cached by this driver, all data is requested
    over the USB bus when queried.
    """

    @classmethod
    def from_filename(cls, port, baudrate=115200, timeout=.2):
        """Constructs and returns an s3g object connected to the
        passed file endpoint passed as a string @port (ie 'COM0' or '/dev/tty9')

        @param port target Serial port name
        @param baurdate baudrace 115200 assumed
        @param timeout, time allowance before timeout error. 0.2 assummed
        @return s3g object, equipped with a StreamWrtier directed at port.
        """
        r = s3g()
        s = serial.Serial(port, baudrate=baudrate, timeout=timeout)

        # begin baud rate hack
        #
        # There is an interaction between the 8U2 firmware and PySerial where
        # PySerial thinks the 8U2 is already running at the specified baud rate and
        # it doesn't actually issue the ioctl calls to set the baud rate. We work
        # around it by setting the baud rate twice, to two different values. This
        # forces PySerial to issue the correct ioctl calls.
        s.baudrate = 9600
        s.baudrate = baudrate
        # end baud rate hack

        r.writer = makerbot_driver.Writer.StreamWriter(s)
        return r

    def __init__(self):
        self.writer = None
        # TODO: Move these to constants file.
        self.extendedPointLength = 5
        self.pointLength = 3

        self.accelerated_firmware_version = 601
        self.set_firmware_version(firmware_version)

    def set_firmware_version(self, firmware_version):
        self.firmware_version = firmware_version
        self.send_accelerated_point = self.convert_to_usable_firmware_version(firmware_version) >= self.accelerated_firmware_version
    
    def convert_to_usable_firmware_version(self, firmware_version):
        """
        Firmware versions come in two flavors: XXX (i.e. 600) or X.X (i.e. 6.0).  Since int
        comparisons are easier than string comparisons, we request all version
        numbers are in int form.  Just in case, however, firmware_version passed
        through the parser will be run through this function.
        """
        compatable = firmware_version
        if isinstance(compatable, float):
            compatable = str(compatable)
        if isinstance(compatable, str):
            compatable = compatable.replace('.', '0')
            compatable = int(compatable)
        return compatable

    def create_reader(self):
        return makerbot_driver.EEPROM.EepromReader.factory(self)

    def close(self):
        """ If any ports are open for this s3g bot, it closes those ports """
        if self.writer:
            self.writer.close()

    def is_open(self):
        """@returns true if we have a writer and it is open. False otherwise."""
        if self.writer:
            return self.writer.is_open()
        return False

    def open(self):
        """ If a writer with data exists in this bot, attempts to open that writer."""
        if self.writer:
            self.writer.open()

    def get_version(self):
        """
        Get the firmware version number of the connected machine
        @return Version number
        """
        payload = struct.pack(
            '<BH',
            host_query_command_dict['GET_VERSION'],
            s3g_version,
        )

        response = self.writer.send_query_payload(payload)
        [response_code, version] = makerbot_driver.Encoder.unpack_response(
            '<BH', response)
        return version

    def get_name(self):
        """
        Get stored Bot Name
        @return a string for the name
        TODO: merge this function with future eeprom read/write module
        """
        reader = self.create_reader()
        name = reader.read_data('MACHINE_NAME')
        return name[0]

    def get_toolhead_count(self):
        """
        @return the toolhead count of this bot. -1 on error
        """
        reader = self.create_reader()
        data = reader.read_data('TOOL_COUNT')
        return data[0]

    def get_vid_pid(self):
        """
        Returns vid and pid as an int
        """
        reader = self.create_reader()
        data = reader.read_data('VID_PID_INFO')
        return data[0], data[1]

    def get_verified_status(self, verified_pid=vid_pid[1]):
        """<commit_msg>* Fixed import mistake in s3g.py<commit_after>from __future__ import absolute_import

# Some utilities for speaking s3g
import struct
import array
import time
import serial

import makerbot_driver
import uuid


class s3g(object):
    """ Represents an interface to a s3g driven bot. Contains methods and functions to
    read and write data to the bot.  No data is cached by this driver, all data is requested
    over the USB bus when queried.
    """

    @classmethod
    def from_filename(cls, port, baudrate=115200, timeout=.2):
        """Constructs and returns an s3g object connected to the
        passed file endpoint passed as a string @port (ie 'COM0' or '/dev/tty9')

        @param port target Serial port name
        @param baurdate baudrace 115200 assumed
        @param timeout, time allowance before timeout error. 0.2 assummed
        @return s3g object, equipped with a StreamWrtier directed at port.
        """
        r = s3g()
        s = serial.Serial(port, baudrate=baudrate, timeout=timeout)

        # begin baud rate hack
        #
        # There is an interaction between the 8U2 firmware and PySerial where
        # PySerial thinks the 8U2 is already running at the specified baud rate and
        # it doesn't actually issue the ioctl calls to set the baud rate. We work
        # around it by setting the baud rate twice, to two different values. This
        # forces PySerial to issue the correct ioctl calls.
        s.baudrate = 9600
        s.baudrate = baudrate
        # end baud rate hack

        r.writer = makerbot_driver.Writer.StreamWriter(s)
        return r

    def __init__(self):
        self.writer = None
        # TODO: Move these to constants file.
        self.extendedPointLength = 5
        self.pointLength = 3

        self.accelerated_firmware_version = 601
        self.set_firmware_version(firmware_version)

    def set_firmware_version(self, firmware_version):
        self.firmware_version = firmware_version
        self.send_accelerated_point = self.convert_to_usable_firmware_version(firmware_version) >= self.accelerated_firmware_version
    
    def convert_to_usable_firmware_version(self, firmware_version):
        """
        Firmware versions come in two flavors: XXX (i.e. 600) or X.X (i.e. 6.0).  Since int
        comparisons are easier than string comparisons, we request all version
        numbers are in int form.  Just in case, however, firmware_version passed
        through the parser will be run through this function.
        """
        compatable = firmware_version
        if isinstance(compatable, float):
            compatable = str(compatable)
        if isinstance(compatable, str):
            compatable = compatable.replace('.', '0')
            compatable = int(compatable)
        return compatable

    def create_reader(self):
        return makerbot_driver.EEPROM.EepromReader.factory(self)

    def close(self):
        """ If any ports are open for this s3g bot, it closes those ports """
        if self.writer:
            self.writer.close()

    def is_open(self):
        """@returns true if we have a writer and it is open. False otherwise."""
        if self.writer:
            return self.writer.is_open()
        return False

    def open(self):
        """ If a writer with data exists in this bot, attempts to open that writer."""
        if self.writer:
            self.writer.open()

    def get_version(self):
        """
        Get the firmware version number of the connected machine
        @return Version number
        """
        payload = struct.pack(
            '<BH',
            host_query_command_dict['GET_VERSION'],
            s3g_version,
        )

        response = self.writer.send_query_payload(payload)
        [response_code, version] = makerbot_driver.Encoder.unpack_response(
            '<BH', response)
        return version

    def get_name(self):
        """
        Get stored Bot Name
        @return a string for the name
        TODO: merge this function with future eeprom read/write module
        """
        reader = self.create_reader()
        name = reader.read_data('MACHINE_NAME')
        return name[0]

    def get_toolhead_count(self):
        """
        @return the toolhead count of this bot. -1 on error
        """
        reader = self.create_reader()
        data = reader.read_data('TOOL_COUNT')
        return data[0]

    def get_vid_pid(self):
        """
        Returns vid and pid as an int
        """
        reader = self.create_reader()
        data = reader.read_data('VID_PID_INFO')
        return data[0], data[1]

    def get_verified_status(self, verified_pid=makerbot_driver.vid_pid[1]):
        """
{'fraction_non_alphanumeric': 0.07508277261561465, 'fraction_numerical': 0.010787140873651607, 'mean_word_length': 2.337134711332858, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 5, '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': '7520485', 'n_tokens_mistral': 2682, 'n_tokens_neox': 2536, 'n_words': 889}