debug=True
-class ManifoldAPI:
+class SessionExpired (Exception):
+ def __init__ (self,message):
+ self.message=message
+ def __repr__ (self):
+ return "<SessionExpired: %s>"%self.message
- def __init__(self, auth=None, cainfo=None):
+class ManifoldAPI:
- config = Config()
- self.auth = auth
- self.cainfo = cainfo
- self.errors = []
- self.trace = []
- self.calls = {}
- self.multicall = False
- self.url = config.manifold_url
- self.server = xmlrpclib.Server(self.url, verbose=False, allow_none=True)
+ def __init__(self, auth=None, cainfo=None):
+
+ config = Config()
+ self.auth = auth
+ self.cainfo = cainfo
+ self.errors = []
+ self.trace = []
+ self.calls = {}
+ self.multicall = False
+ self.url = config.manifold_url
+ self.server = xmlrpclib.Server(self.url, verbose=False, allow_none=True)
- def __getattr__(self, methodName):
- def func(*args, **kwds):
- result=getattr(self.server, methodName)(self.auth, *args, **kwds)
- ### debug
- if debug:
- print '===> backend call',methodName, self.auth, self.url,'->',
- if not result: print "no/empty result"
- elif isinstance (result,str): print "result is '%s'"%result
- elif isinstance (result,list): print "result is a %d-elts list"%len(result)
- else: print "dont know how to display result"
- ###
- return result
- return func
+ def __getattr__(self, methodName):
+ def func(*args, **kwds):
+ try:
+ result=getattr(self.server, methodName)(self.auth, *args, **kwds)
+ if debug:
+ print '===> backend call',methodName, self.auth, self.url,'->',
+ if not result: print "no/empty result"
+ elif isinstance (result,str): print "result is '%s'"%result
+ elif isinstance (result,list): print "result is a %d-elts list"%len(result)
+ else: print "dont know how to display result"
+ return result
+ except xmlrpclib.Fault, error:
+ ### xxx this is very rough for now
+ # until we have some agreement about how the API calls should return error conditions
+ # in some less unpolite way than this anoanymous exception, we assume it's a problem with the session
+ # that needs to be refreshed
+ if error.faultCode == 8002:
+ reason="most likely your session has expired"
+ reason += " (the manifold API has no unambiguous error reporting mechanism yet)"
+ raise SessionExpired(reason)
+ except Exception,error:
+ print "ManifoldAPI: unexpected exception",error
+ raise
+ return func
- def send_manifold_query (self, manifold_query):
- (action,subject)= (manifold_query.action,manifold_query.subject)
- if action=='get':
- return self.server.Get(self.auth, subject, manifold_query.filters, {}, manifold_query.fields)
- # xxx...
- else:
- print "WARNING: ManifoldAPI.send_manifold_query: only 'get' implemented for now"
-
+ def send_manifold_query (self, manifold_query):
+ (action,subject)= (manifold_query.action,manifold_query.subject)
+ if action=='get':
+ # use self.Get rather than self.server.Get so we catch exceptions as per __getattr__
+ return self.Get(self.auth, subject, manifold_query.filters, {}, manifold_query.fields)
+ # xxx...
+ else:
+ print "WARNING: ManifoldAPI.send_manifold_query: only 'get' implemented for now"
from django.http import HttpResponse, HttpResponseForbidden
from manifold.manifoldquery import ManifoldQuery
-from manifold.manifoldapi import ManifoldAPI
+from manifold.manifoldapi import ManifoldAPI, SessionExpired
debug=False
debug=True
# actually forward
manifold_api= ManifoldAPI(auth=manifold_api_session_auth)
if debug: print 'manifoldproxy.proxy: sending to backend', manifold_query
- answer=manifold_api.send_manifold_query (manifold_query)
- if debug:
- try: print "received answer from backend with %d rows"%len(answer)
- except: print "received answer from backend - can't say len"
+ # xxx we should embed the values inside a geni-like wrapper
+ try:
+ answer=manifold_api.send_manifold_query (manifold_query)
+ if debug:
+ try: print "received answer from backend with %d rows"%len(answer)
+ except: print "received answer from backend - can't say len"
+ except SessionExpired,error:
+ answer=[ error.message ]
json_answer=json.dumps(answer)
if (debug):
with (file(offline_filename,"w")) as f:
if debug_spin:
import time
time.sleep(debug_spin)
- # return json-encoded answer
return HttpResponse (json_answer, mimetype="application/json")
+
except:
import traceback
traceback.print_exc()
from manifold.manifoldapi import ManifoldAPI
+# turn this on if you want to work offline
+work_offline=False
+#work_offline=True
+
class MetaData:
def __init__ (self, 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
manifold_api = ManifoldAPI(self.auth)
fields = ['table', 'column.column',
'column.description','column.header', 'column.title',
'column.platforms.platform_url']
results = manifold_api.Get('metadata:table', [], [], fields)
self.hash_by_subject = dict ( [ (result['table'], result) for result in results ] )
+ # save for next time we use offline mode
+ 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)