Implemented stl parser for parsing stl header files.
This commit is contained in:
		| @ -7,53 +7,106 @@ | |||||||
| import argparse as ap | import argparse as ap | ||||||
| import sys | import sys | ||||||
| from os.path import exists | from os.path import exists | ||||||
|  | import log | ||||||
|  |  | ||||||
|  |  | ||||||
| def main(): | def stl_parser(): | ||||||
|     """ Main function |     """ Main function | ||||||
|     Parses stl file header to get info about generated fingerprint. |     Parses stl file header. | ||||||
|  |     Returns command for running main.py with preformatted arguments. | ||||||
|     """ |     """ | ||||||
|  |  | ||||||
|     # Parse arguments |     # Parse arguments | ||||||
|     parser = ap.ArgumentParser(description='Command line utility for parsing fingerprint STL file header') |     parser = ap.ArgumentParser( | ||||||
|  |         description='Command line utility for parsing fingerprint STL file header into working command') | ||||||
|     parser.add_argument('file', type=str, help='STL file') |     parser.add_argument('file', type=str, help='STL file') | ||||||
|     args = parser.parse_args() |     args = parser.parse_args() | ||||||
|  |  | ||||||
|     # Check if file exists |     # Check if file exists | ||||||
|     if not exists(args.file): |     if not exists(args.file): | ||||||
|         sys.stderr.write('Error: File does not exist') |         log.error_exit('File does not exist') | ||||||
|         sys.exit(1) |  | ||||||
|     with open(args.file, "rb") as f: |     with open(args.file, "rb") as f: | ||||||
|         header = f.read(80).decode('UTF-8') |         header = f.read(80).decode('UTF-8') | ||||||
|     #print(header, file=sys.stderr) |  | ||||||
|  |     # Parse header | ||||||
|  |     # First, split header by new line, which serves as header ending character | ||||||
|     header = header.split('\n')[0] |     header = header.split('\n')[0] | ||||||
|  |     # Then, split header by backslash, which separates arguments | ||||||
|     header_arr = header.split('\\') |     header_arr = header.split('\\') | ||||||
|     input_file = header_arr[0] |     input_file = header_arr[0] | ||||||
|     dpi = header_arr[1] |     header_arr = header_arr[1:] | ||||||
|  |     # Create output file name to match input file name | ||||||
|  |     output_file = input_file.split('.')[0] + "_mod.png" | ||||||
|  |     dpi = header_arr[0] | ||||||
|  |     header_arr = header_arr[1:] | ||||||
|  |  | ||||||
|     if exists(header_arr[2]): |     # Create command string, later append arguments | ||||||
|         config = header_arr[2] |     arg_string = "python3 src/main.py " + input_file + " " + output_file + " " + dpi | ||||||
|         preset = header_arr[3] |  | ||||||
|  |     if exists(header_arr[0]): | ||||||
|  |         config_file = header_arr[0] | ||||||
|  |         header_arr = header_arr[1:] | ||||||
|  |         preset = header_arr[0] | ||||||
|  |         arg_string += " -c " + config_file + " " + preset + " --stl " | ||||||
|     else: |     else: | ||||||
|         filters = header_arr[2] |         # This will not work, as filters are stored as shortcuts | ||||||
|  |         # This is done in order to have fewer chars in stl header which is limited to 80 | ||||||
|  |         filters = header_arr[0] | ||||||
|  |         arg_string += " -f " + filters | ||||||
|  |  | ||||||
|     mode = header_arr[-1] |     # Check if the last argument is numeric, if not, it is generation mode | ||||||
|  |     header_arr = header_arr[1:] | ||||||
|  |     last_arg = header_arr[-1] | ||||||
|  |  | ||||||
|     if (mode == "P"): |     # Append mode and stl arguments | ||||||
|         height_base = header_arr[-2] |     if last_arg.isnumeric(): | ||||||
|         height_line = header_arr[-3] |         if len(header_arr) == 2: | ||||||
|         print(input_file, dpi, config, preset, mode, |             mode = "p" | ||||||
|               height_base, height_line, sep='\n') |             height_line = header_arr[0] | ||||||
|     elif (mode == "C"): |             height_base = header_arr[1] | ||||||
|         curv_rate_y = header_arr[-2] |             arg_string += mode + " " + height_line + " " + height_base | ||||||
|         curv_rate_x = header_arr[-3] |         elif len(header_arr) == 4: | ||||||
|         height_base = header_arr[-4] |             mode = "c" | ||||||
|         height_line = header_arr[-5] |             height_line = header_arr[0] | ||||||
|         print(input_file, dpi, config, preset, mode, |             height_base = header_arr[1] | ||||||
|               height_base, height_line, curv_rate_x, curv_rate_y, sep='\n') |             curv_rate_x = header_arr[2] | ||||||
|     elif (mode == "M"): |             curv_rate_y = header_arr[3] | ||||||
|         pass |             arg_string += mode + " " + height_line + \ | ||||||
|  |                 " " + height_base + " " + curv_rate_x + " " + curv_rate_y | ||||||
|  |         elif len(header_arr) == 3: | ||||||
|  |             mode = "m" | ||||||
|  |             height_line = header_arr[0] | ||||||
|  |             # TODO: mapped mode arguments | ||||||
|  |             pass | ||||||
|  |         else: | ||||||
|  |             # Print unfinished command | ||||||
|  |             print(arg_string, file=sys.stdout) | ||||||
|  |             log.error_exit('Corrupted file header') | ||||||
|  |     else: | ||||||
|  |         mode = last_arg.lower() | ||||||
|  |         arg_string += mode + " " | ||||||
|  |  | ||||||
|     sys.exit(1) |         if mode == "p": | ||||||
|  |             height_line = header_arr[0] | ||||||
|  |             height_base = header_arr[1] | ||||||
|  |             arg_string += height_line + " " + height_base | ||||||
|  |         elif mode == "c": | ||||||
|  |             height_line = header_arr[0] | ||||||
|  |             height_base = header_arr[1] | ||||||
|  |             curv_rate_x = header_arr[2] | ||||||
|  |             curv_rate_y = header_arr[3] | ||||||
|  |             arg_string += height_line + " " + height_base + \ | ||||||
|  |                 " " + curv_rate_x + " " + curv_rate_y | ||||||
|  |         elif mode == "m": | ||||||
|  |             height_line = header_arr[0] | ||||||
|  |             pass | ||||||
|  |         else: | ||||||
|  |             # Print unfinished command | ||||||
|  |             print(arg_string, file=sys.stdout) | ||||||
|  |             log.error_exit('Corrupted file header') | ||||||
|  |  | ||||||
|  |     # Print finished command | ||||||
|  |     print(arg_string, file=sys.stdout) | ||||||
|  |  | ||||||
|  |  | ||||||
| main() | stl_parser() | ||||||
|  | |||||||
		Reference in New Issue
	
	Block a user