From 2fb8c4fc23bb1ddd501747825f1474927014dc13 Mon Sep 17 00:00:00 2001 From: Chris Satterlee Date: Thu, 27 Jul 2023 10:44:41 -0700 Subject: [PATCH] Next incremental batch of pathlib changes (Issue #210) Changed from glob.glob() to Path().glob() Protected a few comparisions between Path() objects from generating false positives/negatives if one is actually a string by adding .resolve(). That would generate an exception if it's a string. Replaced call to os.access() with a try/except. --- python3/IV_Swinger.py | 5 +++-- python3/IV_Swinger2.py | 27 ++++++++++----------------- python3/IV_Swinger2_gui.py | 16 +++++++++------- 3 files changed, 22 insertions(+), 26 deletions(-) diff --git a/python3/IV_Swinger.py b/python3/IV_Swinger.py index 2527e58..c347238 100755 --- a/python3/IV_Swinger.py +++ b/python3/IV_Swinger.py @@ -278,7 +278,7 @@ def get_date_time_str(): @staticmethod def extract_date_time_str(input_str): """Method to parse the date/time string from a leaf file name or - other string + other string (or pathlib Path) """ dt_file_re = re.compile(r"(\d{6}_\d{2}_\d{2}_\d{2})") match = dt_file_re.search(str(input_str)) @@ -288,7 +288,8 @@ def extract_date_time_str(input_str): @staticmethod def is_date_time_str(input_str): - """Method to test if a given string is a date/time string + """Method to test if a given string (or pathlib Path) is a date/time + string """ dt_file_re = re.compile(r"^(\d{6}_\d{2}_\d{2}_\d{2})$") match = dt_file_re.search(str(input_str)) diff --git a/python3/IV_Swinger2.py b/python3/IV_Swinger2.py index 7f3845b..94a8010 100755 --- a/python3/IV_Swinger2.py +++ b/python3/IV_Swinger2.py @@ -75,7 +75,6 @@ import configparser import datetime as dt import difflib -import glob import io import math import os @@ -681,7 +680,7 @@ def save_starting_cfg_file(self): but not dependent on DEBUG_CONFIG. """ if self.cfg_filename.exists(): - shutil.copyfile(self.cfg_filename, self.starting_cfg_filename) + shutil.copy(self.cfg_filename, self.starting_cfg_filename) else: if self.starting_cfg_filename.exists(): self.starting_cfg_filename.unlink() @@ -1349,9 +1348,9 @@ def save_snapshot(self): def copy_file(self, dest_dir): """Method to copy the current .cfg file to the specified directory """ - if self.cfg_filename.parent == dest_dir: + if self.cfg_filename.parent.resolve() == dest_dir.resolve(): # Return without doing anything if the property is already - # pointing to the specified directory + # pointing to the specified directory. return if DEBUG_CONFIG: dbg_str = (f"copy_file: Copying config from " @@ -4881,15 +4880,12 @@ def get_bias_batt_csv(self): """Method to find the bias battery CSV file """ bias_battery_csv = None - glob_pattern = "{}/bias_batt_adc_pairs*.csv" # Find the bias battery CSV file. If one exists in the run # directory, use that. Otherwise, copy the one from the Battery # directory to the run directory and then use it. run_dir = self.hdd_output_dir - bb_files = glob.glob(glob_pattern.format(run_dir)) bb_file_count = 0 - for f in bb_files: - bias_battery_csv = f + for bias_battery_csv in run_dir.glob("bias_batt_adc_pairs*.csv"): bb_file_count += 1 if bb_file_count > 1: err_str = (f"ERROR: There are multiple bias_batt_adc_pairs*.csv " @@ -4897,9 +4893,7 @@ def get_bias_batt_csv(self): self.logger.print_and_log(err_str) elif bb_file_count == 0: batt_dir = run_dir.parent / BATTERY_FOLDER_NAME - bb_files = glob.glob(glob_pattern.format(batt_dir)) - for f in bb_files: - bias_battery_csv = f + for bias_battery_csv in batt_dir.glob("bias_batt_adc_pairs*.csv"): bb_file_count += 1 if bb_file_count > 1: err_str = (f"ERROR: There are multiple " @@ -4922,9 +4916,8 @@ def remove_prev_bias_battery_csv(self): """Method to remove old bias battery CSV file(s) from the parent directory""" run_dir = self.hdd_output_dir.parent - bb_files = glob.glob(f"{run_dir}/bias_batt_adc_pairs*.csv") - for f in bb_files: - self.clean_up_file(f) + for bias_battery_csv in run_dir.glob("bias_batt_adc_pairs*.csv"): + self.clean_up_file(bias_battery_csv) # ------------------------------------------------------------------------- def log_meter_debug_info(self): @@ -5684,10 +5677,10 @@ def clean_up_files(self, run_dir, loop_mode=False, self.clean_up_file(self.current_img) # ------------------------------------------------------------------------- - def clean_up_file(self, f): + def clean_up_file(self, filename): """Method to remove one file and log its removal""" - f.unlink() - msg_str = f"Removed {f}" + filename.unlink() + msg_str = f"Removed {filename}" self.logger.log(msg_str) # ------------------------------------------------------------------------- diff --git a/python3/IV_Swinger2_gui.py b/python3/IV_Swinger2_gui.py index 1c1189d..9dc7251 100755 --- a/python3/IV_Swinger2_gui.py +++ b/python3/IV_Swinger2_gui.py @@ -85,7 +85,6 @@ # PreferencesDialog() are more complex dialogs. # import datetime as dt -import os from pathlib import Path import re try: @@ -1564,7 +1563,7 @@ def swap_config(self, run_dir, config_dir): """ cfg_file = None original_cfg_file = None - if run_dir is not None and run_dir != config_dir: + if run_dir is not None and run_dir.resolve() != config_dir.resolve(): cfg_file = run_dir / f"{APP_NAME}.cfg" if cfg_file.exists(): # Snapshot current config @@ -1580,8 +1579,8 @@ def restore_config(self, run_dir, config_dir, cfg_file, original_cfg_file): """Method to restore the swapped config from the snapshot. """ - if (run_dir is not None and run_dir != config_dir and - cfg_file.exists()): + if (run_dir is not None and run_dir.resolve() != config_dir.resolve() + and cfg_file.exists()): self.config.cfg_filename = original_cfg_file self.config.save_snapshot() @@ -3518,7 +3517,10 @@ def get_copy_dest(self): self.copy_dest = self.copy_dest.parent # Check that it is writeable - if not os.access(self.copy_dest, os.W_OK | os.X_OK): + try: + (self.copy_dest / "DUMMY_FILE").touch() + (self.copy_dest / "DUMMY_FILE").unlink() + except PermissionError: err_str = f"ERROR: {self.copy_dest} is not writeable" tkmsg.showerror(message=err_str) return RC_FAILURE @@ -3684,9 +3686,9 @@ def copy_overwrite_precheck(self, selected_src_dirs): # existing directories in destination for src_dir in selected_src_dirs: dest_dir = self.get_dest_dir(src_dir) - if dest_dir == src_dir: + if dest_dir.resolve() == src_dir.resolve(): err_str = (f"ERROR: source and destination are the same: " - f"{dest_dir.parent}") + f"{dest_dir.parent.resolve()}") tkmsg.showerror(message=err_str) return False if dest_dir.exists():