示例#1
0
 def __init__(self, name, **kwargs):
     TaurusDevice.__init__(self, name, **kwargs)
     self._validator = TangoArchivingDeviceNameValidator()
     g = self._validator.getUriGroups(self.getFullName())
     db = g.get('arch_db', '*')
     tango_host = '%s:%s' % (g.get('host'), g.get('port'))
     self._reader = Reader(db, tango_host=tango_host, logger=self)
def minimal_check(schema,
                  interval=3 * 3600,
                  exclude=".*(wavename|waveid)$",
                  csvs=""):
    import re, time
    from PyTangoArchiving import Reader
    from PyTangoArchiving.utils import check_attribute
    r = fun.Struct()
    rd = Reader(schema)
    db = rd.get_database()
    r.ids = db.get_attributes_IDs()
    r.active = db.get_attribute_names(active=True)
    r.shouldbe = get_all_config_attrs(schema, csvs) if csvs else r.active
    r.shouldbe = [a for a in r.shouldbe if not re.match(exclude, a.lower())]
    print('%d attributes are active' % len(r.active))
    print('%d attributes should be active' % len(r.shouldbe))
    r.missing = [a for a in r.shouldbe if a not in r.ids]
    r.polizon = [a for a in r.active if a not in r.shouldbe]
    print('%d attributes are archived but not configured' % len(r.polizon))
    r.updates = db.get_table_updates()
    r.notupdated = [
        a for a in r.active
        if r.updates[db.get_table_name(r.ids[a])] < time.time() - interval
    ]
    print('%d active attributes are not updated' % len(r.notupdated))
    print('%d shouldbe attributes are missing' % len(r.missing))
    r.lost = [
        a for a in r.shouldbe if a in r.ids
        and r.updates[db.get_table_name(r.ids[a])] < time.time() - interval
    ]
    r.lost = filter(check_attribute, r.lost)
    print('%d shouldbe attributes are active but lost' % len(r.lost))
    return r
示例#3
0
def get_attributes_as_event_list(attributes,start_date=None,stop_date=None,formula=None):
    """
    This method returns attribute changes ordered by time (event_list format)
    Attributes can be passed as a list or as a formula (TangoEval) or both. 
    If a formula is available the evaluated value will be added at each row of the list.
    """
    from PyTangoArchiving import Reader
    from fandango import isSequence,isString,TangoEval
    rd = Reader()
    te = fandango.TangoEval()

    if isString(attributes) and formula is None:
        try:
            formula = attributes
            attributes = sorted(set('%s/%s'%t[:2] for t in te.parse_variables(formula)))
            if len(attributes)==1: formula = None
        except:
            formula,attributes = None,[]

    if isSequence(attributes):
        assert start_date, 'start_date argument is missing!'
        attributes = rd.get_attributes_values(attributes,start_date,stop_date)
    
    avals = dict((k,decimate_array(v)) for k,v in attributes.items())
    buffer = sorted((v[0],k,v[1]) for k,l in avals.items() for i,v in enumerate(l) if not i or v[1]!=l[i-1][1])
    
    if formula is not None:
        cache,parsed = {},te.parse_formula(formula)
        for i,event in enumerate(buffer):
            cache[event[1]] = event[2]
            f = te.eval(parsed,cache) if all(k in cache for k in attributes) else None
            buffer[i] = (event[0],event[1],event[2],f)
            
    return buffer
示例#4
0
def get_attributes_as_event_list(attributes,start_date=None,stop_date=None,formula=None):
    """
    This method returns attribute changes ordered by time (event_list format)
    Attributes can be passed as a list or as a formula (TangoEval) or both. 
    If a formula is available the evaluated value will be added at each row of the list.
    """
    from PyTangoArchiving import Reader
    from fandango import isSequence,isString,TangoEval
    rd = Reader()
    te = fandango.TangoEval()

    if isString(attributes) and formula is None:
        try:
            formula = attributes
            attributes = sorted(set('%s/%s'%t[:2] for t in te.parse_variables(formula)))
            if len(attributes)==1: formula = None
        except:
            formula,attributes = None,[]

    if isSequence(attributes):
        assert start_date, 'start_date argument is missing!'
        attributes = rd.get_attributes_values(attributes,start_date,stop_date)
    
    avals = dict((k,decimate_array(v)) for k,v in attributes.items())
    buffer = sorted((v[0],k,v[1]) for k,l in avals.items() for i,v in enumerate(l) if not i or v[1]!=l[i-1][1])
    
    if formula is not None:
        cache,parsed = {},te.parse_formula(formula)
        for i,event in enumerate(buffer):
            cache[event[1]] = event[2]
            f = te.eval(parsed,cache) if all(k in cache for k in attributes) else None
            buffer[i] = (event[0],event[1],event[2],f)
            
    return buffer
