Skip to content

Commit

Permalink
* Added image_details property to source.
Browse files Browse the repository at this point in the history
* Allowed for the specification of additional file extension maps to MIME types, including support for no file extension.
* Modified filename_filter to be interpreted as a complete list of files to look for and include, preventing  glob of entire directory.
* Added error-checking for empty (no images included) UploadableSources.
  • Loading branch information
gsainsbury86 committed Jul 20, 2022
1 parent 5244c2c commit 2b391ad
Showing 1 changed file with 34 additions and 9 deletions.
43 changes: 34 additions & 9 deletions zegami_sdk/source.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,21 @@ def _retrieve(self, key):
raise KeyError('Key "{}" not found in Source _data'.format(key))
return self._data[key]

@property
def image_details():
pass

@image_details.getter
def image_details(self):

collection = self.collection
c = collection.client

ims_url = '{}/{}/project/{}/nodes/{}/images'.format(
c.HOME, c.API_1, collection.workspace_id, self.imageset_id)
ims = c._auth_get(ims_url)

return ims

class UploadableSource():

Expand Down Expand Up @@ -114,7 +129,7 @@ class UploadableSource():
".json"
)

def __init__(self, name, image_dir, column_filename='__auto_join__', recursive_search=True, filename_filter=[]):
def __init__(self, name, image_dir, column_filename='__auto_join__', recursive_search=True, filename_filter=[], additional_mimes=None):
"""
Used in conjunction with create_collection().
Expand All @@ -135,22 +150,26 @@ def __init__(self, name, image_dir, column_filename='__auto_join__', recursive_s
self._source = None
self._index = None


UploadableSource.IMAGE_MIMES = {**UploadableSource.IMAGE_MIMES, **additional_mimes}

# Check the directory exists
if not os.path.exists(image_dir):
raise FileNotFoundError('image_dir "{}" does not exist'.format(self.image_dir))
if not os.path.isdir(image_dir):
raise TypeError('image_dir "{}" is not a directory'.format(self.image_dir))

# Find all files matching the allowed mime-types
fps = sum(
[glob('{}/**/*{}'.format(image_dir, ext), recursive=recursive_search)
for ext in self.IMAGE_MIMES.keys()], [])

# Potentially limit paths based on filename_filter
# Potentially limit paths based on filename_filter.
if filename_filter:
if type(filename_filter) != list:
raise TypeError('filename_filter should be a list')
fps = [fp for fp in fps if os.path.basename(fp) in filename_filter]
fps = [os.path.join(image_dir, fp) for fp in filename_filter if os.path.exists(os.path.join(image_dir, fp))]

else:
# Find all files matching the allowed mime-types
fps = sum(
[glob('{}/**/*{}'.format(image_dir, ext), recursive=recursive_search)
for ext in self.IMAGE_MIMES.keys()], [])

self.filepaths = fps
self.filenames = [os.path.basename(fp) for fp in self.filepaths]
Expand Down Expand Up @@ -268,6 +287,10 @@ def _upload(self):
# Tell the server how many uploads are expected for this source
url = '{}/{}/project/{}/imagesets/{}/extend'.format(c.HOME, c.API_0, collection.workspace_id, self.imageset_id)
delta = len(self)
# If there are no new uploads, ignore.
if delta == 0:
print('No new data to be uploaded.')
return
resp = c._auth_post(url, body=None, json={'delta': delta})
new_size = resp['new_size']
start = new_size - delta
Expand Down Expand Up @@ -369,6 +392,8 @@ def _parse_list(cls, uploadable_sources) -> list:
@classmethod
def _get_mime_type(cls, path) -> str:
"""Gets the mime_type of the path. Raises an error if not a valid image mime_type."""
if '.' not in path:
return cls.IMAGE_MIMES['']
ext = os.path.splitext(path)[-1]
if ext in cls.IMAGE_MIMES.keys():
return cls.IMAGE_MIMES[ext]
Expand Down Expand Up @@ -421,4 +446,4 @@ def _upload(self):
payload = json.dumps(upload_ims['imageset'])
r = c._auth_put(upload_ims_url, payload, return_response=True)

return r
return r

0 comments on commit 2b391ad

Please sign in to comment.