1 # Manifold API Python interface
4 from myslice.config import Config
6 from manifoldresult import ManifoldResult, ManifoldCode
13 def __init__(self, auth=None, cainfo=None):
21 self.multicall = False
22 self.url = config.manifold_url
23 self.server = xmlrpclib.Server(self.url, verbose=False, allow_none=True)
25 def __repr__ (self): return "ManifoldAPI[%s]"%self.url
27 # xxx temporary code for scaffolding a ManifolResult on top of an API that does not expose error info
28 # as of march 2013 we work with an API that essentially either returns the value, or raises
29 # an xmlrpclib.Fault exception with always the same 8002 code
30 # since most of the time we're getting this kind of issues for expired sessions
31 # (looks like sessions are rather short-lived), for now the choice is to map these errors on
32 # a SESSION_EXPIRED code
33 def __getattr__(self, methodName):
34 def func(*args, **kwds):
36 print "entering ManifoldAPI.%s"%methodName,
40 result=getattr(self.server, methodName)(self.auth, *args, **kwds)
41 ### attempt to cope with old APIs and new APIs
42 if isinstance (result, dict) and 'code' in result:
43 # this sounds like a result from a new API, leave it untouched
47 print '===> backend call',methodName, self.auth, self.url,'->',
48 if not result: print "[no/empty result]"
49 elif isinstance (result,str): print "result is '%s'"%result
50 elif isinstance (result,list): print "result is a %d-elts list"%len(result)
51 else: print "[dont know how to display result]"
52 return ManifoldResult (code=ManifoldCode.SUCCESS, value=result)
53 except xmlrpclib.Fault, error:
54 ### xxx this is very rough for now
55 # until we have some agreement about how the API calls should return error conditions
56 # in some less unpolite way than this anonymous exception, we assume it's a problem with the session
57 # that needs to be refreshed
58 if error.faultCode == 8002:
59 reason="most likely your session has expired"
60 reason += " (the manifold API has no unambiguous error reporting mechanism yet)"
61 return ManifoldResult (code=ManifoldCode.SESSION_EXPIRED, output=reason)
63 reason="xmlrpclib.Fault with faultCode = %s (not taken as session expired)"%error.faultCode
64 return ManifoldResult (code=ManifoldCode.UNKNOWN_ERROR, output=reason)
65 except Exception,error:
66 print "ManifoldAPI: unexpected exception",error
67 return ManifoldResult (code=ManifoldCode.UNKNOWN_ERROR, output="%s"%error)
70 def send_manifold_query (self, query):
71 (action,subject)= (query.action,query.subject)
72 # use e.g. self.Get rather than self.server.Get so we use the __getattr__ code
74 # this makes the backend to squeak and one can't login anymore...
75 # return self.Get(subject, query.filters, query.timestamp, query.fields)
76 return self.Get(subject, query.filters, {}, query.fields)
77 elif action=='update':
78 answer=self.Update(subject, query.filters, query.params, query.fields)
79 if not isinstance (answer, ManifoldResult): print "UNEXECPECTED answer", answer
82 warning="WARNING: ManifoldAPI.send_manifold_query: %s not implemented for now"%action
85 return ManifoldResult(code=ManifoldCode.NOT_IMPLEMENTED, output=warning)