示例#5
0
class ArchivingDevice(TaurusDevice):
    """Archiving device object. """
    _scheme = 'archiving'
    _description = "A Archiving Device"

    def __init__(self, name, **kwargs):
        TaurusDevice.__init__(self, name, **kwargs)
        self._validator = ArchivingDeviceNameValidator()
        g = self._validator.getUriGroups(self.getFullName())
        db = g.get('devname')
        tango_host = '%s:%s' % (g.get('host'), g.get('port'))
        self._reader = Reader(db, tango_host=tango_host, logger=self)

    def getReader(self):
        """Get the PyTangoArchiving Reader

        :return PyTangoArchiving.Reader
        """
        return self._reader

    def getAttribute(self, attrname):
        """Returns the attribute object given its name"""
        attrname = "%s/%s" % (self.getFullName(), attrname)
        return self.factory().getAttribute(attrname)

    def add_attribute(self, attribute, device, period):
        pass

    def getArchivedAttributes(self, active=False):
        """getArchivedAttributes show the names of the archived attributes

       :return: A list with the names of the current archived attributes.
        """
        return self._reader.get_attributes(active)
示例#6
0
def tdb_to_hdb(attribute,start=0,stop=fun.END_OF_TIME,modes={},delete=False):
    """
    This method allows to copy an attribute from TDB to HDB, inserting the contents
    of the current TDB buffer into the HDB tables.
    @param start/stop allow to limit the dates for insertion
    @param delete will remove all existing values in HDB for the given interval
    """
    from PyTangoArchiving import Reader
    hdb,tdb = Reader('hdb'),Reader('tdb')
    assert attribute in tdb.get_attributes()
    values = tdb.get_attribute_values(attribute,start or time.time()-tdb.RetentionPeriod,stop)
    if attribute not in hdb.get_attributes(): 
        from PyTangoArchiving import ArchivingAPI
        api = ArchivingAPI('hdb')
        api.start_archiving(*((attribute,modes) if modes else (attribute,)))
        api.load_attribute_descriptions()
    db = hdb.get_database()
    table = db.get_table_name(db.get_attribute_ID(attribute))
    import_into_db(db,table,values,delete)    
示例#7
0
def tdb_to_hdb(attribute,start=0,stop=fun.END_OF_TIME,modes={},delete=False):
    """
    This method allows to copy an attribute from TDB to HDB, inserting the contents
    of the current TDB buffer into the HDB tables.
    @param start/stop allow to limit the dates for insertion
    @param delete will remove all existing values in HDB for the given interval
    """
    from PyTangoArchiving import Reader
    hdb,tdb = Reader('hdb'),Reader('tdb')
    assert attribute in tdb.get_attributes()
    values = tdb.get_attribute_values(attribute,start or time.time()-tdb.RetentionPeriod,stop)
    if attribute not in hdb.get_attributes(): 
        from PyTangoArchiving import ArchivingAPI
        api = ArchivingAPI('hdb')
        api.start_archiving(*((attribute,modes) if modes else (attribute,)))
        api.load_attribute_descriptions()
    db = hdb.get_database()
    table = db.get_table_name(db.get_attribute_ID(attribute))
    import_into_db(db,table,values,delete)    
