Added check for non-existing keys in CSV dictionary

This commit is contained in:
Harrison Deng 2023-04-25 09:15:26 -05:00
parent 7b7f6438d4
commit f31b1b2705

View File

@ -50,10 +50,17 @@ def rename(
raise Exception("Duplicate current key.") raise Exception("Duplicate current key.")
replacement_dict[row[current_col_ind]] = row[become_col_ind] replacement_dict[row[current_col_ind]] = row[become_col_ind]
for subitem_path, subitem, match in candidates: for subitem_path, subitem, match in candidates:
if match.group(1) not in replacement_dict:
logging.warning(
'Group "%s" was not matched to any row in the provided CSV. '
"Skipping...",
match.group(1)
)
continue
original = subitem_path original = subitem_path
objective = os.path.join( objective = os.path.join(
os.path.dirname(subitem_path), os.path.dirname(subitem_path),
re.sub(match.re, replacement_dict[match.group(1)], subitem), re.sub(match.re, replacement_dict[match.group(1)], subitem.strip()),
) )
if keep_extension: if keep_extension:
objective += os.path.splitext(subitem_path)[1] objective += os.path.splitext(subitem_path)[1]