|
1 | 1 | import PySimpleGUI as sg
|
| 2 | +from uuid import uuid4 |
| 3 | +from os import path |
2 | 4 | from main import main
|
3 | 5 |
|
4 | 6 | sg.theme('Default1') # alternatives include, e.g., 'DarkGrey9'
|
5 | 7 |
|
6 |
| -layout = [[sg.Text('Select a folder and CSV file, then click the Launch button.')], |
7 |
| - [sg.Text('Folder', size=(8, 1)), sg.Input(), sg.FolderBrowse()], |
8 |
| - [sg.Text('CSV File', size=(8, 1)), sg.Input(), sg.FileBrowse(file_types=["CSV .csv"])], |
9 |
| - [sg.Button('Launch')]] |
| 8 | +layout = [[sg.Text('Select a folder, a CSV file and, optionally, an output folder, then click the Launch button.')], |
| 9 | + [sg.Text('Folder', size=(17, 1)), sg.Input(key="-FOLDER-"), sg.FolderBrowse()], |
| 10 | + [sg.Text('CSV File', size=(17, 1)), sg.Input(key="-CSV_FILE-"), sg.FileBrowse(file_types=["CSV .csv"])], |
| 11 | + [ |
| 12 | + sg.Text('Output Folder (optional)', size=(17, 1), tooltip='If specified, the main script will generate ' |
| 13 | + 'a CSV file in this folder'), |
| 14 | + sg.Input(key="-OUTPUT_FOLDER-"), |
| 15 | + sg.FolderBrowse() |
| 16 | + ], |
| 17 | + [ |
| 18 | + sg.Text('Log level', size=(17, 1)), |
| 19 | + sg.Radio('NOTSET (default)', "log_level_radio_group", key="-NOTSET-", default=True), |
| 20 | + sg.Radio('DEBUG', "log_level_radio_group", key="-DEBUG-"), |
| 21 | + sg.Radio('INFO', "log_level_radio_group", key="-INFO-"), |
| 22 | + sg.Radio('WARNING', "log_level_radio_group", key="-WARNING-"), |
| 23 | + ], |
| 24 | + [sg.Button('Launch', tooltip='Launch the main script with the specified parameters')]] |
10 | 25 |
|
11 | 26 | # Create the Window
|
12 | 27 | window = sg.Window('dupeguru-post-processor (Graphical Launcher)', layout)
|
|
19 | 34 | break
|
20 | 35 | # Else, if the user clicked the Launch button, invoke the script's main function.
|
21 | 36 | elif event == 'Launch':
|
22 |
| - path_to_folder = values[0] |
23 |
| - path_to_csv_file = values[1] |
| 37 | + # Retrieve and strip input values. |
| 38 | + path_to_folder = values["-FOLDER-"].strip() |
| 39 | + path_to_csv_file = values["-CSV_FILE-"].strip() |
| 40 | + path_to_output_folder = values["-OUTPUT_FOLDER-"].strip() |
24 | 41 |
|
25 |
| - print("\nLaunching dupeguru-post-processor...\n") |
26 |
| - main(path_to_folder, path_to_csv_file) |
| 42 | + # Normalize the log level. |
| 43 | + log_level = "DEBUG" if values["-DEBUG-"] \ |
| 44 | + else "INFO" if values["-INFO-"] \ |
| 45 | + else "WARNING" if values["-WARNING-"] \ |
| 46 | + else "NOTSET" |
| 47 | + |
| 48 | + # Validate stripped input values. |
| 49 | + if not path.isdir(path_to_folder): |
| 50 | + sg.Popup("Folder path is invalid", keep_on_top=True) |
| 51 | + continue # end this iteration |
| 52 | + elif not path.isfile(path_to_csv_file): |
| 53 | + sg.Popup("CSV file path is invalid", keep_on_top=True) |
| 54 | + continue |
| 55 | + elif len(path_to_output_folder) > 0 and not path.isdir(path_to_output_folder): |
| 56 | + sg.Popup("Output folder (optional) path was specified, but is invalid", keep_on_top=True) |
| 57 | + continue |
| 58 | + |
| 59 | + # If the user specified an output _folder_, generate an output file path; otherwise, default to None. |
| 60 | + path_to_output_file = None |
| 61 | + if len(path_to_output_folder) > 0: |
| 62 | + # Keep trying to generate a random file name that does not already exist. |
| 63 | + # |
| 64 | + # Note: This is not a foolproof way of preventing naming collisions. It is technically possible that some |
| 65 | + # other process creates a file at the same path, between now (i.e. the time we perform this name |
| 66 | + # collision check) and the time the main script creates a file at that path. |
| 67 | + # |
| 68 | + while True: |
| 69 | + random_file_name = f"{uuid4().hex}.csv" |
| 70 | + path_to_output_file = path.join(path_to_output_folder, random_file_name) |
| 71 | + if not path.isfile(path_to_output_file): |
| 72 | + break # stop iterating once we have a unique file name |
| 73 | + print(f"Output file path: {path.normpath(path_to_output_file)}\n") |
| 74 | + |
| 75 | + print("Launching dupeguru-post-processor...\n") |
| 76 | + main( |
| 77 | + path.normpath(path_to_folder), |
| 78 | + path.normpath(path_to_csv_file), |
| 79 | + path.normpath(path_to_output_file), |
| 80 | + log_level=log_level |
| 81 | + ) |
| 82 | + |
| 83 | + sg.Popup("Done.", keep_on_top=True) |
27 | 84 |
|
28 | 85 | window.close()
|
0 commit comments