<commit_before># -*- coding: utf-8 -*-
"""
Printer.py

A library to interface with the Form 1 and Form 1+ over USB

Copyright 2016-2017 Formlabs

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
"""
from __future__ import division
import ctypes
import errno
import io
import struct
import time

import usb.core
import numpy as np

from OpenFL import FLP

################################################################################

class DecodeError(RuntimeError):
    pass
class BadResponse(RuntimeError):
    pass
class LaserPowerError(RuntimeError):
    pass

class Printer(object):
    """ Instantiation of a printer object
    """
    # VID and PID for the Form 1 and Form 1+
    VID = 0x16d0
    PID = 0x07eb

    # Endpoint numbers for TX and RX
    TX_EP = 0x81    # Printer -> computer
    RX_EP = 0x03    # Computer -> printer

    SOF = 0xFF  # Special character marking the beginning of a transmission
    EOF = 0xFE  # Special character marking the end of a transmission
    ESCAPE = 0xFD   # Character used to escape special characters in a bytestring

    AUDIT_LASER_POWER = True
    LASER_POWER_MAX_MW = 64

    def __init__(self, connect=True):
        if connect:
            self.dev = usb.core.find(idVendor=self.VID, idProduct=self.PID)
            if self.dev is None:
                raise RuntimeError("Could not find printer")
            self.dev.default_timeout = 100
            try:
                self.dev.set_configuration()
            except usb.USBError as e:
                e.strerror += ". Be sure PreForm is closed."
                raise
        self.incoming = []
        self.packet = bytearray()

        # These values are loaded from the printer as-needed
        self._laser_table = None
        self._grid_table = None

    def _read(self, bufsize=1024):
        """ Reads raw data from the printer's usual endpoint
        """
        return bytearray(self.dev.read(self.TX_EP, bufsize))

    def _write(self, data):
        """ Writes raw data to the printer's usual endpoint
        """
        return self.dev.write(self.RX_EP, data)

    @classmethod
    def _decode(cls, received):
        """ Strips the escape characters from streamed data
        """
        out = b''
        escaping = False
        for byte in received:
            if escaping:<commit_msg>Added timeout. It seems to be necessary now.<commit_after># -*- coding: utf-8 -*-
"""
Printer.py

A library to interface with the Form 1 and Form 1+ over USB

Copyright 2016-2017 Formlabs

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
"""
from __future__ import division
import ctypes
import errno
import io
import struct
import time

import usb.core
import numpy as np

from OpenFL import FLP

################################################################################

class DecodeError(RuntimeError):
    pass
class BadResponse(RuntimeError):
    pass
class LaserPowerError(RuntimeError):
    pass

class Printer(object):
    """ Instantiation of a printer object
    """
    # VID and PID for the Form 1 and Form 1+
    VID = 0x16d0
    PID = 0x07eb

    # Endpoint numbers for TX and RX
    TX_EP = 0x81    # Printer -> computer
    RX_EP = 0x03    # Computer -> printer

    SOF = 0xFF  # Special character marking the beginning of a transmission
    EOF = 0xFE  # Special character marking the end of a transmission
    ESCAPE = 0xFD   # Character used to escape special characters in a bytestring

    AUDIT_LASER_POWER = True
    LASER_POWER_MAX_MW = 64

    def __init__(self, connect=True, timeout_ms=10000):
        if connect:
            self.dev = usb.core.find(idVendor=self.VID, idProduct=self.PID)
            if self.dev is None:
                raise RuntimeError("Could not find printer")
            self.dev.default_timeout = 100
            try:
                self.dev.set_configuration()
            except usb.USBError as e:
                e.strerror += ". Be sure PreForm is closed."
                raise
        self.timeout_ms = timeout_ms
        self.incoming = []
        self.packet = bytearray()

        # These values are loaded from the printer as-needed
        self._laser_table = None
        self._grid_table = None

    def _read(self, bufsize=1024):
        """ Reads raw data from the printer's usual endpoint
        """
        return bytearray(self.dev.read(self.TX_EP, bufsize, timeout=self.timeout_ms))

    def _write(self, data):
        """ Writes raw data to the printer's usual endpoint
        """
        return self.dev.write(self.RX_EP, data, timeout=self.timeout_ms)

    @classmethod
    def _decode(cls, received):
        """ Strips the escape characters from streamed data
        """
        out = b''
        escaping = False
        for byte in received:
            if escaping:
{'fraction_non_alphanumeric': 0.09738219895287958, 'fraction_numerical': 0.015532286212914486, 'mean_word_length': 2.6387301587301586, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '3050721', 'n_tokens_mistral': 1662, 'n_tokens_neox': 1525, 'n_words': 605}