# Path: stoqdrivers/exceptions.py
# class USBDriverError(DriverError):
#     """A USB Error that was raised by StoqDrivers (and not pyusb)"""
# 
# Path: stoqdrivers/utils.py
# def str2bytes(text):
#     if isinstance(text, bytes):
#         return text
#     return bytes(ord(i) for i in text)


    import usb.core
    import usb.util
from stoqdrivers.exceptions import USBDriverError
from stoqdrivers.utils import str2bytes
# Based on python-escpos's escpos.printer.Usb:
#
# https://github.com/python-escpos/python-escpos/blob/master/src/escpos/printer.py


class UsbBase(object):
    """Base class for a USB Printer"""

    #: Out Endpoint address. Subclasses must define this.
    out_ep = None

    def __init__(self, vendor_id, product_id, interface=0,
                 timeout=0, *args, **kwargs):
        assert has_usb
        self.device = None
        self.vendor_id = vendor_id
        self.product_id = product_id
        self.interface = interface
        self.timeout = timeout
        assert self.out_ep is not None
        super(UsbBase, self).__init__(*args, **kwargs)

    def __del__(self):
        """Stop using any unnecessary resources upon destruction"""
        self.close()

    def open(self):
        self.device = usb.core.find(idVendor=self.vendor_id,
                                    idProduct=self.product_id)
        if self.device is None:

# Path: stoqdrivers/exceptions.py
# class USBDriverError(DriverError):
#     """A USB Error that was raised by StoqDrivers (and not pyusb)"""
# 
# Path: stoqdrivers/utils.py
# def str2bytes(text):
#     if isinstance(text, bytes):
#         return text
#     return bytes(ord(i) for i in text)


# Path: stoqdrivers/usbbase.py
    import usb.core
    import usb.util
from stoqdrivers.exceptions import USBDriverError
from stoqdrivers.utils import str2bytes

# Based on python-escpos's escpos.printer.Usb:
#
# https://github.com/python-escpos/python-escpos/blob/master/src/escpos/printer.py


class UsbBase(object):
    """Base class for a USB Printer"""

    #: Out Endpoint address. Subclasses must define this.
    out_ep = None

    def __init__(self, vendor_id, product_id, interface=0,
                 timeout=0, *args, **kwargs):
        assert has_usb
        self.device = None
        self.vendor_id = vendor_id
        self.product_id = product_id
        self.interface = interface
        self.timeout = timeout
        assert self.out_ep is not None
        super(UsbBase, self).__init__(*args, **kwargs)

    def __del__(self):
        """Stop using any unnecessary resources upon destruction"""
        self.close()

    def open(self):
        self.device = usb.core.find(idVendor=self.vendor_id,
                                    idProduct=self.product_id)
        if self.device is None:

            raise USBDriverError('USB Device not found using %s:%s' %