X-Git-Url: http://git.onelab.eu/?a=blobdiff_plain;f=manifold%2Fmetadata.py;h=de9bf2ebbe82a9b2bce21667835ddbc3e291b8d7;hb=35c36209bd8ebea07f2c77699530aaaae1b06bd7;hp=5f287ec95c5c8dee94e2a67cafb8a2e35d3a0997;hpb=74b3a7675c43e2884b396cd9d5febfebc6e9ff97;p=myslice.git diff --git a/manifold/metadata.py b/manifold/metadata.py index 5f287ec9..de9bf2eb 100644 --- a/manifold/metadata.py +++ b/manifold/metadata.py @@ -1,55 +1,62 @@ import json +import os.path from manifold.manifoldresult import ManifoldResult from manifold.manifoldapi import ManifoldAPI -debug=False -debug=True +from django.contrib import messages -# turn this on if you want to work offline -work_offline=False -#work_offline=True +debug=False +#debug=True class MetaData: def __init__ (self, auth): self.auth=auth - self.hash_by_subject={} - - def fetch (self): - offline_filename="offline-metadata.json" - if work_offline: - try: - with file(offline_metadata) as f: - self.hash_by_subject=json.loads(f.read()) - return - except: - print "metadata.work_offline: failed to decode %s"%offline_filename + self.hash_by_object={} + + def fetch (self, request): manifold_api = ManifoldAPI(self.auth) - fields = ['table', 'column.column', - 'column.description','column.header', 'column.title', - 'column.unit', 'column.info_type', - 'column.resource_type', 'column.value_type', - 'column.allowed_values', 'column.platforms.platform', - 'column.platforms.platform_url'] - rows_result = manifold_api.Get(fact_table='metadata:table', fields=fields) -#old# rows_result = manifold_api.Get('metadata:table', [], [], fields) - rows = rows_result.ok_value() - if not rows: - print "Failed to retrieve metadata",rows_result.error() - rows=[] - self.hash_by_subject = dict ( [ (row['table'], row) for row in rows ] ) - # save for next time we use offline mode - if debug: - with file(offline_filename,'w') as f: - f.write(json.dumps(self.hash_by_subject)) + fields = ['table', 'column.name', 'column.qualifier', 'column.type', + 'column.is_array', 'column.description', 'column.default', 'key', 'capability'] + #fields = ['table', 'column.column', + # 'column.description','column.header', 'column.title', + # 'column.unit', 'column.info_type', + # 'column.resource_type', 'column.value_type', + # 'column.allowed_values', 'column.platforms.platform', + # 'column.platforms.platform_url'] + request={ 'action': 'get', + 'object': 'local:object', # proposed to replace metadata:table + 'fields': fields , + } + result = manifold_api.forward(request) + + # xxx need a way to export error messages to the UI + if result['code'] == 1: # warning + # messages.warning(request, result['description']) + print ("METADATA WARNING -",request,result['description']) + elif result['code'] == 2: + # messages.error(request, result['description']) + print ("METADATA ERROR -",request,result['description']) + # XXX FAIL HERE XXX + return + + rows = result.ok_value() +# API errors will be handled by the outer logic +# if not rows: +# print "Failed to retrieve metadata",rows_result.error() +# rows=[] + self.hash_by_object = dict ( [ (row['table'], row) for row in rows ] ) def to_json(self): - return json.dumps(self.hash_by_subject) + return json.dumps(self.hash_by_object) - def details_by_subject (self, subject): - return self.hash_by_subject[subject] + def details_by_object (self, object): + return self.hash_by_object[object] - def sorted_fields_by_subject (self, subject): - return self.hash_by_subject[subject]['columns'].sort() + def sorted_fields_by_object (self, object): + return self.hash_by_object[object]['column'].sort() + def get_field_type(self, object, field): + if debug: print "Temp fix for metadata::get_field_type() -> consider moving to manifold.core.metadata soon" + return field