def trial(tries,excepts=None,args=None,kwargs=None,return_exception=None): """ This method executes a try,except clause in a single line :param tries: may be a callable or a list of callables :param excepts: it can be a callable, a list of callables, a map of {ExceptionType:[callables]} or just a default value to return :param args,kwargs: arguments to be passed to the callable :return exception: whether to return exception or None (default) """ try: return_exception = return_exception or (excepts is not None and not any(fun.isCallable(x) for x in fun.toSequence(excepts))) tries = fun.toSequence(tries) args = fun.toSequence(fun.notNone(args,[])) kwargs = fun.notNone(kwargs,{}) excepts = fun.notNone(excepts,lambda e: log.printf(str(e))) result = [t(*args,**kwargs) for t in tries if fun.isCallable(t)] return result[0] if len(result)==1 else result except Exception,e: if fun.isCallable(excepts): v = excepts(e) return v if return_exception else None else: if fun.isDictionary(excepts): if type(e) in excepts: excepts = excepts.get(type(e)) elif type(e).__name__ in excepts: excepts = excepts.get(type(e).__name__) else: candidates = [t for t in excepts if isinstance(e,t)] if candidates: excepts = excepts[candidates[0]] else: excepts = excepts.get('') or excepts.get(None) or [] vals = [x(e) for x in fun.toSequence(excepts) if fun.isCallable(x)] if return_exception: return vals or fun.notNone(excepts,None)
def setModel(self, model): self.info('VaccaAction(%s).setModel(%s)' % (self._default_cmd, model)) model = map(str, fun.toSequence(model)) if len(model) > 1: self._text = model[0] if len(model) > 2: self._icon = model[1] self._cmdargs = model[2 if len(model) > 2 else 1 if len(model ) > 1 else 0:] self._action = taurus.qt.qtgui.util.ExternalAppAction( cmdargs=self._default_cmd + self._cmdargs, text=self._text, icon=self._icon) #self._action = ExternalApp(cmdargs=['mambo'], text="ArchivingViewer", icon=WDIR+'image/icons/Mambo-icon.png') self.setDefaultAction(self._action) self.setText(self._text) self.setToolButtonStyle(Qt.Qt.ToolButtonTextUnderIcon)
def GetAttDataBetweenDates(self, argin): """ Arguments to be AttrName, StartDate, StopDate, Synchronous If Synchronous is missing or False, data is buffered into attributes, which names are returned If True or Yes, all the data is returned when ready Data returned will be (rows,[t0,v0,t1,v1,t2,v2,...]) """ print time.ctime() + "In ", self.get_name( ), "::GetAttDataBetweenDates(%s)" % argin # Add your own code here size = 0 aname = argin[0] tag = self.attr2tag(aname) dates = self.dates2times(argin[1:3]) RW = False synch = fn.searchCl('yes|true', str(argin[3:4])) attrs = [tag, tag + '_r', tag + '_w', tag + '_t'] if RW else [tag, tag + '_r', tag + '_w', tag + '_t'] self.reader.get_attribute_values( aname, (lambda v: self.reader_hook(aname, v)), dates[0], dates[1], decimate=True, cache=self.UseApiCache) self.counter += 1 print(self.counter) argout = [fn.shape(attrs), [a for a in attrs]] if not synch: print '\t%s' % argout return argout else: while not self.IsDataReady(aname): fandango.wait(0.1) data = self.AttrData[aname][-1] for t, v in data: argout.append(t) argout.extend(fn.toSequence(v)) return [fn.shape(data), argout]
def GetAttDataBetweenDates(self, argin): """ Arguments to be AttrName, StartDate, StopDate, Synchronous If Synchronous is missing or False, data is buffered into attributes, which names are returned If True or Yes, all the data is returned when ready Data returned will be (rows,[t0,v0,t1,v1,t2,v2,...]) """ print time.ctime()+"In ", self.get_name(), "::GetAttDataBetweenDates(%s)"%argin # Add your own code here size = 0 aname = argin[0] tag = self.attr2tag(aname) dates = self.dates2times(argin[1:3]) RW = False synch = fn.searchCl('yes|true',str(argin[3:4])) attrs = [tag,tag+'_r',tag+'_w',tag+'_t'] if RW else [tag,tag+'_r',tag+'_w',tag+'_t'] self.reader.get_attribute_values(aname, (lambda v: self.reader_hook(aname,v)),dates[0],dates[1], decimate=True, cache=self.UseApiCache) self.counter+=1 print(self.counter) argout = [fn.shape(attrs),[a for a in attrs]] if not synch: print '\t%s'%argout return argout else: while not self.IsDataReady(aname): fandango.wait(0.1) data = self.AttrData[aname][-1] for t,v in data: argout.append(t) argout.extend(fn.toSequence(v)) return [fn.shape(data),argout]
def export_attribute_to_dict(model,attribute=None,value=None, keep=False,as_struct=False): """ get attribute config, format and value from Tango and return it as a dict :param model: can be a full tango model, a device name or a device proxy keys: min_alarm,name,data_type,format,max_alarm,ch_event,data_format,value, label,writable,device,polling,alarms,arch_event,unit """ attr,proxy = Struct(),None if not isString(model): model,proxy = model.name(),model model = parse_tango_model(model) attr.device = model['device'] proxy = proxy or get_device(attr.device,keep=keep) attr.database = '%s:%s'%(model['host'],model['port']) attr.name = model.get('attribute',None) or attribute or 'state' attr.model = '/'.join((attr.database,attr.device,attr.name)) attr.color = 'Lime' attr.time = 0 attr.events = Struct() def vrepr(v): try: return str(attr.format)%(v) except: return str(v) def cleandict(d): for k,v in d.items(): if v in ('Not specified','No %s'%k): d[k] = '' return d try: v = (value or check_attribute(attr.database+'/'+attr.device+'/'+attr.name)) if v and 'DevFailed' not in str(v): ac = proxy.get_attribute_config(attr.name) try: attr.data_format = str(ac.data_format) attr.data_type = str(PyTango.CmdArgType.values[ac.data_type]) attr.writable = str(ac.writable) attr.label = ac.label attr.standard_unit = ac.standard_unit attr.display_unit = ac.display_unit attr.unit = ac.unit attr.description = (ac.description if ac.description!='No description' else '') attr.format = ac.format attr.dim_x = v.dim_x attr.dim_y = v.dim_y attr.min_value = ac.min_value attr.max_value = ac.max_value attr.max_dim_x = ac.max_dim_x attr.max_dim_y = ac.max_dim_y attr.min_alarm = ac.min_alarm attr.max_alarm = ac.max_alarm attr.enum_labels = list( getattr(ac,'enum_labels',[])) # New in T9 except: traceback.print_exc() attr.events.ch_event = fandango.obj2dict(ac.events.ch_event) attr.events.arch_event = fandango.obj2dict(ac.events.arch_event) attr.events.per_event = fandango.obj2dict(ac.events.per_event) attr.alarms = fandango.obj2dict(ac.alarms) attr.quality = str(v.quality) attr.time = ctime2time(v.time) sep = '\n' if attr.data_type == 'DevString' else ',' if attr.data_format == 'SCALAR': if attr.data_type in ('DevState','DevBoolean'): attr.value = int(v.value) attr.string = str(v.value) else: attr.value = v.value attr.string = vrepr(v.value) else: if v.value is None or not v.dim_x: attr.value = [] attr.string = '[]' elif attr.data_format == 'SPECTRUM': attr.value = list(v.value) svalue = map(vrepr,attr.value) attr.string = sep.join(svalue) elif attr.data_format=='IMAGE': attr.value = list(map(list,v.value)) svalue = [[vrepr(w) for w in vv] for vv in attr.value] attr.string = sep.join('[%s]' % sep.join(vv) for vv in svalue) if 'numpy' in str(type(v.value)): #print('%s("%s") => python' % (type(v.value), attr.string)) attr.value = toSequence(str2type(attr.string)) if attr.unit.strip() not in ('','No unit'): attr.string += ' %s'%(attr.unit) attr.polling = proxy.get_attribute_poll_period(attr.name) else: print((attr.device,attr.name,'unreadable!')) attr.value = None attr.string = str(v) if attr.value is None: attr.data_type = None attr.color = TANGO_STATE_COLORS['UNKNOWN'] elif attr.data_type == 'DevState': attr.color = TANGO_STATE_COLORS.get(attr.string,'Grey') elif 'ALARM' in attr.quality: attr.color = TANGO_STATE_COLORS['FAULT'] elif 'WARNING' in attr.quality: attr.color = TANGO_STATE_COLORS['ALARM'] elif 'INVALID' in attr.quality: attr.color = TANGO_STATE_COLORS['OFF'] except Exception,e: print('export_attribute_to_dict(%s) failed!: %s' % (model,v)) traceback.print_exc() raise(e)