# 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
        # specify its options, but right now I think that's ok, since these
        # options are common to most of the drivers.
        Serial.__init__(self, device, baudrate=baudrate, bytesize=EIGHTBITS,
                        parity=PARITY_NONE, stopbits=STOPBITS_ONE, timeout=3,
                        write_timeout=3)
        self.setDTR(True)
        self.flushInput()
        self.flushOutput()


# When we are printing NFCe danfe there is a hack that forces the reinitialization of the printer.
# So this variable is used to check if we need to create a new socket or reuse the previous one.
active_device = None


class EthernetPort:
    def __init__(self, address, port):
        self.address = address
        self.port = port
        self._get_or_create_connection()

    def _get_or_create_connection(self):
        self.device = active_device or self._create_connection()

    def _create_connection(self):
        global active_device
        try:
            active_device = self.device = socket.create_connection(
                (self.address, self.port), timeout=5)
        except OSError:

# 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

        # specify its options, but right now I think that's ok, since these
        # options are common to most of the drivers.
        Serial.__init__(self, device, baudrate=baudrate, bytesize=EIGHTBITS,
                        parity=PARITY_NONE, stopbits=STOPBITS_ONE, timeout=3,
                        write_timeout=3)
        self.setDTR(True)
        self.flushInput()
        self.flushOutput()


# When we are printing NFCe danfe there is a hack that forces the reinitialization of the printer.
# So this variable is used to check if we need to create a new socket or reuse the previous one.
active_device = None


class EthernetPort:
    def __init__(self, address, port):
        self.address = address
        self.port = port
        self._get_or_create_connection()

    def _get_or_create_connection(self):
        self.device = active_device or self._create_connection()

    def _create_connection(self):
        global active_device
        try:
            active_device = self.device = socket.create_connection(
                (self.address, self.port), timeout=5)
        except OSError:

            raise PrinterError