7e61d70409261b76c66f676a54e8eff46238141d
[myslice.git] / manifoldapi / metadata.py
1 import json 
2 import os.path
3
4 from manifoldapi.manifoldresult  import ManifoldResult
5 from manifoldapi.manifoldapi     import ManifoldAPI
6
7 from django.contrib     import messages
8
9 from myslice.settings   import logger
10
11 debug=False
12 #debug=True
13
14 class MetaData:
15
16     def __init__ (self, url, auth):
17         self.url = url
18         self.auth=auth
19         self.hash_by_object={}
20
21     def fetch (self, request):
22         manifold_api = ManifoldAPI(self.url, self.auth)
23         fields = ['table', 'column.name', 'column.qualifier', 'column.type', 
24                   'column.is_array', 'column.description', 'column.default', 'key', 'capability']
25         #fields = ['table', 'column.column',
26         #          'column.description','column.header', 'column.title',
27         #          'column.unit', 'column.info_type',
28         #          'column.resource_type', 'column.value_type',
29         #          'column.allowed_values', 'column.platforms.platform',
30         #          'column.platforms.platform_url']
31         request={ 'action': 'get',
32                   'object': 'local:object', # proposed to replace metadata:table
33                   'fields':  fields ,
34                   }
35         result = manifold_api.forward(request)
36
37         # xxx need a way to export error messages to the UI
38         if result['code'] == 1: # warning
39             # messages.warning(request, result['description'])
40             logger.warning(("METADATA WARNING - {} {}".format(request,result['description'])))
41         elif result['code'] == 2:
42             # messages.error(request, result['description'])
43             logger.error(("METADATA ERROR - {} {}".format(request,result['description'])))
44             # XXX FAIL HERE XXX
45             return
46
47         rows = result.ok_value()
48 # API errors will be handled by the outer logic
49 #        if not rows:
50 #            print "Failed to retrieve metadata",rows_result.error()
51 #            rows=[]
52         self.hash_by_object = dict ( [ (row['table'], row) for row in rows ] )
53
54     def to_json(self):
55         return json.dumps(self.hash_by_object)
56
57     def details_by_object (self, object):
58         return self.hash_by_object[object]
59
60     def sorted_fields_by_object (self, object):
61         return self.hash_by_object[object]['column'].sort()
62
63     def get_field_type(self, object, field):
64         if debug:
65             logger.debug("Temp fix for metadata::get_field_type() -> consider moving to manifold.core.metadata soon")
66         return field