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