Last active
July 22, 2022 08:52
-
-
Save aflaag/aeb05b9b84edc1bbb221fb75b74904a0 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import sys | |
| HEADER_LINES = 7 | |
| PADDING_MAP = { | |
| "CodiceLRT": 0, | |
| "Part Name": 0, | |
| "CodiceFornitore": 0, | |
| "Descrizione": 0, | |
| "Valore": 0, | |
| "Tol.": 0, | |
| "Pkg": 0, | |
| "Fornitore": 0, | |
| "Qty": 0, | |
| "Ref": 0, | |
| } | |
| def find_first_char(line, starting_point): | |
| current_char = " " | |
| while current_char == " ": | |
| current_char = line[starting_point] | |
| starting_point += 1 | |
| starting_point -= 1 | |
| return starting_point | |
| def rows_to_fix(chunk): | |
| for i, line in enumerate(chunk): | |
| if line[0] == "=": | |
| return i | |
| def fix_column_padding(file_lines): | |
| rows_number = rows_to_fix(file_lines[HEADER_LINES:]) | |
| unpiped_rows = [[part for part in line.split("|") if part != ""] for line in file_lines[HEADER_LINES:HEADER_LINES + rows_number]] | |
| for i, key in enumerate(PADDING_MAP.keys()): | |
| PADDING_MAP[key] = len(max([line[i] for line in unpiped_rows], key=lambda line: len(line))) | |
| for y in range(rows_number): | |
| for x, key in enumerate(PADDING_MAP.keys()): | |
| unpiped_rows[y][x] += " " * (PADDING_MAP[key] - len(unpiped_rows[y][x])) | |
| file_lines[y + HEADER_LINES] = "|" + "|".join(unpiped_rows[y]) + "|" | |
| def patch(input_file, output_file): | |
| try: | |
| input_file_handler = open(input_file) | |
| except: | |
| print("The given file does not exist. (If the filename contains spaces, please add quotes).") | |
| return False | |
| file_lines = input_file_handler.read().split("\n") | |
| input_file_handler.close() | |
| i = 0 | |
| while i < len(file_lines): | |
| curr_line = file_lines[i] | |
| if curr_line.startswith("| >"): | |
| starting_point, prefix = (3, "") if curr_line[3] != "|" else (4, " | ") | |
| first_char = find_first_char(curr_line, starting_point) | |
| file_lines[i - 1] += prefix + curr_line[first_char:] | |
| file_lines.pop(i) | |
| else: | |
| i += 1 | |
| fix_column_padding(file_lines) | |
| output_file = open(output_file, "w") | |
| output_file.write("\n".join(file_lines)) | |
| output_file.close() | |
| return True | |
| def evaluate_output_filename(input_filename): | |
| output_filename = input_filename + "_patched" | |
| if "." in input_filename: | |
| input_filname_parts = input_filename.split(".") | |
| name = input_filname_parts[0] | |
| ext = input_filname_parts[1] | |
| output_filename = name + "_patched." + ext | |
| return output_filename | |
| def main(): | |
| len_sysargv = len(sys.argv) | |
| show_error = True | |
| if len_sysargv < 2: | |
| print("Missing commands.") | |
| elif sys.argv[1] == "-h" or sys.argv[1] == "--help": | |
| print(" Usage:") | |
| print(" bom_patcher.py [command] [options]") | |
| print("") | |
| print(" Commands:") | |
| print(" -h | --help: show this help message") | |
| print(" -p | --patch [input_file]: patch some given input file (default output filename is '`input_file`_patched.txt')") | |
| print(" -o | --output [output_file]: choose the output filename (must used after --patch)") | |
| print("") | |
| print(" Example usage:") | |
| print(" bom_patcher.py -p my_file.txt -o out_file.txt") | |
| show_error = False | |
| elif sys.argv[1] == "-p" or sys.argv[1] == "--patch": | |
| if len_sysargv < 3: | |
| print("Missing input file.") | |
| else: | |
| input_filename = sys.argv[2] | |
| output_filename = evaluate_output_filename(input_filename) | |
| default = True | |
| if len_sysargv > 3: | |
| if sys.argv[3] == "-o" or sys.argv[3] == "--output": | |
| if len_sysargv < 5: | |
| print("Missing output file.") | |
| else: | |
| output_filename = sys.argv[4] | |
| default = False | |
| else: | |
| print("Invalid command.") | |
| if default: | |
| print("Using default output filename.") | |
| saved = patch(input_filename, output_filename) | |
| if saved: | |
| print("Successfully saved the patched file in '" + output_filename + "'.") | |
| show_error = False | |
| else: | |
| show_error = True | |
| else: | |
| print("Unknown command.") | |
| if show_error: | |
| print("Run -h or --help to see the supported commands.") | |
| if __name__ == "__main__": | |
| main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment