Skip to content
Snippets Groups Projects
Commit 0d59783b authored by Albert Feghaly's avatar Albert Feghaly
Browse files

Split download file content into get and download

parent 8049ef60
No related branches found
No related tags found
1 merge request!11Resolve "Allow to keep file content in memory"
......@@ -8,10 +8,14 @@ import numpy as np
import json
from bs4 import BeautifulSoup
import sys
from collections import namedtuple
from .tools import is_json
IDF = namedtuple('IricDataFile', ['name', 'data', 'metadata'])
class Client:
""" python client to IRICdata API"""
def __init__(self, username, password=None, url='https://thepond.bioinfo.iric.ca'):
......@@ -199,22 +203,35 @@ class Client:
def search_dataset_names(self, term):
return self.datasets.loc[self.datasets.dataset_name.str.contains(term),:]
""" Get file content according to file_id """
def get_file(self, file_id):
file_object = self.get_file_metadata(file_id)
path = os.path.join(self.url, 'secure/datafiles/download', str(file_id))
try:
file_name = file_object['filename']
file_content = self.session.get(path, allow_redirects=True).content
file_annotation = self.get_file_metadata(file_id)
return IDF(file_name, file_content, file_annotation)
except TypeError:
return IDF(None, None, None)
""" Download file according to file_id """
def dwnl_file_content(self, file_id, folder_out='', filename=''):
folder_out = folder_out if folder_out and folder_out[0] == '/' else os.path.join(os.getcwd(), folder_out)
os.makedirs(folder_out, exist_ok=True)
file_object = self.get_file_metadata(file_id)
if not file_object is None:
filename = file_object['filename'] if not filename else filename
idf = self.get_file(file_id)
if idf.data is not None:
if folder_out and folder_out[0] == '/':
pass
else:
folder_out = os.path.join(os.getcwd(), folder_out)
os.makedirs(folder_out, exist_ok=True)
filename = idf.name if not filename else filename
out_file_path = os.path.join(folder_out, filename)
path = os.path.join(self.url, 'secure/datafiles/download', str(file_id))
r = self.session.get(path, allow_redirects=True)
if os.path.exists(out_file_path):
sys.stderr.write('Warning: File already exists at location %s, skipping.\n' % out_file_path)
else:
with open(out_file_path, 'wb') as outfile:
print('Downloading %s' % out_file_path)
outfile.write(r.content)
outfile.write(idf.data)
""" Write file annotations json to disk """
def dwnl_file_annotation(self, file_id, folder_out='', filename=''):
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment