This commit is contained in:
Cédric Leporcq 2022-04-17 21:58:56 +02:00
parent e60dab7f3b
commit 27587468ba
2 changed files with 35 additions and 15 deletions

View File

@ -43,9 +43,9 @@ class FPath:
'camera_make': '{camera_make}', 'camera_make': '{camera_make}',
'camera_model': '{camera_model}', 'camera_model': '{camera_model}',
'city': '{city}', 'city': '{city}',
'custom': '{".*"}', 'custom': r'{".*"}',
'country': '{country}', 'country': '{country}',
'date': '{(%[a-zA-Z][^a-zA-Z]*){1,8}}', # search for date format string 'date': r'{(%[a-zA-Z][^a-zA-Z]*){1,8}}', # search for date format string
'ext': '{ext}', 'ext': '{ext}',
'folder': '{folder}', 'folder': '{folder}',
'folders': r'{folders(\[[0-9:]{0,3}\])?}', 'folders': r'{folders(\[[0-9:]{0,3}\])?}',
@ -144,15 +144,16 @@ class FPath:
date = metadata['date_media'] date = metadata['date_media']
# early morning photos can be grouped with previous day # early morning photos can be grouped with previous day
if date is not None: if date is not None:
part = self.get_early_morning_photos_date(date, mask) part = str(self.get_early_morning_photos_date(date, mask))
elif item == 'folder': elif item in ('folder', 'folders'):
folder = os.path.basename(metadata['subdirs'])
if folder != metadata['src_dir']:
part = folder
elif item == 'folders':
folders = Path(metadata['subdirs']).parts folders = Path(metadata['subdirs']).parts
folders = self._get_folders(folders, mask) if folders:
part = os.path.join(*folders) if item == 'folder':
folder = folders[-1]
part = folder
else:
folders = self._get_folders(folders, mask)
part = os.path.join(*folders)
elif item in ( elif item in (
'album', 'album',
@ -169,14 +170,14 @@ class FPath:
mask = 'default' mask = 'default'
if metadata[mask]: if metadata[mask]:
part = metadata[mask] part = str(metadata[mask])
elif item in 'custom': elif item in 'custom':
# Fallback string # Fallback string
part = mask[1:-1] part = mask[1:-1]
return part return part
def _set_case(self, regex, part, this_part): def _substitute(self, regex, part, this_part):
# Capitalization # Capitalization
u_regex = '%u' + regex u_regex = '%u' + regex
l_regex = '%l' + regex l_regex = '%l' + regex
@ -193,7 +194,9 @@ class FPath:
for item, regex in self.items.items(): for item, regex in self.items.items():
matched = re.search(regex, this_part) matched = re.search(regex, this_part)
if matched: if matched:
self.log.debug(f'item: {item}, mask: {matched.group()[1:-1]}')
part = self.get_part(item, matched.group()[1:-1], metadata) part = self.get_part(item, matched.group()[1:-1], metadata)
self.log.debug(f'part: {part}')
part = part.strip() part = part.strip()
@ -207,7 +210,7 @@ class FPath:
this_part = re.sub( this_part = re.sub(
self.whitespace_regex, self.whitespace_sub, this_part self.whitespace_regex, self.whitespace_sub, this_part
) )
this_part = self._set_case(regex, part, this_part) this_part = self._substitute(regex, part, this_part)
# remove alternate parts inside bracket separated by | # remove alternate parts inside bracket separated by |
regex = r'[-_ .]?\(\|\)' regex = r'[-_ .]?\(\|\)'
@ -228,6 +231,9 @@ class FPath:
if re.match(regex, this_part[0]): if re.match(regex, this_part[0]):
this_part = this_part[1:] this_part = this_part[1:]
# Remove unwanted chars in filename
this_part = utils.filename_filter(this_part)
return this_part return this_part
def get_path(self, metadata: dict) -> list: def get_path(self, metadata: dict) -> list:
@ -256,7 +262,7 @@ class FPath:
# If last path is empty or start with dot # If last path is empty or start with dot
if part == '' or re.match(r'^\..*', part): if part == '' or re.match(r'^\..*', part):
path.append(metadata['filename']) path.append(utils.filename_filter(metadata['filename']))
return os.path.join(*path) return os.path.join(*path)

View File

@ -42,6 +42,21 @@ def empty_dir(dir_path):
return not next(os.scandir(dir_path), None) return not next(os.scandir(dir_path), None)
def filename_filter(filename):
"""
Take a string and return a valid filename constructed from the string.
"""
blacklist = '/\\:*"<>|'
if filename is None:
return filename
# Remove blacklisted chars.
for char in blacklist:
filename = filename.replace(char, '')
return filename
def get_date_regex(user_regex=None): def get_date_regex(user_regex=None):
"""Return date regex generator""" """Return date regex generator"""
if user_regex: if user_regex:
@ -146,7 +161,6 @@ def camel2snake(name):
r'(?!^)[A-Z]', lambda x: '_' + x.group(0).lower(), name[1:] r'(?!^)[A-Z]', lambda x: '_' + x.group(0).lower(), name[1:]
) )
import os import os
import platform import platform
import subprocess import subprocess