def move_file(self, src_path, current_bill, new_line): os.rename(src_path, current_bill.move_path) # get's the path without the file name move_path = current_bill.move_path.split(self.destination_base_folder + "/")[1] move_path = move_path.split("/" + current_bill.file_name)[0] write_log("Moved \"{0}\" to \"{1}\".{2}".format(current_bill.file_name, move_path, new_line))
def get_bill_number(file_name, split_char): values = file_name.split(split_char) try: return int(values[3]) except IndexError: write_log("\tDer Filename: \"{0}\" entspricht nicht der formatierung.". format(file_name)) return None
def add_dict_json(bill, open_bills, dict_key, bill_type): open_bills[dict_key].append({ "company_name": bill.company_name, "month": bill.month, "year": bill.year, "file_name": bill.file_name, "file_path": bill.move_path, "description": bill.description, "date_of_issue": bill.date_of_issue }) write_log("\tAdd {0} open Bill \"{1}\" from {2}.".format( bill_type, bill.file_name, bill.company_name))
def copy_file(current_bill, copy_path): copy2(current_bill.move_path, copy_path) write_log("Copied \"{0}\" to \"{1}\".\n".format(current_bill.file_name, copy_path))
def handle_bill_move(event): # only rename if it is not a directory if not os.path.isdir(event.src_path) and os.path.exists(event.src_path): # checks if the file is moved completely start_move = check_file_complete(event.src_path) # if it file is complete it can be moved if start_move: bill = BillItem.Bill(bill_prefix, own_bill_unique, outgoing_bills_folder, incoming_bills_folder, open_bills_folder) # get's the file name, type an the name without the file type file_name, file_type, file_name_without_ending = get_file_name_type( event.src_path) format_okay = BillItem.check_file_name_format( file_name_without_ending) if format_okay: # get's the creation date of the path bill.file_type = file_type # get's the values of the file name bill.set_bill_values(file_name_without_ending, event.src_path) # file name in the right format to extract data bill.file_name = file_name move_bill.create_move_path(bill) # get's the bill number for the incoming bill if not bill.outgoing: bill.bill_number = RenameBill.get_next_bill_number(bill) # renames the bills in the correct format RenameBill.rename_file(bill) file_name_existing = RenameBill.check_file_name_existing(bill) if not file_name_existing: # check's if the bill in unpaid and in this case add's it to the json file check_add_open_bill(bill) bill.move_path = os.path.join(bill.move_path, bill.file_name) # moves the file and copy's it if the file is outgoing if bill.outgoing: move_bill.move_file(event.src_path, bill, "") MoveBill.copy_file(bill, copy_path) # increases the sequential number and writes it to the file BillItem.write_sequential_number( bill.year, bill.sequential_numbers_list) else: move_bill.move_file(event.src_path, bill, "\n") return True else: write_log( "\tDie Rechnung mit dem Namen \"{0}\" existiert bereits, " "daher wurde sie nicht verschoben.".format( bill.file_name)) return False else: write_log( "\tDer Filename: \"{0}\" entspricht nicht der formatierung." .format(file_name)) return False else: return False else: return False
class MyHandler(FileSystemEventHandler): file_format_correct = True # file has been created def on_created(self, event): if not os.path.isdir(event.src_path): self.file_format_correct = handle_bill_move(event) # if a file is modified def on_modified(self, event): if self.file_format_correct and not os.path.isdir(event.src_path): self.file_format_correct = handle_bill_move(event) event_handler = MyHandler() # starting the observer and keep it running until you enter "control + c" observer = Observer() observer.schedule(event_handler, track_folder, recursive=True) observer.start() write_log("Watcher started!") try: while True: time.sleep(10) except KeyboardInterrupt: observer.stop() observer.join()