forked from paxapos/fiscalberry
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathComandoInterface.py
More file actions
164 lines (140 loc) · 4.74 KB
/
ComandoInterface.py
File metadata and controls
164 lines (140 loc) · 4.74 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
# -*- coding: iso-8859-1 -*-
from ConectorDriverComando import ConectorDriverComando
import unicodedata
import importlib
import logging
import string
import types
from array import array
from past.builtins import unicode
class ValidationError(Exception):
pass
class FiscalPrinterError(Exception):
pass
class ComandoException(RuntimeError):
pass
def valid_utf8_bytes(s):
"""
"""
if isinstance(s, unicode):
s = s.encode('utf-8')
bytearray = array('B', s)
return str_skip_bytes(s, invalid_utf8_indexes(bytearray))
def str_skip_bytes(s, dels):
"""
"""
if not dels:
return s
return ''.join(c for i, c in enumerate(s) if i not in dels)
def invalid_utf8_indexes(bytes):
"""
"""
skips = []
i = 0
len_bytes = len(bytes)
while i < len_bytes:
c1 = bytes[i]
if c1 < 0x80:
# U+0000 - U+007F - 7 bits
i += 1
continue
try:
c2 = bytes[i + 1]
if ((c1 & 0xE0 == 0xC0) and (c2 & 0xC0 == 0x80)):
# U+0080 - U+07FF - 11 bits
c = (((c1 & 0x1F) << 6) |
(c2 & 0x3F))
if c < 0x80:
# Overlong encoding
skips.extend([i, i + 1])
i += 2
continue
c3 = bytes[i + 2]
if ((c1 & 0xF0 == 0xE0) and
(c2 & 0xC0 == 0x80) and
(c3 & 0xC0 == 0x80)):
# U+0800 - U+FFFF - 16 bits
c = (((((c1 & 0x0F) << 6) |
(c2 & 0x3F)) << 6) |
(c3 & 0x3f))
if ((c < 0x800) or (0xD800 <= c <= 0xDFFF)):
# Overlong encoding or surrogate.
skips.extend([i, i + 1, i + 2])
i += 3
continue
c4 = bytes[i + 3]
if ((c1 & 0xF8 == 0xF0) and
(c2 & 0xC0 == 0x80) and
(c3 & 0xC0 == 0x80) and
(c4 & 0xC0 == 0x80)):
# U+10000 - U+10FFFF - 21 bits
c = (((((((c1 & 0x0F) << 6) |
(c2 & 0x3F)) << 6) |
(c3 & 0x3F)) << 6) |
(c4 & 0x3F))
if (c < 0x10000) or (c > 0x10FFFF):
# Overlong encoding or invalid code point.
skips.extend([i, i + 1, i + 2, i + 3])
i += 4
continue
except IndexError:
pass
skips.append(i)
i += 1
return skips
def formatText(text):
text = valid_utf8_bytes(text)
text = text.replace('á', 'a')
text = text.replace('é', 'e')
text = text.replace('í', 'i')
text = text.replace('ó', 'o')
text = text.replace('ú', 'u')
text = text.replace('Á', 'A')
text = text.replace('É', 'E')
text = text.replace('Í', 'I')
text = text.replace('Ó', 'O')
text = text.replace('Ú', 'U')
text = text.replace('Ä', 'A')
text = text.replace('Ë', 'E')
text = text.replace('Ï', 'I')
text = text.replace('Ö', 'O')
text = text.replace('Ü', 'U')
text = text.replace('ä', 'a')
text = text.replace('ë', 'e')
text = text.replace('ï', 'i')
text = text.replace('ö', 'o')
text = text.replace('ü', 'u')
text = text.replace('ñ', 'n')
text = text.replace('Ñ', 'N')
text = text.replace('\\', ' ')
text = text.replace('\'', ' ')
text = text.replace('º', ' ')
text = text.replace('"', ' ')
text = text.replace('|', ' ')
text = text.replace('¿', ' ')
text = text.replace('¡', ' ')
text = text.replace('ª', ' ')
return text
class ComandoInterface:
"""Interfaz que deben cumplir las impresoras fiscales."""
DEFAULT_DRIVER = None
def __init__(self, *args, **kwargs):
self.model = kwargs.pop("modelo", None)
driver = kwargs.pop("driver", self.DEFAULT_DRIVER)
# inicializo el driver
self.conector = ConectorDriverComando(self, driver, **kwargs)
# inicializo el traductor
traductorModule = importlib.import_module(self.traductorModule)
traductorClass = getattr(traductorModule, self.traductorModule[12:])
self.traductor = traductorClass(self, *args)
# seteo anchos de columnas
if hasattr(self.conector.driver, 'cols'):
self.total_cols = self.conector.driver.cols
self.price_cols = 12
self.cant_cols = 4
self.desc_cols = self.total_cols - self.cant_cols - self.price_cols
def _sendCommand(self, commandNumber, parameters, skipStatusErrors=False):
raise Exception("NotImplementedException")
def close(self):
"""Cierra la impresora"""
self.conector.close()