# Path: stoqdrivers/interfaces.py
# class ISerialPort(Interface):
#     """ Interface used by drivers to write commands and get reply from devices
#     """
# 
#     def getDSR():
#         """ Returns True if the device is done to send data. Some drivers
#         block in a loop waiting for this function returns True before call
#         read.
#         """
# 
#     def setDTR(value):
#         """ Set to True when the driver is going to send data to the device
#         """
# 
#     def read(n_bytes=1):
#         """ Read data """
# 
#     def write(data):
#         """ Write data """
# 
# Path: stoqdrivers/exceptions.py
# class DriverError(Exception):
#     "Base exception for all printer errors"
# 
#     def __init__(self, error='', code=-1):
#         if code != -1:
#             error = '%d: %s' % (code, error)
#         Exception.__init__(self, error)
#         self.code = code
# 
# class PrinterError(Exception):
#     "General printer errors"
# 
# Path: stoqdrivers/translation.py
# def stoqdrivers_gettext(message):
#     return gettext.dgettext('stoqdrivers', message)
# 
# Path: stoqdrivers/utils.py
# def str2bytes(text):
#     if isinstance(text, bytes):
#         return text
#     return bytes(ord(i) for i in text)
# 
# def bytes2str(data):
#     return ''.join(chr(i) for i in data)


import logging
import socket
from serial import Serial, EIGHTBITS, PARITY_NONE, STOPBITS_ONE
from zope.interface import implementer
from stoqdrivers.interfaces import ISerialPort
from stoqdrivers.exceptions import DriverError, PrinterError
from stoqdrivers.translation import stoqdrivers_gettext
from stoqdrivers.utils import str2bytes, bytes2str
    # open the drawer, and also check its status. Some models, for instance,
    # the Radiant drawers, use inverted logic to describe whether they are
    # open, specified by this attribute, settable via BaseDevice config.
    inverted_drawer = False

    def __init__(self, port):
        self.set_port(port)

    def set_port(self, port):
        self._port = port

    def get_port(self):
        return self._port

    def fileno(self):
        return self._port.fileno()

    def writeline(self, data):
        self.write(self.CMD_PREFIX + data + self.CMD_SUFFIX)
        return self.readline()

    def write(self, data):
        # pyserial is expecting bytes but we work with str in stoqdrivers
        data = str2bytes(data)
        log.debug(">>> %r (%d bytes)" % (data, len(data)))
        self._port.write(data)

    def read(self, n_bytes):
        # stoqdrivers is expecting str but pyserial will reply with bytes
        data = self._port.read(n_bytes)

# Path: stoqdrivers/interfaces.py
# class ISerialPort(Interface):
#     """ Interface used by drivers to write commands and get reply from devices
#     """
# 
#     def getDSR():
#         """ Returns True if the device is done to send data. Some drivers
#         block in a loop waiting for this function returns True before call
#         read.
#         """
# 
#     def setDTR(value):
#         """ Set to True when the driver is going to send data to the device
#         """
# 
#     def read(n_bytes=1):
#         """ Read data """
# 
#     def write(data):
#         """ Write data """
# 
# Path: stoqdrivers/exceptions.py
# class DriverError(Exception):
#     "Base exception for all printer errors"
# 
#     def __init__(self, error='', code=-1):
#         if code != -1:
#             error = '%d: %s' % (code, error)
#         Exception.__init__(self, error)
#         self.code = code
# 
# class PrinterError(Exception):
#     "General printer errors"
# 
# Path: stoqdrivers/translation.py
# def stoqdrivers_gettext(message):
#     return gettext.dgettext('stoqdrivers', message)
# 
# Path: stoqdrivers/utils.py
# def str2bytes(text):
#     if isinstance(text, bytes):
#         return text
#     return bytes(ord(i) for i in text)
# 
# def bytes2str(data):
#     return ''.join(chr(i) for i in data)


# Path: stoqdrivers/serialbase.py
import logging
import socket
from serial import Serial, EIGHTBITS, PARITY_NONE, STOPBITS_ONE
from zope.interface import implementer
from stoqdrivers.interfaces import ISerialPort
from stoqdrivers.exceptions import DriverError, PrinterError
from stoqdrivers.translation import stoqdrivers_gettext
from stoqdrivers.utils import str2bytes, bytes2str

    # open the drawer, and also check its status. Some models, for instance,
    # the Radiant drawers, use inverted logic to describe whether they are
    # open, specified by this attribute, settable via BaseDevice config.
    inverted_drawer = False

    def __init__(self, port):
        self.set_port(port)

    def set_port(self, port):
        self._port = port

    def get_port(self):
        return self._port

    def fileno(self):
        return self._port.fileno()

    def writeline(self, data):
        self.write(self.CMD_PREFIX + data + self.CMD_SUFFIX)
        return self.readline()

    def write(self, data):
        # pyserial is expecting bytes but we work with str in stoqdrivers
        data = str2bytes(data)
        log.debug(">>> %r (%d bytes)" % (data, len(data)))
        self._port.write(data)

    def read(self, n_bytes):
        # stoqdrivers is expecting str but pyserial will reply with bytes
        data = self._port.read(n_bytes)

        return bytes2str(data)