Add RegEx match mode option logic

This commit is contained in:
2023-05-11 22:18:05 -05:00
parent 6ccc048b96
commit 60099b9188
2 changed files with 22 additions and 3 deletions

View File

@@ -15,7 +15,7 @@ from google.cloud import vision
from rich.progress import Progress, BarColumn
from phototag import config, TEMP_PATH
from phototag.helpers import select_files, convert_to_bytes, walk
from phototag.helpers import select_files, convert_to_bytes, walk, path_to_match_mode
from phototag.process import MasterFileProcessor
logger = logging.getLogger(__name__)
@@ -46,7 +46,7 @@ def cli():
@click.option('-t', '--test', is_flag=True,
help='Don\'t actually query the Vision API, just generate fake tags for testing purposes.')
def run(files: Tuple[str], all: bool = False, regex: str = None, recursive: bool = None, depth: int = None,
glob_pattern: str = None,
glob_pattern: str = None, regex_mode: str = None,
max_threads: int = None,
max_buffer: str = None, forget: bool = False, overwrite: bool = False, dry_run: bool = False,
test: bool = False):
@@ -76,7 +76,8 @@ def run(files: Tuple[str], all: bool = False, regex: str = None, recursive: bool
if regex:
logger.debug('Applying RegEx pattern: {}'.format(regex))
compiled_regex = re.compile(regex)
files = [file for file in files if compiled_regex.match(file)]
files = [file for file in files if
compiled_regex.match(path_to_match_mode(file, regex_mode, root=cwd))]
if len(files) < 1:
logger.error('No files selected for processing. Cannot proceed.')

View File

@@ -121,6 +121,24 @@ def walk(root: Path, current: Optional[Path] = None, depth: Optional[int] = None
yield from walk(root, current=item, depth=depth)
def path_to_match_mode(path: Path, match_mode: str, root: Optional[Path] = None) -> str:
"""
Converts a path to a string based on the match mode.
:param path: The path to convert
:param match_mode: The match mode to use
:param root: The relative directory to use for 'relative' match mode. Defaults to the current working directory.
"""
root = root or Path.cwd()
if match_mode == "relative":
return str(path.relative_to(root or Path.cwd()))
elif match_mode == "absolute":
return str(path.resolve())
elif match_mode == "filename":
return path.name
else:
raise ValueError(f"Invalid match mode: {match_mode}")
def select_files(files: List[str], regex: Optional[str], glob_pattern: Optional[str]) -> List[str]:
"""
Helper function for selecting files in the CWD (or subdirectories, via Glob) and filtering them.