1 # Manifold API Python interface
4 from myslice.config import Config
6 from django.contrib import messages
7 from manifoldresult import ManifoldResult, ManifoldCode, ManifoldException
8 from manifold.core.result_value import ResultValue
15 def __init__(self, auth=None, cainfo=None):
23 self.multicall = False
24 self.url = config.manifold_url
25 self.server = xmlrpclib.Server(self.url, verbose=False, allow_none=True)
27 def __repr__ (self): return "ManifoldAPI[%s]"%self.url
29 # a one-liner to give a hint of what the return value looks like
30 def _print_result (self, result):
31 if not result: print "[no/empty result]"
32 elif isinstance (result,str): print "result is '%s'"%result
33 elif isinstance (result,list): print "result is a %d-elts list"%len(result)
34 else: print "[dont know how to display result]"
36 # xxx temporary code for scaffolding a ManifolResult on top of an API that does not expose error info
37 # as of march 2013 we work with an API that essentially either returns the value, or raises
38 # an xmlrpclib.Fault exception with always the same 8002 code
39 # since most of the time we're getting this kind of issues for expired sessions
40 # (looks like sessions are rather short-lived), for now the choice is to map these errors on
41 # a SESSION_EXPIRED code
42 def __getattr__(self, methodName):
43 def func(*args, **kwds):
45 if debug: print "====> ManifoldAPI.%s"%methodName,"auth",self.auth,"args",args,"kwds",kwds
46 result=getattr(self.server, methodName)(self.auth, *args, **kwds)
48 print '<==== backend call %s(*%s,**%s) returned'%(methodName,args,kwds),
49 print '.ctd. Authmethod=',self.auth['AuthMethod'], self.url,'->',
50 self._print_result(result)
52 return ResultValue(**result)
54 except Exception,error:
55 # XXX Connection refused for example
56 print "** API ERROR **"
59 if debug: print "KO (unexpected exception)",error
60 raise ManifoldException ( ManifoldResult (code=ManifoldCode.UNKNOWN_ERROR, output="%s"%error) )