def on_start(self): print(self.start_message) pos_txt = CheckParser.read_init_pos() end_pos = CheckParser.get_file_end_pos() if (end_pos - pos_txt) > 0: print(self.reprint) user_ans = raw_input() try: assert isinstance(user_ans, str), "Bad user input" except AssertionError as e: check_logger.debug("{0}: {1}".format(e, user_ans)) print(self.bad_input) if user_ans == 'Y' or user_ans == 'y': try: check = CheckParser.CheckParser(pos_txt) check.read_file() CheckParser.write_init_pos(end_pos) except Exception as e: print(self.user_err_msg) check_logger.debug(e) else: CheckParser.write_init_pos(end_pos) print("Bonurile existente au fost omise") print("Omitere -> Status: OK")
def execute_batch_file(): """ This method spawns a process that executes commands from a batch file, thus starting the FP driver. """ batch_file_path = r"C:\Listener\start.bat" check_logger.debug("{0}: {1}".format(stack()[0][3], "execute batch file")) print_job = Popen(batch_file_path, shell=False) # spawn a process that starts the FP driver stdout, stderr = print_job.communicate()
def on_modified(self, event): try: start_pos = CheckParser.read_init_pos() check = CheckParser.CheckParser(start_pos) sleep(1) check.read_file() CheckParser.write_init_pos(check.position) except Exception as e: print(self.user_err_msg) check_logger.debug(e)
def read_init_pos(): """ This function reads initial position of the source file cursor """ pos_filepath = r"C:\Vectron\pos.txt" with open(pos_filepath, "r") as fp: init_pos = fp.readline() check_logger.debug("{0}: {1}".format(stack()[0][3], init_pos)) if init_pos == '': init_pos = 0 return int(init_pos)
def execute_batch_file(): """ This method spawns a process that executes commands from a batch file, thus starting the FP driver. """ batch_file_path = r"C:\Listener\start.bat" check_logger.debug("{0}: {1}".format(stack()[0][3], "execute batch file")) print_job = Popen( batch_file_path, shell=False) # spawn a process that starts the FP driver stdout, stderr = print_job.communicate()
def get_file_end_pos(): """ This functions tells the EOF value, used when the program starts to check if the EOF is different from the init position of the source file cursor. If there is a difference this means there are unprocessed(and unprinted) checks :return: """ filename = r"C:\Vectron\VPosPC\files.txt" with open(filename, "r") as ff: ff.seek(0, 2) check_logger.debug("{0}: {1}".format(stack()[0][3], ff.tell())) return int(ff.tell())
def generate_new_check(self): check_logger.debug("{0}: {1}".format( stack()[0][3], "starting creation of products and payment")) for elem in self.check_data: check_logger.debug("{0}: {1}".format(stack()[0][3], elem)) backup_list = list(filter(lambda x: x != '', elem.split(' '))) price = CheckParser.get_field_value('\d+\,\d+', elem, backup_list, 5, ',', '', 8) decimals = "2" quantity = CheckParser.get_field_value('\d+', elem, backup_list, 0, ',', '', 6) + "000" tva = CheckParser.get_field_value('\d{1,2}%', elem, backup_list, 6, '%', '') tva = CheckParser.tva_by_time(tva) subgroup = "1" group = "1" prod_name = CheckParser.get_field_value( '[a-zA-Z]{2,}[\S\s]?[a-zA-Z]*[\S\s]?[a-zA-Z]*', elem, backup_list, 3) # Information that is written to the target file is composed accordingly to a special grammar # required by the FP driver. # '*'<product name><padding until 26 index><price|decimals><quantity><tva><subgroup><group> final_check = '*' + prod_name + " " * (24 - len(prod_name)) + price + decimals + quantity + \ tva + subgroup + group + '\n' check_logger.debug("{0}: {1}".format(stack()[0][3], final_check)) self.check_to_print.append(final_check) self.payment_method() check_logger.debug("{0}: {1}".format( stack()[0][3], "finished creation of products and payment"))
def write_init_pos(pos): """ This function writes the value of the source file cursor :param pos: source file cursor value """ if not isinstance(pos, str): try: pos = str(pos) except (ValueError, TypeError): pos = "0" pos_filepath = r"C:\Vectron\pos.txt" check_logger.debug("{0}: {1}".format(stack()[0][3], pos)) with open(pos_filepath, "w") as fp: fp.write(pos)
def write_2_file(to_print): """ This method writes selected and processed information from the source file to the target file. :param to_print: information to be written into the target file """ header_line = "KARAT\n" # header of the target file, needed by the FP driver footer_line = "T0000010000 TOTAL\nEND KARAT\n" # footer of the target file file_bon = r"C:\Listener\bon.txt" check_logger.debug("{0}: {1}".format(stack()[0][3], to_print)) with open(file_bon, "w") as fp: fp.write(header_line) for item in to_print: fp.write(item) fp.write(footer_line) print("Tiparire -> Status: OK!")
def generate_new_check(self): check_logger.debug("{0}: {1}".format(stack()[0][3], "starting creation of products and payment")) for elem in self.check_data: check_logger.debug("{0}: {1}".format(stack()[0][3], elem)) backup_list = list(filter(lambda x: x != '', elem.split(' '))) price = CheckParser.get_field_value('\d+\,\d+', elem, backup_list, 5, ',', '', 8) decimals = "2" quantity = CheckParser.get_field_value('\d+', elem, backup_list, 0, ',', '', 6) + "000" tva = CheckParser.get_field_value('\d{1,2}%', elem, backup_list, 6, '%', '') tva = CheckParser.tva_by_time(tva) subgroup = "1" group = "1" prod_name = CheckParser.get_field_value('[a-zA-Z]{2,}[\S\s]?[a-zA-Z]*[\S\s]?[a-zA-Z]*', elem, backup_list, 3) # Information that is written to the target file is composed accordingly to a special grammar # required by the FP driver. # '*'<product name><padding until 26 index><price|decimals><quantity><tva><subgroup><group> final_check = '*' + prod_name + " " * (24 - len(prod_name)) + price + decimals + quantity + \ tva + subgroup + group + '\n' check_logger.debug("{0}: {1}".format(stack()[0][3], final_check)) self.check_to_print.append(final_check) self.payment_method() check_logger.debug("{0}: {1}".format(stack()[0][3], "finished creation of products and payment"))
def read_file(self): """ This method extracts needed information from a check written by the POS system into the source file(self.filename). Extraction(selection) operation is done according to the check grammar, all products with information about their quantity, price, vat etc will be embraced by two lines containing only the '*' character. After the last (products) delimiter line, the payment method(s) and total amount information will follow. """ check_logger.debug("{0}: {1}".format(stack()[0][3], "_____START_____")) with open(self.filename, "rb") as fh: check_logger.debug("{0}: {1}".format(stack()[0][3], "file.txt opened")) fh.seek(self.position) # move file cursor to start position, start of source file or line = fh.readline() # end of the last read check check_logger.debug("{0}: {1}".format(stack()[0][3], line)) delimiter = "**************************************************" while line: # Read source file lines until EOF if delimiter in line: line = fh.readline() check_logger.debug("{0}: {1}".format(stack()[0][3], line)) while delimiter not in line: # Until the second delimiter line is not encountered self.check_data.append(line) line = fh.readline() check_logger.debug("{0}: {1}".format(stack()[0][3], line)) line = fh.readline() check_logger.debug("{0}: {1}".format(stack()[0][3], line)) if "Cash" in line: # Save the line where cash payment method is present self.cash = line if "Plata card" in line: # Save the line where card payment method is present self.card = line if "= Cut =" in line and self.check_data != []: # Got to end of current check check_logger.debug("{0}: {1}".format(stack()[0][3], self.check_data)) self.generate_new_check() # Use extracted information to generate new information for FP check_queue.put(self.check_to_print) self.check_to_print = [] self.check_data = [] self.position = fh.tell() # Save the position of the EOF check_logger.debug("{0}: {1}".format(stack()[0][3], "_____END_____"))
def read_file(self): """ This method extracts needed information from a check written by the POS system into the source file(self.filename). Extraction(selection) operation is done according to the check grammar, all products with information about their quantity, price, vat etc will be embraced by two lines containing only the '*' character. After the last (products) delimiter line, the payment method(s) and total amount information will follow. """ check_logger.debug("{0}: {1}".format(stack()[0][3], "_____START_____")) with open(self.filename, "rb") as fh: check_logger.debug("{0}: {1}".format(stack()[0][3], "file.txt opened")) fh.seek( self.position ) # move file cursor to start position, start of source file or line = fh.readline() # end of the last read check check_logger.debug("{0}: {1}".format(stack()[0][3], line)) delimiter = "**************************************************" while line: # Read source file lines until EOF if delimiter in line: line = fh.readline() check_logger.debug("{0}: {1}".format(stack()[0][3], line)) while delimiter not in line: # Until the second delimiter line is not encountered self.check_data.append(line) line = fh.readline() check_logger.debug("{0}: {1}".format( stack()[0][3], line)) line = fh.readline() check_logger.debug("{0}: {1}".format(stack()[0][3], line)) if "Cash" in line: # Save the line where cash payment method is present self.cash = line if "Plata card" in line: # Save the line where card payment method is present self.card = line if "= Cut =" in line and self.check_data != []: # Got to end of current check check_logger.debug("{0}: {1}".format( stack()[0][3], self.check_data)) self.generate_new_check( ) # Use extracted information to generate new information for FP check_queue.put(self.check_to_print) self.check_to_print = [] self.check_data = [] self.position = fh.tell() # Save the position of the EOF check_logger.debug("{0}: {1}".format(stack()[0][3], "_____END_____"))