FileToolImpl.py

Go to the documentation of this file.
00001 #!/usr/bin/env python
00002 #
00003 #                               Copyright 2005
00004 #                                     by
00005 #                        The Board of Trustees of the
00006 #                     Leland Stanford Junior University.
00007 #                            All rights reserved.
00008 #
00009 
00010 __facility__ = "Online"
00011 __author__   = "S. Tuvi <stuvi@SLAC.Stanford.edu> SLAC - GLAST LAT I&T/Online"
00012 __abstract__ = "ScriptEngine Global Panel Implementation"
00013 __date__     = "2002/10/11 00:00:00"
00014 __updated__  = "$Date: 2006/03/23 02:12:23 $"
00015 __version__  = "$Revision: 1.1 $"
00016 __release__  = "$Name: HEAD $"
00017 __credits__  = "SLAC"
00018 
00019 import LICOS.copyright_SLAC
00020 
00021 import sys, os
00022 from qt import *
00023 import logging as log
00024 
00025 from LICOS.scriptEngine.FileTool import FileTool
00026 
00027 from LICOS.lib.LATconstants import *
00028 from LICOS.lib.cmdTlmDb.FILE import FswFile, FswFileFromDisk, FswFileFromData, FswFileID
00029 
00030 class FileToolImpl(FileTool):
00031   """!\brief Real time file upload and download GUI.
00032 
00033      This tool provides the capability of uploading and downloading
00034      files to and from the LAT instrument through the VSC interface.
00035      
00036      It uses the high level FILE methods to construct a series of
00037      telecommands to start the upload/download.
00038 
00039      It is a current feature that this tool will not upload a
00040      file to either of the EEPROMS.
00041   """
00042   def __init__(self, common,
00043                parent = None, name = None, modal = False, fl = 0):
00044     """!\brief FileToolImpl Constructor
00045 
00046     \param common A ScriptEngineCommon instance
00047     
00048     \param parent The parent GUI (typically the Run Control GUI)
00049     \param name   Name of the GUI
00050     \param modal  Whether the GUI is Modal (blocking)
00051     \param fl     Qt flags
00052      """
00053     FileTool.__init__(self, parent, name, modal, fl)
00054 
00055     # Three buttons, three connections...
00056     self.connect(self.ApplyButton, SIGNAL("clicked()"), self.apply)
00057     self.connect(self.ClearButton, SIGNAL("clicked()"), self.clear)
00058     self.connect(self.ExecuteButton, SIGNAL("clicked()"), self.execute)
00059 
00060     # cache the ids of some of the radio buttons.  We'll use them later
00061     self.__downloadBtnID = self.TransferSelector.id(self.DownloadRadioBtn)
00062     self.__uploadBtnID   = self.TransferSelector.id(self.UploadRadioBtn)
00063 
00064     self.TransferSelector.setButton(self.__downloadBtnID)
00065 
00066     if common is None:
00067       print "no Common, setting to None"
00068     else:
00069       pass
00070 
00071     self.__common = common
00072 
00073   def lcat(self):
00074     """ Return the LCATcmdDb instance of self.__common
00075     """
00076     if self.__common is not None:
00077       return self.__common.getLCATcmd()
00078 
00079   def apply(self):
00080     """ Apply the settings to the text edits
00081     """
00082     unit = self.UnitSelector.currentText()
00083     
00084     onboardTextRep = "%s:%s" % (unit, str(self.__fid()))
00085 
00086     # upload or download?
00087     if self.TransferSelector.selectedId() == self.__uploadBtnID:
00088       self.DestinationEdit.setText( onboardTextRep )
00089     else:
00090       self.OriginEdit.setText( onboardTextRep )
00091 
00092   def clear(self):
00093     """ Clear the form and reset to default values
00094     """
00095     self.UnitSelector.setCurrentItem(0)     # SIU
00096     self.DeviceSelector.setCurrentItem(1)   # RAM
00097     self.FileNoSelector.setValue(0)         # file 0
00098     self.DirSelector.setValue(0)            # directory 0
00099     self.OriginEdit.clear()
00100     self.DestinationEdit.clear()
00101 
00102   def execute(self):
00103     """ Execute upload or download command
00104         Also perform consistency checks
00105     """
00106     
00107     if self.TransferSelector.selectedId() == self.__uploadBtnID:
00108       if self.uploadConsistent():
00109         self.upload()
00110     else:
00111       if self.downloadConsistent():
00112         self.download()
00113 
00114 
00115   def upload(self):
00116     """ Upload the file pointed to by the current GUI state.
00117 
00118     """
00119     fName = str(self.OriginEdit.text())
00120 
00121     fswFile = FswFileFromDisk( self.__fid(), fName )
00122 
00123     if self.lcat() is not None:
00124       # Gotta enable command confirmation
00125       cmdConf = self.lcat().isCmdConfirmTelemEnabled()
00126       if not cmdConf:
00127         self.lcat().enableCmdConfirmTelem()
00128         
00129       status = self.lcat().FILE.uploadFile( fswFile, FSW_UNIT_NAMES.index(self.UnitSelector.currentText()) )
00130 
00131       if not cmdConf: # put cmdConf back
00132         self.lcat().disableCmdConfirmTelem()
00133 
00134     else:
00135       self.errorBox("LCAT object is None.  Not uploading")
00136       return
00137 
00138     if not status:
00139       self.errorBox("Upload failed")
00140 
00141 
00142   def download(self):
00143     """ Download the file pointed to by the current GUI state to disk
00144 
00145         Several consistency checks are done:
00146         a) If there is nothing in the destination box, mit an errorbox and return
00147     """
00148     fName = str(self.DestinationEdit.text())
00149     
00150     print fName, os.path.exists(fName)
00151 
00152     if self.lcat() is not None:
00153       # Gotta enable command confirmation
00154       cmdConf = self.lcat().isCmdConfirmTelemEnabled()
00155       if not cmdConf:
00156         self.lcat().enableCmdConfirmTelem()
00157         
00158       fswFile = self.lcat().FILE.downloadFile( FSW_UNIT_NAMES.index(self.UnitSelector.currentText()),
00159                                                self.__fid() )
00160       if not cmdConf:
00161         self.lcat().disableCmdConfirmTelem()
00162     else:
00163       self.errorBox("LCAT object is None.  Not uploading")
00164       return
00165 
00166     outFile = file(fName, 'w+')
00167     outFile.write( fswFile.binary() )
00168     outFile.close()
00169 
00170   def uploadConsistent(self):
00171     """
00172         Several consistency checks are done for uploads:
00173         a) If destination != RAM, emit an errorbox and return
00174         b) If there is nothing in the origin box, emit an errorbox and return
00175         c) If the origin file doesn't exist, emit an errorbox and return.
00176 
00177     """
00178     # Refuse to upload when device != RAM
00179     if self.DeviceSelector.currentText() != 'RAM':
00180       self.errorBox("Uploads through this interface are only allowed to go to RAM")
00181       #self.DeviceSelector.setCurrentItem(1) # Reset deviceSelector to RAM
00182       return False
00183 
00184     # no file name?
00185     if len(self.OriginEdit.text()) == 0:
00186       self.errorBox("You must define a valid origin file to upload.")
00187       return False
00188 
00189     fName = str(self.OriginEdit.text())
00190     if not os.path.exists(fName):
00191       self.errorBox("File %s does not exist. Is this a full pathname?" % fName)
00192       return False
00193 
00194     return True
00195 
00196     
00197   def downloadConsistent(self):
00198     """
00199         Several consistency checks are done:
00200         a) If there is nothing in the destination box, emit an errorbox and return
00201     """
00202     fName = str(self.DestinationEdit.text())
00203     # no file name?
00204     if len(fName) == 0:
00205       self.errorBox("You must define a valid target filename to which to download.")
00206       return False
00207 
00208     if os.path.exists(fName):
00209       m = QMessageBox( 'File exists, overwrite?',
00210                        'File %s exists.  Do you wish to overwrite this file?' % fName,
00211                        QMessageBox.Question,
00212                        QMessageBox.Yes,
00213                        QMessageBox.No | QMessageBox.Default,
00214                        QMessageBox.NoButton,
00215                        self,
00216                        None,
00217                        True )
00218       if m.exec_loop() == QMessageBox.No:
00219         return False
00220 
00221     return True
00222     
00223 
00224 
00225 
00226   def errorBox(self, msg):
00227     """ Dumb error message box.  Pops open a QMessageBox with CRITICAL state.
00228 
00229     \param msg   Any text.
00230     """
00231     
00232     m = QMessageBox( 'Error in FileTool:',
00233                      msg,
00234                      QMessageBox.Critical,
00235                      QMessageBox.Ok,
00236                      QMessageBox.NoButton,
00237                      QMessageBox.NoButton,
00238                      self,
00239                      None,
00240                      False )
00241     m.show()
00242 
00243   def __fid(self):
00244     """Parse the current settings into a file ID and return it
00245 
00246     \return A FswFileID object
00247     """
00248 
00249     return FswFileID( FSW_DEVICE_NAMES.index(str(self.DeviceSelector.currentText()).lower()),
00250                      self.DirSelector.value(),
00251                      self.FileNoSelector.value())
00252 
00253 
00254 
00255 
00256 if __name__ == "__main__":
00257 
00258   
00259 
00260   a = QApplication(sys.argv)
00261   QObject.connect(a,SIGNAL("lastWindowClosed()"),a,SLOT("quit()"))
00262   w = FileToolImpl(None)
00263   a.setMainWidget(w)
00264   w.show()
00265   a.exec_loop()

Generated on Thu Apr 27 20:52:41 2006 for LICOS L02-01-00 by doxygen 1.4.6-NO