4 from manifold.manifoldresult import ManifoldResult
5 from manifold.manifoldapi import ManifoldAPI
10 # turn this on if you want to work offline
16 def __init__ (self, auth):
18 self.hash_by_object={}
21 offline_filename="%s/../offline-metadata.json"%os.path.dirname(__file__)
24 with file(offline_metadata) as f:
25 self.hash_by_object=json.loads(f.read())
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
42 result = manifold_api.forward(request)
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'])
54 rows = result.ok_value()
55 # API errors will be handled by the outer logic
57 # print "Failed to retrieve metadata",rows_result.error()
59 self.hash_by_object = dict ( [ (row['table'], row) for row in rows ] )
60 # save for next time we use offline mode
62 with file(offline_filename,'w') as f:
63 f.write(json.dumps(self.hash_by_object))
66 return json.dumps(self.hash_by_object)
68 def details_by_object (self, object):
69 return self.hash_by_object[object]
71 def sorted_fields_by_object (self, object):
72 return self.hash_by_object[object]['column'].sort()
74 def get_field_type(self, object, field):
75 print "Temp fix for metadata::get_field_type() -> consider moving to manifold.core.metadata soon"