6c942b4fcd46ab47f3a07f649a717671699806c3
[myslice.git] / manifold / manifoldapi.py
1 # Manifold API Python interface
2 import xmlrpclib
3
4 from myslice.config import Config
5
6 from manifoldresult import ManifoldResult, ManifoldCode
7
8 debug=False
9 debug=True
10
11 class ManifoldAPI:
12
13     def __init__(self, auth=None, cainfo=None):
14         
15         config = Config()
16         self.auth = auth
17         self.cainfo = cainfo
18         self.errors = []
19         self.trace = []
20         self.calls = {}
21         self.multicall = False
22         self.url = config.manifold_url
23         self.server = xmlrpclib.Server(self.url, verbose=False, allow_none=True)
24
25     def __repr__ (self): return "ManifoldAPI[%s]"%self.url
26
27     def __getattr__(self, methodName):
28         def func(*args, **kwds):
29             if (debug): 
30                 print "entering ManifoldAPI.%s"%methodName,
31                 print "args",args,
32                 print "kwds",kwds
33             try:
34                 result=getattr(self.server, methodName)(self.auth, *args, **kwds)
35                 if debug:
36                     print '===> backend call',methodName, self.auth, self.url,'->',
37                     if not result:                        print "[no/empty result]"
38                     elif isinstance (result,str):         print "result is '%s'"%result
39                     elif isinstance (result,list):        print "result is a %d-elts list"%len(result)
40                     else:                                 print "[dont know how to display result]"
41                 return ManifoldResult (code=ManifoldCode.SUCCESS, value=result)
42             except xmlrpclib.Fault, error:
43                 ### xxx this is very rough for now
44                 # until we have some agreement about how the API calls should return error conditions
45                 # in some less unpolite way than this anoanymous exception, we assume it's a problem with the session
46                 # that needs to be refreshed
47                 if debug: print "Session Expired"
48                 if error.faultCode == 8002:
49                     reason="most likely your session has expired"
50                     reason += " (the manifold API has no unambiguous error reporting mechanism yet)"
51                     return ManifoldResult (code=ManifoldCode.SESSION_EXPIRED, output=reason)
52             except Exception,error:
53                 print "ManifoldAPI: unexpected exception",error
54                 return ManifoldResult (code=ManifoldResult.UNKNOWN_ERROR, output="%s"%error)
55         return func
56
57     def send_manifold_query (self, manifold_query):
58         (action,subject)= (manifold_query.action,manifold_query.subject)
59         if action=='get':
60             # use self.Get rather than self.server.Get so we catch exceptions as per __getattr__
61             return self.Get(subject, manifold_query.filters, {}, manifold_query.fields)
62         # xxx...
63         else:
64             print "WARNING: ManifoldAPI.send_manifold_query: only 'get' implemented for now"