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