1 | """ |
---|
2 | Demonstration handle dump for CMIP/ESGF files .. |
---|
3 | |
---|
4 | -h: print this message; |
---|
5 | -v: print version; |
---|
6 | -t: run a test; |
---|
7 | -f <file name>: examine file, print path to replacement if this file is obsolete, print path to sibling files. |
---|
8 | """ |
---|
9 | |
---|
10 | import string, collections, os |
---|
11 | import ncq3 |
---|
12 | try: |
---|
13 | import urllib |
---|
14 | from urllib import request |
---|
15 | except: |
---|
16 | import requests |
---|
17 | |
---|
18 | from testdata import * |
---|
19 | |
---|
20 | class phandle(object): |
---|
21 | |
---|
22 | def __init__(self, hdlDict, k='values'): |
---|
23 | self.h = hdlDict |
---|
24 | self.d = {} |
---|
25 | try: |
---|
26 | for r in hdlDict[k]: |
---|
27 | self.d[r['type']] = r['data'] |
---|
28 | except: |
---|
29 | print ( hdlDict[k] ) |
---|
30 | raise |
---|
31 | |
---|
32 | |
---|
33 | class ghandle(object): |
---|
34 | def __init__(self,url): |
---|
35 | try: |
---|
36 | fh = request.urlopen( url ) |
---|
37 | msg = eval( fh.read() ) |
---|
38 | except: |
---|
39 | r = requests.get( url ) |
---|
40 | msg = r.json() |
---|
41 | assert type( msg ) == type( {} ), 'Response of wrong type' |
---|
42 | for k in ['responseCode', 'handle']: |
---|
43 | assert k in msg, 'Required key %s not found: %s' % (k, str( msg.keys() ) ) |
---|
44 | |
---|
45 | ###print ( 'url read ok' ) |
---|
46 | self.msg = msg |
---|
47 | |
---|
48 | class main(object): |
---|
49 | knownargs0 = ['-h','-v','-t'] |
---|
50 | knownargs1 = ['-f'] |
---|
51 | def __init__(self, args): |
---|
52 | self.htmpl = 'http://hdl.handle.net/api/handles/%s' |
---|
53 | self.version = '0.01.01' |
---|
54 | self.args = args |
---|
55 | self.parseArgs() |
---|
56 | if self.d.get( '-v', False ): |
---|
57 | print ( self.version ) |
---|
58 | return |
---|
59 | |
---|
60 | if self.d.get( '-h', False ): |
---|
61 | print (self.version) |
---|
62 | print ( __doc__ ) |
---|
63 | return |
---|
64 | |
---|
65 | if self.d.get( '-t', False ): |
---|
66 | self.runTest() |
---|
67 | return |
---|
68 | |
---|
69 | if '-f' in self.d: |
---|
70 | fn = self.d['-f'] |
---|
71 | self.dumpF(fn) |
---|
72 | |
---|
73 | def dumpF(self,fn): |
---|
74 | assert os.path.isfile( fn ), 'File %s not found' % fn |
---|
75 | f = ncHead( fn ) |
---|
76 | thisid = string.replace(f.ga['tracking_id'], 'hdl:999999', '10876.test' ) |
---|
77 | url = self.htmpl % thisid |
---|
78 | g = ghandle( url ) |
---|
79 | self.p = phandle( g.msg ) |
---|
80 | gp = ghandle( self.htmpl % self.p.d['parent']['value'] ) |
---|
81 | self.pp = phandle( gp.msg ) |
---|
82 | if 'replaced_by' in self.p.d: |
---|
83 | print '******* OBSOLETE FILE ******' |
---|
84 | pr = phandle( ghandle( self.htmpl % self.p.d['replaced_by']['value'] ).msg ) |
---|
85 | print 'REPLACED BY: ',pr.d['URL']['value'] |
---|
86 | else: |
---|
87 | print 'File is current' |
---|
88 | |
---|
89 | cl = eval( self.pp.d['children']['value'] ) |
---|
90 | thisfound=False |
---|
91 | print '------- SIBLINGS -------' |
---|
92 | for c in cl: |
---|
93 | if c == thisid: |
---|
94 | thisfound = True |
---|
95 | else: |
---|
96 | gc = ghandle( self.htmpl % c ) |
---|
97 | pc = phandle( gc.msg ) |
---|
98 | if 'replaced_by' in pc.d: |
---|
99 | pcr = phandle( ghandle( self.htmpl % pc.d['replaced_by']['value'] ).msg ) |
---|
100 | print '>>>', pcr.d['URL']['value'] |
---|
101 | elif 'URL' in pc.d: |
---|
102 | print pc.d['URL']['value'] |
---|
103 | else: |
---|
104 | print 'Sibling url not found',pc.d.keys() |
---|
105 | |
---|
106 | def runTest(self): |
---|
107 | f = ncHead( fn1 ) |
---|
108 | url = self.htmpl % f.ga['tracking_id'] |
---|
109 | url = string.replace( url, 'hdl:999999', '10876.test' ) |
---|
110 | print url |
---|
111 | g = ghandle( url ) |
---|
112 | self.p = phandle( g.msg ) |
---|
113 | expected= ['creation_date', 'aggregation_level', 'HS_ADMIN', '10320/loc', 'checksum', 'URL', 'parent'] |
---|
114 | ## 'replaced_by' if obsolete |
---|
115 | for k in expected: |
---|
116 | assert k in self.p.d, 'Expected handle content key %s not found:: %s' % (k,str(self.p.d.keys())) |
---|
117 | assert 'tracking_id' in self.p.d or 'trackingID' in self.p.d, 'No tracking id found: %s' % str(self.p.d.keys()) |
---|
118 | for k in expected: |
---|
119 | print ('%s: %s' % (k,self.p.d[k])) |
---|
120 | |
---|
121 | print ('PARSING PARENT ..... ' ) |
---|
122 | print ( self.htmpl % self.p.d['parent']['value'] ) |
---|
123 | g = ghandle( self.htmpl % self.p.d['parent']['value'] ) |
---|
124 | self.pp = phandle( g.msg ) |
---|
125 | for k in self.pp.d.keys(): |
---|
126 | print ('%s: %s' % (k,self.pp.d[k])) |
---|
127 | #'isReplacedBy' if obsolete |
---|
128 | expected= ['creation_date', 'aggregation_level', 'HS_ADMIN', '10320/loc', 'checksum', 'URL', 'children', 'tracking_id'] |
---|
129 | |
---|
130 | def parseArgs(self): |
---|
131 | self.d = {} |
---|
132 | kn = self.knownargs0 + self.knownargs1 |
---|
133 | xx = [] |
---|
134 | al = self.args[1:] |
---|
135 | while len(al) > 0: |
---|
136 | a = al.pop(0) |
---|
137 | if a not in kn: |
---|
138 | xx.append(a) |
---|
139 | elif a in self.knownargs1: |
---|
140 | self.d[a] = al.pop(0) |
---|
141 | else: |
---|
142 | self.d[a] = True |
---|
143 | if len(xx) > 0: |
---|
144 | print ('ARGUMENTS NOT RECOGNISED: %s' % str(xx) ) |
---|
145 | |
---|
146 | class ncHead(object): |
---|
147 | def __init__(self, fn): |
---|
148 | nc0 = ncq3.open( fn ) |
---|
149 | nc0.getDigest() |
---|
150 | self.ga = {} |
---|
151 | for a in nc0.alla: |
---|
152 | self.ga[a.name] = a.value |
---|
153 | |
---|
154 | |
---|
155 | if __name__ == "__main__": |
---|
156 | import sys |
---|
157 | m = main( sys.argv ) |
---|
158 | |
---|
159 | |
---|