def minimal_check(schema,interval=3*3600,exclude=".*(wavename|waveid)$",csvs=""):
    import re,time
    from PyTangoArchiving import Reader
    from PyTangoArchiving.utils import check_attribute
    r = fun.Struct()
    rd = Reader(schema)
    db = rd.get_database()
    r.ids = db.get_attributes_IDs()
    r.active = db.get_attribute_names(active=True)
    r.shouldbe = get_all_config_attrs(schema,csvs) if csvs else r.active
    r.shouldbe = [a for a in r.shouldbe if not re.match(exclude,a.lower())]
    print('%d attributes are active'%len(r.active))
    print('%d attributes should be active'%len(r.shouldbe))
    r.missing = [a for a in r.shouldbe if a not in r.ids]
    r.polizon = [a for a in r.active if a not in r.shouldbe]
    print('%d attributes are archived but not configured'%len(r.polizon))
    r.updates = db.get_table_updates()
    r.notupdated = [a for a in r.active if  r.updates[db.get_table_name(r.ids[a])]<time.time()-interval]
    print('%d active attributes are not updated'%len(r.notupdated))
    print('%d shouldbe attributes are missing'%len(r.missing))    
    r.lost = [a for a in r.shouldbe if a in r.ids and r.updates[db.get_table_name(r.ids[a])]<time.time()-interval]
    r.lost = filter(check_attribute,r.lost)
    print('%d shouldbe attributes are active but lost'%len(r.lost))
    return r
    def show_history(self, attribute):
        TABS = []
        print 'getting archiving readers ...'
        from PyTangoArchiving import Reader
        hdb = Reader(db='hdb', schema='hdb')
        tdb = Reader(db='tdb', schema='tdb')
        tformat = '%Y-%m-%d %H:%M:%S'
        str2epoch = lambda s: time.mktime(time.strptime(s, tformat))
        epoch2str = lambda f: time.strftime(tformat, time.localtime(f))
        attribute = attribute.lower()

        if attribute in hdb.get_attributes(
        ) or attribute in tdb.get_attributes():
            print '%s is being archived' % attribute
            di = Qt.QDialog()
            wi = di  #QtGui.QWidget(di)
            wi.setLayout(Qt.QGridLayout())
            begin = Qt.QLineEdit()
            begin.setText(epoch2str(time.time() - 3600))
            end = Qt.QLineEdit()
            end.setText(epoch2str(time.time()))
            wi.setWindowTitle('Show Archiving')
            wi.layout().addWidget(
                Qt.QLabel('Enter Begin and End dates in %s format' % tformat),
                0, 0, 1, 2)
            wi.layout().addWidget(Qt.QLabel('Begin:'), 1, 0, 1, 1)
            wi.layout().addWidget(Qt.QLabel('End:'), 2, 0, 1, 1)
            wi.layout().addWidget(begin, 1, 1, 1, 1)
            wi.layout().addWidget(end, 2, 1, 1, 1)
            buttons = Qt.QDialogButtonBox(Qt.QDialogButtonBox.Ok
                                          | Qt.QDialogButtonBox.Cancel)
            wi.connect(buttons, Qt.SIGNAL('accepted()'), wi.accept)
            wi.connect(buttons, Qt.SIGNAL('rejected()'), wi.reject)
            wi.layout().addWidget(buttons, 3, 0, 1, 2)

            def check_values():
                di.exec_()
                if di.result():
                    print 'checking result ...'
                    start, stop = str(begin.text()), str(end.text())
                    if not all(
                            re.match(
                                '[0-9]+-[0-9]+-[0-9]+ [0-9]+:[0-9]+:[0-9]+',
                                str(s).strip()) for s in (start, stop)):
                        print 'dates are wrong ...'
                        Qt.QMessageBox.warning(
                            None, 'Show archiving',
                            'Dates seem not in %s format' % (tformat),
                            Qt.QMessageBox.Ok)
                        return check_values()
                    else:
                        print 'getting values ...'
                        reader = tdb if str2epoch(start) > (
                            time.time() - 5 * 24 * 3600.
                        ) and attribute in tdb.get_attributes() else hdb
                        print 'using %s reader' % reader.schema
                        values = reader.get_attribute_values(
                            attribute, str2epoch(start), str2epoch(stop))
                        if not len(
                                values
                        ) and reader is tdb and attribute in hdb.get_attributes(
                        ):
                            print 'tdb failed, retrying with hdb'
                            values = hdb.get_attribute_values(
                                attribute, str2epoch(start), str2epoch(stop))
                        print 'drawing table from %d values' % len(values)
                        tab = Qt.QTableWidget()
                        tab.setWindowTitle('%s: %s to %s' %
                                           (attribute, start, stop))
                        tab.setRowCount(len(values))
                        tab.setColumnCount(2)
                        tab.setHorizontalHeaderLabels(['TIME', 'VALUE'])
                        for i, tup in enumerate(values):
                            date, value = tup
                            tab.setItem(i, 0,
                                        Qt.QTableWidgetItem(epoch2str(date)))
                            tab.setItem(i, 1, Qt.QTableWidgetItem(str(value)))
                        tab.show()
                        tab.resizeColumnsToContents()
                        tab.horizontalHeader().setStretchLastSection(True)
                        TABS.append(tab)
                        tab.connect(tab,
                                    Qt.SIGNAL('close()'),
                                    lambda o=tab: TABS.remove(o))
                        print 'show_history done ...'
                        return tab
                else:
                    print 'dialog closed'
                    return None

            print 'asking for dates ...'
            return check_values()
        else:
            Qt.QMessageBox.warning(
                None, 'Show archiving',
                'Attribute %s is not being archived' % attribute,
                Qt.QMessageBox.Ok)
