fix metadata fetching when problems
[myslice.git] / manifold / metadata.py
1 import json 
2 import os.path
3
4 from manifold.manifoldresult import ManifoldResult
5 from manifold.manifoldapi import ManifoldAPI
6
7 debug=False
8 debug=True
9
10 # turn this on if you want to work offline
11 work_offline=False
12 #work_offline=True
13
14 class MetaData:
15
16     def __init__ (self, auth):
17         self.auth=auth
18         self.hash_by_object={}
19
20     def fetch (self):
21         offline_filename="%s/../offline-metadata.json"%os.path.dirname(__file__)
22         if work_offline:
23             try:
24                 with file(offline_metadata) as f:
25                     self.hash_by_object=json.loads(f.read())
26                 return
27             except:
28                 print "metadata.work_offline: failed to decode %s"%offline_filename
29         manifold_api = ManifoldAPI(self.auth)
30         fields = ['table', 'column.name', 'column.qualifier', 'column.type', 
31                   'column.is_array', 'column.description', 'column.default', 'key', 'capability']
32         #fields = ['table', 'column.column',
33         #          'column.description','column.header', 'column.title',
34         #          'column.unit', 'column.info_type',
35         #          'column.resource_type', 'column.value_type',
36         #          'column.allowed_values', 'column.platforms.platform',
37         #          'column.platforms.platform_url']
38         request={ 'action': 'get',
39                   'object': 'local:object', # proposed to replace metadata:table
40                   'fields':  fields ,
41                   }
42         result = manifold_api.forward(request)
43
44         # xxx need a way to export error messages to the UI
45         if result['code'] == 1: # warning
46             # messages.warning(request, result['description'])
47             print ("METADATA WARNING -",request,result['description'])
48         elif result['code'] == 2:
49             # messages.error(request, result['description'])
50             print ("METADATA ERROR -",request,result['description'])
51             # XXX FAIL HERE XXX
52             return
53
54         rows = result.ok_value()
55 # API errors will be handled by the outer logic
56 #        if not rows:
57 #            print "Failed to retrieve metadata",rows_result.error()
58 #            rows=[]
59         self.hash_by_object = dict ( [ (row['table'], row) for row in rows ] )
60         # save for next time we use offline mode
61         if debug and rows:
62             with file(offline_filename,'w') as f:
63                 f.write(json.dumps(self.hash_by_object))
64
65     def to_json(self):
66         return json.dumps(self.hash_by_object)
67
68     def details_by_object (self, object):
69         return self.hash_by_object[object]
70
71     def sorted_fields_by_object (self, object):
72         return self.hash_by_object[object]['column'].sort()
73
74     def get_field_type(self, object, field):
75         print "Temp fix for metadata::get_field_type() -> consider moving to manifold.core.metadata soon"
76         return field