def _gen_decode(self, key): '''Decoding generator (internal, for use by kb.KB only) Usage: instance._gen_decode( key ) key - the key (variable name) to be decoded''' results = self.ask("var('" + key + "', ?Value, ?Type)") if isinstance(results, bool) or results == []: yield None for res in results: value = str( res["Value"] ) typ = str( res["Type"] ) if typ == "int": yield int(value) elif typ == "str": yield str(value) elif typ == "float": yield float(value) elif typ == "list": l = [] listID = str(value) gen = self._gen_decode(listID) hasElements = True while hasElements: try: l.append(gen.next()) except: hasElements = False yield l elif typ == "dict": dictID = str(value) d = {} for i in self.ask("pair('" + dictID + "', ?ElemID)"): elemID = str(i["ElemID"]) if elemID == 'empty_object': continue newkey = self._gen_decode(elemID + "Key").next() newvalue = self._gen_decode(elemID + "Value").next() d[newkey] = newvalue yield d elif typ == "noneType": yield None else: objid = str(key) res = self.ask("var('" + objid + "', ?Value, ?Type)")[0] classname = res["Type"] objdict = res["Value"] try: module, clas = classname.split(".") obj = get_object_instance(clas, module) d = self._gen_decode(objdict).next() if d == None: d = {} obj.__dict__ = d yield obj except: raise GeneratorExit, "No such class in namespace: %s" % classname
def _gen_decode(self, key): '''Decoding generator (internal, for use by kb.KB only) Usage: instance._gen_decode( key ) key - the key (variable name) to be decoded''' results = self.ask("var('" + key + "', Value, Type)") if isinstance(results, bool): yield None for res in results: value = str(res["Value"]) typ = str(res["Type"]) if typ == "int": yield int(value) elif typ == "str": yield str(value) elif typ == "float": yield float(value) elif typ == "list": l = [] listID = str(value) gen = self._gen_decode(listID) hasElements = True while hasElements: try: l.append(gen.next()) except: hasElements = False yield l elif typ == "dict": dictID = str(value) d = {} for i in self.ask("pair('" + dictID + "', ElemID)"): elemID = str(i["ElemID"]) if elemID == 'empty_object': continue newkey = self._gen_decode(elemID + "Key").next() newvalue = self._gen_decode(elemID + "Value").next() d[newkey] = newvalue yield d elif typ == "noneType": yield None else: objid = str(key) res = self.ask("var('" + objid + "', Value, Type)")[0] classname = res["Type"] objdict = res["Value"] try: module, clas = classname.split(".") obj = get_object_instance(clas, module) d = self._gen_decode(objdict).next() if d == None: d = {} obj.__dict__ = d yield obj except: raise GeneratorExit, "No such class in namespace: %s" % classname
def _gen_decode(self, key): key = key.capitalize() results = self.ask("Var(" + key + ", value, type)") if isinstance(results, bool): yield None for res in results: value = str(res[expr("value")]) typ = str(res[expr("type")]) if typ == "Int": yield int(value) elif typ == "Str": yield str(value).lower() elif typ == "Float": yield float(value) elif typ == "List": l = [] listID = str(value) gen = self._gen_decode(listID) hasElements = True while hasElements: try: l.append(gen.next()) except: hasElements = False yield l elif typ == "Dict": dictID = str(value) d = {} for i in self.ask("Pair(" + dictID + ", elemid)"): elemID = str(i[expr("elemid")]) if elemID == "Empty_object": continue newkey = self._gen_decode(elemID + "Key").next() newvalue = self._gen_decode(elemID + "Value").next() d[newkey] = newvalue yield d elif typ == "NoneType": yield None else: objid = str(key) res = self.ask("Var(" + objid + ", value, type)")[0] classname = str(res[expr("type")]) objdict = str(res[expr("value")]) try: module, clas = classname.split(".") obj = get_object_instance(clas, module) d = self._gen_decode(objdict).next() if d == None: d = {} obj.__dict__ = d yield obj except: raise GeneratorExit, "No such class in namespace: %s" % classname