Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Updates from appf #129

Merged
merged 3 commits into from
Jul 28, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
from setuptools import setup

# Update version here when you want to increment the version in PyPi
sdk_version = '0.4.7'
sdk_version = '0.4.8'

# If no ZEGAMI_SDK_VERSION set use the version
try:
Expand Down
55 changes: 42 additions & 13 deletions zegami_sdk/source.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,22 @@ 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 +130,8 @@ 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={}):
"""
Used in conjunction with create_collection().

Expand All @@ -125,6 +142,10 @@ def __init__(self, name, image_dir, column_filename='__auto_join__', recursive_s
To limit to an allowed specific list of filenames, provide
'filename_filter'. This filter will check against
os.path.basename(filepath).

Common mime types are inferred from the file extension,
but a dict of additional mime type mappings can be provided eg to
cater for files with no extension.
"""

self.name = name
Expand All @@ -135,22 +156,25 @@ def __init__(self, name, image_dir, column_filename='__auto_join__', recursive_s
self._source = None
self._index = None

self.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 +292,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 @@ -366,13 +394,14 @@ def _parse_list(cls, uploadable_sources) -> list:

return uploadable_sources

@classmethod
def _get_mime_type(cls, path) -> str:
def _get_mime_type(self, path) -> str:
"""Gets the mime_type of the path. Raises an error if not a valid image mime_type."""
if '.' not in path:
return self.image_mimes['']
ext = os.path.splitext(path)[-1]
if ext in cls.IMAGE_MIMES.keys():
return cls.IMAGE_MIMES[ext]
raise TypeError('"{}" is not a supported image mime_type ({})'.format(path, cls.IMAGE_MIMES))
if ext in self.image_mimes.keys():
return self.image_mimes[ext]
raise TypeError('"{}" is not a supported image mime_type ({})'.format(path, self.image_mimes))


class UrlSource(UploadableSource):
Expand Down