scc_access/scc_access.py

branch
send_rs_data
changeset 20
111f05973553
parent 16
ddaea4327bd5
child 21
4fcec88bfe3d
equal deleted inserted replaced
16:ddaea4327bd5 20:111f05973553
23 23
24 class SCC: 24 class SCC:
25 """ A simple class that will attempt to upload a file on the SCC server. 25 """ A simple class that will attempt to upload a file on the SCC server.
26 26
27 The uploading is done by simulating a normal browser session. In the current 27 The uploading is done by simulating a normal browser session. In the current
28 version no check is performed, and no feedback is given if the upload 28 version no check is performed, and no feedback is given if the upload
29 was successful. If everything is setup correctly, it will work. 29 was successful. If everything is setup correctly, it will work.
30 """ 30 """
31 31
32 def __init__(self, auth, output_dir, base_url): 32 def __init__(self, auth, output_dir, base_url):
33 self.auth = auth 33 self.auth = auth
34 self.output_dir = output_dir 34 self.output_dir = output_dir
61 if login_page.status_code != 200: 61 if login_page.status_code != 200:
62 logger.error('Could not access login pages. Status code %s' % login_page.status_code) 62 logger.error('Could not access login pages. Status code %s' % login_page.status_code)
63 sys.exit(1) 63 sys.exit(1)
64 64
65 logger.debug("Submiting credentials.") 65 logger.debug("Submiting credentials.")
66 66
67 # Submit the login data 67 # Submit the login data
68 login_submit = self.session.post(self.login_url, 68 login_submit = self.session.post(self.login_url,
69 data=self.login_credentials, 69 data=self.login_credentials,
70 headers={'X-CSRFToken': login_page.cookies['csrftoken'], 70 headers={'X-CSRFToken': login_page.cookies['csrftoken'],
71 'referer': self.login_url}, 71 'referer': self.login_url},
74 return login_submit 74 return login_submit
75 75
76 def logout(self): 76 def logout(self):
77 pass 77 pass
78 78
79 def upload_file(self, filename, system_id): 79 def upload_file(self, filename, system_id, rs_filename=None):
80 """ Upload a filename for processing with a specific system. If the 80 """ Upload a filename for processing with a specific system. If the
81 upload is successful, it returns the measurement id. """ 81 upload is successful, it returns the measurement id. """
82 # Get submit page 82 # Get submit page
83 upload_page = self.session.get(self.upload_url, 83 upload_page = self.session.get(self.upload_url,
84 auth=self.auth, 84 auth=self.auth,
85 verify=False) 85 verify=False)
86 86
87 # Submit the data 87 # Submit the data
88 upload_data = {'system': system_id} 88 upload_data = {'system': system_id}
89 files = {'data': open(filename, 'rb')} 89 files = {'data': open(filename, 'rb')}
90
91 if rs_filename is not None:
92 files['sounding_file'] = open(rs_filename, 'rb')
90 93
91 logger.info("Uploading of file %s started." % filename) 94 logger.info("Uploading of file %s started." % filename)
92 95
93 upload_submit = self.session.post(self.upload_url, 96 upload_submit = self.session.post(self.upload_url,
94 data=upload_data, 97 data=upload_data,
112 115
113 return measurement_id 116 return measurement_id
114 117
115 def download_files(self, measurement_id, subdir, download_url): 118 def download_files(self, measurement_id, subdir, download_url):
116 """ Downloads some files from the download_url to the specified 119 """ Downloads some files from the download_url to the specified
117 subdir. This method is used to download preprocessed file, optical 120 subdir. This method is used to download preprocessed file, optical
118 files etc. 121 files etc.
119 """ 122 """
120 # Get the file 123 # Get the file
121 request = self.session.get(download_url, auth=self.auth, 124 request = self.session.get(download_url, auth=self.auth,
122 verify=False, 125 verify=False,
274 logger.error("No measurement with id %s found on the SCC." % measurement_id) 277 logger.error("No measurement with id %s found on the SCC." % measurement_id)
275 return None 278 return None
276 279
277 def delete_measurement(self, measurement_id): 280 def delete_measurement(self, measurement_id):
278 """ Deletes a measurement with the provided measurement id. The user 281 """ Deletes a measurement with the provided measurement id. The user
279 should have the appropriate permissions. 282 should have the appropriate permissions.
280 283
281 The procedures is performed directly through the web interface and 284 The procedures is performed directly through the web interface and
282 NOT through the API. 285 NOT through the API.
283 """ 286 """
284 # Get the measurement object 287 # Get the measurement object
285 measurement = self.get_measurement(measurement_id) 288 measurement = self.get_measurement(measurement_id)
335 338
336 return measurements 339 return measurements
337 340
338 def measurement_id_for_date(self, t1, call_sign='bu', base_number=0): 341 def measurement_id_for_date(self, t1, call_sign='bu', base_number=0):
339 """ Give the first available measurement id on the SCC for the specific 342 """ Give the first available measurement id on the SCC for the specific
340 date. 343 date.
341 """ 344 """
342 date_str = t1.strftime('%Y%m%d') 345 date_str = t1.strftime('%Y%m%d')
343 search_url = urlparse.urljoin(self.api_base_url, 'measurements/?id__startswith=%s' % date_str) 346 search_url = urlparse.urljoin(self.api_base_url, 'measurements/?id__startswith=%s' % date_str)
344 347
345 response = self.session.get(search_url, 348 response = self.session.get(search_url,
413 self.upload, 416 self.upload,
414 self.pre_processing, 417 self.pre_processing,
415 self.processing) 418 self.processing)
416 419
417 420
418 def upload_file(filename, system_id, settings): 421 def upload_file(filename, system_id, settings, rs_filename=None):
419 """ Shortcut function to upload a file to the SCC. """ 422 """ Shortcut function to upload a file to the SCC. """
420 logger.info("Uploading file %s, using sytem %s" % (filename, system_id)) 423 logger.info("Uploading file %s, using sytem %s" % (filename, system_id))
421 424
422 scc = SCC(settings['basic_credentials'], settings['output_dir'], settings['base_url']) 425 scc = SCC(settings['basic_credentials'], settings['output_dir'], settings['base_url'])
423 scc.login(settings['website_credentials']) 426 scc.login(settings['website_credentials'])
424 measurement_id = scc.upload_file(filename, system_id) 427 measurement_id = scc.upload_file(filename, system_id, rs_filename=rs_filename)
425 scc.logout() 428 scc.logout()
426 return measurement_id 429 return measurement_id
427 430
428 431
429 def process_file(filename, system_id, settings): 432 def process_file(filename, system_id, settings):
500 action="store_true") 503 action="store_true")
501 parser.add_argument("--delete", help="Measurement ID to delete.") 504 parser.add_argument("--delete", help="Measurement ID to delete.")
502 parser.add_argument("--rerun-all", help="Measurement ID to rerun.") 505 parser.add_argument("--rerun-all", help="Measurement ID to rerun.")
503 parser.add_argument("--rerun-processing", help="Measurement ID to rerun processing routines.") 506 parser.add_argument("--rerun-processing", help="Measurement ID to rerun processing routines.")
504 507
508 # others files
509 parser.add_argument("--radiosounding", default=None, help="Radiosounding file name or path")
510
505 # Verbosity settings from http://stackoverflow.com/a/20663028 511 # Verbosity settings from http://stackoverflow.com/a/20663028
506 parser.add_argument('-d', '--debug', help="Print debugging information.", action="store_const", 512 parser.add_argument('-d', '--debug', help="Print debugging information.", action="store_const",
507 dest="loglevel", const=logging.DEBUG, default=logging.INFO, 513 dest="loglevel", const=logging.DEBUG, default=logging.INFO,
508 ) 514 )
509 parser.add_argument('-s', '--silent', help="Show only warning and error messages.", action="store_const", 515 parser.add_argument('-s', '--silent', help="Show only warning and error messages.", action="store_const",
530 parser.error('Provide a valid filename and system parameters.\nRun with -h for help.\n') 536 parser.error('Provide a valid filename and system parameters.\nRun with -h for help.\n')
531 537
532 if args.process: 538 if args.process:
533 process_file(args.filename, args.system, settings) 539 process_file(args.filename, args.system, settings)
534 else: 540 else:
535 upload_file(args.filename, args.system, settings) 541 upload_file(args.filename, args.system, settings, rs_filename=args.radiosounding)

mercurial