# Path: stoqdrivers/interfaces.py
# class INonFiscalPrinter(IDevice):
#     """ Interface used to formatting texts in non fiscal printers.
#     """
# 
#     max_characters = Attribute("The maximum characters per line")
# 
#     def centralize():
#         """ Centralize the text to be sent to coupon. """
# 
#     def descentralize():
#         """ Descentralize the text to be sent to coupon. """
# 
#     def set_bold():
#         """ The sent text will be appear in bold. """
# 
#     def unset_bold():
#         """ Remove the bold option. """
# 
#     def print_line(data):
#         """ Performs a line break to the given text. """
# 
#     def print_inline(data):
#         """ Print a given text in a unique line. """
# 
#     def print_barcode(code):
#         """ Print a barcode representing the given code. """
# 
#     def print_qrcode(code):
#         """ Print a qrcode representing the given code. """
# 
#     def cut_paper():
#         """ Performs a paper cutting. """
# 
# Path: stoqdrivers/serialbase.py
# class SerialBase(object):
# 
#     # All commands will have this prefixed
#     CMD_PREFIX = '\x1b'
#     CMD_SUFFIX = ''
# 
#     # used by readline()
#     EOL_DELIMIT = '\r'
# 
#     # Most serial printers allow connecting a cash drawer to them. You can then
#     # 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)
# 
#     def readline(self):
#         out = ''
#         a = 0
#         retries = 10
#         while True:
#             if a > retries:
#                 raise DriverError(_("Timeout communicating with fiscal "
#                                     "printer"))
# 
#             c = self.read(1)
#             if not c:
#                 a += 1
#                 print('take %s' % a)
#                 continue
#             a = 0
#             if c == self.EOL_DELIMIT:
#                 log.debug('<<< %r' % out)
#                 return out
#             out += c
# 
#     def open(self):
#         if not self._port.is_open:
#             self._port.open()
# 
#     def close(self):
#         if self._port.is_open:
#             # Flush whaterver is pending to write, since port.close() will close it
#             # *imediatally*, losing what was pending to write.
#             self._port.flush()
#             self._port.close()


from zope.interface import implementer
from stoqdrivers.interfaces import INonFiscalPrinter
from stoqdrivers.serialbase import SerialBase
##
## You should have received a copy of the GNU Lesser General Public License
## along with this program; if not, write to the Free Software
## Foundation, Inc., or visit: http://www.gnu.org/.
##
## Author(s): Stoq Team <stoq-devel@async.com.br>
##



ESC = '\x1b'
SI = '\x0f'
DC2 = '\x12'

NORMAL_FONT = ESC + '\x21\x00\x00'

CONDENSED_MODE = ESC + SI + '\x00'
NORMAL_MODE = DC2

CENTRALIZE = ESC + 'j\x01'
DESCENTRALIZE = ESC + 'j\x00'

DOUBLE_HEIGHT_ON = ESC + 'w\x01'
DOUBLE_HEIGHT_OFF = ESC + 'w\x00'

SET_BOLD = ESC + 'E'
UNSET_BOLD = ESC + 'F'


@implementer(INonFiscalPrinter)

# Path: stoqdrivers/interfaces.py
# class INonFiscalPrinter(IDevice):
#     """ Interface used to formatting texts in non fiscal printers.
#     """
# 
#     max_characters = Attribute("The maximum characters per line")
# 
#     def centralize():
#         """ Centralize the text to be sent to coupon. """
# 
#     def descentralize():
#         """ Descentralize the text to be sent to coupon. """
# 
#     def set_bold():
#         """ The sent text will be appear in bold. """
# 
#     def unset_bold():
#         """ Remove the bold option. """
# 
#     def print_line(data):
#         """ Performs a line break to the given text. """
# 
#     def print_inline(data):
#         """ Print a given text in a unique line. """
# 
#     def print_barcode(code):
#         """ Print a barcode representing the given code. """
# 
#     def print_qrcode(code):
#         """ Print a qrcode representing the given code. """
# 
#     def cut_paper():
#         """ Performs a paper cutting. """
# 
# Path: stoqdrivers/serialbase.py
# class SerialBase(object):
# 
#     # All commands will have this prefixed
#     CMD_PREFIX = '\x1b'
#     CMD_SUFFIX = ''
# 
#     # used by readline()
#     EOL_DELIMIT = '\r'
# 
#     # Most serial printers allow connecting a cash drawer to them. You can then
#     # 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)
# 
#     def readline(self):
#         out = ''
#         a = 0
#         retries = 10
#         while True:
#             if a > retries:
#                 raise DriverError(_("Timeout communicating with fiscal "
#                                     "printer"))
# 
#             c = self.read(1)
#             if not c:
#                 a += 1
#                 print('take %s' % a)
#                 continue
#             a = 0
#             if c == self.EOL_DELIMIT:
#                 log.debug('<<< %r' % out)
#                 return out
#             out += c
# 
#     def open(self):
#         if not self._port.is_open:
#             self._port.open()
# 
#     def close(self):
#         if self._port.is_open:
#             # Flush whaterver is pending to write, since port.close() will close it
#             # *imediatally*, losing what was pending to write.
#             self._port.flush()
#             self._port.close()


# Path: stoqdrivers/printers/daruma/DR700.py
from zope.interface import implementer
from stoqdrivers.interfaces import INonFiscalPrinter
from stoqdrivers.serialbase import SerialBase

##
## You should have received a copy of the GNU Lesser General Public License
## along with this program; if not, write to the Free Software
## Foundation, Inc., or visit: http://www.gnu.org/.
##
## Author(s): Stoq Team <stoq-devel@async.com.br>
##



ESC = '\x1b'
SI = '\x0f'
DC2 = '\x12'

NORMAL_FONT = ESC + '\x21\x00\x00'

CONDENSED_MODE = ESC + SI + '\x00'
NORMAL_MODE = DC2

CENTRALIZE = ESC + 'j\x01'
DESCENTRALIZE = ESC + 'j\x00'

DOUBLE_HEIGHT_ON = ESC + 'w\x01'
DOUBLE_HEIGHT_OFF = ESC + 'w\x00'

SET_BOLD = ESC + 'E'
UNSET_BOLD = ESC + 'F'


@implementer(INonFiscalPrinter)

class DR700(SerialBase):