From ec1810933685c09c541b30d02ac278373a104994 Mon Sep 17 00:00:00 2001 From: manetta Date: Thu, 14 Mar 2019 13:19:48 +0100 Subject: [PATCH] some error preventing elements, for printing and searching --- printer.copy.py | 123 +++++++++++++++++++++++++++++ printer.py | 157 +++++++++++++++++++------------------- static/css/stylesheet.css | 11 +++ templates/en/base.html | 18 ++++- templates/en/results.html | 17 +++++ templates/fr/base.html | 18 ++++- templates/fr/results.html | 23 +++++- 7 files changed, 284 insertions(+), 83 deletions(-) create mode 100644 printer.copy.py diff --git a/printer.copy.py b/printer.copy.py new file mode 100644 index 0000000..d509aea --- /dev/null +++ b/printer.copy.py @@ -0,0 +1,123 @@ +#!/usr/bin/env python3 + +import readings +from pprint import pprint +import re, sys +from escpos import escpos +from sys import stdin, stderr, stdout +from time import sleep + +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*') + +def insert_linebreaks(string, linelength): + words = tokenizer.tokenize(string) + count = 0 + tmp = '' + new = '' + for x, word in enumerate(words): + length = len(word) + count += len(word) + if x + 1 == len(words): + tmp += word + new += tmp + elif count <= linelength: + tmp += word + ' ' + else: + new += tmp + '\n' + tmp = '' + count = 0 + return new + +def printNow(query, results, results_count): + if connected == True: + 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['margin_left'], file=stdout, flush=True) + print(escpos['justify_center'], file=stdout, flush=True) + + printed = False + + if 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) + if current_count == results_count: + printed = True + if results_count < results_count_max: + print('results_count', results_count) + results_count += 1 + sentence = str(sentence) # Turn Flask Markup object back to normal python string + sentence = sentence.replace('\n', ' ').replace(' ', ' ') + pattern = r'[\s\W\_]'+query+r'[\s\W\_\n]|^'+query+'|'+query+'$' + match = re.search(pattern, sentence, flags=re.IGNORECASE) + # print('match:', match) + if match: + match = match.group() + sentence_splitted = re.compile(match).split(sentence) # find the query in the sentence, and split the sentence up + # print('sentence splitted:', sentence_splitted) + for i, part in enumerate(sentence_splitted): + if n == 0: # first sentence of this document + if i == 0: # start of a new sentence + print('', file=stdout, flush=True) + linebreak = ''' ** + ** /** ** + //** /** ** + ************** +///**//**//**/ + ** /** //** + // /** // + // ''' + 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 + print('\n', file=stdout, flush=True) + + # start of sentence + part = insert_linebreaks(part, 40) + 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 + print(escpos['bold'], match, escpos['reset'], file=stdout, flush=True) # print query + + # line breaker after each sentence + if i + 1 == len(sentence_splitted): + print('\n | \n', file=stdout, flush=True) + sleep(3) + current_count += 1 + + print(escpos['paperfeed_1l'], file=stdout, flush=True) + print(escpos['paperfeed_1l'], file=stdout, flush=True) + print(escpos['paperfeed_1l'], file=stdout, flush=True) + sleep(3) + print(escpos['papercut'], file=stdout, flush=True) + print(escpos['reset'], file=stdout, flush=True) + + # reset when all results are printed + if printed == False: + results_count = 0 + # printNow(query, results, results_count) + + return results_count + + +query = 'noise' +results, _, _ = readings.request_results(query) +# pprint(results) +printNow(query, results, 0) \ No newline at end of file diff --git a/printer.py b/printer.py index bd801cf..8aea103 100755 --- a/printer.py +++ b/printer.py @@ -2,7 +2,7 @@ import readings from pprint import pprint -import re, sys +import re, sys, glob from escpos import escpos from sys import stdin, stderr, stdout from time import sleep @@ -11,14 +11,19 @@ import nltk from nltk.tokenize import RegexpTokenizer tokenizer = RegexpTokenizer(r'\w+') # initialize tokenizer - connected = True +def check_printer_path(): + printers = glob.glob('/dev/usb/lp*') + if printers: + return printers[0] + else: + return None + 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): @@ -40,53 +45,44 @@ def insert_linebreaks(string, linelength): else: 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_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 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) - if current_count == results_count: - printed = True - if results_count < results_count_max: - #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) - # print('match:', match) - if match: - match = match.group() - sentence_splitted = re.compile(match).split(sentence) # find the query in the sentence, and split the sentence up - # print('sentence splitted:', sentence_splitted) - for i, part in enumerate(sentence_splitted): - if n == 0: # first sentence of this document - if i == 0: # start of a new sentence - print('', file=stdout, flush=True) - linebreak = ''' ** + printerpath = check_printer_path() + try: + print_pointer = open(printerpath, 'w') if printerpath else sys.__stdout__ + print(escpos['reset'], file=print_pointer, flush=True) + print(escpos['init_printer'], file=print_pointer, flush=True) + print(escpos['margin_left'], file=print_pointer, flush=True) + print(escpos['justify_center'], file=print_pointer, flush=True) + + printed = False + + 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']): + if current_count == results_count: + printed = True + if results_count < results_count_max: + results_count += 1 + sentence = str(sentence) # Turn Flask Markup object back to normal python string + 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) + if match: + match = match.group() + sentence_splitted = re.compile(match).split(sentence) # find the query in the sentence, and split the sentence up + for i, part in enumerate(sentence_splitted): + if n == 0: # first sentence of this document + if i == 0: # start of a new sentence + print('', file=print_pointer, flush=True) + linebreak = ''' ** ** /** ** //** /** ** ************** @@ -94,38 +90,43 @@ 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'], 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, 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 - print(escpos['bold'], match, escpos['reset'], file=stdout, flush=True) # print query - - # line breaker after each sentence - if i + 1 == len(sentence_splitted): - print('\n | \n', file=stdout, flush=True) - sleep(3) - current_count += 1 - - print(escpos['paperfeed_1l'], file=stdout, flush=True) - print(escpos['paperfeed_1l'], file=stdout, flush=True) - print(escpos['paperfeed_1l'], file=stdout, flush=True) - sleep(3) - print(escpos['papercut'], file=stdout, flush=True) - print(escpos['reset'], file=stdout, flush=True) - - # reset when all results are printed - if printed == False: - results_count = 0 - # printNow(query, results, results_count) - - return results_count + print(linebreak, file=print_pointer, flush=True) # print line break ascii art + print('\n', file=print_pointer, flush=True) + title = insert_linebreaks(document['name'], 13) + print(escpos['largefont'], title, escpos['normalfont'], file=print_pointer, flush=True) # print the document name + print('\n', file=print_pointer, flush=True) + + # start of sentence + part = insert_linebreaks(part, 35) + print(part, file=print_pointer, flush=True) # print current part of sentence + + if i + 1 != len(sentence_splitted): # if this part is not the last one + print(escpos['bold'], match, escpos['reset'], file=print_pointer, flush=True) # print query + + # line breaker after each sentence + if i + 1 == len(sentence_splitted): + print('\n | \n', file=print_pointer, flush=True) + sleep(3) + current_count += 1 + + print(escpos['paperfeed_1l'], file=print_pointer, flush=True) + print(escpos['paperfeed_1l'], file=print_pointer, flush=True) + print(escpos['paperfeed_1l'], file=print_pointer, flush=True) + sleep(3) + print(escpos['papercut'], file=print_pointer, flush=True) + print(escpos['reset'], file=print_pointer, flush=True) + + # reset when all results are printed + if printed == False: + results_count = 0 + # printNow(query, results, results_count) + + return results_count + + except: + e = sys.exc_info()[0] + print("Could not print: {}".format(e)) + return 0 # query = 'noise' # results, _, _ = readings.request_results(query) diff --git a/static/css/stylesheet.css b/static/css/stylesheet.css index 35ab80f..7e1c195 100644 --- a/static/css/stylesheet.css +++ b/static/css/stylesheet.css @@ -77,6 +77,17 @@ i{ .blink{ animation: 2s linear 1s infinite blink; } +.blink a.printing{ + animation: 0.5s linear 0.5s infinite blink; +} +.blink a.printing:before, +.blink a.printing::before { + content: "printing ..."; + position: fixed; + top: 187px; + right: 299px; + font-size: 85%; +} #wrapper{ position: absolute; diff --git a/templates/en/base.html b/templates/en/base.html index 2299d39..7a15b5c 100644 --- a/templates/en/base.html +++ b/templates/en/base.html @@ -35,7 +35,23 @@
-
+ + {% endblock %} {% block nav %} diff --git a/templates/en/results.html b/templates/en/results.html index 9d9e6e3..1da381d 100644 --- a/templates/en/results.html +++ b/templates/en/results.html @@ -5,6 +5,23 @@ + {% endblock %} {% block results %} diff --git a/templates/fr/base.html b/templates/fr/base.html index 1850331..9afd89d 100644 --- a/templates/fr/base.html +++ b/templates/fr/base.html @@ -35,7 +35,23 @@
-
+ + {% endblock %} {% block nav %} diff --git a/templates/fr/results.html b/templates/fr/results.html index 352f1fe..d6bc02f 100644 --- a/templates/fr/results.html +++ b/templates/fr/results.html @@ -2,9 +2,26 @@ {% block title %}{{ query }} - cyber/technofeminist cross-readings{% endblock %} {% block nav %} - + + {% endblock %} {% block results %}