def _get_data(self): if self._data is None: try: self._data = json.loads( read_file_in_tar(self.filename, self.FILENAME_DATA)) except (IOError, FileNotFoundError) as error: raise CorruptArchive(str(error)) return self._data
def _retrieve_version(self) -> str: try: metadata = json.loads( read_file_in_tar(self.filepath, 'metadata.json')) except (IOError, FileNotFoundError) as error: raise CorruptArchive(str(error)) if 'export_version' not in metadata: raise CorruptArchive( "metadata.json doest not contain an 'export_version' key") return metadata['export_version']
def read_json_files(path, *, names=('metadata.json', 'data.json')) -> List[dict]: """Get metadata.json and data.json from an exported AiiDA archive :param path: the filepath of the archive :param names: the files to retrieve """ jsons: List[dict] = [] if zipfile.is_zipfile(path): for name in names: jsons.append(json.loads(read_file_in_zip(path, name))) elif tarfile.is_tarfile(path): for name in names: jsons.append(json.loads(read_file_in_tar(path, name))) else: raise ValueError( 'invalid file format, expected either a zip archive or gzipped tarball' ) return jsons