1515from pyclowder .datasets import get_file_list
1616from pyclowder .collections import get_datasets , get_child_collections
1717
18+ from dotenv import load_dotenv
19+ load_dotenv ()
20+ clowder_version = float (os .getenv ('clowder_version' ))
21+
1822# Some sources of urllib3 support warning suppression, but not all
1923try :
2024 from urllib3 import disable_warnings
2529
2630
2731# pylint: disable=too-many-arguments
28- def download (connector , host , key , fileid , intermediatefileid = None , ext = "" ):
32+ def download (connector , host , key , fileid , intermediatefileid = None , ext = "" , token = None ):
2933 """Download file to be processed from Clowder.
3034
3135 Keyword arguments:
@@ -39,60 +43,44 @@ def download(connector, host, key, fileid, intermediatefileid=None, ext=""):
3943
4044 connector .message_process ({"type" : "file" , "id" : fileid }, "Downloading file." )
4145
42- # TODO: intermediateid doesn't really seem to be used here, can we remove entirely?
43- if not intermediatefileid :
44- intermediatefileid = fileid
45-
46- url = '%sapi/files/%s?key=%s' % (host , intermediatefileid , key )
47- result = connector .get (url , stream = True , verify = connector .ssl_verify if connector else True )
4846
49- (inputfile , inputfilename ) = tempfile .mkstemp (suffix = ext )
50-
51- try :
52- with os .fdopen (inputfile , "wb" ) as outputfile :
53- for chunk in result .iter_content (chunk_size = 10 * 1024 ):
54- outputfile .write (chunk )
55- return inputfilename
56- except Exception :
57- os .remove (inputfilename )
58- raise
59-
60-
61- # pylint: disable=too-many-arguments
62- def download_v2 (connector , host , token , fileid , intermediatefileid = None , ext = "" ):
63- """Download file to be processed from Clowder.
64-
65- Keyword arguments:
66- connector -- connector information, used to get missing parameters and send status updates
67- host -- the clowder host, including http and port, should end with a /
68- key -- the secret key to login to clowder
69- fileid -- the file that is currently being processed
70- intermediatefileid -- either same as fileid, or the intermediate file to be used
71- ext -- the file extension, the downloaded file will end with this extension
72- """
73-
74- connector .message_process ({"type" : "file" , "id" : fileid }, "Downloading file." )
7547
7648 # TODO: intermediateid doesn't really seem to be used here, can we remove entirely?
7749 if not intermediatefileid :
7850 intermediatefileid = fileid
7951
80- url = '%sapi/v2/files/%s' % (host , intermediatefileid )
81- headers = {"Authorization" : "Bearer " + token }
82- result = connector .get (url , stream = True , verify = connector .ssl_verify if connector else True , headers = headers )
52+ if clowder_version >= 2.0 :
53+ url = '%sapi/v2/files/%s' % (host , intermediatefileid )
54+ headers = {"Authorization" : "Bearer " + token }
55+ result = connector .get (url , stream = True , verify = connector .ssl_verify if connector else True , headers = headers )
56+
57+ (inputfile , inputfilename ) = tempfile .mkstemp (suffix = ext )
58+
59+ try :
60+ with os .fdopen (inputfile , "wb" ) as outputfile :
61+ for chunk in result .iter_content (chunk_size = 10 * 1024 ):
62+ outputfile .write (chunk )
63+ return inputfilename
64+ except Exception :
65+ os .remove (inputfilename )
66+ raise
67+ else :
68+ url = '%sapi/files/%s?key=%s' % (host , intermediatefileid , key )
69+ result = connector .get (url , stream = True , verify = connector .ssl_verify if connector else True )
70+
71+ (inputfile , inputfilename ) = tempfile .mkstemp (suffix = ext )
8372
84- (inputfile , inputfilename ) = tempfile .mkstemp (suffix = ext )
73+ try :
74+ with os .fdopen (inputfile , "wb" ) as outputfile :
75+ for chunk in result .iter_content (chunk_size = 10 * 1024 ):
76+ outputfile .write (chunk )
77+ return inputfilename
78+ except Exception :
79+ os .remove (inputfilename )
80+ raise
8581
86- try :
87- with os .fdopen (inputfile , "wb" ) as outputfile :
88- for chunk in result .iter_content (chunk_size = 10 * 1024 ):
89- outputfile .write (chunk )
90- return inputfilename
91- except Exception :
92- os .remove (inputfilename )
93- raise
9482
95- def download_info (connector , host , key , fileid ):
83+ def download_info (connector , host , key , fileid , token = None ):
9684 """Download file summary metadata from Clowder.
9785
9886 Keyword arguments:
@@ -102,30 +90,21 @@ def download_info(connector, host, key, fileid):
10290 fileid -- the file to fetch metadata of
10391 """
10492
105- url = '%sapi/files/%s/metadata?key=%s' % (host , fileid , key )
106- headers = {"Authorization" : "Bearer " + token }
107-
108- # fetch data
109- result = connector .get (url , stream = True , verify = connector .ssl_verify if connector else True )
110-
111- return result .json ()
112-
113- def download_info_v2 (connector , host , token , fileid ):
114- """Download file summary metadata from Clowder.
93+ if clowder_version >= 2.0 :
94+ url = '%sapi/v2/files/%s/metadata' % (host , fileid )
95+ headers = {"Authorization" : "Bearer " + token }
96+ # fetch data
97+ result = connector .get (url , stream = True , verify = connector .ssl_verify if connector else True , headers = headers )
11598
116- Keyword arguments:
117- connector -- connector information, used to get missing parameters and send status updates
118- host -- the clowder host, including http and port, should end with a /
119- key -- the secret key to login to clowder
120- fileid -- the file to fetch metadata of
121- """
99+ return result .json ()
100+ else :
101+ url = '%sapi/files/%s/metadata?key=%s' % (host , fileid , key )
102+ headers = {"Authorization" : "Bearer " + token }
122103
123- url = '%sapi/v2/files/%s/metadata' % (host , fileid )
124- headers = {"Authorization" : "Bearer " + token }
125- # fetch data
126- result = connector .get (url , stream = True , verify = connector .ssl_verify if connector else True , headers = headers )
104+ # fetch data
105+ result = connector .get (url , stream = True , verify = connector .ssl_verify if connector else True )
127106
128- return result .json ()
107+ return result .json ()
129108
130109
131110def download_metadata (connector , host , key , fileid , extractor = None ):
@@ -235,6 +214,8 @@ def upload_metadata(connector, host, key, fileid, metadata):
235214 connector .message_process ({"type" : "file" , "id" : fileid }, "Uploading file metadata." )
236215
237216 headers = {'Content-Type' : 'application/json' }
217+ # TODO if version 2.0
218+
238219 url = '%sapi/files/%s/metadata.jsonld?key=%s' % (host , fileid , key )
239220 result = connector .post (url , headers = headers , data = json .dumps (metadata ),
240221 verify = connector .ssl_verify if connector else True )
0 commit comments