示例#10
0
    def show_new_dialog(klass, attribute, schema='*', parent=None, dates=[]):
        try:
            if not Qt.QApplication.instance(): Qt.QApplication([])
        except:
            pass
        print 'in Vacca.widgets.show_history(%s)'

        print 'getting archiving readers ...'
        from PyTangoArchiving import Reader
        rd = Reader(schema.lower())
        hdb = Reader('hdb')
        tdb = Reader('tdb')
        attribute = str(attribute).lower()

        try:
            dates = dates or klass.get_default_dates()
            if not all(map(fandango.isString, dates)):
                dates = map(epoch2str, dates)
        except:
            traceback.print_exc()
            dates = epoch2str(), epoch2str()

        schemas = rd.is_attribute_archived(attribute, preferent=False)
        if schemas:
            print '%s is being archived' % attribute
            di = Qt.QDialog(parent)
            wi = di  #QtGui.QWidget(di)
            wi.setLayout(Qt.QGridLayout())
            begin = Qt.QLineEdit()
            begin.setText(dates[0])
            end = Qt.QLineEdit()
            end.setText(dates[1])
            tfilter = Qt.QLineEdit()
            vfilter = Qt.QCheckBox()
            wi.setWindowTitle('Show %s Archiving' % attribute)
            wil = wi.layout()
            wi.layout().addWidget(Qt.QLabel(attribute), 0, 0, 1, 2)
            wi.layout().addWidget(Qt.QLabel('Preferred Schema'), 1, 0, 1, 1)
            qschema = Qt.QComboBox()
            qschema.insertItems(0, ['*'] + list(schemas))
            wil.addWidget(qschema, 1, 1, 1, 1)
            #qb = Qt.QPushButton("Save as preferred schema")
            #wil.addWidget(qb,wil.rowCount(),0,1,2)
            #qi.connect(qb,Qt.SIGNAL('pushed()'),save_schema)
            wil.addWidget(
                Qt.QLabel('Enter Begin and End dates in %s format' % tformat),
                2, 0, 1, 2)
            wil.addWidget(Qt.QLabel('Begin:'), 3, 0, 1, 1)
            wil.addWidget(begin, 3, 1, 1, 1)
            wil.addWidget(Qt.QLabel('End:'), 4, 0, 1, 1)
            wil.addWidget(end, 4, 1, 1, 1)
            wil.addWidget(Qt.QLabel('Time Filter:'), 5, 0, 1, 1)
            wil.addWidget(tfilter, 5, 1, 1, 1)
            wil.addWidget(Qt.QLabel('Value Filter:'), 6, 0, 1, 1)
            wil.addWidget(vfilter, 6, 1, 1, 1)
            buttons = Qt.QDialogButtonBox(wi)
            buttons.addButton(Qt.QPushButton("Export"),
                              Qt.QDialogButtonBox.AcceptRole)

            bt = Qt.QPushButton("Apply")
            buttons.addButton(bt, Qt.QDialogButtonBox.ApplyRole)

            def set_schema(r=rd, a=attribute, qs=qschema):
                print 'setting schema ...'
                schema = str(qs.currentText()).strip()
                r.set_preferred_schema(a, schema)

            buttons.connect(bt, Qt.SIGNAL('clicked()'), set_schema)

            buttons.addButton(Qt.QPushButton("Close"),
                              Qt.QDialogButtonBox.RejectRole)

            #  Qt.QDialogButtonBox.Apply\
            #    |Qt.QDialogButtonBox.Open|Qt.QDialogButtonBox.Close)
            wi.connect(buttons, Qt.SIGNAL('accepted()'), wi.accept)
            wi.connect(buttons, Qt.SIGNAL('rejected()'), wi.reject)
            wi.layout().addWidget(buttons, 7, 0, 1, 2)

            def check_values():
                di.exec_()
                print 'checking schema ...'
                schema = str(qschema.currentText()).strip()
                if schema != '*':
                    rd.set_preferred_schema(attribute, schema)
                if di.result():
                    print 'checking result ...'
                    try:
                        start, stop = str(begin.text()), str(end.text())
                        try:
                            tf = int(str(tfilter.text()))
                        except:
                            tf = 0
                        vf = vfilter.isChecked()
                        if not all(
                                re.match(
                                    '[0-9]+-[0-9]+-[0-9]+ [0-9]+:[0-9]+:[0-9]+',
                                    str(s).strip()) for s in (start, stop)):
                            print 'dates are wrong ...'
                            Qt.QMessageBox.warning(
                                None, 'Show archiving',
                                'Dates seem not in %s format' % (tformat),
                                Qt.QMessageBox.Ok)
                            return check_values()
                        else:
                            print 'getting values ...'
                            print 'using %s reader' % rd.schema
                            values = rd.get_attribute_values(
                                attribute, str2epoch(start), str2epoch(stop))
                            if not len(
                                    values
                            ) and schema == '*' and hdb.is_attribute_archived(
                                    attribute, active=True):
                                print 'tdb failed, retrying with hdb'
                                values = hdb.get_attribute_values(
                                    attribute, str2epoch(start),
                                    str2epoch(stop))
                            if vf:
                                values = fandango.arrays.decimate_array(values)
                            if tf:
                                print 'Filtering %d values (1/%dT)' % (
                                    len(values), tf)
                                values = fandango.arrays.filter_array(
                                    values, window=tf)  #,begin=start,end=stop)

                            twi = klass.setup_table(attribute, start, stop,
                                                    values)
                            klass.set_default_dates(str2epoch(start),
                                                    str2epoch(stop))

                            button = Qt.QPushButton('Save to file')
                            button2 = Qt.QPushButton('Send to email')

                            def save_to_file(var=attribute,
                                             data=values,
                                             parent=twi,
                                             edit=True):
                                try:
                                    options = {
                                        'sep': '\\t',
                                        'arrsep': '\\ ',
                                        'linesep': '\\n'
                                    }
                                    import codecs
                                    dd = QOptionDialog(model=options,
                                                       title='CSV Options')
                                    dd.exec_()
                                    for k, v in options.items():
                                        options[k] = codecs.escape_decode(
                                            str(v))[0]
                                    print options

                                    filename = Qt.QFileDialog.getSaveFileName(
                                        parent, 'File to save',
                                        '/data/' + PyTangoArchiving.files.
                                        get_data_filename(
                                            var, data, 'csv', 'human'),
                                        'CSV files (*.csv)')

                                    PyTangoArchiving.files.save_data_file(
                                        var,
                                        data,
                                        filename,
                                        format='csv',
                                        **options)
                                    if edit:
                                        try:
                                            os.system('gedit %s &' % filename)
                                        except:
                                            pass
                                    return filename
                                except Exception, e:
                                    Qt.QMessageBox.warning(
                                        None, "Warning",
                                        "Unable to save %s\n:%s" %
                                        (filename, e))

                            def send_by_email(var=attribute,
                                              data=values,
                                              parent=twi):
                                #subject,text,receivers,sender='',attachments=None,trace=False):
                                try:
                                    receivers, ok = Qt.QInputDialog.getText(
                                        None, 'Send by email', 'to:')
                                    if ok:
                                        filename = str(
                                            save_to_file(var,
                                                         data,
                                                         parent,
                                                         edit=False))
                                        fandango.linos.sendmail(
                                            filename,
                                            attribute,
                                            receivers=str(receivers),
                                            attachments=[filename])
                                except Exception, e:
                                    Qt.QMessageBox.warning(
                                        None, "Warning",
                                        "Unable to send %s\n:%s" %
                                        (filename, e))

                            #button.setTextAlignment(Qt.Qt.AlignCenter)
                            twi.connect(button, Qt.SIGNAL("pressed ()"),
                                        save_to_file)
                            twi.layout().addWidget(button)
                            twi.connect(button2, Qt.SIGNAL("pressed ()"),
                                        send_by_email)
                            twi.layout().addWidget(button2)
                            twi.show()

                            TABS.append(twi)
                            twi.connect(twi,
                                        Qt.SIGNAL('close()'),
                                        lambda o=twi: TABS.remove(o))
                            print 'show_history done ...'
                            return twi
                    except Exception, e:
                        print traceback.format_exc()
                        Qt.QMessageBox.warning(
                            None, "Warning",
                            "Unable to retrieve the values (%s), sorry" % e)
