diff --git a/hapi-server3.py b/hapi-server3.py index 3ff7b2b..a1e20e5 100644 --- a/hapi-server3.py +++ b/hapi-server3.py @@ -182,12 +182,14 @@ if api_datatype == 'aws': import s3netcdf + +#if HAPI_HOME.startswith("s3:"): +from smart_open import open ### GET HAPI VERSION we need to support # (mostly needed for id/dataset, time.min/start, time.max/stop keywords) -fin=open( HAPI_HOME + 'capabilities.json','r') -jset=json.loads(fin.read()) -fin.close() +with open( HAPI_HOME + 'capabilities.json','r') as fin: + jset=json.loads(fin.read()) # below now moved to info/*.json instead of capabilities.json ### potential "x_*" parameters in capabilities.json extracted here @@ -380,7 +382,7 @@ def generic_check_error(id, timemin, timemax, parameters): #print("debug: errorcode is ",errorcode) return(errorcode,qtimemin,qtimemax) -def do_write_info( s, id, parameters, prefix ): +def do_write_info( stream, id, parameters, prefix ): try: infoJson= open( HAPI_HOME + 'info/' + id + '.json' ).read() ##import json @@ -396,11 +398,11 @@ def do_write_info( s, id, parameters, prefix ): infoJson= json.dumps( infoJsonModel, indent=4, separators=(',', ': ')) for l in infoJson.split('\n'): l= do_info_macros(l) - if ( prefix!=None ): s.wfile.write(bytes(prefix,"utf-8")) - s.wfile.write(bytes(l,"utf-8")) - s.wfile.write(bytes('\n',"utf-8")) + if ( prefix!=None ): stream.wfile.write(bytes(prefix,"utf-8")) + stream.wfile.write(bytes(l,"utf-8")) + stream.wfile.write(bytes('\n',"utf-8")) except: - send_exception(s.wfile,'Not Found') + send_exception(stream.wfile,'Not Found') def get_last_modified( id, timemin, timemax ): '''return the time stamp of the most recently modified file, @@ -660,8 +662,8 @@ def get_lastModified(api_datatype, id, timemin, timemax): lastModified=time.time() return(lastModified) -def fetch_modifiedsince(s): - lms= s.headers['If-Modified-Since'] +def fetch_modifiedsince(stream): + lms= stream.headers['If-Modified-Since'] ###from email.utils import parsedate_tz,formatdate #import time timecomponents= parsedate_tz(lms) @@ -717,7 +719,7 @@ def prep_data(query, tags): ## xopts = jset['x_customRequestOptions'] return(parameters, xopts, mydata, check_error) -def print_hapi_intropage(hapi_version, s): +def print_hapi_intropage(hapi_version, stream): if hapi_version >= 3: datasetkey = 'dataset' startkey = 'start' @@ -727,19 +729,19 @@ def print_hapi_intropage(hapi_version, s): datasetkey = 'id' startkey = 'time.min' stopkey = 'time.max' - s.wfile.write(bytes("
"+USE_CASE+" Catalogs:\n","utf-8")) + stream.wfile.write(bytes("
"+USE_CASE+" Catalogs:\n","utf-8")) u= "/hapi/catalog" - s.wfile.write(bytes("%s
\n" % ( u,u ) ,"utf-8")) - s.wfile.write(bytes("HAPI requests:
\n","utf-8")) + stream.wfile.write(bytes("%s\n" % ( u,u ) ,"utf-8")) + stream.wfile.write(bytes("HAPI requests:
\n","utf-8")) ff= glob.glob( HAPI_HOME + 'info/*.json' ) n= len( HAPI_HOME + 'info/' ) for f in sorted(ff): (stat,mydata)=fetch_info_params(f,True) u= "/hapi/info?%s=%s" % ( datasetkey, f[n:-5] ) - s.wfile.write(bytes("%s\n" % ( u,u ) ,"utf-8")) + stream.wfile.write(bytes("%s\n" % ( u,u ) ,"utf-8")) # also extract dates etc from file #print("debug: checking info file ",f) #print("debug:",mydata) @@ -753,48 +755,48 @@ def print_hapi_intropage(hapi_version, s): u= "/hapi/data?%s=%s&%s=%s&%s=%s" % ( datasetkey, f[n:-5], startkey, timemin, stopkey, timemax ) u= do_info_macros_var(u) - s.wfile.write(bytes("%s\n" % ( u,u ) ,"utf-8")) - s.wfile.write(bytes("Parameters:\n","utf-8")) - s.wfile.write(bytes("| %s: | " % (para['name']), "utf-8")) + #stream.wfile.write(bytes("|||
| %s: | " % (para['name']), "utf-8")) parakeys = sorted(para.keys()) parakeys.remove("name") for parakey in parakeys: - s.wfile.write(bytes("","utf-8")) - s.wfile.write(bytes("%s %s " % + stream.wfile.write(bytes(" | ","utf-8")) + stream.wfile.write(bytes("%s %s " % (parakey, para[parakey]), "utf-8")) - s.wfile.write(bytes(" | ","utf-8")) - s.wfile.write(bytes("
HAPI Server for " + USE_CASE + ", visit /hapi/ for data.\n","utf-8")) - s.wfile.write(bytes("\n","utf-8")) + stream.wfile.write(bytes("