Example #1
0
 def __setitem__(self, key, value):
     key = key.replace('folderds:', '')
     key = tango.get_full_name(key)
     ProxiesDict.__setitem__(self, key, value)
     try:
         i = fn.get_device_info(key)
         self.hosts[i.host].append(key)
     except:
         pass
Example #2
0
    def get_comms_form(self, device, form=None, parent=None):

        self.trace('In TaurusDevicePanel.get_comms_form(%s)' % device)
        dev_class = fandango.get_device_info(device).dev_class
        filters = type(self).getCommandFilters(dev_class)
        params = get_regexp_dict(filters, device, []) or get_regexp_dict(
            filters, dev_class, [])

        if filters and not params:  #If filters are defined only listed devices will show commands
            self.debug(
                'TaurusDevicePanel.get_comms_form(%s): By default an unknown device type will display no commands'
                % device)
            return None

        if not form:
            form = TaurusCommandsForm(parent)

        elif hasattr(form, 'setModel'):
            form.setModel('')

        try:
            form.setModel(device)
            if params:
                form.setSortKey(lambda x, vals=[s[0].lower() for s in params]:
                                vals.index(x.cmd_name.lower())
                                if str(x.cmd_name).lower() in vals else 100)
                form.setViewFilters([
                    lambda c: str(c.cmd_name).lower() not in
                    ('state', 'status') and any(
                        searchCl(s[0], str(c.cmd_name)) for s in params)
                ])
                form.setDefaultParameters(
                    dict((k, v)
                         for k, v in (params if not hasattr(params, 'items')
                                      else params.items()) if v))
            for wid in form._cmdWidgets:
                if not hasattr(wid, 'getCommand') or not hasattr(
                        wid, 'setDangerMessage'):
                    continue
                if re.match('.*(on|off|init|open|close).*',
                            str(wid.getCommand().lower())):
                    wid.setDangerMessage(
                        'This action may affect other systems!')
            #form._splitter.setStretchFactor(1,70)
            #form._splitter.setStretchFactor(0,30)
            form._splitter.setSizes([80, 20])

        except Exception:
            self.warning(
                'Unable to setModel for TaurusDevicePanel.comms_form!!: %s' %
                traceback.format_exc())
        return form
Example #3
0
def archiving_check(schema,csvpath=''):
    api = PyTangoArchiving.ArchivingAPI(schema)
    
    states = api.servers.states()
    
    values = api.load_last_values()#time consuming on HDB
    shouldbe = sorted(a for a in api if values[a] and fandango.date2time(values[a][0][0]) > time.time()-2*30*3600*24)
    active = api.get_archived_attributes()
    updated = sorted(a for a in active if values[a] and fandango.date2time(values[a][0][0]) > time.time()-3*3600)
    
    missing = sorted(a for a in shouldbe if a not in active)
    lost = sorted(a for a in active if a not in updated)
    
    loadarchivers = defaultdict(list)
    loadservers = defaultdict(list)
    lostarchivers = defaultdict(list)
    lostservers = defaultdict(list)
    for a in active:
        arch = api[a].archiver.lower()
        server = api.servers.get_device_server(arch).lower()
        loadarchivers[arch].append(a)
        loadservers[server].append(a)
        if a in lost:
            lostarchivers[arch].append(a)
            lostservers[server].append(a)
            
    [loadservers[api.servers.get_device_server(api[a].archiver.lower()).lower()].append(a) for a in active]

    emptyarchivers = [a for a,v in loadarchivers.items() if not len(v)]
    lostrate = dict((a,len(v) and len([a for a in v if a in lost])/float(len(v))) for a,v in loadarchivers)
    lostserversrate = dict((a,len(v) and len([a for a in v if a in lost])/float(len(v))) for a,v in loadservers.items())
    
    dedi = api.load_dedicated_archivers()
    dediattrs = defaultdict(list)
    [dediattrs[a.lower()].append(d) for d,v in dedi.items() for a in v];
    dmult = [a for a,v in dediattrs.items() if len(v)>1]
    wrongnames = [a for a in dediattrs if not attribute_name_check(a)]
    wrongarchivers = set(k.lower() for k,v in dedi.items() if any(a.lower() in map(str.lower,v) for a in wrongnames))
    wrongattrs = [a for a,v in dediattrs if a in api and api[a].archiver.lower()!=v[0].lower()]
    deleteattrs = [a for a in dediattrs if a not in shouldbe]
    
    fnames = GetConfigFiles(csvpath) if csvpath else GetConfigFiles()
    csvs = dict((f,pta.ParseCSV(f,schema)) for f in fnames)
    csvattrs = defaultdict(list)
    [csvattrs[a.lower().strip()].append(f) for f,v in csvs.items() for a in v]
    
    stats = sorted([(len(v),len(v) and len([a for a in v if a in lost])/float(len(v))) for v in loadservers.values()])
    stats = [(x,fandango.avg(t[1] for t in stats if t[0]==x)) for x in sorted(set(v[0] for v in stats))]
    # pylab.plot([t[0] for t in stats], [t[1] for t in stats]); pylab.show()
    exported = dict((d,fandango.str2time(fandango.get_device_info(d).started,'%dst %B %Y at %H:%M:%S')) for d in api.get_archivers())
    first = min(exported.values())
    #SLOWER SPEEDS ALWAYS HAVE MORE LOST ATTRIBUTES
    
    #Let's try a different approach to restart, much less agressive than fandango.start_servers()!
    #It seems that there's a lock when so many devices are restarted at once!
    torestart = list(reversed(sorted((len(v),k) for k,v in lostservers.items())))
    
    for k in torestart.values():
        print('Restarting %s')
        fandango.Astor(k).stop_servers()
        time.sleep(20.)
        fandango.Astor(k).start_servers(wait=240.)
    
    allattrs = sorted(set([a for a in csvattrs if a in api]+shouldbe+active))
