1c812f0cb58009f098073ec0dba927b79522b760
[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     NOT_IMPLEMENTED=2,
9     UNKNOWN_ERROR=3,
10 )
11
12 # being a dict this can be used with json.dumps
13 class ManifoldResult (dict):
14     def __init__ (self, code=ManifoldCode.SUCCESS, value=None, output=""):
15         self['code']=code
16         self['value']=value
17         self['output']=output
18
19     def from_json (self, json_string):
20         d=json.dumps(json_string)
21         for k in ['code','value','output']:
22             self[k]=d[k]
23
24     # this returns None if there's a problem, the value otherwise
25     def ok_value (self):
26         if self['code']==ManifoldCode.SUCCESS:
27             return self['value']
28
29     def error (self):
30         return "code=%s -- %s"%(self['code'],self['output'])
31     
32
33     def __repr__ (self):
34         result="[[MFresult code=%s"%self['code']
35         if self['code']==0:
36             value=self['value']
37             if isinstance(value,list): result += " [value=list with %d elts]"%len(value)
38             else: result += " [value=other %s]"%value
39         else:
40             result += " [output=%s]"%self['output']
41         result += "]]"
42         return result