Browse Source

oh no, encoding problemz

english-french
Votre Nom 6 years ago
parent
commit
c29e0654ea
  1. 4
      escpos.py
  2. 68
      printer.py
  3. 7
      start.py

4
escpos.py

@ -5,7 +5,9 @@
escpos = {
"init_printer": "\x1B\x40",
"select_international_character_set": "\x1B\x52\x00",
"select_INT_character_set": "\x1B\x52\x00",
"select_FR_character_set": "\x1B\x52\x01",
"select_character_code_table": "\x1B\x74\x28",
"justify_left": "\x1B\x61\x00",
"justify_center": "\x1B\x61\x01",

68
printer.py

@ -11,58 +11,69 @@ import nltk
from nltk.tokenize import RegexpTokenizer
tokenizer = RegexpTokenizer(r'\w+') # initialize tokenizer
try:
printerpath = '/dev/usb/lp0'
stdout = open(printerpath, 'w')
connected = True
print('*receipt printer connected*')
except:
stdout = sys.__stdout__
connected = False
print('*no receipt printer connected*')
connected = True
def char_swap(some_string):
swaps = [('','-'), ('','"'),('','"'),('ù','u'), ("","'"), ('à','a'), ('â','a'),('é','e'),('è','e'),('î','i')]
for swap in swaps:
some_string = some_string.replace(swap[0], swap[1])
#print(swap, some_string)
return some_string
def insert_linebreaks(string, linelength):
words = tokenizer.tokenize(string)
count = 0
tmp = ''
new = ''
for word in words:
length = len(word)
count += len(word)
if word == words[-1]:
tmp += word
new += tmp
elif count <= linelength:
tmp += word + ' '
for x, ch in enumerate(string):
tmp_ch = []
if ch == ' ':
count = count + 1
if count < linelength:
tmp += ' '
else:
new += tmp + '\n'
tmp = ''
count = 0
elif x+1 == len(string):
new += tmp + ch
else:
new += tmp + '\n'
tmp = ''
count = 0
tmp += ch
count += 1
#print('string <<<', string)
#print('new >>>', new)
return new
def printNow(query, results, results_count):
if connected == True:
printerpath = '/dev/usb/lp1'
stdout = open(printerpath, 'w')
#print('printingNow')
print(escpos['reset'], file=stdout, flush=True)
print(escpos['init_printer'], file=stdout, flush=True)
# print(escpos['select_international_character_set'], file=stdout, flush=True)
#print(escpos['select_INT_character_set'], file=stdout, flush=True)
#print(escpos['select_character_code_table'], file=stdout, flush=True)
print(escpos['margin_left'], file=stdout, flush=True)
print(escpos['justify_center'], file=stdout, flush=True)
printed = False
if results_count == None:
if results_count == None or results_count == 'None':
results_count = 0
results_count_max = results_count + 5
current_count = 0
for x, document in results.items():
for n, sentence in enumerate(document['matches']):
print('current_count', current_count)
#print('current_count', current_count)
if current_count == results_count:
printed = True
if results_count < results_count_max:
print('results_count', results_count)
#print('results_count', results_count)
results_count += 1
sentence = str(sentence) # Turn Flask Markup object back to normal python string
print(type(sentence))
sentence = char_swap(sentence)
query = char_swap(query)
sentence = sentence.replace('\n', ' ').replace(' ', ' ')
pattern = r'[\s\W\_]'+query+r'[\s\W\_\n]|^'+query+'|'+query+'$'
match = re.search(pattern, sentence, flags=re.IGNORECASE)
@ -85,12 +96,12 @@ def printNow(query, results, results_count):
// '''
print(linebreak, file=stdout, flush=True) # print line break ascii art
print('\n', file=stdout, flush=True)
title = insert_linebreaks(document['name'], 40)
print(title, file=stdout, flush=True) # print the document name
title = insert_linebreaks(document['name'], 13)
print(escpos['largefont'], title, escpos['normalfont'], file=stdout, flush=True) # print the document name
print('\n', file=stdout, flush=True)
# start of sentence
part = insert_linebreaks(part, 40)
part = insert_linebreaks(part, 35)
print(part, file=stdout, flush=True) # print current part of sentence
if i + 1 != len(sentence_splitted): # if this part is not the last one
@ -116,7 +127,6 @@ def printNow(query, results, results_count):
return results_count
# query = 'noise'
# results, _, _ = readings.request_results(query)
# pprint(results)

7
start.py

@ -45,7 +45,7 @@ def index_lang(lang):
if printer.connected == True:
connection = 'connected'
else:
connection = 'disconnected'
connection = 'connected'
# Check if print command
if request.args.get('results_count', ''):
@ -53,17 +53,14 @@ def index_lang(lang):
print('results_count', results_count)
else:
results_count = 0
if request.args.get('q', ''):
results, filenames, analytics = readings.request_results(query)
# print commands
if request.args.get('print', '') == 'now':
if connection == 'connected':
print('*print!*', file=stdout)
print('results_count (before)', results_count)
results_count = printer.printNow(query, results, results_count)
print('results_count (after)', results_count)
return flask.render_template(lang+'/results.html', query=query, results=results, filenames=filenames, connection=connection, suggestions=suggestions, analytics=analytics, lang=lang, results_count=results_count)

Loading…
Cancel
Save