Example #4
0
class VaccaPanel(fandango.qt.Dropable(taurus.qt.qtgui.panel.TaurusDevicePanel)
                 ):
    """

    :param parent:
    :param model: a device name or an state attribute
    :param palette:
    :param bound:
    :param filters:
    :return:

    It is a class that inherits from TaurusDevicePanel and Dropable module from fandango.

    If connect=True at init, it checks if it exists any shareDataManager to
        subscribe in it. It can be done later using getPanelDescription or connectSharedSignals

    This Widget shows the device Commands and Attributes, it is listening the shareDataManager to show the device selected information.

    The title of this Widget can be draggable.

    VaccaPanel has the follow functionalities:

        * Title is draggable.
        * Is connected to shareDataManager to share information in the GUI.

    """
    def __init__(self,
                 parent=None,
                 model=None,
                 filters=[],
                 connect=False):  #,palette=None, bound=True,filters=[]):
        """
        In Init, the class VaccaPanel check if exist any shareDataManager to
        subscribe in it.

        :param parent:
        :param model: Model to start the Panel.
        :param filters: dictionary with 'attrs' and 'comms' filters as regexp or tuples lists
        :return:
        """

        self.call__init__(taurus.qt.qtgui.panel.TaurusDevicePanel, parent,
                          model)
        #self.setLogLevel(self.Info)
        self.info('init(%s,%s): connecting ...' % (model, filters))

        self._connected = []
        if connect: self.connectSharedSignals()

        if self.checkDropSupport():
            self.setSupportedMimeTypes([
                self.TAURUS_DEV_MIME_TYPE, self.TEXT_MIME_TYPE,
                self.TAURUS_MODEL_MIME_TYPE
            ])
            self.setDropEventCallback(self.setModelHook)

        self.info('init(...): layout ...')
        self._header.layout().removeWidget(self._label)
        #self._label = vacca.utils.DraggableLabel()
        self._label = fandango.qt.Draggable(Qt.QLabel)()
        self._label.font().setBold(True)
        self._label.setText('SELECT A DEVICE FROM THE TREE')
        self._header.layout().addWidget(self._label, 0, 1, Qt.Qt.AlignLeft)
        self._label.setDragEventCallback(self._label.text)
        #self.setToolTip(getattr(self,'__help__',self.__doc__))

        if filters:
            self.info('VaccaPanel(filters=%s)' % filters)
            if 'attrs' in filters:
                type(self)._attribute_filter = {'.*': filters['attrs']}
            if 'comms' in filters:
                type(self)._command_filter = {
                    '.*': [
                        c if fun.isSequence(c) else (c, ())
                        for c in filters['comms']
                    ]
                }

    def connectSharedSignals(self, read='SelectedInstrument', write=''):
        self.info('connectSharedSignals(%s,%s)' % (read, write))
        sdm = vacca.utils.get_shared_data_manager()
        if sdm and read and read not in self._connected:
            sdm.connectReader(read, self.setModelHook, readOnConnect=True)
            self._connected.append(read)
        return

    def setModelHook(self, model):
        #self.info('%s,%s'%(repr(args),repr(kwargs)))
        #l = [(str(type(a)),) for l in (args,kwargs.values()) for a in args]
        self.info('In setModelHook(%s)' % str(model))
        try:
            fandango.tango.parse_tango_model(str(model).strip())['device']
            self.setModel(model)
        except:
            self.warning('Invalid model: %s\n%s' %
                         (repr(model), traceback.format_exc()))

    @classmethod
    def getAttributeFilters(klass, dev_class=None):
        """
        TaurusDevicePanel filters are fixed to work by device name.
        But, if AttributeFilters is declared as class property, it will override them.
        
        get{Property}(klass,dev_class) will update it from Tango DB and return the matching values.
        set{Property}(dict) will update the parent TaurusDevicePanel dictionary, not the DB
        """
        if dev_class is not None and dev_class not in klass._attribute_filter:
            filters = get_class_property(dev_class,
                                         'AttributeFilters',
                                         extract=False)
            if filters:
                filters = [(l.split(':')[0], l.split(':')[-1].split(','))
                           for l in filters]
                klass._attribute_filter[dev_class] = filters
                #return {'.*':filters}

        print('getAttributeFilters(%s,%s): ...' % (klass, dev_class)
              )  #,klass._attribute_filter))
        return klass._attribute_filter

    @classmethod
    def getCommandFilters(klass, dev_class=None):
        """
        get{Property}(klass,dev_class) will update it from Tango DB and return the matching values.
        set{Property}(dict) will update the parent TaurusDevicePanel dictionary, not the DB
        """
        if dev_class is not None and dev_class not in klass._command_filter:
            filters = get_class_property(dev_class,
                                         'CommandFilters',
                                         extract=False)
            if filters:
                #filters = dict((k,eval(v)) for k,v in (l.split(':',1) for l in filters))
                filters = [(c, ()) for c in filters]
                klass._command_filter[dev_class] = filters

        print('getCommandFilters(%s,%s): ...' % (klass, dev_class)
              )  #,klass._command_filter))
        return klass._command_filter

    @classmethod
    def getIconMap(klass, dev_class=None):
        if dev_class is not None and dev_class not in klass._icon_map:
            p = get_class_property(dev_class, 'Icon', extract=True)
            if p: klass._icon_map[dev_class] = p  #Not trivial!
        print('getIconMap(%s): ...' % (klass))
        #print('getIconMap(%s): ...%s'%(klass,klass._icon_map))
        return klass._icon_map

    def setModel(self, model, pixmap=None):
        """
        Set Model is the callback used in shareDataManager to manage device
        selections.

        :param model: Model to VaccaPanel
        :param pixmap:
        :return:
        """
        try:
            #self.setLogLevel(self.Debug)
            self.info('VaccaPanel(%s).setModel(%s,%s)' %
                      (id(self), model, pixmap))
            model, modelclass, raw = str(model).strip(), '', model
            model = fandango.tango.parse_tango_model(model)
            if model is None:
                self.warning(
                    'VaccaPanel(%s).setModel(%s,%s): MODEL NOT PARSABLE!' %
                    (id(self), model, pixmap))
                return
            else:
                model = model['device']
            if model:
                model = model and model.split()[0] or ''
                modelclass = taurus.Factory().findObjectClass(model)
            self.debug('In TaurusDevicePanel.setModel(%s(%s),%s)' %
                       (raw, modelclass, pixmap))
            if model == self.getModel():
                return
            elif raw is None or not model or not modelclass:
                if self.getModel(): self.detach()
                return
            elif issubclass(modelclass, TaurusAttribute):
                #if model.lower().endswith('/state'):
                model = model.rsplit('/', 1)[0]
            elif not issubclass(modelclass, TaurusDevice):
                self.warning('TaurusDevicePanel accepts only Device models')
                return
        except Exception, e:
            traceback.print_exc()
            raise e

        try:
            taurus.Device(model).ping()
            dev_class = fandango.get_device_info(model).dev_class
            if self.getModel():
                self.detach(
                )  #Do not dettach previous model before pinging the new one (fail message will be shown at except: clause)
            TaurusWidget.setModel(self, model)
            self.setWindowTitle(str(model).upper())
            model = self.getModel()
            self._label.setText(model.upper())
            font = self._label.font()
            font.setPointSize(15)
            self._label.setFont(font)
            if pixmap is None and self.getIconMap(dev_class=dev_class):
                for k, v in self.getIconMap().items():
                    if searchCl(k, model) or searchCl(k, dev_class):
                        pixmap = v
                        break
            if pixmap is not None:
                #print 'Pixmap is %s'%pixmap
                qpixmap = Qt.QPixmap(pixmap)
                if qpixmap.height() > .9 * IMAGE_SIZE[1]:
                    qpixmap = qpixmap.scaledToHeight(.9 * IMAGE_SIZE[1])
                if qpixmap.width() > .9 * IMAGE_SIZE[0]:
                    qpixmap = qpixmap.scaledToWidth(.9 * IMAGE_SIZE[0])
            else:
                qpixmap = getPixmap(':/logo.png')

            self._image.setPixmap(qpixmap)
            self._state.setModel(model + '/state')
            if hasattr(self, '_statelabel'):
                self._statelabel.setModel(model + '/state')
            self._status.setModel(model + '/status')

            try:
                self._attrsframe.clear()
                class_filters = type(self).getAttributeFilters(
                    dev_class=dev_class)
                filters = get_regexp_dict(self._attribute_filter, model, [])
                if not filters:
                    filters = get_regexp_dict(self._attribute_filter,
                                              dev_class, ['.*'])

                search_tab = None
                if filters == [
                        '.*'
                ] and len(taurus.Device(model).get_attribute_list()) > 32:
                    filters = [('Attributes', ['.*'])]

                if hasattr(filters, 'keys'):
                    filters = filters.items()  #Dictionary!
                print('\tfilters = %s' % filters)

                #Showing multiple Tabs
                if filters and isinstance(filters[0], (list, tuple)):
                    self._attrs = []
                    for tab, attrs in filters:
                        if attrs[1:] or attrs[0] not in ('*', '.*'):
                            self.debug(
                                'VaccaPanel.setModel.get_attrs_form(%s,%s)' %
                                (model, attrs))
                            self._attrs.append(
                                self.get_attrs_form(device=model,
                                                    filters=attrs,
                                                    parent=self))
                            self._attrsframe.addTab(self._attrs[-1], tab)
                        else:
                            self.info('Embedding a Search panel')
                            search_tab = tab, VaccaSearchForm(preffix=model +
                                                              '/*',
                                                              suffix='*',
                                                              labels=True)
                #Mapping into a single form
                else:
                    if self._attrs and isinstance(self._attrs, list):
                        self._attrs = self._attrs[0]
                    if not isinstance(self._attrs, Qt.QWidget):
                        self._attrs = None
                    self._attrs = self.get_attrs_form(device=model,
                                                      form=self._attrs,
                                                      filters=filters,
                                                      parent=self)
                    if self._attrs:
                        self._attrsframe.addTab(self._attrs, 'Attributes')

                if not TaurusDevicePanel.READ_ONLY:
                    self._comms = self.get_comms_form(model, self._comms, self)
                    if self._comms:
                        self._attrsframe.addTab(self._comms, 'Commands')

                    if search_tab:
                        self._attrsframe.addTab(search_tab[1], search_tab[0])

                if SPLIT_SIZES: self._splitter.setSizes(SPLIT_SIZES)

            except Exception, e:
                self.warning('setModel(%s) failed!' % model)
                self.warning(traceback.format_exc())
                qmsg = Qt.QMessageBox(Qt.QMessageBox.Critical,
                                      '%s Error' % model,
                                      '%s not available' % model,
                                      Qt.QMessageBox.Ok, self)
                qmsg.setDetailedText(traceback.format_exc())
                qmsg.show()
                raise e

        except Exception, e:
            self.warning('setModel(%s) failed!' % model)
            self.warning(traceback.format_exc())
            qmsg = Qt.QMessageBox(Qt.QMessageBox.Critical, '%s Error' % model,
                                  '%s not available' % model,
                                  Qt.QMessageBox.Ok, self)
            qmsg.setDetailedText(traceback.format_exc())
            qmsg.show()
            raise e