Skip to content

Commit

Permalink
feat: throw an exception for mismatching wildcards.
Browse files Browse the repository at this point in the history
  • Loading branch information
BenjaminMummery committed Jan 9, 2024
1 parent b653d89 commit bc34a0f
Showing 1 changed file with 34 additions and 0 deletions.
34 changes: 34 additions & 0 deletions src/plex_footage_sorter/_rename.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,38 @@ def _convert_glob_to_format_string(pattern: str) -> str:
return "".join(chars)


def _check_consistent_wildcards(pattern_1: str, pattern_2: str):
"""Raise an exception if the two patterns have different wildcards.
Args:
pattern_1 (str): A pattern containing one or more glob strings to be checked.
pattern_2 (str): A pattern containing one or more glob strings to be checked.
Raises:
ValueError: when there are mismatching wildcards between the patterns.
"""
glob_wildcards: List[str] = ["*", "?", "["]

p1_chars: List[str] = []
for char in pattern_1:
if char in glob_wildcards:
p1_chars.append(char)
p2_chars: List[str] = []
for char in pattern_2:
if char in glob_wildcards:
p2_chars.append(char)

if p1_chars == p2_chars:
return

for char1, char2 in zip(p1_chars, p2_chars):
if char1 != char2:
raise ValueError(
"Mismatching glob wildcards between input and output patterns "
f"'{pattern_1}' and '{pattern_2}': '{char1}' vs '{char2}'."
)


def main(directory: str, match_pattern: str, target_pattern: str):
"""Find and rename matching files.
Expand All @@ -104,6 +136,8 @@ def main(directory: str, match_pattern: str, target_pattern: str):
regex (bool): If True, interpret the match and target patterns using regex. If
False, glob will be used. Defaults to False.
"""
_check_consistent_wildcards(match_pattern, target_pattern)

if (
len(
files := _discover_files(
Expand Down

0 comments on commit bc34a0f

Please sign in to comment.