Skip to content
Snippets Groups Projects

Develop

Merged
Szymon Ciomborrequested to merge
develop into master
6 open threads
5 files
+ 58
9
Compare changes
  • Side-by-side
  • Inline

Files

+ 46
4
@@ -60,15 +60,18 @@ def upload_file(file_path: str) -> FileID:
def download_file(
file_id: FileID,
output_path: tp.Optional[str] = None
output_path: tp.Optional[str] = None,
filename: tp.Optional[str] = None
) -> str:
"""Downloads file by its FileID.
Args:
file_id (FileID): File id of a file to download
path (Optional[str]): Path for downloaded file.
output_path (Optional[str]): Directory to save downloaded file in.
If not given the file will be saved in default location
(config.ini).
filename (Optional[str]): Output filename without .zip format.
If not specified, filename is random.
Returns:
str. A path to downloaded file.
@@ -76,8 +79,8 @@ def download_file(
response = requests.get(url=f"{Config.get('base_url')}/download{file_id}")
if response.status_code == 404 or response.status_code == 500:
raise FileNotFoundError("File not found")
filename = file_id.split("/")[-1] + ".zip"
if not filename:
filename = f"{file_id.split('/')[-1]}.zip"
if not output_path:
output_path = Config.get("output_path")
os.makedirs(output_path, exist_ok=True)
@@ -86,3 +89,42 @@ def download_file(
with open(output_path, "wb") as f:
f.write(response.content)
return output_path
def download_file_as_dict(
file_id: FileID,
) -> tp.Dict[str, str]:
"""Downloads file by its FileID.
Args:
file_id (FileID): File id of a file to download
Returns:
dict. Dictionary mapping filenames in output zipfile to file contents.
"""
def try_decode(bytes_data):
Please register or sign in to reply
try:
bytes_data.decode("utf-8")
return True
except UnicodeDecodeError:
return False
response = requests.get(url=f"{Config.get('base_url')}/download{file_id}")
if response.status_code == 404 or response.status_code == 500:
raise FileNotFoundError("File not found")
zip_buffer = io.BytesIO(initial_bytes=response.content)
with zipfile.ZipFile(
zip_buffer,
mode="r",
compression=zipfile.ZIP_DEFLATED,
) as zip_file:
return {
Please register or sign in to reply
i.filename: zip_file.open(i).read().decode("utf-8")
for i in filter(
lambda x:
try_decode(zip_file.read(x)) and not x.is_dir(),
Please register or sign in to reply
zip_file.infolist()
)
}
Loading