示例#11
0
    def show_history(self, attribute):
        TABS=[]
        print 'getting archiving readers ...'
        from PyTangoArchiving import Reader
        hdb = Reader(db='hdb',schema='hdb')
        tdb = Reader(db='tdb',schema='tdb')
        tformat = '%Y-%m-%d %H:%M:%S'
        str2epoch = lambda s: time.mktime(time.strptime(s,tformat))
        epoch2str = lambda f: time.strftime(tformat,time.localtime(f))
        attribute = attribute.lower()

        if attribute in hdb.get_attributes() or attribute in tdb.get_attributes():
            print '%s is being archived' % attribute
            di = Qt.QDialog()
            wi = di #QtGui.QWidget(di)
            wi.setLayout(Qt.QGridLayout())
            begin = Qt.QLineEdit()
            begin.setText(epoch2str(time.time()-3600))
            end = Qt.QLineEdit()
            end.setText(epoch2str(time.time()))
            wi.setWindowTitle('Show Archiving')
            wi.layout().addWidget(Qt.QLabel('Enter Begin and End dates in %s format'%tformat),0,0,1,2)
            wi.layout().addWidget(Qt.QLabel('Begin:'),1,0,1,1)
            wi.layout().addWidget(Qt.QLabel('End:'),2,0,1,1)
            wi.layout().addWidget(begin,1,1,1,1)
            wi.layout().addWidget(end,2,1,1,1)
            buttons = Qt.QDialogButtonBox(Qt.QDialogButtonBox.Ok|Qt.QDialogButtonBox.Cancel)
            wi.connect(buttons,Qt.SIGNAL('accepted()'),wi.accept)
            wi.connect(buttons,Qt.SIGNAL('rejected()'),wi.reject)
            wi.layout().addWidget(buttons,3,0,1,2)

            def check_values():
                di.exec_()
                if di.result():
                    print 'checking result ...'
                    start,stop = str(begin.text()),str(end.text())
                    if not all(re.match('[0-9]+-[0-9]+-[0-9]+ [0-9]+:[0-9]+:[0-9]+',str(s).strip()) for s in (start,stop)):
                        print 'dates are wrong ...'
                        Qt.QMessageBox.warning(None,'Show archiving', 'Dates seem not in %s format'%(tformat), Qt.QMessageBox.Ok)
                        return check_values()
                    else:
                        print 'getting values ...'
                        reader = tdb if str2epoch(start)>(time.time()-5*24*3600.) and attribute in tdb.get_attributes() else hdb
                        print 'using %s reader' % reader.schema
                        values = reader.get_attribute_values(attribute,str2epoch(start),str2epoch(stop))
                        if not len(values) and reader is tdb and attribute in hdb.get_attributes():
                            print 'tdb failed, retrying with hdb'
                            values = hdb.get_attribute_values(attribute,str2epoch(start),str2epoch(stop))
                        print 'drawing table from %d values' % len(values)
                        tab = Qt.QTableWidget()
                        tab.setWindowTitle('%s: %s to %s' % (attribute,start,stop))
                        tab.setRowCount(len(values))
                        tab.setColumnCount(2)
                        tab.setHorizontalHeaderLabels(['TIME','VALUE'])
                        for i,tup in enumerate(values):
                            date,value = tup
                            tab.setItem(i,0,Qt.QTableWidgetItem(epoch2str(date)))
                            tab.setItem(i,1,Qt.QTableWidgetItem(str(value)))
                        tab.show()
                        tab.resizeColumnsToContents()
                        tab.horizontalHeader().setStretchLastSection(True)
                        TABS.append(tab)
                        tab.connect(tab,Qt.SIGNAL('close()'),lambda o=tab: TABS.remove(o))
                        print 'show_history done ...'
                        return tab
                else:
                    print 'dialog closed'
                    return None
            print 'asking for dates ...'
            return check_values()
        else:
            Qt.QMessageBox.warning(None,'Show archiving', 'Attribute %s is not being archived'%attribute, Qt.QMessageBox.Ok) 
