def pmt_to_dict(p): d = dict() items = pmt.pmt_dict_items(p) for i in range(pmt.pmt_length(items)): pair = pmt.pmt_nth(i, items) k = pmt.pmt_car(pair) v = pmt.pmt_cdr(pair) d[pmt_to_python(k)] = pmt_to_python(v) return d
def handle_request(self, pdu ): if pmt.pmt_is_pair(pdu): # get the first and last elements of the pair fcn_name = pmt.to_python(pmt.pmt_car(pdu)) request = pmt.to_python(pmt.pmt_cdr(pdu)) #try to parse the request try: args, kwargs = request if args is None: args = tuple() if kwargs is None: kwargs = dict() except: err = 'cannot parse request for %s, expected tuple of args, kwargs'%fcn_name print err #return request, None, err return #exec python code and squash down to objects args = map(self._exec_arg, args) for key, val in kwargs.iteritems(): kwargs[key] = self._exec_arg(val) #fly through dots to get the fcn pointer try: fcn_ptr = self._obj for name in fcn_name.split('.'): fcn_ptr = getattr(fcn_ptr, name) except: err = 'cannot find function %s in %s'%(fcn_name, self._obj) print err #return request, None, err return #try to execute the request try: ret = fcn_ptr(*args, **kwargs) except Exception as ex: err = 'cannot execute request for %s, got error %s'%(fcn_name, ex) print err #return request, None, err return #return the sucess result #return request, ret, None self.message_port_pub(self.OUT_PORT, pmt.from_python(ret))
def parse_extra_dict(p, info, VERBOSE=False): if(pmt.pmt_is_dict(p) is False): sys.stderr.write("Extra header is not a PMT dictionary: invalid or corrupt data file.\n") sys.exit(1) items = pmt.pmt_dict_items(p) nitems = pmt.pmt_length(items) for i in xrange(nitems): item = pmt.pmt_nth(i, items) key = pmt.pmt_symbol_to_string(pmt.pmt_car(item)) val = pmt.pmt_cdr(item) info[key] = val if(VERBOSE): print "{0}: ".format(key) pmt.pmt_print(val) return info