Skip to content

Commit

Permalink
Next incremental batch of pathlib changes (Issue #210)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
csatt committed Jul 27, 2023
1 parent 869ec70 commit 2fb8c4f
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 26 deletions.
5 changes: 3 additions & 2 deletions python3/IV_Swinger.py
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand All @@ -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))
Expand Down
27 changes: 10 additions & 17 deletions python3/IV_Swinger2.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,6 @@
import configparser
import datetime as dt
import difflib
import glob
import io
import math
import os
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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 "
Expand Down Expand Up @@ -4881,25 +4880,20 @@ 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 "
f"files in {run_dir}")
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 "
Expand All @@ -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):
Expand Down Expand Up @@ -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)

# -------------------------------------------------------------------------
Expand Down
16 changes: 9 additions & 7 deletions python3/IV_Swinger2_gui.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,6 @@
# PreferencesDialog() are more complex dialogs.
#
import datetime as dt
import os
from pathlib import Path
import re
try:
Expand Down Expand Up @@ -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
Expand All @@ -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()

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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():
Expand Down

0 comments on commit 2fb8c4f

Please sign in to comment.