Skip to content

Commit

Permalink
Fixes for OasisPlatform (#12)
Browse files Browse the repository at this point in the history
* return None if null ref and not required

* Fix storing single file - keep existing extention

* test remove trailing dot

* WIP
  • Loading branch information
sambles authored Apr 30, 2024
1 parent cbe5ff4 commit b0dccf5
Showing 1 changed file with 23 additions and 8 deletions.
31 changes: 23 additions & 8 deletions oasis_data_manager/filestore/backends/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,8 @@ def _get_unique_filename(self, suffix=""):
:return: filename string
:rtype str
"""
if suffix.startswith('.'):
suffix = suffix[1:]
return "{}.{}".format(uuid.uuid4().hex, suffix)

def _is_valid_url(self, url):
Expand Down Expand Up @@ -246,6 +248,13 @@ def get(self, reference, output_path="", subdir="", required=False):
:return: Absolute filepath to stored Object
:rtype str
"""
# null ref given
if not reference:
if required:
raise MissingInputsException(reference)
else:
return None

target = os.path.abspath(
os.path.join(output_path, subdir) if subdir else output_path
)
Expand Down Expand Up @@ -298,17 +307,23 @@ def put(self, reference, filename=None, subdir="", suffix=None, arcname=None):
if not reference:
return None

ext = "tar.gz" if not suffix else suffix
filename = filename if filename else self._get_unique_filename(ext)
storage_path = os.path.join(subdir, filename) if subdir else filename
if os.path.isfile(reference):
ext = "".join(Path(reference).suffixes) if not suffix else suffix
filename = filename if filename else self._get_unique_filename(ext)
storage_path = subdir if subdir else ''
self.fs.mkdirs(os.path.dirname(storage_path), exist_ok=True)
storage_location = os.path.join(storage_path, filename)

self.fs.mkdirs(os.path.dirname(storage_path), exist_ok=True)
self.logger.info("Store file: {} -> {}".format(reference, storage_location))
self.fs.put(reference, storage_location)
return storage_location

if os.path.isfile(reference):
self.logger.info("Store file: {} -> {}".format(reference, storage_path))
self.fs.put(reference, storage_path)
return storage_path
elif os.path.isdir(reference):
ext = "tar.gz" if not suffix else suffix
filename = filename if filename else self._get_unique_filename(ext)
storage_path = os.path.join(subdir, filename) if subdir else filename
self.fs.mkdirs(os.path.dirname(storage_path), exist_ok=True)

self.logger.info("Store dir: {} -> {}".format(reference, storage_path))
with tempfile.NamedTemporaryFile() as f:
self.compress(f.name, reference, arcname)
Expand Down

0 comments on commit b0dccf5

Please sign in to comment.