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 elif isinstance (result,dict):
35 print "result is a dict with %d keys : %s"%(len(result),result.keys())
36 for (k,v) in result.iteritems(): print '...',k,':',v
37 print "result is a dict with %d keys : %s"%(len(result),result.keys()),
38 else: print "[dont know how to display result] %s"%result
40 # xxx temporary code for scaffolding a ManifolResult on top of an API that does not expose error info
41 # as of march 2013 we work with an API that essentially either returns the value, or raises
42 # an xmlrpclib.Fault exception with always the same 8002 code
43 # since most of the time we're getting this kind of issues for expired sessions
44 # (looks like sessions are rather short-lived), for now the choice is to map these errors on
45 # a SESSION_EXPIRED code
46 def __getattr__(self, methodName):
47 def func(*args, **kwds):
49 if debug: print "====> ManifoldAPI.%s"%methodName,"auth",self.auth,"args",args,"kwds",kwds
50 result=getattr(self.server, methodName)(self.auth, *args, **kwds)
52 print '<==== backend call %s(*%s,**%s) returned'%(methodName,args,kwds),
53 print '.ctd. Authmethod=',self.auth['AuthMethod'], self.url,'->',
54 self._print_result(result)
56 return ResultValue(**result)
58 except Exception,error:
59 # XXX Connection refused for example
60 print "** API ERROR **"
63 if debug: print "KO (unexpected exception)",error
64 raise ManifoldException ( ManifoldResult (code=ManifoldCode.UNKNOWN_ERROR, output="%s"%error) )
68 def execute_query(request, query):
69 if not 'manifold' in request.session or not 'auth' in request.session['manifold']:
70 print "W: Used hardcoded demo account for execute_query"
71 manifold_api_session_auth = {'AuthMethod': 'password', 'Username': 'demo', 'AuthString': 'demo'}
73 manifold_api_session_auth = request.session['manifold']['auth']
74 manifold_api = ManifoldAPI(auth=manifold_api_session_auth)
79 result = manifold_api.forward(query.to_dict())
80 if result['code'] == 2:
81 raise Exception, 'Error running query: %r' % result
84 #Error running query: {'origin': [0, 'XMLRPCAPI'], 'code': 2, 'description': 'No such session: No row was found for one()', 'traceback': 'Traceback (most recent call last):\n File "/usr/local/lib/python2.7/dist-packages/manifold/core/xmlrpc_api.py", line 68, in xmlrpc_forward\n user = Auth(auth).check()\n File "/usr/local/lib/python2.7/dist-packages/manifold/auth/__init__.py", line 245, in check\n return self.auth_method.check()\n File "/usr/local/lib/python2.7/dist-packages/manifold/auth/__init__.py", line 95, in check\n raise AuthenticationFailure, "No such session: %s" % e\nAuthenticationFailure: No such session: No row was found for one()\n', 'type': 2, 'ts': None, 'value': None}
87 return result['value']