Skip to content

Commit

Permalink
On drag-and-drop event, filter out non-image files instead of crashin…
Browse files Browse the repository at this point in the history
…g; Issue #29
  • Loading branch information
JWCook committed Apr 17, 2021
1 parent 52adc03 commit 15ec311
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 13 deletions.
2 changes: 1 addition & 1 deletion naturtag/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import logging
logging.basicConfig(level='INFO')

__version__ = '0.5.0'
__version__ = '0.6.0'
4 changes: 2 additions & 2 deletions naturtag/controllers/image_selection_controller.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ async def load_images(self, paths):
# TODO: Use tasks to load incremental results in the UI
async def load_image(self, path):
if path in self.file_list:
return
return None, None

# Add to file list
logger.info(f'Main: Adding image {path}')
Expand Down Expand Up @@ -89,7 +89,7 @@ def select_photo_observation(self, observation_id):

def select_first_result(self, results):
""" Select the first taxon and/or observations discovered from tags, if any """
if not results:
if not results or not any(*results):
return
taxa, observations = zip(*results)

Expand Down
27 changes: 17 additions & 10 deletions naturtag/image_glob.py
Original file line number Diff line number Diff line change
@@ -1,53 +1,55 @@
from glob import glob
from fnmatch import fnmatch
from logging import getLogger
from os.path import expanduser, isdir, isfile, join
from typing import List, Union

from naturtag.constants import IMAGE_FILETYPES

logger = getLogger().getChild(__name__)


def glob_paths(path_patterns):
def glob_paths(path_patterns: List[str]) -> List[str]:
"""
Given one to many glob patterns, expand all into a list of matching files
Args:
path_patterns (list): Glob patterns
path_patterns: Glob patterns
Returns:
list: Expanded list of file paths
Expanded list of file paths
"""
expanded_paths = []
for pattern in path_patterns:
expanded_paths.extend([expanduser(path) for path in glob(pattern)])
return expanded_paths


def get_images_from_dir(dir):
def get_images_from_dir(dir: str) -> List[str]:
"""
Get all images of supported filetypes from the selected directory.
Note: Currently not recursive.
Args:
dir (list): Path to image directory
dir: Path to image directory
Returns:
list: Paths of supported image files in the directory
Paths of supported image files in the directory
"""
paths = glob_paths([join(dir, pattern) for pattern in IMAGE_FILETYPES])
logger.info(f'{len(paths)} images found in directory: {dir}')
return paths


def get_images_from_paths(paths):
def get_images_from_paths(paths: Union[str, List[str]]) -> List[str]:
"""
Get all images of supported filetypes from one or more dirs and/or image paths
Args:
paths (``str`` or ``list``): Paths to images and/or image directories
paths: Paths to images and/or image directories
Returns:
list: Combined list of image file paths
Combined list of image file paths
"""
image_paths = []
paths = [paths] if isinstance(paths, (str, bytes)) else paths
Expand All @@ -58,10 +60,15 @@ def get_images_from_paths(paths):
path = path.decode('utf-8')
if isdir(path):
image_paths.extend(get_images_from_dir(path))
elif isfile(path):
elif is_image_path(path):
image_paths.append(path)
else:
logger.warning(f'Not a valid path: {path}')

logger.info(f'{len(image_paths)} total images found in paths')
return image_paths


def is_image_path(path: str) -> bool:
""" Determine if a path points to a valid image of a supported type """
return isfile(path) and any(fnmatch(path.lower(), pattern) for pattern in IMAGE_FILETYPES)

0 comments on commit 15ec311

Please sign in to comment.