refactored render_template, closes #149
This commit is contained in:
@@ -641,7 +641,6 @@ class PhotoInfo:
|
|||||||
template: str template
|
template: str template
|
||||||
none_str: str to use default for None values, default is '_'
|
none_str: str to use default for None values, default is '_'
|
||||||
path_sep: optional character to use as path separator, default is os.path.sep """
|
path_sep: optional character to use as path separator, default is os.path.sep """
|
||||||
|
|
||||||
if path_sep is None:
|
if path_sep is None:
|
||||||
path_sep = os.path.sep
|
path_sep = os.path.sep
|
||||||
elif path_sep is not None and len(path_sep) != 1:
|
elif path_sep is not None and len(path_sep) != 1:
|
||||||
@@ -722,36 +721,6 @@ class PhotoInfo:
|
|||||||
|
|
||||||
rendered_strings = set([rendered])
|
rendered_strings = set([rendered])
|
||||||
for field in MULTI_VALUE_SUBSTITUTIONS:
|
for field in MULTI_VALUE_SUBSTITUTIONS:
|
||||||
if field == "album":
|
|
||||||
values = self.albums
|
|
||||||
elif field == "keyword":
|
|
||||||
values = self.keywords
|
|
||||||
elif field == "person":
|
|
||||||
values = self.persons
|
|
||||||
# remove any _UNKNOWN_PERSON values
|
|
||||||
values = [val for val in values if val != _UNKNOWN_PERSON]
|
|
||||||
elif field == "label":
|
|
||||||
values = self.labels
|
|
||||||
elif field == "label_normalized":
|
|
||||||
values = self.labels_normalized
|
|
||||||
elif field == "folder_album":
|
|
||||||
values = []
|
|
||||||
# photos must be in an album to be in a folder
|
|
||||||
for album in self.album_info:
|
|
||||||
if album.folder_names:
|
|
||||||
# album in folder
|
|
||||||
folder = path_sep.join(album.folder_names)
|
|
||||||
folder += path_sep + album.title
|
|
||||||
values.append(folder)
|
|
||||||
else:
|
|
||||||
# album not in folder
|
|
||||||
values.append(album.title)
|
|
||||||
else:
|
|
||||||
raise ValueError(f"Unhandleded template value: {field}")
|
|
||||||
|
|
||||||
# If no values, insert None so code below will substite none_str for None
|
|
||||||
values = values or [None]
|
|
||||||
|
|
||||||
# Build a regex that matches only the field being processed
|
# Build a regex that matches only the field being processed
|
||||||
re_str = r"(?<!\\)\{(" + field + r")(,(([\w\-. ]{0,})))?\}"
|
re_str = r"(?<!\\)\{(" + field + r")(,(([\w\-. ]{0,})))?\}"
|
||||||
regex_multi = re.compile(re_str)
|
regex_multi = re.compile(re_str)
|
||||||
@@ -760,6 +729,8 @@ class PhotoInfo:
|
|||||||
new_strings = set()
|
new_strings = set()
|
||||||
|
|
||||||
for str_template in rendered_strings:
|
for str_template in rendered_strings:
|
||||||
|
if regex_multi.search(str_template):
|
||||||
|
values = self._template_value_multi(field, path_sep)
|
||||||
for val in values:
|
for val in values:
|
||||||
|
|
||||||
def get_template_value_multi(lookup_value):
|
def get_template_value_multi(lookup_value):
|
||||||
@@ -981,6 +952,39 @@ class PhotoInfo:
|
|||||||
# if here, didn't get a match
|
# if here, didn't get a match
|
||||||
raise KeyError(f"No rule for processing {lookup}")
|
raise KeyError(f"No rule for processing {lookup}")
|
||||||
|
|
||||||
|
def _template_value_multi(self, field, path_sep):
|
||||||
|
""" return list of values for a multi-valued template field """
|
||||||
|
if field == "album":
|
||||||
|
values = self.albums
|
||||||
|
elif field == "keyword":
|
||||||
|
values = self.keywords
|
||||||
|
elif field == "person":
|
||||||
|
values = self.persons
|
||||||
|
# remove any _UNKNOWN_PERSON values
|
||||||
|
values = [val for val in values if val != _UNKNOWN_PERSON]
|
||||||
|
elif field == "label":
|
||||||
|
values = self.labels
|
||||||
|
elif field == "label_normalized":
|
||||||
|
values = self.labels_normalized
|
||||||
|
elif field == "folder_album":
|
||||||
|
values = []
|
||||||
|
# photos must be in an album to be in a folder
|
||||||
|
for album in self.album_info:
|
||||||
|
if album.folder_names:
|
||||||
|
# album in folder
|
||||||
|
folder = path_sep.join(album.folder_names)
|
||||||
|
folder += path_sep + album.title
|
||||||
|
values.append(folder)
|
||||||
|
else:
|
||||||
|
# album not in folder
|
||||||
|
values.append(album.title)
|
||||||
|
else:
|
||||||
|
raise ValueError(f"Unhandleded template value: {field}")
|
||||||
|
|
||||||
|
# If no values, insert None so code below will substite none_str for None
|
||||||
|
values = values or [None]
|
||||||
|
return values
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def _longitude(self):
|
def _longitude(self):
|
||||||
""" Returns longitude, in degrees """
|
""" Returns longitude, in degrees """
|
||||||
|
|||||||
Reference in New Issue
Block a user