From ec4be8cfef3ed6b3dc1742a95efd014da8f01893 Mon Sep 17 00:00:00 2001 From: Thierry Parmentelat Date: Tue, 26 Mar 2013 18:57:59 +0100 Subject: [PATCH] start to deal with xmlrpclib errors add an offline mode for metadata as well --- manifold/manifoldapi.py | 81 +++++++++++++++++++++++---------------- manifold/manifoldproxy.py | 16 +++++--- manifold/metadata.py | 15 ++++++++ 3 files changed, 74 insertions(+), 38 deletions(-) diff --git a/manifold/manifoldapi.py b/manifold/manifoldapi.py index 18c555ca..6a0e1d23 100644 --- a/manifold/manifoldapi.py +++ b/manifold/manifoldapi.py @@ -5,39 +5,56 @@ from myslice.config import Config debug=True -class ManifoldAPI: +class SessionExpired (Exception): + def __init__ (self,message): + self.message=message + def __repr__ (self): + return ""%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" diff --git a/manifold/manifoldproxy.py b/manifold/manifoldproxy.py index 733f2097..c444a379 100644 --- a/manifold/manifoldproxy.py +++ b/manifold/manifoldproxy.py @@ -4,7 +4,7 @@ import json 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 @@ -63,10 +63,14 @@ with the query passed using POST""" # 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: @@ -74,8 +78,8 @@ with the query passed using POST""" 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() diff --git a/manifold/metadata.py b/manifold/metadata.py index 8aed1268..3475139f 100644 --- a/manifold/metadata.py +++ b/manifold/metadata.py @@ -2,6 +2,10 @@ import json 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): @@ -9,6 +13,14 @@ class MetaData: 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', @@ -18,6 +30,9 @@ class MetaData: '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) -- 2.43.0