1- import os
21import requests
2+ import logging
33
4+ from pathlib import Path
45
5- def get_authentication_token (nomad_url , username , password ):
6+ logging .basicConfig (level = logging .INFO )
7+ logger = logging .getLogger (__name__ )
8+
9+ handler = logging .StreamHandler ()
10+ formatter = logging .Formatter ("%(asctime)s - %(name)s - %(levelname)s - %(message)s" )
11+ handler .setFormatter (formatter )
12+
13+
14+ def get_authentication_token (
15+ nomad_url : str , username : str , password : str , upload_logger : logging .Logger = None
16+ ):
617 """Get the token for accessing your NOMAD unpublished uploads remotely"""
18+ if upload_logger is None :
19+ upload_logger = logger
20+
721 try :
822 response = requests .get (
923 nomad_url + "auth/token" ,
@@ -12,18 +26,23 @@ def get_authentication_token(nomad_url, username, password):
1226 )
1327 token = response .json ().get ("access_token" )
1428 if token :
29+ logger .info ("Successfully retrieved authentication token" )
1530 return token
16-
17- print ("response is missing token: " )
18- print (response .json ())
31+ logger .error ("Authentication token not found in response" )
32+ logger .error ("Response: " + str (response .json ()))
1933 return
20- except Exception :
21- print ("something went wrong trying to get authentication token" )
34+ except Exception as e :
35+ upload_logger .error (f"Something went wrong trying to get authentication token." )
36+ upload_logger .error (f"Error in dataset creation: { e } " )
2237 return
2338
2439
25- def create_dataset (nomad_url , token , dataset_name ):
40+ def create_dataset (
41+ nomad_url : str , token : str , dataset_name : str , upload_logger : logging .Logger = None
42+ ):
2643 """Create a dataset to group a series of NOMAD entries"""
44+ if upload_logger is None :
45+ upload_logger = logger
2746 try :
2847 response = requests .post (
2948 nomad_url + "datasets/" ,
@@ -34,23 +53,34 @@ def create_dataset(nomad_url, token, dataset_name):
3453 dataset_id = response .json ().get ("dataset_id" )
3554 if dataset_id :
3655 return dataset_id
37-
38- print ("response is missing dataset_id: " )
39- print (response .json ())
56+ upload_logger .error ("Dataset ID not found in response" )
57+ upload_logger .error ("Response: " + str (response .json ()))
4058 return
41- except Exception :
42- print ("something went wrong trying to create a dataset" )
59+ except Exception as e :
60+ upload_logger .error ("Something went wrong trying to create a dataset" )
61+ upload_logger .error (f"Error in dataset creation: { e } " )
4362 return
4463
4564
46- def upload_to_NOMAD (nomad_url , token , upload_file ):
65+ def upload_to_NOMAD (
66+ nomad_url : str ,
67+ token : str ,
68+ upload_file : Path ,
69+ file_name : str = None ,
70+ upload_name : str = None ,
71+ upload_logger : logging .Logger = None ,
72+ ):
4773 """Upload a single file as a new NOMAD upload. Compressed zip/tar files are
4874 automatically decompressed.
4975 """
76+ upload_name = upload_name if upload_name else upload_file .name
77+ file_name = file_name if file_name else upload_file .name
78+ if upload_logger is None :
79+ upload_logger = logger
5080 with open (upload_file , "rb" ) as f :
5181 try :
5282 response = requests .post (
53- f"{ nomad_url } uploads?file_name={ os . path . basename ( upload_file ) } " ,
83+ f"{ nomad_url } uploads?file_name={ file_name } &upload_name= { upload_name } " ,
5484 headers = {
5585 "Authorization" : f"Bearer { token } " ,
5686 "Accept" : "application/json" ,
@@ -60,34 +90,73 @@ def upload_to_NOMAD(nomad_url, token, upload_file):
6090 )
6191 upload_id = response .json ().get ("upload_id" )
6292 if upload_id :
93+ logger .info (
94+ f"Successfully uploaded { upload_file } to NOMAD with ID { upload_id } "
95+ )
6396 return upload_id
6497
65- print ( "response is missing upload_id: " )
66- print ( response .json ())
98+ logger . error ( "Upload ID not found in response " )
99+ logger . error ( "Response: " + str ( response .json () ))
67100 return
68101 except Exception :
69- print ("something went wrong uploading to NOMAD" )
102+ upload_logger .error (
103+ f"Something went wrong uploading { upload_file } to NOMAD"
104+ )
105+ upload_logger .error (f"Error in upload: { upload_file } " )
70106 return
71107
72- def trigger_reprocess_upload (nomad_url , token , upload_id ):
108+
109+ def trigger_reprocess_upload (
110+ nomad_url : str , token : str , upload_id : str , upload_logger : logging .Logger = None
111+ ):
73112 """Trigger reprocessing of an upload"""
113+ if upload_logger is None :
114+ upload_logger = logger
74115 try :
75116 response = requests .post (
76117 f"{ nomad_url } uploads/{ upload_id } /action/process" ,
77118 headers = {"Authorization" : f"Bearer { token } " , "Accept" : "application/json" },
78119 timeout = 30 ,
79120 )
80121 return response
81- except Exception :
82- print ("something went wrong trying to reprocess upload: " + upload_id )
122+ except Exception as e :
123+ upload_logger .error (
124+ "Something went wrong trying to reprocess upload: " + upload_id
125+ )
126+ upload_logger .error (f"Error in reprocess: { e } " )
127+ return
128+
129+
130+ def delete_upload (
131+ nomad_url : str , token : str , upload_id : str , upload_logger : logging .Logger = None
132+ ):
133+ """Delete an upload"""
134+ if upload_logger is None :
135+ upload_logger = logger
136+ try :
137+ response = requests .delete (
138+ nomad_url + "uploads/" + upload_id ,
139+ headers = {"Authorization" : f"Bearer { token } " , "Accept" : "application/json" },
140+ timeout = 30 ,
141+ )
142+ return response
143+ except Exception as e :
144+ upload_logger .error (
145+ "Something went wrong trying to delete upload: " + upload_id
146+ )
147+ upload_logger .error (f"Error in delete: { e } " )
83148 return
84149
85150
86- def check_upload_status (nomad_url , token , upload_id ):
151+ def check_upload_status (
152+ nomad_url : str , token : str , upload_id : str , upload_logger : logging .Logger = None
153+ ):
87154 """
88155 # upload success => returns 'Process publish_upload completed successfully'
89156 # publish success => 'Process publish_upload completed successfully'
90157 """
158+ if upload_logger is None :
159+ upload_logger = logger
91160 try :
92161 response = requests .get (
93162 nomad_url + "uploads/" + upload_id ,
@@ -98,16 +167,23 @@ def check_upload_status(nomad_url, token, upload_id):
98167 if status_message :
99168 return status_message
100169
101- print ( "response is missing status_message: " )
102- print ( response .json ())
170+ upload_logger . error ( "Upload status message not found in response " )
171+ upload_logger . error ( "Response: " + str ( response .json () ))
103172 return
104- except Exception :
105- print ("something went wrong trying to check the status of upload" + upload_id )
106- # upload gets deleted from the upload staging area once published...or in this case something went wrong
173+ except Exception as e :
174+ upload_logger .error (
175+ "Something went wrong trying to check the status of upload: " + upload_id
176+ )
107177 return
108178
109179
110- def edit_upload_metadata (nomad_url , token , upload_id , metadata ):
180+ def edit_upload_metadata (
181+ nomad_url : str ,
182+ token : str ,
183+ upload_id : str ,
184+ metadata : dict ,
185+ upload_logger : logging .Logger = None ,
186+ ):
111187 """
112188 Example of new metadata:
113189 upload_name = 'Test_Upload_Name'
@@ -122,7 +198,8 @@ def edit_upload_metadata(nomad_url, token, upload_id, metadata):
122198 },
123199 }
124200 """
125-
201+ if upload_logger is None :
202+ upload_logger = logger
126203 try :
127204 response = requests .post (
128205 nomad_url + "uploads/" + upload_id + "/edit" ,
@@ -132,19 +209,29 @@ def edit_upload_metadata(nomad_url, token, upload_id, metadata):
132209 )
133210 return response
134211 except Exception :
135- print ("something went wrong trying to add metadata to upload" + upload_id )
212+ upload_logger .error (
213+ "Something went wrong trying to edit metadata of upload: " + upload_id
214+ )
215+ upload_logger .error (f"Error in metadata edit: { upload_id } " )
136216 return
137217
138218
139- def publish_upload (nomad_url , token , upload_id ):
219+ def publish_upload (
220+ nomad_url : str , token : str , upload_id : str , upload_logger : logging .Logger = None
221+ ):
140222 """Publish an upload"""
223+ if upload_logger is None :
224+ upload_logger = logger
141225 try :
142226 response = requests .post (
143227 nomad_url + "uploads/" + upload_id + "/action/publish" ,
144228 headers = {"Authorization" : f"Bearer { token } " , "Accept" : "application/json" },
145229 timeout = 30 ,
146230 )
147231 return response
148- except Exception :
149- print ("something went wrong trying to publish upload: " + upload_id )
232+ except Exception as e :
233+ upload_logger .error (
234+ "Something went wrong trying to publish upload: " + upload_id
235+ )
236+ upload_logger .error (f"Error in publish: { e } " )
150237 return
0 commit comments