-
Notifications
You must be signed in to change notification settings - Fork 1
/
motu_api.py.1654795092.filtered.1013074087.formatted
executable file
·480 lines (388 loc) · 19.1 KB
/
motu_api.py.1654795092.filtered.1013074087.formatted
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
#! /usr/bin/env python
# -*- coding: utf-8 -*-
#
# Python motu client v.1.0.3
#
# Motu, a high efficient, robust and Standard compliant Web Server for Geographic
# Data Dissemination.
#
# http://cls-motu.sourceforge.net/
#
# (C) Copyright 2009-2010, by CLS (Collecte Localisation Satellites) -
# http://www.cls.fr - and Contributors
#
#
# This library is free software; you can redistribute it and/or modify it
# under the terms of the GNU Lesser General Public License as published by
# the Free Software Foundation; either version 2.1 of the License, or
# (at your option) any later version.
#
# This library is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
# or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
# License for more details.
#
# You should have received a copy of the GNU Lesser General Public License
# along with this library; if not, write to the Free Software Foundation,
# Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
import urlparse # WARNING : The urlparse module is renamed to urllib.parse
import urllib
import urllib2
import traceback
import platform
import sys
import httplib
import os
import re
import tempfile
import datetime
import time
import shutil
import zipfile
import logging
import logging.config
import ConfigParser
import optparse
import socket
# Import project libraries
import utils_log
import utils_unit
import utils_stream
import utils_http
import utils_messages
import utils_cas
import utils_collection
import stop_watch
# constant for authentication modes
AUTHENTICATION_MODE_NONE = 'none'
AUTHENTICATION_MODE_BASIC = 'basic'
AUTHENTICATION_MODE_CAS = 'cas'
# constant for date time string format
DATETIME_FORMAT = "%Y-%m-%d% %H:%M:%S"
# shared logger
log = None
def get_client_version():
"""Return the version (as a string) of this client.
The value is automatically set by the maven processing build, so don't
touch it unless you know what you are doing."""
return '1.0.3'
def get_client_artefact():
"""Return the artifact identifier (as a string) of this client.
The value is automatically set by the maven processing build, so don't
touch it unless you know what you are doing."""
return 'motu-client-python'
def build_params(_options):
"""Function that builds the query string for Motu according to the given options"""
temporal = ''
geographic = ''
vertical = ''
other_opt = ''
"""
Build the main url to connect to
"""
query_options = utils_collection.ListMultimap()
query_options.insert( action = 'productdownload',
mode = 'console',
service = _options.service_id,
product = _options.product_id
)
if _options.extraction_geographic:
query_options.insert( x_lo = _options.longitude_min,
x_hi = _options.longitude_max,
y_lo = _options.latitude_min,
y_hi = _options.latitude_max
)
if _options.extraction_vertical:
query_options.insert( z_lo = _options.depth_min,
z_hi = _options.depth_max
)
if _options.extraction_temporal:
# date are strings, and they are send to Motu "as is". If not, we convert them into string
if _options.date_min is not None or _options.date_min != None:
date_min = _options.date_min
if not isinstance(date_min, basestring):
date_min = date_min.strftime(DATETIME_FORMAT)
query_options.insert( t_lo = date_min )
if _options.date_max is not None or _options.date_max != None:
date_max = _options.date_max
if not isinstance(date_max, basestring):
date_max = date_max.strftime(DATETIME_FORMAT)
query_options.insert( t_hi = date_max )
variable = _options.variable
if variable is not None:
for i, opt in enumerate(variable):
query_options.insert( variable = opt )
return utils_http.encode(query_options)
def check_options(_options):
"""function that checks the given options for coherency."""
# Check Mandatory Options
if (_options.auth_mode != AUTHENTICATION_MODE_NONE and
_options.auth_mode != AUTHENTICATION_MODE_BASIC and
_options.auth_mode != AUTHENTICATION_MODE_CAS):
raise Exception(utils_messages.get_external_messages()['motu-client.exception.option.invalid'] % ( _options.auth_mode, 'auth-mode', [AUTHENTICATION_MODE_NONE, AUTHENTICATION_MODE_BASIC, AUTHENTICATION_MODE_CAS]) )
# if authentication mode is set we check both user & password presence
if (_options.user == None and
_options.auth_mode != AUTHENTICATION_MODE_NONE):
raise Exception(utils_messages.get_external_messages()['motu-client.exception.option.mandatory.user'] % ('user',_options.auth_mode))
# check that if a user is set, a password should be set also
if (_options.pwd == None and
_options.user != None):
raise Exception(utils_messages.get_external_messages()['motu-client.exception.option.mandatory.password'] % ( 'pwd', _options.user ) )
#check that if a user is set, an authentication mode should also be set
if (_options.user != None and
_options.auth_mode == AUTHENTICATION_MODE_NONE):
raise Exception(utils_messages.get_external_messages()['motu-client.exception.option.mandatory.mode'] % ( AUTHENTICATION_MODE_NONE, 'auth-mode', _options.user ) )
# those following parameters are required
if _options.motu == None :
raise Exception(utils_messages.get_external_messages()['motu-client.exception.option.mandatory'] % 'motu')
if _options.service_id == None :
raise Exception(utils_messages.get_external_messages()['motu-client.exception.option.mandatory'] % 'service-id')
if _options.product_id == None :
raise Exception(utils_messages.get_external_messages()['motu-client.exception.option.mandatory'] % 'product-id')
if _options.out_dir == None :
raise Exception(utils_messages.get_external_messages()['motu-client.exception.option.mandatory'] % 'out-dir')
out_dir = _options.out_dir
# check directory existence
if not os.path.exists(out_dir):
raise Exception(utils_messages.get_external_messages()['motu-client.exception.option.outdir-notexist'] % out_dir)
# check whether directory is writable or not
if not os.access(out_dir, os.W_OK):
raise Exception(utils_messages.get_external_messages()['motu-client.exception.option.outdir-notwritable'] % out_dir)
if _options.out_name == None :
raise Exception(utils_messages.get_external_messages()['motu-client.exception.option.mandatory'] % 'out-name')
# Check PROXY Options
_options.proxy = False
if (_options.proxy_server != None) and (len(_options.proxy_server) != 0):
_options.proxy = True
# check that proxy server is a valid url
url = _options.proxy_server
p = re.compile('^(ftp|http|https):\/\/(\w+:{0,1}\w*@)?(\S+)(:[0-9]+)?(\/|\/([\w#!:.?+=&%@!\-\/]))?')
m = p.match(url)
if not m :
raise Exception( utils_messages.get_external_messages()['motu-client.exception.option.not-url'] % ( 'proxy-server', url ) )
# check that if proxy-user is defined then proxy-pwd shall be also, and reciprocally.
if (_options.proxy_user != None) != ( _options.proxy_pwd != None ) :
raise Exception( utils_messages.get_external_messages()['motu-client.exception.option.linked'] % ('proxy-user', 'proxy-name') )
# Check VERTICAL Options
_options.extraction_vertical = False
if _options.depth_min != None or _options.depth_max != None :
_options.extraction_vertical = True
# Check TEMPORAL Options
_options.extraction_temporal = False
if _options.date_min != None or _options.date_max != None :
_options.extraction_temporal = True
# Check GEOGRAPHIC Options
_options.extraction_geographic = False
if _options.latitude_min != None or _options.latitude_max != None or _options.longitude_min != None or _options.longitude_max != None :
_options.extraction_geographic = True
if( _options.latitude_min == None ):
raise Exception(utils_messages.get_external_messages()['motu-client.exception.option.geographic-box'] % 'latitude_min' )
if( _options.latitude_max == None ):
raise Exception(utils_messages.get_external_messages()['motu-client.exception.option.geographic-box'] % 'latitude_max' )
if( _options.longitude_min == None ):
raise Exception(utils_messages.get_external_messages()['motu-client.exception.option.geographic-box'] % 'longitude_min' )
if( _options.longitude_max == None ):
raise Exception(utils_messages.get_external_messages()['motu-client.exception.option.geographic-box'] % 'longitude_max' )
tempvalue = float(_options.latitude_min)
if tempvalue < -90 or tempvalue > 90 :
raise Exception( utils_messages.get_external_messages()['motu-client.exception.option.out-of-range'] % ( 'latitude_min', str(tempvalue)) )
tempvalue = float(_options.latitude_max)
if tempvalue < -90 or tempvalue > 90 :
raise Exception(utils_messages.get_external_messages()['motu-client.exception.option.out-of-range'] % ( 'latitude_max', str(tempvalue)))
tempvalue = float(_options.longitude_min)
if tempvalue < -180 or tempvalue > 180 :
raise Exception(utils_messages.get_external_messages()['motu-client.exception.option.out-of-range'] % ( 'logitude_min', str(tempvalue)))
tempvalue = float(_options.longitude_max)
if tempvalue < -180 or tempvalue > 180 :
raise Exception(utils_messages.get_external_messages()['motu-client.exception.option.out-of-range'] % ( 'longitude_max', str(tempvalue)))
def total_seconds(td):
return total_milliseconds(td) / 10**3
def total_milliseconds(td):
return (td.microseconds + (td.seconds + td.days * 24 * 3600) * 10**6) / 10**3
def get_url_config(_options, data = None):
# prepare arguments
kargs = {}
# proxy
if _options.proxy:
#proxyUrl = _options.proxy_server.partition(':')
proxyUrl = urlparse.urlparse(_options.proxy_server)
kargs['proxy'] = { "scheme": proxyUrl.scheme,
"netloc": proxyUrl.netloc }
if _options.proxy_user != None:
kargs['proxy']['user'] = _options.proxy_user
kargs['proxy']['password'] = _options.proxy_pwd
# authentication
if _options.auth_mode == AUTHENTICATION_MODE_BASIC:
kargs['authentication'] = { 'mode' : 'basic',
'user' : _options.user,
'password': _options.pwd }
# headers
kargs['headers'] = {"X-Client-Id" : get_client_artefact(),
"X-Client-Version": get_client_version()}
# data
if data != None:
kargs['data'] = data
return kargs
def dl_2_file(dl_url, fh, block_size = 65535, describe = 'None', **options):
""" Download the file with the main url (of Motu) file.
Motu can return an error message in the response stream without setting an
appropriate http error code. So, in that case, the content-type response is
checked, and if it is text/plain, we consider this as an error.
dl_url: the complete download url of Motu
fh: file handler to use to write the downstream"""
stopWatch = stop_watch.localThreadStopWatch()
start_time = datetime.datetime.now()
log.info( "Requesting file to download (this can take a while)..." )
temp = open(fh, 'w+b')
try:
stopWatch.start('processing')
m = utils_http.open_url(dl_url, **options)
try:
# check the real url (after potential redirection) is not a CAS Url scheme
match = re.search(utils_cas.CAS_URL_PATTERN, m.url)
if match is not None:
service, _, _ = dl_url.partition('?')
redirection, _, _ = m.url.partition('?')
raise Exception(utils_messages.get_external_messages()['motu-client.exception.authentication.redirected'] % (service, redirection) )
# check that content type is not text/plain
headers = m.info()
if "Content-Type" in headers:
if len(headers['Content-Type']) > 0:
if headers['Content-Type'].startswith('text') or headers['Content-Type'].find('html') != -1:
raise Exception( utils_messages.get_external_messages()['motu-client.exception.motu.error'] % m.read() )
log.info( 'File type: %s' % headers['Content-Type'] )
# check if a content length (size of the file) has been send
if "Content-Length" in headers:
try:
# it should be an integer
size = int(headers["Content-Length"])
log.info( 'File size: %s (%i B)' % ( utils_unit.convert_bytes(size), size ) )
except Exception, e:
size = -1
log.warn( 'File size is not an integer: %s' % headers["Content-Length"] )
else:
size = -1
log.warn( 'File size: %s' % 'unknown' )
processing_time = datetime.datetime.now();
stopWatch.stop('processing')
stopWatch.start('downloading')
# performs the download
log.info( 'Downloading file %s' % os.path.abspath(fh) )
def progress_function(sizeRead):
percent = sizeRead*100./size
log.info( "- %s (%.1f%%)", utils_unit.convert_bytes(size).rjust(8), percent )
td = datetime.datetime.now()- start_time;
def none_function(sizeRead):
percent = 100
log.info( "- %s (%.1f%%)", utils_unit.convert_bytes(size).rjust(8), percent )
td = datetime.datetime.now()- start_time;
read = utils_stream.copy(m,temp,progress_function if size != -1 else none_function, block_size )
end_time = datetime.datetime.now()
stopWatch.stop('downloading')
log.info( "Processing time : %s", str(processing_time - start_time) )
log.info( "Downloading time : %s", str(end_time - processing_time) )
log.info( "Total time : %s", str(end_time - start_time) )
log.info( "Download rate : %s/s", utils_unit.convert_bytes((read / total_milliseconds(end_time - start_time)) * 10**3) )
finally:
m.close()
finally:
temp.flush()
temp.close()
# raise exception if actual size does not match content-length header
if size >= 0 and read < size:
raise ContentTooShortError( utils_messages.get_external_messages()['motu-client.exception.download.too-short'] % (read, size), result)
def execute_request(_options):
"""
the main function that submit a request to motu. Available options are:
* Proxy configuration (with eventually user credentials)
- proxy_server: 'http://my-proxy.site.com:8080'
- proxy_user : 'john'
- proxy_pwd :'doe'
* Autorisation mode: 'cas', 'basic', 'none'
- auth_mode: 'cas'
* User credentials for authentication 'cas' or 'basic'
- user: 'john'
- pwd: 'doe'
* Motu service URL
- motu: 'http://atoll-dev.cls.fr:30080/mis-gateway-servlet/Motu'
* Dataset identifier to download
- product_id: 'dataset-duacs-global-nrt-madt-merged-h'
* Service identifier to use for retrieving dataset
- service_id: 'http://purl.org/myocean/ontology/service/database#yourduname'
* Geographic extraction parameters
- latitude_max : 10.0
- latitude_min : -10.0
- longitude_max: -0.333333333369
- longitude_min: 0.0
* Vertical extraction parameters
- depth_max: 1000
- depth_min: 0
* Temporal extraction parameters, as a datetime instance or a string (format: '%Y-%m-%d %H:%M:%S')
- date_max: 2010-04-25 12:05:36
- date_min: 2010-04-25
* Variable extraction
- variable: ['variable1','variable2']
* The file name and the directory of the downloaded dataset
- out_dir : '.'
- out_name: 'dataset'
* The block size used to perform download
- block_size: 12001
* The socket timeout configuration
- socket_timeout: 515
* The user agent to use when performing http requests
- user_agent: 'motu-api-client'
"""
global log
stopWatch = stop_watch.localThreadStopWatch()
stopWatch.start()
try:
log = logging.getLogger("motu_api")
# at first, we check given options are ok
check_options(_options)
# print some trace info about the options set
log.log( utils_log.TRACE_LEVEL, '-'*60 )
for option in dir(_options):
if not option.startswith('_'):
log.log(utils_log.TRACE_LEVEL, "%s=%s" % (option, getattr( _options, option ) ) )
log.log( utils_log.TRACE_LEVEL, '-'*60 )
# start of url to invoke
url_service = _options.motu
# parameters of the invoked service
url_params = build_params(_options)
url_config = get_url_config(_options)
# check if question mark is in the url
questionMark = '?'
if url_service.endswith(questionMark) :
questionMark = ''
url = url_service+questionMark+url_params
if _options.describe == True:
url = url.replace('productdownload','describeProduct')
_options.out_name = _options.out_name.replace('.nc','.xml')
# set-up the socket timeout if any
if _options.socket_timeout != None:
log.debug("Setting timeout %s" % _options.socket_timeout)
socket.setdefaulttimeout(_options.socket_timeout)
if _options.auth_mode == AUTHENTICATION_MODE_CAS:
stopWatch.start('authentication')
# perform authentication before acceding service
download_url = utils_cas.authenticate_CAS_for_URL(url,
_options.user,
_options.pwd,**url_config)
stopWatch.stop('authentication')
else:
# if none, we do nothing more, in basic, we let the url requester doing the job
download_url = url
# create a file for storing downloaded stream
fh = os.path.join(_options.out_dir,_options.out_name)
try:
dl_2_file(download_url, fh, _options.block_size, _options.describe, **url_config)
log.info( "Done" )
except:
try:
if (os.path.isfile(fh)):
os.remove(fh)
except:
pass
raise
finally:
stopWatch.stop()