X-Git-Url: http://git.onelab.eu/?a=blobdiff_plain;f=manifoldapi%2Fmanifoldresult.py;fp=manifoldapi%2Fmanifoldresult.py;h=4ffe072bb9dc8fba1abf70dd6a4a40775f59110b;hb=8cd242571082562afa089d7da255c8234055f685;hp=0000000000000000000000000000000000000000;hpb=574ab14b3b3daf0f407dcc2a73a54ad5f87af372;p=unfold.git diff --git a/manifoldapi/manifoldresult.py b/manifoldapi/manifoldresult.py new file mode 100644 index 00000000..4ffe072b --- /dev/null +++ b/manifoldapi/manifoldresult.py @@ -0,0 +1,61 @@ +def enum(*sequential, **named): + enums = dict(zip(sequential, range(len(sequential))), **named) + return type('Enum', (), enums) + +ManifoldCode = enum ( + UNKNOWN_ERROR=-1, + SUCCESS=0, + SESSION_EXPIRED=1, + NOT_IMPLEMENTED=2, + SERVER_UNREACHABLE=3, +) + +_messages_ = { -1 : "Unknown", 0: "OK", 1: "Session Expired", 2: "Not Implemented", 3: "Backend server unreachable"} + +# being a dict this can be used with json.dumps +class ManifoldResult (dict): + def __init__ (self, code=ManifoldCode.SUCCESS, value=None, output=""): + self['code']=code + self['value']=value + self['output']=output + self['description'] = '' # Jordan: needed by javascript code + + def from_json (self, json_string): + d=json.dumps(json_string) + for k in ['code','value','output']: + self[k]=d[k] + + # raw accessors + def code (self): return self['code'] + def output (self): return self['output'] + + # this returns None if there's a problem, the value otherwise + def ok_value (self): + if self['code']==ManifoldCode.SUCCESS: + return self['value'] + + # both data in a single string + def error (self): + return "code=%s -- %s"%(self['code'],self['output']) + + + def __repr__ (self): + code=self['code'] + result="[MFresult %s (code=%s)"%(_messages_.get(code,"???"),code) + if code==0: + value=self['value'] + if isinstance(value,list): result += " [value=list with %d elts]"%len(value) + elif isinstance(value,dict): result += " [value=dict with keys %s]"%value.keys() + else: result += " [value=%s: %s]"%(type(value).__name__,value) + else: + result += " [output=%s]"%self['output'] + result += "]" + return result + +# probably simpler to use a single class and transport the whole result there +# instead of a clumsy set of derived classes +class ManifoldException (Exception): + def __init__ (self, manifold_result): + self.manifold_result=manifold_result + def __repr__ (self): + return "Manifold Exception %s"%(self.manifold_result.error())