Example #1
0
    def load_metadata_files(cls, roc_link, authorization_header=None):
        with tempfile.TemporaryDirectory() as tmpdir:
            tmpdir_path = Path(tmpdir)
            local_zip = download_url(roc_link,
                                     target_path=(tmpdir_path /
                                                  'rocrate.zip').as_posix(),
                                     authorization=authorization_header)

            logger.debug("ZIP Archive: %s", local_zip)
            extracted_roc_path = tmpdir_path / 'extracted-rocrate'
            logger.info("Extracting RO Crate to %s", extracted_roc_path)
            extract_zip(local_zip, target_path=extracted_roc_path.as_posix())

            if logger.isEnabledFor(logging.DEBUG):
                logger.debug(
                    "RO-crate contents: %s",
                    ', '.join(str(s) for s in extracted_roc_path.iterdir()))

            try:
                crate = ROCrateHelper(extracted_roc_path.as_posix())
                metadata_path = extracted_roc_path / crate.metadata.id
                with open(metadata_path, "rt") as f:
                    metadata = json.load(f)
                return crate, metadata
            except Exception as e:
                raise lm_exceptions.NotValidROCrateException(detail=str(e))
Example #2
0
 def extract_rocrate(roc_link, target_path=None, authorization_header=None):
     with tempfile.NamedTemporaryFile(dir="/tmp") as archive_path:
         zip_archive = download_url(roc_link,
                                    target_path=archive_path.name,
                                    authorization=authorization_header)
         logger.debug("ZIP Archive: %s", zip_archive)
         roc_path = target_path or Path(tempfile.mkdtemp(dir="/tmp"))
         logger.info("Extracting RO Crate @ %s", roc_path)
         extract_zip(archive_path, target_path=roc_path.as_posix())
         return roc_path
Example #3
0
def test_download_url_404():
    with tempfile.TemporaryDirectory() as d:
        with pytest.raises(lm_exceptions.NotValidROCrateException) as excinfo:
            _ = utils.download_url('http://httpbin.org/status/404', os.path.join(d, 'get_404'))
        assert excinfo.value.status == 400
Example #4
0
 def download_url(self, url, user, target_path=None):
     return download_url(
         url,
         target_path,
         authorization=
         f'Bearer {self._get_access_token(user.id)["access_token"]}')