示例#12
0
  def show_new_dialog(klass,attribute,schema='*',parent=None,dates=[]):
      try:
          if not Qt.QApplication.instance(): Qt.QApplication([])
      except:
          pass
      print 'in Vacca.widgets.show_history(%s)'
      
      print 'getting archiving readers ...'
      from PyTangoArchiving import Reader
      rd = Reader(schema.lower())
      hdb = Reader('hdb')
      tdb = Reader('tdb')
      attribute = str(attribute).lower()
      
      try:
        dates = dates or klass.get_default_dates()
        if not all(map(fandango.isString,dates)):
          dates = map(epoch2str,dates)
      except:
          traceback.print_exc()
          dates = epoch2str(),epoch2str()

      schemas = rd.is_attribute_archived(attribute,preferent=False)
      if schemas:
          print '%s is being archived' % attribute
          di = Qt.QDialog(parent)
          wi = di #QtGui.QWidget(di)
          wi.setLayout(Qt.QGridLayout())
          begin = Qt.QLineEdit()
          begin.setText(dates[0])
          end = Qt.QLineEdit()
          end.setText(dates[1])
          tfilter = Qt.QLineEdit()
          vfilter = Qt.QCheckBox()
          wi.setWindowTitle('Show %s Archiving'%attribute)
          wil = wi.layout()
          wi.layout().addWidget(Qt.QLabel(attribute),0,0,1,2)
          wi.layout().addWidget(Qt.QLabel('Preferred Schema'),1,0,1,1)
          qschema = Qt.QComboBox()
          qschema.insertItems(0,['*']+list(schemas))
          wil.addWidget(qschema,1,1,1,1)
          #qb = Qt.QPushButton("Save as preferred schema")
          #wil.addWidget(qb,wil.rowCount(),0,1,2)
          #qi.connect(qb,Qt.SIGNAL('pushed()'),save_schema)
          wil.addWidget(Qt.QLabel('Enter Begin and End dates in %s format'%tformat),2,0,1,2)
          wil.addWidget(Qt.QLabel('Begin:'),3,0,1,1)
          wil.addWidget(begin,3,1,1,1)
          wil.addWidget(Qt.QLabel('End:'),4,0,1,1)
          wil.addWidget(end,4,1,1,1)
          wil.addWidget(Qt.QLabel('Time Filter:'),5,0,1,1)
          wil.addWidget(tfilter,5,1,1,1)
          wil.addWidget(Qt.QLabel('Value Filter:'),6,0,1,1)
          wil.addWidget(vfilter,6,1,1,1)
          buttons = Qt.QDialogButtonBox(wi)
          buttons.addButton(Qt.QPushButton("Export"),Qt.QDialogButtonBox.AcceptRole)

          bt = Qt.QPushButton("Apply")
          buttons.addButton(bt,Qt.QDialogButtonBox.ApplyRole)
          def set_schema(r=rd,a=attribute,qs=qschema):
              print 'setting schema ...'
              schema = str(qs.currentText()).strip()
              r.set_preferred_schema(a,schema)
              
          buttons.connect(bt,Qt.SIGNAL('clicked()'),set_schema)

          buttons.addButton(Qt.QPushButton("Close"),Qt.QDialogButtonBox.RejectRole)

          #  Qt.QDialogButtonBox.Apply\
          #    |Qt.QDialogButtonBox.Open|Qt.QDialogButtonBox.Close)
          wi.connect(buttons,Qt.SIGNAL('accepted()'),wi.accept)
          wi.connect(buttons,Qt.SIGNAL('rejected()'),wi.reject)
          wi.layout().addWidget(buttons,7,0,1,2)
          
          def check_values():
              di.exec_()
              print 'checking schema ...'
              schema = str(qschema.currentText()).strip()
              if schema != '*':
                    rd.set_preferred_schema(attribute,schema)              
              if di.result():
                  print 'checking result ...'
                  try:
                      start,stop = str(begin.text()),str(end.text())
                      try: tf = int(str(tfilter.text()))
                      except: tf = 0
                      vf = vfilter.isChecked()
                      if not all(re.match('[0-9]+-[0-9]+-[0-9]+ [0-9]+:[0-9]+:[0-9]+',str(s).strip()) for s in (start,stop)):
                          print 'dates are wrong ...'
                          Qt.QMessageBox.warning(None,'Show archiving', 'Dates seem not in %s format'%(tformat), Qt.QMessageBox.Ok)
                          return check_values()
                      else:
                          print 'getting values ...'
                          print 'using %s reader' % rd.schema
                          values = rd.get_attribute_values(attribute,str2epoch(start),str2epoch(stop))
                          if not len(values) and schema=='*' and hdb.is_attribute_archived(attribute,active=True):
                              print 'tdb failed, retrying with hdb'
                              values = hdb.get_attribute_values(attribute,str2epoch(start),str2epoch(stop))
                          if vf:
                              values = fandango.arrays.decimate_array(values)
                          if tf:
                              print 'Filtering %d values (1/%dT)'%(len(values),tf)
                              values = fandango.arrays.filter_array(values,window=tf) #,begin=start,end=stop)

                          twi = klass.setup_table(attribute,start,stop,values)
                          klass.set_default_dates(str2epoch(start),str2epoch(stop))
                          
                          button = Qt.QPushButton('Save to file')
                          button2 = Qt.QPushButton('Send to email')
                          
                          def save_to_file(var=attribute,data=values,parent=twi,edit=True):
                              try:
                                  options = {'sep':'\\t','arrsep': '\\ ','linesep':'\\n'}
                                  import codecs
                                  dd = QOptionDialog(model=options,title='CSV Options')
                                  dd.exec_()
                                  for k,v in options.items():
                                    options[k] = codecs.escape_decode(str(v))[0]
                                  print options
                                
                                  filename = Qt.QFileDialog.getSaveFileName(parent,
                                      'File to save',
                                      '/data/'+PyTangoArchiving.files.get_data_filename(var,data,'csv','human'),
                                      'CSV files (*.csv)')
                                  
                                  PyTangoArchiving.files.save_data_file(var,data,filename,format='csv',**options)
                                  if edit:
                                    try: os.system('gedit %s &'%filename)
                                    except: pass
                                  return filename
                              except Exception,e:
                                  Qt.QMessageBox.warning(None, "Warning" , "Unable to save %s\n:%s"%(filename,e))
                                  
                          def send_by_email(var=attribute,data=values,parent=twi):
                              #subject,text,receivers,sender='',attachments=None,trace=False):
                              try:
                                receivers,ok = Qt.QInputDialog.getText(None,'Send by email','to:')
                                if ok:
                                  filename = str(save_to_file(var,data,parent,edit=False))
                                  fandango.linos.sendmail(filename,attribute,receivers=str(receivers),attachments=[filename])
                              except Exception,e:
                                Qt.QMessageBox.warning(None, "Warning" , "Unable to send %s\n:%s"%(filename,e))
                              
                          #button.setTextAlignment(Qt.Qt.AlignCenter)
                          twi.connect(button, Qt.SIGNAL("pressed ()"), save_to_file)
                          twi.layout().addWidget(button)
                          twi.connect(button2, Qt.SIGNAL("pressed ()"), send_by_email)
                          twi.layout().addWidget(button2)                        
                          twi.show()

                          TABS.append(twi)
                          twi.connect(twi,Qt.SIGNAL('close()'),lambda o=twi: TABS.remove(o))
                          print 'show_history done ...'
                          return twi
                  except Exception,e:
                      print traceback.format_exc()
                      Qt.QMessageBox.warning(None, "Warning" , "Unable to retrieve the values (%s), sorry"%e)