import time from java.util import Collections from java.lang import System from org.das2.fsm import FileStorageModel from org.das2.util.filesystem import FileSystem setScriptDescription('''Demonstrate parsing and formatting of URI_Templates. For parsing, we list a remote website and interpret the times for each file. For formatting, the names are generated.''') setScriptTitle('URI_Templates') # see also https://github.com/autoplot/dev/blob/master/demos/2024/20240802/URI_Templates.jy resourceURI= getParam( 'resourceURI', 'http://autoplot.org/data/C1_CP_EDI_EGD__$Y$m$d_V03.cef', 'example file to load', [ 'data_$Y_$(m)_$(d;delta=7;phasestart=2019-05-05).qds', 'http://autoplot.org/data/C1_CP_EDI_EGD__$Y$m$d_V03.cef', 'C1_CP_EDI_EGD__$Y$m$d_$(enum,values=A|B).cef' ] ) timerange= getParam( 'timerange', '2005-02-12/2005-02-19', 'timerange to load' ) generate= getParam( 'generate', False, 'Generation doesn''t list remote folders', [True,False] ) if ( len(timerange.split(' '))==10 ): # make it easier to use Jon V.'s document "uri_template_test_cases.txt" trs= timerange.split(' ') timerange= '-'.join( trs[0:3] ) + 'T' + ':'.join( trs[3:5] ) + '/' + '-'.join( trs[5:8] ) + 'T' + ':'.join( trs[8:10] ) drtr= DatumRangeUtil.parseTimeRange(timerange) count=0 if ( False ): print params print '# resourceURI='+resourceURI print '# timerange='+timerange print '# generate='+generate print '' t0= System.currentTimeMillis() if ( generate ): if ( resourceURI.find('$v')>-1 ): print 'Template cannot contain $v.' count=-1 else: tp= TimeParser.create(resourceURI) i1= resourceURI.find('$(enum') if ( i1>-1 ): ix= resourceURI.find('$(enum',i1+6) if (ix>-1 ): print 'Template can only contain one $(enum).' count=-1 else: fh= tp.getFieldHandlerByCode('enum') enums= fh.getValues() id= fh.getId() else: enums= [''] id= '' st= tp.format( drtr.min(), None, Collections.singletonMap( id, enums[0] ) ) dr= tp.parse( st,None ).getTimeRange() print( "

Generate Result

") print '

'+resourceURI+'

' print '

search limited to '+timerange+'

' print '' print '' while ( count<=10000 and dr.min().lt( drtr.max() ) ): for enum in enums: st= tp.format(dr.min(), dr.max(), Collections.singletonMap( id, enum ) ) print '' count= count+1 if ( count>10000 ): print '' break dr= dr.next() else: i= FileStorageModel.splitIndex( resourceURI ) root= resourceURI[0:i] # the static part of the name template= resourceURI[i:] # the templated part of the name fs= FileSystem.create( root ) fsm= FileStorageModel.create( fs, template ) names= fsm.getNamesFor( DatumRangeUtil.parseTimeRange(timerange) ) print( "

Parse Result

") print '

'+resourceURI+'

' print '

search limited to '+timerange+'

' print '
Time RangeGenerated filename
'+ dr.toString() + ''+st + '
Search limited to 10000 results.
' print '' for n in names: print '' tr= fsm.getRangeFor( n ) try: v= fsm.getField('v',n) except: v= 'N/A' ll= '' + root + n + '' print '' count= count+1 print '' print '
FilenameTime RangeVersion
'+ll + ''+tr.toString() + '' + v +'
' if ( count==0 ): print '(none found)' dt= System.currentTimeMillis() - t0 print '%d results calculated in %d milliseconds.\n' % ( count,dt ) print ''