we embed the (currently raw) results from manifold API in a ManifoldResult dict
[myslice.git] / manifold / manifoldresult.py
1 def enum(*sequential, **named):
2     enums = dict(zip(sequential, range(len(sequential))), **named)
3     return type('Enum', (), enums)
4
5 ManifoldCode = enum (
6     SUCCESS=0,
7     SESSION_EXPIRED=1,
8     OTHERS=2,
9 )
10
11 # being a dict this can be used with json.dumps
12 class ManifoldResult (dict):
13     def __init__ (self, code=ManifoldCode.SUCCESS, value=None, output=""):
14         self['code']=code
15         self['value']=value
16         self['output']=output
17
18     def from_json (self, json_string):
19         d=json.dumps(json_string)
20         for k in ['code','value','output']:
21             self[k]=d[k]
22
23     # this returns None if there's a problem, the value otherwise
24     def ok_value (self):
25         if self['code']==ManifoldCode.SUCCESS:
26             return self['value']
27
28     def error (self):
29         return "code=%s -- %s"%(self['code'],self['output'])
30