wip
[myslice.git] / manifold / metadata.py
index e8578c4..eb0e48a 100644 (file)
@@ -1,7 +1,11 @@
 import json 
 
+from manifold.manifoldresult import ManifoldResult
 from manifold.manifoldapi import ManifoldAPI
 
+debug=False
+debug=True
+
 # turn this on if you want to work offline
 work_offline=False
 #work_offline=True
@@ -12,6 +16,9 @@ class MetaData:
         self.auth=auth
         self.hash_by_subject={}
 
+        # XXX Retrieve all metadata the first time we instanciate the class
+        self.fetch()
+
     def fetch (self):
         offline_filename="offline-metadata.json"
         if work_offline:
@@ -28,11 +35,20 @@ class MetaData:
                   'column.resource_type', 'column.value_type',
                   'column.allowed_values', 'column.platforms.platform',
                   'column.platforms.platform_url']
-        results = manifold_api.Get('metadata:table', [], [], fields)
-        self.hash_by_subject = dict ( [ (result['table'], result) for result in results ] )
+        rows_result = manifold_api.Get({
+            'fact_table': 'local:objects', # proposed to replace 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
-        with file(offline_filename,'w') as f:
-            f.write(json.dumps(self.hash_by_subject))
+        if debug:
+            with file(offline_filename,'w') as f:
+                f.write(json.dumps(self.hash_by_subject))
 
     def to_json(self):
         return json.dumps(self.hash_by_subject)
@@ -42,4 +58,3 @@ class MetaData:
 
     def sorted_fields_by_subject (self, subject):
         return self.hash_by_subject[subject]['columns'].sort()
-