-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
changed image selection. Now image can be a regex, too.
- Loading branch information
1 parent
f2e49a8
commit d53e30f
Showing
3 changed files
with
40 additions
and
24 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
""" | ||
Methods for image selection | ||
""" | ||
import difflib | ||
import re | ||
|
||
from bibigrid.models.exceptions import ImageDeactivatedException | ||
|
||
|
||
def select_image(configuration, provider, image, log): | ||
# check if image is active | ||
active_images = provider.get_active_images() | ||
if image not in active_images: | ||
old_image = image | ||
log.info(f"Image '{old_image}' has no direct match. Maybe it's a regex? Trying regex match.") | ||
image = next((elem for elem in active_images if re.match(image, elem)), None) | ||
if not image: | ||
log.warning(f"Couldn't find image '{old_image}'.") | ||
if isinstance(configuration.get("fallbackOnOtherImage"), str): | ||
image = next(elem for elem in active_images if re.match(configuration["fallbackOnOtherImage"], elem)) | ||
log.info(f"Taking first regex ('{configuration['fallbackOnOtherImage']}') match '{image}'.") | ||
elif configuration.get("fallbackOnOtherImage", False): | ||
image = difflib.get_close_matches(old_image, active_images)[0] | ||
log.info(f"Taking closest active image (in name) '{image}' instead.") | ||
else: | ||
raise ImageDeactivatedException(f"Image {old_image} no longer active or doesn't exist.") | ||
log.info(f"Using alternative '{image}' instead of '{old_image}'. You should change the configuration.") | ||
else: | ||
log.info(f"Taking first regex match: '{image}'") | ||
return image |