import time import os # library to use bash commands import keyboard def preview_output(preview_button_count, last_module): #open last_module.ps, read its content and save it as print.ps fileInput = open(last_module + ".ps", "rt") # rt = read text mode #output file to write the result to fileOutput = open("print.ps", "wt") # wt = write text mode # read the input file, search for a patterns, replace it with new patterns (that take the values from the input sensor) # uses regular expressions library (re) content = fileInput.read() # write the new text to the output file fileOutput.write(content) #close input and output files fileInput.close() fileOutput.close() #on first button press: open preview if preview_button_count == 1: print("start preview of print.ps") os.system("gv -fullscreen print.ps &") # open file in ghostscript viewer in fullscreen mode #on additional button press: refresh preview elif preview_button_count >= 2: print("refresh preview of print.ps") keyboard.send(".") # refresh file that is open in ghostscript viewer def print_output(last_module): #open last_module.ps, read its content and save it as print.ps fileInput = open(last_module + ".ps", "rt") # rt = read text mode #output file to write the result to fileOutput = open("print.ps", "wt") # wt = write text mode # read the input file, search for a patterns, replace it with new patterns (that take the values from the input sensor) # uses regular expressions library (re) content = fileInput.read() # write the new text to the output file fileOutput.write(content) #close input and output files fileInput.close() fileOutput.close() #on button press: send to printer print("send print.ps to printer") os.system("lp print.ps") # lp is the bash print command