def setGlobalLanguageParameters( language = "" ): """ @summary : Sets up all the needed global language tranlator so that it can be used everywhere in this program. @Note : The scope of the global _ function is restrained to this module only and does not cover the entire project. @param language : language to set. @return: None """ global _ global SUPPORTED_RX_DATATYPES global SUPPORTED_TX_DATATYPES if language == "": _ = LanguageTools.getTranslatorForModule( CURRENT_MODULE_ABS_PATH ) else : _ = LanguageTools.getTranslatorForModule( CURRENT_MODULE_ABS_PATH, language ) SUPPORTED_RX_DATATYPES = { "bytecount":_("bytecount") , "filecount": _("filecount"), "errors":_("errors") } SUPPORTED_TX_DATATYPES = { "latency" :_("latency"), "filesOverMaxLatency":_("filesOverMaxLatency"), "bytecount":_("bytecount"), "filecount" :_("filecount"), "errors":_("errors") }
def updateWordsFromDB(wordType, word, language): """ @summary: Updates words within the db depending on the specified type ofdatabases @param wordType : Type of word : "products" or "groupName" @parameter language : Language that is currently used by the caller. @param word : Word to add to the database @return : None """ _ = LanguageTools.getTranslatorForModule(CURRENT_MODULE_ABS_PATH, language) statsPaths = StatsPaths() statsPaths.setPaths(language) if wordType == "products": updateWordsFromFile(statsPaths.STATSWEBWORDDATABASES + _('products'), word) elif wordType == "groupName": updateWordsFromFile(statsPaths.STATSWEBWORDDATABASES + _('groupNames'), word)
def setLanguage( language ): """ @summary : sets specified language as the language used for translations throughout the entire class. """ if language in LanguageTools.getSupportedLanguages() : global _ _ = LanguageTools.getTranslatorForModule( CURRENT_MODULE_ABS_PATH, language )
def giveOutPermissionsToFolders(currentlyUsedLanguages): """ @summary : opens up permissions to folders that might be required by the web user. @param currentlyUsedLanguages: Languages currently set to be displayed in the web interface """ for language in currentlyUsedLanguages: _ = LanguageTools.getTranslatorForModule(CURRENT_MODULE_ABS_PATH, language) paths = StatsPaths() paths.setPaths(language) pathsToOpenUp = [] pathsToOpenUp.append(paths.STATSLOGGING) pathsToOpenUp.append(paths.STATSPICKLES) pathsToOpenUp.append(paths.STATSDB) pathsToOpenUp.append(paths.STATSCURRENTDB) pathsToOpenUp.append(paths.STATSCURRENTDB + _("bytecount")) pathsToOpenUp.append(paths.STATSCURRENTDB + _("errors")) pathsToOpenUp.append(paths.STATSCURRENTDB + _("filecount")) pathsToOpenUp.append(paths.STATSCURRENTDB + _("filesOverMaxLatency")) pathsToOpenUp.append(paths.STATSCURRENTDB + _("latency")) pathsToOpenUp.append(paths.STATSCURRENTDBUPDATES) pathsToOpenUp.append(paths.STATSCURRENTDBUPDATES + _("rx")) pathsToOpenUp.append(paths.STATSCURRENTDBUPDATES + _("tx")) pathsToOpenUp.append(paths.STATSCURRENTDBUPDATES + _("totals")) pathsToOpenUp.append(paths.STATSDBBACKUPS) pathsToOpenUp.append(paths.STATSDBBACKUPS + "*/" + _("rx")) pathsToOpenUp.append(paths.STATSDBBACKUPS + "*/" + _("tx")) pathsToOpenUp.append(paths.STATSDBBACKUPS + "*/" + _("totals")) pathsToOpenUp.append(paths.STATSGRAPHS) pathsToOpenUp.append(paths.STATSGRAPHS + _("others/")) pathsToOpenUp.append(paths.STATSGRAPHS + _("others/") + "gnuplot/") pathsToOpenUp.append(paths.STATSGRAPHS + _("others/") + "rrd/") pathsToOpenUp.append(paths.STATSWEBPAGESHTML + "/popUps/") for path in pathsToOpenUp: if not os.path.isdir(path): os.makedirs(path, 0777) commands.getstatusoutput("chmod 0777 %s" % path) commands.getstatusoutput("chmod 0777 %s/*" % path)
def setLanguage(language): """ @summary : sets specified language as the language used for translations throughout the entire class. """ if language in LanguageTools.getSupportedLanguages(): global _ _ = LanguageTools.getTranslatorForModule(CURRENT_MODULE_ABS_PATH, language)
def giveOutPermissionsToFolders( currentlyUsedLanguages ): """ @summary : opens up permissions to folders that might be required by the web user. @param currentlyUsedLanguages: Languages currently set to be displayed in the web interface """ for language in currentlyUsedLanguages: _ = LanguageTools.getTranslatorForModule(CURRENT_MODULE_ABS_PATH, language) paths = StatsPaths() paths.setPaths(language) pathsToOpenUp = [] pathsToOpenUp.append( paths.STATSLOGGING) pathsToOpenUp.append( paths.STATSPICKLES ) pathsToOpenUp.append( paths.STATSDB) pathsToOpenUp.append( paths.STATSCURRENTDB ) pathsToOpenUp.append( paths.STATSCURRENTDB + _("bytecount") ) pathsToOpenUp.append( paths.STATSCURRENTDB + _("errors") ) pathsToOpenUp.append( paths.STATSCURRENTDB + _("filecount") ) pathsToOpenUp.append( paths.STATSCURRENTDB + _("filesOverMaxLatency")) pathsToOpenUp.append( paths.STATSCURRENTDB + _("latency")) pathsToOpenUp.append( paths.STATSCURRENTDBUPDATES) pathsToOpenUp.append( paths.STATSCURRENTDBUPDATES + _("rx") ) pathsToOpenUp.append( paths.STATSCURRENTDBUPDATES + _("tx") ) pathsToOpenUp.append( paths.STATSCURRENTDBUPDATES + _("totals") ) pathsToOpenUp.append( paths.STATSDBBACKUPS ) pathsToOpenUp.append( paths.STATSDBBACKUPS + "*/" + _("rx") ) pathsToOpenUp.append( paths.STATSDBBACKUPS + "*/" + _("tx") ) pathsToOpenUp.append( paths.STATSDBBACKUPS + "*/" + _("totals") ) pathsToOpenUp.append( paths.STATSGRAPHS ) pathsToOpenUp.append( paths.STATSGRAPHS +_("others/")) pathsToOpenUp.append( paths.STATSGRAPHS +_("others/") + "gnuplot/") pathsToOpenUp.append( paths.STATSGRAPHS +_("others/") + "rrd/") pathsToOpenUp.append( paths.STATSWEBPAGESHTML + "/popUps/") for path in pathsToOpenUp: if not os.path.isdir(path): os.makedirs(path, 0777) commands.getstatusoutput( "chmod 0777 %s" %path ) commands.getstatusoutput( "chmod 0777 %s/*" %path )
def setPaths(self, language=None): global _ from pxStats.lib.LanguageTools import LanguageTools _ = LanguageTools.getTranslatorForModule(CURRENT_MODULE_ABS_PATH, language) """ Sets all the paths that can not be translated. """ self.setBasicPaths() """ Translatable paths. """ self.STATSDATA = self.STATSROOT + _('data/') self.STATSDOC = self.STATSROOT + _('doc/') self.STATSLOGGING = self.STATSROOT + _('logs/') self.STATSMAN = self.STATSROOT + _('man/') self.STATSTEMP = self.STATSROOT + _("temp/") #csvfiles self.STATSCSVFILES = self.STATSDATA + _("csvFiles/") #Databases related paths. self.STATSDB = self.STATSDATA + _('databases/') self.STATSCURRENTDB = self.STATSDB + _('currentDatabases/') self.STATSCURRENTDBUPDATES = self.STATSDB + _( 'currentDatabasesTimeOfUpdates/') self.STATSDBBACKUPS = self.STATSDB + _('databasesBackups/') self.STATSDBUPDATESBACKUPS = self.STATSDB + _( 'databasesTimeOfUpdatesBackups/') #Various paths under pxStats/data/ self.STATSFILEVERSIONS = self.STATSDATA + _('fileAcessVersions/') self.STATSLOGACCESS = self.STATSDATA + _('logFileAccess/') self.STATSMONITORING = self.STATSDATA + _('monitoring/') self.STATSPICKLES = self.STATSDATA + _('pickles/') self.STATSLOGS = self.STATSDATA + _('logFiles/') self.STATSWEBPAGES = self.STATSDATA + _('webPages/') self.STATSWEBPAGESHTML = self.STATSWEBPAGES + _('html/') self.STATSWEBPAGESWORDDBS = self.STATSWEBPAGES + _('wordDatabases/') self.STATSGRAPHS = self.STATSDATA + _('graphics/') self.STATSWEBGRAPHS = self.STATSGRAPHS + _('webGraphics/') self.STATSWEBWORDDATABASES = self.STATSWEBPAGES + _('wordDatabases/') self.STATSGRAPHSARCHIVES = self.STATSWEBGRAPHS + _('archives/') self.STATSCOLGRAPHS = self.STATSWEBGRAPHS + _('columbo/') self.STATSPICKLESTIMEOFUPDATES = self.STATSDATA + _( 'picklesTimeOfUpdates/') self.STATSPREVIOUSMACHINEPARAMS = self.STATSDATA + _( 'previousMachineParameters') self.STATSTEMPLOCKFILES = self.STATSTEMP + _("lockFiles/") self.STATSTEMPAUTUPDTLOGS = self.STATSTEMP + _("automaticUpdatesLogs/")
def setGlobalLanguageParameters(language=""): """ @summary : Sets up all the needed global language tranlator so that it can be used everywhere in this program. @Note : The scope of the global _ function is restrained to this module only and does not cover the entire project. @param language : language to set. @return: None """ global _ global SUPPORTED_RX_DATATYPES global SUPPORTED_TX_DATATYPES if language == "": _ = LanguageTools.getTranslatorForModule(CURRENT_MODULE_ABS_PATH) else: _ = LanguageTools.getTranslatorForModule(CURRENT_MODULE_ABS_PATH, language) SUPPORTED_RX_DATATYPES = { "bytecount": _("bytecount"), "filecount": _("filecount"), "errors": _("errors") } SUPPORTED_TX_DATATYPES = { "latency": _("latency"), "filesOverMaxLatency": _("filesOverMaxLatency"), "bytecount": _("bytecount"), "filecount": _("filecount"), "errors": _("errors") }
def getTranslatorForModule(self, moduleName, language = None ): """ @summary : Sets up all the needed global language tranlator so that it can be used everywhere in this program. @Note : The scope of the global _ function is restrained to this class and the one in. @return: None """ return LanguageTools.getTranslatorForModule( moduleName, language )
def setPaths( self, language = None ): global _ from pxStats.lib.LanguageTools import LanguageTools _ = LanguageTools.getTranslatorForModule( CURRENT_MODULE_ABS_PATH, language ) """ Sets all the paths that can not be translated. """ self.setBasicPaths() """ Translatable paths. """ self.STATSDATA = self.STATSROOT + _('data/') self.STATSDOC = self.STATSROOT + _( 'doc/' ) self.STATSLOGGING = self.STATSROOT + _( 'logs/' ) self.STATSMAN = self.STATSROOT + _( 'man/' ) self.STATSTEMP = self.STATSROOT + _( "temp/" ) #csvfiles self.STATSCSVFILES = self.STATSDATA + _("csvFiles/") #Databases related paths. self.STATSDB = self.STATSDATA + _( 'databases/' ) self.STATSCURRENTDB = self.STATSDB + _( 'currentDatabases/' ) self.STATSCURRENTDBUPDATES = self.STATSDB + _( 'currentDatabasesTimeOfUpdates/' ) self.STATSDBBACKUPS = self.STATSDB + _( 'databasesBackups/' ) self.STATSDBUPDATESBACKUPS = self.STATSDB + _( 'databasesTimeOfUpdatesBackups/' ) #Various paths under pxStats/data/ self.STATSFILEVERSIONS = self.STATSDATA + _( 'fileAcessVersions/' ) self.STATSLOGACCESS = self.STATSDATA + _( 'logFileAccess/' ) self.STATSMONITORING = self.STATSDATA + _( 'monitoring/' ) self.STATSPICKLES = self.STATSDATA + _( 'pickles/' ) self.STATSLOGS = self.STATSDATA + _( 'logFiles/' ) self.STATSWEBPAGES = self.STATSDATA + _( 'webPages/' ) self.STATSWEBPAGESHTML = self.STATSWEBPAGES + _( 'html/' ) self.STATSWEBPAGESWORDDBS = self.STATSWEBPAGES + _( 'wordDatabases/' ) self.STATSGRAPHS = self.STATSDATA + _( 'graphics/' ) self.STATSWEBGRAPHS = self.STATSGRAPHS + _( 'webGraphics/' ) self.STATSWEBWORDDATABASES = self.STATSWEBPAGES + _( 'wordDatabases/' ) self.STATSGRAPHSARCHIVES = self.STATSWEBGRAPHS + _( 'archives/' ) self.STATSCOLGRAPHS = self.STATSWEBGRAPHS + _( 'columbo/' ) self.STATSPICKLESTIMEOFUPDATES = self.STATSDATA + _( 'picklesTimeOfUpdates/' ) self.STATSPREVIOUSMACHINEPARAMS = self.STATSDATA + _( 'previousMachineParameters' ) self.STATSTEMPLOCKFILES = self.STATSTEMP + _( "lockFiles/" ) self.STATSTEMPAUTUPDTLOGS = self.STATSTEMP + _( "automaticUpdatesLogs/" )
def getTranslatorForModule(self, moduleName, language=None): """ @summary : Sets up all the needed global language tranlator so that it can be used everywhere in this program. @Note : The scope of the global _ function is restrained to this class and the one in. @return: None """ return LanguageTools.getTranslatorForModule(moduleName, language)
def setGlobalLanguageParameters( language ): """ @summary : Sets up all the needed global language variables so that they can be used everywhere in this program. @param language: language with whom this script was called. @return: None """ global _ _ = LanguageTools.getTranslatorForModule( CURRENT_MODULE_ABS_PATH, language)
def setGlobalLanguageParameters(language): """ @summary : Sets up all the needed global language variables so that they can be used everywhere in this program. @param language: language with whom this script was called. @return: None """ global _ _ = LanguageTools.getTranslatorForModule(CURRENT_MODULE_ABS_PATH, language)
def setGlobalLanguageParameters(): """ @summary : Sets up all the needed global language tranlator so that it can be used everywhere in this program. @Note : The scope of the global _ function is restrained to this module only and does not cover the entire project. @return: None """ global _ _ = LanguageTools.getTranslatorForModule(CURRENT_MODULE_ABS_PATH)
def setGlobalLanguageParameters(): """ @summary : Sets up all the needed global language tranlator so that it can be used everywhere in this program. @Note : The scope of the global _ function is restrained to this module only and does not cover the entire project. @return: None """ global _ _ = LanguageTools.getTranslatorForModule( CURRENT_MODULE_ABS_PATH )
def __getFileNameFromExecutionOutput( self, output ): """ @summary : Parses an execution output coming from the csvDataConversion.py file and searchs for the filename that was generated. """ global _ _ = LanguageTools.getTranslatorForModule(CURRENT_MODULE_ABS_PATH, self.outputLanguage ) fileName = "" lines = str( output ).splitlines() for line in lines: if _( "generated filename : " ) in str(line).lower(): fileName = line.split( _( "generated filename : " ) )[1].replace(' ','') break return fileName
def createSubFolders(path, currentlyUsedLanguages): """ @summary : Creates all the required sub folders. @param path : Paths in which we are installing the web interface. @param currentlyUsedLanguages: Languages currently set to be displayed in the web interface. """ global _ subFolders = ["scripts"] for language in currentlyUsedLanguages: _ = LanguageTools.getTranslatorForModule(CURRENT_MODULE_ABS_PATH, language) subFolders.append(_("wordDatabases")) for subFolder in subFolders: if not os.path.isdir(path + '/' + subFolder): os.makedirs(path + '/' + subFolder)
def createSubFolders( path, currentlyUsedLanguages ): """ @summary : Creates all the required sub folders. @param path : Paths in which we are installing the web interface. @param currentlyUsedLanguages: Languages currently set to be displayed in the web interface. """ global _ subFolders = [ "scripts" ] for language in currentlyUsedLanguages : _ = LanguageTools.getTranslatorForModule( CURRENT_MODULE_ABS_PATH, language ) subFolders.append( _("wordDatabases") ) for subFolder in subFolders : if not os.path.isdir( path + '/'+ subFolder ): os.makedirs( path + '/' + subFolder )
def __getFileNameFromExecutionOutput(self, output): """ @summary : Parses an execution output coming from the csvDataConversion.py file and searchs for the filename that was generated. """ global _ _ = LanguageTools.getTranslatorForModule(CURRENT_MODULE_ABS_PATH, self.outputLanguage) fileName = "" lines = str(output).splitlines() for line in lines: if _("generated filename : ") in str(line).lower(): fileName = line.split(_("generated filename : "))[1].replace( ' ', '') break return fileName
def updateWordsFromDB(wordType, word, language): """ @summary: Updates words within the db depending on the specified type ofdatabases @param wordType : Type of word : "products" or "groupName" @parameter language : Language that is currently used by the caller. @param word : Word to add to the database @return : None """ _ = LanguageTools.getTranslatorForModule(CURRENT_MODULE_ABS_PATH, language) statsPaths = StatsPaths() statsPaths.setPaths(language) if wordType == "products": updateWordsFromFile(statsPaths.STATSWEBWORDDATABASES + _("products"), word) elif wordType == "groupName": updateWordsFromFile(statsPaths.STATSWEBWORDDATABASES + _("groupNames"), word)
class CpickleWrapper: global _ _ = LanguageTools.getTranslatorForModule(CURRENT_MODULE_ABS_PATH) def save(object, filename): """ @summary : Saves an object to disk using cpickle. @note : Will create folder up to destination if folders don't exist. User must have permission to write to the specified folder. @raise Exception: if application is unable to save file. @warning: Objects containing opened files, such as log files, cannot be saved. Remember to close file or delete file property from object prior to saving. """ splitName = filename.split("/") if filename[0] == "/": directory = "/" else: directory = "" for i in range(1, len(splitName) - 1): directory = directory + splitName[i] + "/" if not os.path.isdir(directory): os.makedirs(directory, mode=0777) file = open(filename, 'wb') file.write(cPickle.dumps(object, True)) file.close() save = staticmethod(save) def load(filename): """ @summary : Loads ands returns an object saved with cpickle. @param filename : Fielname containing the object that is to be loaded. @precondition: file must exist. File must have been created using cpickle. @raise Exception: If file does not exist or file cannot be loaded thus breaking one ofthe preconditions. """ object = None if os.path.isfile(filename): try: file = open(filename, 'rb') object = cPickle.load(file) file.close() except Exception, e: raise Exception( _("Error occured in cpickleWrapper.load(). Exception was the following : %s" ) % e) else:
def buildRRDFileName( dataType = 'errors', clients = None , machines = None,\ groupName = "", fileType = "", usage = "regular" ): """ @summary : Returns the name of the round robin database bases on the parameters. @param dataType: byteocunt, errors, filecount, filesOverMaxLatency and latency. @param clients: list of clients/sources names. If only one is wanted, include it in a list. @param machines: list of machines associated with client/sources names. If only one is wanted, include it in a list. @param fileType : Useless for regular and group databases. Obligatory for totalForMachine databases. @param groupName: optional. Use only if the list of client/sources has a KNOWN group name. @param usage: regular, group or totalForMachine. @return: Returns the name of the round robin database bases on the parameters. """ _ = LanguageTools.getTranslatorForModule( CURRENT_MODULE_ABS_PATH, LanguageTools.getMainApplicationLanguage() ) clients = clients or ['client1','client1'] machines = machines or ['machine1','machine2'] fileName = "" combinedMachineName = "" for machine in machines: combinedMachineName = combinedMachineName + machine combinedClientsName = "" for client in clients: combinedClientsName = combinedClientsName + client if len(clients) ==1: if usage == "regular": fileName = STATSPATHS.STATSCURRENTDB + "%s/%s_%s" %( dataType, combinedClientsName, combinedMachineName ) elif usage == "group": fileName = STATSPATHS.STATSCURRENTDB + "%s/%s_%s" %( dataType, groupName, combinedMachineName ) elif usage == "totalForMachine": fileName = STATSPATHS.STATSCURRENTDB + "%s/%s_%s" %( dataType, fileType, combinedMachineName ) else: if usage == "regular": fileName = STATSPATHS.STATSCURRENTDB + _("%s/combined/%s_%s") %( dataType, combinedClientsName, combinedMachineName ) elif usage == "group": #fileName = STATSPATHS.STATSCURRENTDB + _("%s/combined/%s_%s") %( dataType, groupName, combinedMachineName ) try: existingFiles= os.listdir(STATSPATHS.STATSCURRENTDB + "%s/combined/"%( dataType) ) for file in existingFiles: if fnmatch.fnmatch(file, groupName + "*"): fileName = file break except: pass if fileName == "":#no preexisitng file exist for this group fileName = STATSPATHS.STATSCURRENTDB + "%s/%s_%s" %( dataType, groupName, combinedMachineName ) else:#else fileName = STATSPATHS.STATSCURRENTDB + "%s/combined/"%(dataType) + fileName elif usage == "totalForMachine": fileName = STATSPATHS.STATSCURRENTDB + _("%s/combined/%s_%s") %( dataType, fileType, combinedMachineName ) #print "before ", fileName #fileName = fileName.replace("année","year").replace("nbreDeBytes","bytecount").replace("nbreDeFichiers","filecount").replace("erreurs","errors").replace("latence","latency").replace("fichiersAvecLatenceInnacceptable","filesOverMaxLatency").replace("heures","hours").replace("mois","month").replace("jour","day").replace("","") #print "after ", fileName return fileName
def buildRRDFileName( dataType = 'errors', clients = None , machines = None,\ groupName = "", fileType = "", usage = "regular" ): """ @summary : Returns the name of the round robin database bases on the parameters. @param dataType: byteocunt, errors, filecount, filesOverMaxLatency and latency. @param clients: list of clients/sources names. If only one is wanted, include it in a list. @param machines: list of machines associated with client/sources names. If only one is wanted, include it in a list. @param fileType : Useless for regular and group databases. Obligatory for totalForMachine databases. @param groupName: optional. Use only if the list of client/sources has a KNOWN group name. @param usage: regular, group or totalForMachine. @return: Returns the name of the round robin database bases on the parameters. """ _ = LanguageTools.getTranslatorForModule( CURRENT_MODULE_ABS_PATH, LanguageTools.getMainApplicationLanguage()) clients = clients or ['client1', 'client1'] machines = machines or ['machine1', 'machine2'] fileName = "" combinedMachineName = "" for machine in machines: combinedMachineName = combinedMachineName + machine combinedClientsName = "" for client in clients: combinedClientsName = combinedClientsName + client if len(clients) == 1: if usage == "regular": fileName = STATSPATHS.STATSCURRENTDB + "%s/%s_%s" % ( dataType, combinedClientsName, combinedMachineName) elif usage == "group": fileName = STATSPATHS.STATSCURRENTDB + "%s/%s_%s" % ( dataType, groupName, combinedMachineName) elif usage == "totalForMachine": fileName = STATSPATHS.STATSCURRENTDB + "%s/%s_%s" % ( dataType, fileType, combinedMachineName) else: if usage == "regular": fileName = STATSPATHS.STATSCURRENTDB + _( "%s/combined/%s_%s") % (dataType, combinedClientsName, combinedMachineName) elif usage == "group": #fileName = STATSPATHS.STATSCURRENTDB + _("%s/combined/%s_%s") %( dataType, groupName, combinedMachineName ) try: existingFiles = os.listdir(STATSPATHS.STATSCURRENTDB + "%s/combined/" % (dataType)) for file in existingFiles: if fnmatch.fnmatch(file, groupName + "*"): fileName = file break except: pass if fileName == "": #no preexisitng file exist for this group fileName = STATSPATHS.STATSCURRENTDB + "%s/%s_%s" % ( dataType, groupName, combinedMachineName) else: #else fileName = STATSPATHS.STATSCURRENTDB + "%s/combined/" % ( dataType) + fileName elif usage == "totalForMachine": fileName = STATSPATHS.STATSCURRENTDB + _( "%s/combined/%s_%s") % (dataType, fileType, combinedMachineName) #print "before ", fileName #fileName = fileName.replace("année","year").replace("nbreDeBytes","bytecount").replace("nbreDeFichiers","filecount").replace("erreurs","errors").replace("latence","latency").replace("fichiersAvecLatenceInnacceptable","filesOverMaxLatency").replace("heures","hours").replace("mois","month").replace("jour","day").replace("","") #print "after ", fileName return fileName
class PickleMerging: global _ _ = LanguageTools.getTranslatorForModule(CURRENT_MODULE_ABS_PATH) def entryListIsValid(entryList): """ @summary : Returns whether or not an entry list of pickles contains a list of pickles that can be merged. @return : True or False """ isValid = True if entryList != []: i = 0 startTime = entryList[0].startTime totalWidth = entryList[0].totalWidth interval = entryList[0].interval statsTypes = entryList[0].statsTypes while i < len(entryList) and isValid == True: if startTime != entryList[ i].startTime or totalWidth != entryList[ i].totalWidth or interval != entryList[i].interval: isValid = False else: for type in statsTypes: if type not in entryList[i].statsTypes: isValid = False i = i + 1 else: isValid = False return isValid entryListIsValid = staticmethod(entryListIsValid) def fillWithEmptyEntries(nbEmptyEntries, entries): """ @summary : Append certain number of empty entries to the entry list. """ for i in xrange(nbEmptyEntries): entries[i] = _FileStatsEntry() return entries fillWithEmptyEntries = staticmethod(fillWithEmptyEntries) def mergePicklesFromDifferentHours( logger = None , startTime = "2006-07-31 13:00:00",\ endTime = "2006-07-31 19:00:00", client = "satnet",\ machine = "pdsPM", fileType = "tx" ): """ @summary : This method merges entire hourly pickles files together. @None : This does not support merging part of the data of pickles. """ if logger != None: logger.debug(_("Call to mergeHourlyPickles received.")) logging = True else: logging = False pickles = [] entries = {} width = StatsDateLib.getSecondsSinceEpoch( endTime) - StatsDateLib.getSecondsSinceEpoch(startTime) startTime = StatsDateLib.getIsoWithRoundedHours(startTime) seperators = [startTime] seperators.extend( StatsDateLib.getSeparatorsWithStartTime(startTime=startTime, width=width, interval=60 * StatsDateLib.MINUTE)[:-1]) for seperator in seperators: pickles.append( StatsPickler.buildThisHoursFileName(client=client, offset=0, currentTime=seperator, machine=machine, fileType=fileType)) startingNumberOfEntries = 0 #print "prior to loading and merging pickles : %s " %( StatsDateLib.getIsoFromEpoch( time.time() ) ) for pickle in pickles: if os.path.isfile(pickle): tempCollection = CpickleWrapper.load(pickle) if tempCollection != None: for i in xrange(len(tempCollection.fileEntries)): entries[startingNumberOfEntries + i] = tempCollection.fileEntries[i] startingNumberOfEntries = startingNumberOfEntries + len( tempCollection.fileEntries) else: sys.exit() else: emptyEntries = PickleMerging.fillWithEmptyEntries( nbEmptyEntries=60, entries={}) for i in xrange(60): entries[i + startingNumberOfEntries] = emptyEntries[i] startingNumberOfEntries = startingNumberOfEntries + 60 #print "after the loading and merging og pickles : %s " %( StatsDateLib.getIsoFromEpoch( time.time() ) ) statsCollection = FileStatsCollector(startTime=startTime, endTime=endTime, interval=StatsDateLib.MINUTE, totalWidth=width, fileEntries=entries, fileType=fileType, logger=logger, logging=logging) return statsCollection mergePicklesFromDifferentHours = staticmethod( mergePicklesFromDifferentHours) def mergePicklesFromSameHour( logger = None , pickleNames = None, mergedPickleName = "",\ clientName = "" , combinedMachineName = "", currentTime = "",\ fileType = "tx" ): """ @summary: This methods receives a list of filenames referring to pickled FileStatsEntries. After the merger pickles get saved since they might be reused somewhere else. @precondition: Pickle should be of the same timespan and bucket width. If not no merging will occur. """ if logger != None: logger.debug(_("Call to mergePickles received.")) logging = True else: logging = False entryList = [] for pickle in pickleNames: #for every pickle we eneed to merge if os.path.isfile(pickle): entryList.append(CpickleWrapper.load(pickle)) else: #Use empty entry if there is no existing pickle of that name endTime = StatsDateLib.getIsoFromEpoch( StatsDateLib.getSecondsSinceEpoch(currentTime) + StatsDateLib.HOUR) entryList.append( FileStatsCollector(startTime=currentTime, endTime=endTime, logger=logger, logging=logging)) if logger != None: logger.warning( _("Pickle named %s did not exist. Empty entry was used instead." ) % pickle) #start off with a carbon copy of first pickle in list. newFSC = FileStatsCollector( files = entryList[0].files , statsTypes = entryList[0].statsTypes, startTime = entryList[0].startTime,\ endTime = entryList[0].endTime, interval=entryList[0].interval, totalWidth = entryList[0].totalWidth,\ firstFilledEntry = entryList[0].firstFilledEntry, lastFilledEntry = entryList[0].lastFilledEntry,\ maxLatency = entryList[0].maxLatency, fileEntries = entryList[0].fileEntries,logger = logger,\ logging = logging ) if PickleMerging.entryListIsValid(entryList) == True: for i in range(1, len(entryList)): #add other entries for file in entryList[i].files: if file not in newFSC.files: newFSC.files.append(file) for j in range(len(newFSC.fileEntries)): # add all entries newFSC.fileEntries[j].values.productTypes.extend( entryList[i].fileEntries[j].values.productTypes) newFSC.fileEntries[j].files.extend( entryList[i].fileEntries[j].files) newFSC.fileEntries[j].times.extend( entryList[i].fileEntries[j].times) newFSC.fileEntries[j].nbFiles = newFSC.fileEntries[ j].nbFiles + (newFSC.fileEntries[j].nbFiles) for type in newFSC.statsTypes: newFSC.fileEntries[j].values.dictionary[type].extend( entryList[i].fileEntries[j].values.dictionary[type] ) newFSC.fileEntries[j].values.rows = newFSC.fileEntries[ j].values.rows + entryList[i].fileEntries[j].values.rows newFSC = newFSC.setMinMaxMeanMedians( startingBucket=0, finishingBucket=newFSC.nbEntries - 1) else: #Did not merge pickles named. Pickle list was not valid." if logger != None: logger.warning( _("Did not merge pickles named : %s. Pickle list was not valid." ) % pickleNames) logger.warning( _("Filled with empty entries instead.") % pickleNames) newFSC.fileEntries = PickleMerging.fillWithEmptyEntries( nbEmptyEntries=60, entries={}) #prevents us from having ro remerge file later on. temp = newFSC.logger del newFSC.logger CpickleWrapper.save(newFSC, mergedPickleName) try: os.chmod(mergedPickleName, 0777) except: pass #print "saved :%s" %mergedPickleName newFSC.logger = temp return newFSC mergePicklesFromSameHour = staticmethod(mergePicklesFromSameHour) def createNonMergedPicklesList(currentTime, machines, fileType, clients): """ @summary : Create a list of all pickles names concerning different machines for a certain hour. """ pickleList = [] #print machines #print clients for machine in machines: for client in clients: pickleList.append( StatsPickler.buildThisHoursFileName( client=client, currentTime=currentTime, fileType=fileType, machine=machine)) return pickleList createNonMergedPicklesList = staticmethod(createNonMergedPicklesList) def createMergedPicklesList(startTime, endTime, clients, groupName, fileType, machines, seperators): """ @param machines: Machines must be an array containing the list of machines to use. If only one machine is to be used still use an array containing a single item. """ pickleList = [] combinedMachineName = "" combinedMachineName = combinedMachineName.join( [machine for machine in machines]) if groupName == "" or groupName is None: groupName = groupName.join([client for client in clients]) for seperator in seperators: pickleList.append( StatsPickler.buildThisHoursFileName( client=groupName, currentTime=seperator, fileType=fileType, machine=combinedMachineName)) return pickleList createMergedPicklesList = staticmethod(createMergedPicklesList) def mergePicklesFromDifferentSources( logger = None , startTime = "2006-07-31 13:00:00",\ endTime = "2006-07-31 19:00:00", clients = ["someclient"],\ fileType = "tx", machines = [], groupName = "" ): """ @summary : This method allows user to merge pickles coming from numerous machines covering as many hours as wanted, into a single FileStatsCollector entry. Very usefull when creating graphics on a central server with pickle files coming from remote locations. """ combinedMachineName = "" combinedClientName = "" combinedMachineName = combinedMachineName.join( [machine for machine in machines]) combinedClientName = combinedClientName.join( [client for client in clients]) if groupName != "": clientsForVersionManagement = groupName else: clientsForVersionManagement = clients vc = PickleVersionChecker() vc.getClientsCurrentFileList(clients) vc.getSavedList(user=combinedMachineName, clients=clientsForVersionManagement) width = StatsDateLib.getSecondsSinceEpoch( endTime) - StatsDateLib.getSecondsSinceEpoch(startTime) startTime = StatsDateLib.getIsoWithRoundedHours(startTime) seperators = [startTime] seperators.extend( StatsDateLib.getSeparatorsWithStartTime(startTime=startTime, width=width, interval=60 * StatsDateLib.MINUTE)[:-1]) mergedPickleNames = PickleMerging.createMergedPicklesList( startTime = startTime, endTime = endTime, machines = machines,\ fileType = fileType, clients = clients, groupName = groupName,\ seperators = seperators ) #Resulting list of the merger. for i in xrange(len(mergedPickleNames)): #for every merger needed needToMergeSameHoursPickle = False pickleNames = PickleMerging.createNonMergedPicklesList( currentTime=seperators[i], machines=machines, fileType=fileType, clients=clients) if not os.path.isfile(mergedPickleNames[i]): needToMergeSameHoursPickle = True else: for pickle in pickleNames: #Verify every pickle implicated in merger. # if for some reason pickle has changed since last time if vc.isDifferentFile( file=pickle, user=combinedMachineName, clients=clientsForVersionManagement) == True: needToMergeSameHoursPickle = True break if needToMergeSameHoursPickle == True: #First time or one element has changed PickleMerging.mergePicklesFromSameHour( logger = logger , pickleNames = pickleNames , clientName = combinedClientName,\ combinedMachineName = combinedMachineName, currentTime = seperators[i],\ mergedPickleName = mergedPickleNames[i], fileType = fileType ) for pickle in pickleNames: vc.updateFileInList(file=pickle) vc.saveList(user=combinedMachineName, clients=clientsForVersionManagement) # Once all machines have merges the necessary pickles we merge all pickles # into a single file stats entry. if groupName != "": nameToUseForMerger = groupName else: nameToUseForMerger = "" nameToUseForMerger = nameToUseForMerger.join( [client for client in clients]) newFSC = PickleMerging.mergePicklesFromDifferentHours( logger = logger , startTime = startTime, endTime = endTime, client = nameToUseForMerger,\ machine = combinedMachineName,fileType = fileType ) return newFSC mergePicklesFromDifferentSources = staticmethod( mergePicklesFromDifferentSources)
STATSPATHS = StatsPaths() STATSPATHS.setPaths() sys.path.append(STATSPATHS.PXLIB) """ Imports which require pxlib """ import PXManager import PXPaths from PXManager import * #Constants LOCAL_MACHINE = os.uname()[1] CURRENT_MODULE_ABS_PATH = os.path.abspath(__file__).replace(".pyc", ".py") global _ _ = LanguageTools.getTranslatorForModule(CURRENT_MODULE_ABS_PATH) class GeneralStatsLibraryMethods: def createLockFile(processName): """ @summary : Creates a lock file associated with the specified processName. @param processName : Name of the process for which to create the lock file. @return : None """ fileName = STATSPATHS.STATSTEMPLOCKFILES + str(processName) + _(
""" Imports which require pxlib """ import PXManager import PXPaths from PXManager import * #Constants LOCAL_MACHINE = os.uname()[1] CURRENT_MODULE_ABS_PATH = os.path.abspath(__file__).replace( ".pyc", ".py" ) global _ _ = LanguageTools.getTranslatorForModule( CURRENT_MODULE_ABS_PATH ) class GeneralStatsLibraryMethods: def createLockFile( processName ): """ @summary : Creates a lock file associated with the specified processName. @param processName : Name of the process for which to create the lock file. @return : None """
class StatsDateLib: global _ _ = LanguageTools.getTranslatorForModule(CURRENT_MODULE_ABS_PATH) #Constants can be removed once we add methods to the datelibrary and include it MINUTE = 60 HOUR = 60 * MINUTE DAY = 24 * HOUR MINUTES_PER_DAY = 24 * 60 LIST_OF_MONTHS_3LETTER_FORMAT = [ _("Jan"), _("Feb"), _("Mar"), _("Apr"), _("May"), _("Jun"), _("Jul"), _("Aug"), _("Sep"), _("Oct"), _("Nov"), _("Dec") ] LIST_OF_MONTHS = [ _("January"), _("February"), _("March"), _("April"), _("May"), _("June"), _("July"), _("August"), _("September"), _("October"), _("November"), _("December") ] def setLanguage(language): """ @summary : sets specified language as the language used for translations throughout the entire class. """ if language in LanguageTools.getSupportedLanguages(): global _ _ = LanguageTools.getTranslatorForModule(CURRENT_MODULE_ABS_PATH, language) setLanguage = staticmethod(setLanguage) def addMonthsToIsoDate(isodate, monthstoAdd): """ @summary : Add a certain number of months to a date. @param isodate: Date in iso format to which to add months. @param monthstoAdd: Number of months to add.( 0 or bigger) @return : The resulting date. Will return the date received as a parameter if error occurs """ monthsWith30Days = [4, 6, 9, 11] validDate = True resultingDate = isodate try: StatsDateLib.getSecondsSinceEpoch(isodate) except: validDate = False if validDate == True: dayFromDate = int(isodate.split("-")[2].split(" ")[0]) monthFromDate = int(isodate.split("-")[1]) yearFromDate = int(isodate.split("-")[0]) hourFromDate = isodate.split(" ")[1] yearsToAdd, resultingMonth = divmod((monthFromDate + monthstoAdd), 12) if resultingMonth == 0: resultingMonth = 12 yearsToAdd = yearsToAdd - 1 resultingYear = yearFromDate + yearsToAdd if resultingMonth in monthsWith30Days and dayFromDate == 31: resultingDay = 30 elif resultingMonth == 2 and (dayFromDate == 30 or dayFromDate == 31): if ((resultingYear % 4 == 0 and resultingYear % 100 != 0) or resultingYear % 400 == 0): resultingDay = 29 else: resultingDay = 28 else: resultingDay = dayFromDate if len(str(resultingDay)) < 2: resultingDay = '0' + str(resultingDay) if len(str(resultingMonth)) < 2: resultingMonth = '0' + str(resultingMonth) resultingDate = str(resultingYear) + '-' + str( resultingMonth) + '-' + str(resultingDay) + ' ' + str( hourFromDate) return resultingDate addMonthsToIsoDate = staticmethod(addMonthsToIsoDate) def getCurrentTimeInIsoformat(): """ @summary : Returns current system time in iso format. @return : Returns current system time in iso format. """ currentTimeInEpochFormat = time.time() return StatsDateLib.getIsoFromEpoch(currentTimeInEpochFormat) getCurrentTimeInIsoformat = staticmethod(getCurrentTimeInIsoformat) def isValidIsoDate(isoDate): """ @summary : Verifies whether or not the received date is a valid iso format date. @return : Returns whether or not the received date is a valid iso format date. """ isValid = True try: StatsDateLib.getSecondsSinceEpoch(isoDate) except: isValid = False return isValid isValidIsoDate = staticmethod(isValidIsoDate) def getYearMonthDayInStrfTime(timeInEpochFormat): """ @summary : Return the year month day in strftime based on an epoch date. @param timeInEpochFormat : Time, in seconds since epoch format from which you want to get the year month day. @return : a three item tuple containing the following : - year - month - day """ global _ months = { "January": _("January"), "February": _("February"), "March":_("March"), "April":_("April"),\ "May":_("May"), "June":_("June"), "July":_("July"), "August":_("August"), "September":_("September"),\ "October":_("October"), "November":_("November"), "December":_("December") } year = time.strftime('%Y', time.gmtime(timeInEpochFormat)) month = time.strftime('%B', time.gmtime(timeInEpochFormat)) day = time.strftime('%d', time.gmtime(timeInEpochFormat)) month = months[month] return year, month, day getYearMonthDayInStrfTime = staticmethod(getYearMonthDayInStrfTime) def getDayOfTheWeek(timeInEpochFormat): """ @summary : Return the year month day in strftime based on an epoch date. @Note : The returned day of the week will be written in the language that has currently been set. @param : Time, in seconds since epoch format from which you want to get the day of the week. """ global _ days = { "Mon": _("Mon"), "Tue": _("Tue"), "Wed": _("Wed"), "Thu": _("Thu"),\ "Fri": _("Fri"),"Sat": _("Sat"),"Sun": _("Sun"), "Monday": _("Monday"),\ "Tuesday": _("Tuesday"), "Wednesday": _("Wednesday"), "Thursday": _("Thursday"),\ "Friday": _("Friday"),"Saturday": _("Saturday"),"Sunday":_("Sunday") } day = time.strftime("%a", time.gmtime(timeInEpochFormat)) day = days[day] return day getDayOfTheWeek = staticmethod(getDayOfTheWeek) def getStartEndFromPreviousDay(currentTime, nbDays=1): """ Returns the start and end time of the day prior to the currentTime. currentTime must be in iso format. start and end are returned in iso format. """ end = StatsDateLib.getIsoTodaysMidnight(currentTime) yesterday = StatsDateLib.getIsoFromEpoch( StatsDateLib.getSecondsSinceEpoch(currentTime) - (24 * 60 * 60)) start = StatsDateLib.getIsoTodaysMidnight(yesterday) return start, end getStartEndFromPreviousDay = staticmethod(getStartEndFromPreviousDay) def getStartEndFromPreviousWeek(currentTime, nbWeeks=1): """ Returns the start and end time of the week prior to the currentTime. currentTime must be in iso format. start and end are returned in iso format. """ currentTimeInSecs = StatsDateLib.getSecondsSinceEpoch(currentTime) weekDay = int(time.strftime("%w", time.gmtime(currentTimeInSecs))) endInSecs = currentTimeInSecs - (weekDay * 24 * 60 * 60) startInSecs = endInSecs - (7 * 24 * 60 * 60) start = StatsDateLib.getIsoTodaysMidnight( StatsDateLib.getIsoFromEpoch(startInSecs)) end = StatsDateLib.getIsoTodaysMidnight( StatsDateLib.getIsoFromEpoch(endInSecs)) return start, end getStartEndFromPreviousWeek = staticmethod(getStartEndFromPreviousWeek) def getStartEndFromPreviousMonth(currentTime): """ Returns the start and end time of the month prior to the currentTime. currentTime must be in iso format. start and end are returned in iso format. """ date = currentTime.split()[0] splitDate = date.split("-") end = splitDate[0] + "-" + splitDate[1] + "-" + "01 00:00:00" splitTime = currentTime.split() date = splitTime[0] splitDate = date.split("-") if int(splitDate[1]) != 1: month = int(splitDate[1]) - 1 if month < 10: month = "0" + str(month) splitDate[1] = month else: year = int(splitDate[0]) - 1 splitDate[0] = str(year) splitDate[1] = "01" firstDayOfPreviousMonth = str(splitDate[0]) + "-" + str( splitDate[1]) + "-01" start = firstDayOfPreviousMonth + " 00:00:00" return start, end getStartEndFromPreviousMonth = staticmethod(getStartEndFromPreviousMonth) def getStartEndFromPreviousYear(currentTime): """ Returns the start and end time of the day prior to the currentTime. currentTime must be in iso format. start and end are returned in iso format. """ year = currentTime.split("-")[0] year = str(int(year) - 1) start = year + "-01-01 00:00:00" year = currentTime.split("-")[0] end = year + "-01-01 00:00:00" return start, end getStartEndFromPreviousYear = staticmethod(getStartEndFromPreviousYear) def getStartEndFromCurrentDay(currentTime): """ Returns the start and end time of the current day. currentTime must be in iso format. start and end are returned in iso format. """ start = StatsDateLib.getIsoTodaysMidnight(currentTime) tomorrow = StatsDateLib.getIsoFromEpoch( StatsDateLib.getSecondsSinceEpoch(currentTime) + 24 * 60 * 60) end = StatsDateLib.getIsoTodaysMidnight(tomorrow) return start, end getStartEndFromCurrentDay = staticmethod(getStartEndFromCurrentDay) def getStartEndFromCurrentWeek(currentTime): """ Returns the start and end time of the currentweek. currentTime must be in iso format. start and end are returned in iso format. """ currentTimeInSecs = StatsDateLib.getSecondsSinceEpoch(currentTime) weekDay = int(time.strftime("%w", time.gmtime(currentTimeInSecs))) endInSecs = currentTimeInSecs + ((7 - weekDay) * 24 * 60 * 60) end = StatsDateLib.getIsoTodaysMidnight( StatsDateLib.getIsoFromEpoch(endInSecs)) startInSecs = currentTimeInSecs - (weekDay * 24 * 60 * 60) start = StatsDateLib.getIsoTodaysMidnight( StatsDateLib.getIsoFromEpoch(startInSecs)) return start, end getStartEndFromCurrentWeek = staticmethod(getStartEndFromCurrentWeek) def getStartEndFromCurrentMonth(currentTime): """ Returns the start and end time of the currentDay. currentTime must be in iso format. start and end are returned in iso format. """ splitTime = currentTime.split() date = splitTime[0] splitDate = date.split("-") start = splitDate[0] + "-" + splitDate[1] + "-01 00:00:00" if int(splitDate[1]) != 12: month = int(splitDate[1]) + 1 if month < 10: month = "0" + str(month) splitDate[1] = month else: year = int(splitDate[0]) + 1 splitDate[0] = str(year) splitDate[1] = "01" firstDayOfMonth = str(splitDate[0]) + "-" + str(splitDate[1]) + "-01" end = firstDayOfMonth + " 00:00:00" return start, end getStartEndFromCurrentMonth = staticmethod(getStartEndFromCurrentMonth) def getStartEndFromCurrentYear(currentTime): """ Returns the start and end time of the currentDay. currentTime must be in iso format. start and end are returned in iso format. """ year = currentTime.split("-")[0] start = year + "-01-01 00:00:00" year = currentTime.split("-")[0] year = str(int(year) + 1) end = year + "-01-01 00:00:00" return start, end getStartEndFromCurrentYear = staticmethod(getStartEndFromCurrentYear) def getHoursFromIso(iso='2005-08-30 20:06:59'): """ Returns the hours field from a iso format date. """ iso = iso.split(" ")[1] hours, minutes, seconds = iso.split(':') return hours getHoursFromIso = staticmethod(getHoursFromIso) def getMinutesFromIso(iso='2005-08-30 20:06:59'): """ Returns the minute field from a iso format date. """ hours, minutes, seconds = iso.split(':') return minutes getMinutesFromIso = staticmethod(getMinutesFromIso) def rewindXDays(date='2005-08-30 20:06:59', x=0): """ Takes an iso format date and substract the number of days specified by x. """ seconds = StatsDateLib.getSecondsSinceEpoch(date) seconds = seconds - (x * 24 * 60 * 60) rewindedDate = StatsDateLib.getIsoFromEpoch(seconds) return rewindedDate rewindXDays = staticmethod(rewindXDays) def getNumberOfDaysBetween(date1='2005-08-30 20:06:59', date2='2005-08-30 20:06:59'): """ Takes two iso format dates and returns the number of days between them """ seconds1 = StatsDateLib.getSecondsSinceEpoch( date1) - StatsDateLib.getSecondsSinceStartOfDay(date1) seconds2 = StatsDateLib.getSecondsSinceEpoch( date2) - StatsDateLib.getSecondsSinceStartOfDay(date2) numberOfDays = abs(float((seconds1 - seconds2) / (24 * 60 * 60))) numberOfDays = int(numberOfDays) return numberOfDays getNumberOfDaysBetween = staticmethod(getNumberOfDaysBetween) def areDifferentDays(date1='2005-08-30 20:06:59', date2='2005-08-30 20:06:59'): """ Takes two iso format dates and returns whether or not both date are on different days. """ day1 = date1.split(" ")[0] day2 = date2.split(" ")[0] return day1 != day2 areDifferentDays = staticmethod(areDifferentDays) def getSecondsSinceEpoch(date='2005-08-30 20:06:59', format='%Y-%m-%d %H:%M:%S'): try: timeStruct = time.strptime(date, format) except: print "date tried : %s" % date return time.mktime(timeStruct) getSecondsSinceEpoch = staticmethod(getSecondsSinceEpoch) def getIsoLastMinuteOfDay(iso='2005-08-30 20:06:59'): """ Takes an iso format date like 2005-08-30 20:06:59. Replaces hour, minutes and seconds by last minute of day. Returns 2005-08-30 23:59:59. """ iso = iso.split(" ") iso = iso[0] iso = iso + " 23:59:59" return iso getIsoLastMinuteOfDay = staticmethod(getIsoLastMinuteOfDay) def getIsoTodaysMidnight(iso): """ Takes an iso format date like 2005-08-30 20:06:59. Replaces hour, minutes and seconds by 00. Returns 2005-08-30 00:00:00. """ iso = iso.split(" ") iso = iso[0] iso = iso + " 00:00:00" return iso getIsoTodaysMidnight = staticmethod(getIsoTodaysMidnight) def getIsoWithRoundedHours(iso): """ Takes an iso format date like 2005-08-30 20:06:59. Replaces minutes and seconds by 00. Returns 2005-08-30 20:00:00. """ iso = iso.split(":") iso = iso[0] iso = iso + ":00:00" return iso getIsoWithRoundedHours = staticmethod(getIsoWithRoundedHours) def getIsoWithRoundedSeconds(iso): """ Takes a numbers of seconds since epoch and tranforms it in iso format 2005-08-30 20:06:59. Replaces minutes and seconds by 00 thus returning 2005-08-30 20:00:00. """ #print "iso before modif : %s" %iso iso = iso.split(":") iso = iso[0] + ":" + iso[1] + ":00" return iso getIsoWithRoundedSeconds = staticmethod(getIsoWithRoundedSeconds) def getSeconds(string): # Should be used with string of following format: hh:mm:ss hours, minutes, seconds = string.split(':') return int(hours) * HOUR + int(minutes) * MINUTE + int(seconds) getSeconds = staticmethod(getSeconds) def getHoursSinceStartOfDay(date='2005-08-30 20:06:59'): """ This method takes an iso style date and returns the number of hours that have passed since 00:00:00 of the same day. """ try: splitDate = date.split(" ") splitDate = splitDate[1] splitDate = splitDate.split(":") hoursSinceStartOfDay = int(splitDate[0]) return hoursSinceStartOfDay except: print "Cannot convert %s in getMinutesSinceStartOfDay. " % date sys.exit() getHoursSinceStartOfDay = staticmethod(getHoursSinceStartOfDay) def isoDateDashed(date="20060613162653"): """ This method takes in parameter a non dashed iso date and returns the date dashed and the time with : as seperator. """ dashedDate = '%Y-%m-%d %H:%M:%S' % date return dashedDate isoDateDashed = staticmethod(isoDateDashed) def getMinutesSinceStartOfDay(date='2005-08-30 20:06:59'): """ This method receives an iso date as parameter and returns the number of minutes wich have passed since the start of that day. """ try: splitDate = date.split(" ") splitDate = splitDate[1] splitDate = splitDate.split(":") minutesSinceStartOfDay = int(splitDate[0]) * 60 + int(splitDate[1]) return minutesSinceStartOfDay except: print "Cannot convert %s in getMinutesSinceStartOfDay. " % date sys.exit() getMinutesSinceStartOfDay = staticmethod(getMinutesSinceStartOfDay) def getSecondsSinceStartOfDay(date='2005-08-30 20:06:59'): """ This method receives an iso date as parameter and returns the number of seconds wich have passed since the start of that day. """ try: splitDate = date.split(" ") splitDate = splitDate[1] splitDate = splitDate.split(":") minutesSinceStartOfDay = (int(splitDate[0]) * 60 * 60) + ( int(splitDate[1]) * 60) + int(splitDate[2]) return minutesSinceStartOfDay except: print "Cannot convert %s in getMinutesSinceStartOfDay. " % date sys.exit() getSecondsSinceStartOfDay = staticmethod(getSecondsSinceStartOfDay) def getNumericMonthFromString(month): """ This method takes a month in the string format and returns the month. Returns 00 if month is unknown. """ value = '00' if month == 'Jan': value = '01' elif month == 'Feb': value = '02' elif month == 'Mar': value = '03' elif month == 'Apr': value = '04' elif month == 'May': value = '05' elif month == 'Jun': value = '06' elif month == 'Jul': value = '07' elif month == 'Aug': value = '08' elif month == 'Sep': value = '09' elif month == 'Oct': value = '10' elif month == 'Nov': value = '11' elif month == 'Dec': value = '12' return value getNumericMonthFromString = staticmethod(getNumericMonthFromString) def getIsoFromEpoch(seconds): """ Take a number of seconds built with getSecondsSinceEpoch and returns a date in the format of '2005-08-30 20:06:59' Thu May 18 13:00:00 2006 """ timeString = time.ctime(seconds) timeString = timeString.replace( " ", " ") #in speicla case there may be two spaces splitTimeString = timeString.split(" ") if int(splitTimeString[2]) < 10: splitTimeString[2] = "0" + splitTimeString[2] originalDate = splitTimeString[ 4] + '-' + StatsDateLib.getNumericMonthFromString( splitTimeString[1] ) + '-' + splitTimeString[2] + ' ' + splitTimeString[3] return originalDate getIsoFromEpoch = staticmethod(getIsoFromEpoch) def getOriginalHour(seconds): """ Take a number of seconds built with getSecondsSinceEpoch and returns a date in the format of '2005-08-30 20:06:59' Thu May 18 13:00:00 2006 """ timeString = time.ctime(seconds) splitTimeString = timeString.split(" ") originalHour = splitTimeString[3] originalHour = originalHour.split(":") originalHour = originalHour[0] return originalHour getOriginalHour = staticmethod(getOriginalHour) def getSeparators(width=DAY, interval=20 * MINUTE): separators = [] for value in range(interval, width + interval, interval): separators.append(value) return separators getSeparators = staticmethod(getSeparators) def getSeparatorsWithStartTime(startTime="2006-06-06 00:00:00", width=DAY, interval=60 * MINUTE): """ This method works exactly like getSeparators but it uses a start time to set the separators """ separators = [] startTime = StatsDateLib.getSecondsSinceEpoch(startTime) if interval <= width: for value in range(int(interval + startTime), int(width + interval + startTime), int(interval)): separators.append(StatsDateLib.getIsoFromEpoch(value)) if separators[len(separators) - 1] > width + startTime: separators[len(separators) - 1] = StatsDateLib.getIsoFromEpoch(width + startTime) return separators getSeparatorsWithStartTime = staticmethod(getSeparatorsWithStartTime) def getStartEndInIsoFormat(timeOfTheCall, span, spanType="", fixedCurrent=False, fixedPrevious=False): """ @summary : Calculates the start and end of a timespan based on specified parameters. @param timeOfTheCall: Time at which these graphics were requested. In format. @param spanOfTheGraphics: Span in hours of the graphics. @param graphicType : daily | weekly | monthly | yearly @param fixedCurrent: Whether to use the fixedCurrent day, week month or year. @param fixedPrevious: Whether to use the fixedPrevious day week month or year. """ global _ #TODO :fixStartEnd method??? if fixedPrevious: if spanType == _("daily"): start, end = StatsDateLib.getStartEndFromPreviousDay( timeOfTheCall) elif spanType == _("weekly"): start, end = StatsDateLib.getStartEndFromPreviousWeek( timeOfTheCall) elif spanType == _("monthly"): start, end = StatsDateLib.getStartEndFromPreviousMonth( timeOfTheCall) elif spanType == _("yearly"): start, end = StatsDateLib.getStartEndFromPreviousYear( timeOfTheCall) elif fixedCurrent: if spanType == _("daily"): start, end = StatsDateLib.getStartEndFromCurrentDay( timeOfTheCall) elif spanType == _("weekly"): start, end = StatsDateLib.getStartEndFromCurrentWeek( timeOfTheCall) elif spanType == _("monthly"): start, end = StatsDateLib.getStartEndFromCurrentMonth( timeOfTheCall) elif spanType == _("yearly"): start, end = StatsDateLib.getStartEndFromCurrentYear( timeOfTheCall) else: if spanType == _("daily"): start = StatsDateLib.getIsoFromEpoch( StatsDateLib.getSecondsSinceEpoch(timeOfTheCall) - StatsDateLib.DAY) elif spanType == _("weekly"): start = StatsDateLib.getIsoFromEpoch( StatsDateLib.getSecondsSinceEpoch(timeOfTheCall) - (7 * StatsDateLib.DAY)) elif spanType == _("monthly"): start = StatsDateLib.getIsoFromEpoch( StatsDateLib.getSecondsSinceEpoch(timeOfTheCall) - (30 * StatsDateLib.DAY)) elif spanType == _("yearly"): start = StatsDateLib.getIsoFromEpoch( StatsDateLib.getSecondsSinceEpoch(timeOfTheCall) - (365 * StatsDateLib.DAY)) else: start = StatsDateLib.getIsoFromEpoch( StatsDateLib.getSecondsSinceEpoch(timeOfTheCall) - span * 60 * 60) end = timeOfTheCall return start, end getStartEndInIsoFormat = staticmethod(getStartEndInIsoFormat)
def generateWebPage(sourlientNames, groups, fileType, outputFileName, language): """ @summary: Generates popupAdder web page named after the received outputFileName and based on the list of sourlients names received @param sourlientNames : List of sources or clients that need to be printed. @param groups : List of groups that need to be printed. @param fileType: tx or rx @param outputFileName : Filename that needs to be created. @return : None """ statsPaths = StatsPaths() statsPaths.setPaths(language) _ = LanguageTools.getTranslatorForModule(CURRENT_MODULE_ABS_PATH, language) if not os.path.isdir(os.path.dirname(outputFileName)): os.makedirs(os.path.dirname(outputFileName)) """ Redirect the output""" #print outputFileName fileHandle = open(outputFileName, "w") oldStdOut = sys.stdout #save previous stdout sys.stdout = fileHandle print """ <html> <head> <title>Add items to list.</title> <meta name="Author" content="Nicholas Lemay"> <meta name="Description" content="Small popup window used to add items into a list. To be used with graphicsResquests.py"> <meta name="Keywords" content=""> <style type="text/css"> div.selectObject{ width:300px; height: auto; } </style> <link rel="stylesheet" type="text/css" href="/css/style.css"> <script type="text/javascript" language="JavaScript"> function popupAddingWindow( url ) { var newWindow; var props = 'scrollBars=no,resizable=no,toolbar=no,menubar=no,location=no,directories=no,width=700,height=300'; newWindow = window.open(url, "Add_from_Src_to_Dest", props); } function closeWindow(){ window.close(); } // Fill the selcted item list with the items already present in parent. function copyLists( srcList, destList ) { var len = destList.length; for(var i = 0; i < srcList.length; i++) { if ( srcList.options[i] != null ) { //Check if this value already exist in the destList or not //if not then add it otherwise do not add it. var found = false; for(var count = 0; count < len; count++) { if (destList.options[count] != null) { if (srcList.options[i].text == destList.options[count].text) { found = true; break; } } } if (found != true) { destList.options[len] = new Option(srcList.options[i].text); len++; } } } } // Add the SELECTED items from the source to destination list // will only add the items wich are not allready present in dest list. function addSrcToDestList( srcList, destList ) { var len = destList.length; for(var i = 0; i < srcList.length; i++) { if ((srcList.options[i] != null) && (srcList.options[i].selected)) { //Check if this value already exist in the destList or not //if not then add it otherwise do not add it. var found = false; for(var count = 0; count < len; count++) { if (destList.options[count] != null) { if (srcList.options[i].text == destList.options[count].text) { found = true; break; } } } if (found != true) { destList.options[len] = new Option(srcList.options[i].text); len++; } } } } // Deletes from the destination list. function deleteFromList( list ) { var len = list.options.length; for(var i = (len-1); i >= 0; i--) { if ((list.options[i] != null) && (list.options[i].selected == true)) { list.options[i] = null; } } } </script> </head> <body text="#FFFFFF" link="#FFFFFF" vlink="000000" bgcolor="#7ACC7A"> <center> <form name="adderForm" method="POST"> <table bgcolor="#FFF4E5" > <tr> <font color ="white"> <td bgcolor="#006699" width="300" >""" + _( "Available") + """</td> <td bgcolor="#006699" > </td> <td bgcolor="#006699" width="300" >""" + _( "Selected") + """</td> </font> </tr> <tr> <td bgcolor="#7ACC7A" width="300"> <select size="12" style="width: 300px;height: 225px;font: 14px;" name="srcList" multiple> """ startingIndex = 1 if len(groups) > 0: print """ <optgroup label=""" + '"' + _( "Groups:") + """">""" + _("Groups:") + """</optgroup> """ for i in range(len(groups)): print """ <option value="%s">%s</option> """ % (i + startingIndex, groups[i]) startingIndex = i if len(sourlientNames) > 0: if fileType == "tx": print """ <optgroup label=""" + '"' + _( "TX clients :") + """">""" + _( "TX clients :") + """ </optgroup> """ elif fileType == "rx": print """ <optgroup label=""" + '"' + _( "RX sources :") + """">""" + _( "RX sources :") + """ </optgroup> """ else: print """ <optgroup label=""" + '"' + _( "Sourlients :") + """">""" + _( "Sourlients :") + """ </optgroup> """ for i in range(len(sourlientNames)): print """ <option value="%s">%s</option> """ % (i + startingIndex, sourlientNames[i]) print """ </select> </td> <td bgcolor="#FFF4E5" width="74" align="center"> <input type="button" value=" >> " style="font: 14px;" onClick="javascript:addSrcToDestList( document.forms['adderForm'].elements['srcList'], document.forms['adderForm'].elements['destList'] )"> <br><br> <input type="button" value=" << " style="font: 14px;" onclick="javascript:deleteFromList( document.forms['adderForm'].elements['destList'] );"> <br><br> <input type="button" value=""" + '"' + _( "Done" ) + '"' + """ style="font: 14px;" onClick ="javascript:window.opener.copyLists(document.forms['adderForm'].elements['destList'], window.opener.document.forms['inputForm'].elements['sourlientList']);javascript:closeWindow();"> </td> <td bgcolor="#7ACC7A" width="300"> <select size="12" style="width: 300px;height: 225px;font: 14px;" name="destList" multiple> </select> </td> </tr> </table> </form> </body> </html> """ fileHandle.close() sys.stdout = oldStdOut #resets standard output
def generateWebPage( sourlientNames, groups, fileType, outputFileName, language ): """ @summary: Generates popupAdder web page named after the received outputFileName and based on the list of sourlients names received @param sourlientNames : List of sources or clients that need to be printed. @param groups : List of groups that need to be printed. @param fileType: tx or rx @param outputFileName : Filename that needs to be created. @return : None """ statsPaths = StatsPaths() statsPaths.setPaths(language) _ = LanguageTools.getTranslatorForModule( CURRENT_MODULE_ABS_PATH, language ) if not os.path.isdir( os.path.dirname(outputFileName) ): os.makedirs( os.path.dirname(outputFileName) ) """ Redirect the output""" #print outputFileName fileHandle = open( outputFileName, "w" ) oldStdOut = sys.stdout #save previous stdout sys.stdout = fileHandle print """ <html> <head> <title>Add items to list.</title> <meta name="Author" content="Nicholas Lemay"> <meta name="Description" content="Small popup window used to add items into a list. To be used with graphicsResquests.py"> <meta name="Keywords" content=""> <style type="text/css"> div.selectObject{ width:300px; height: auto; } </style> <link rel="stylesheet" type="text/css" href="/css/style.css"> <script type="text/javascript" language="JavaScript"> function popupAddingWindow( url ) { var newWindow; var props = 'scrollBars=no,resizable=no,toolbar=no,menubar=no,location=no,directories=no,width=700,height=300'; newWindow = window.open(url, "Add_from_Src_to_Dest", props); } function closeWindow(){ window.close(); } // Fill the selcted item list with the items already present in parent. function copyLists( srcList, destList ) { var len = destList.length; for(var i = 0; i < srcList.length; i++) { if ( srcList.options[i] != null ) { //Check if this value already exist in the destList or not //if not then add it otherwise do not add it. var found = false; for(var count = 0; count < len; count++) { if (destList.options[count] != null) { if (srcList.options[i].text == destList.options[count].text) { found = true; break; } } } if (found != true) { destList.options[len] = new Option(srcList.options[i].text); len++; } } } } // Add the SELECTED items from the source to destination list // will only add the items wich are not allready present in dest list. function addSrcToDestList( srcList, destList ) { var len = destList.length; for(var i = 0; i < srcList.length; i++) { if ((srcList.options[i] != null) && (srcList.options[i].selected)) { //Check if this value already exist in the destList or not //if not then add it otherwise do not add it. var found = false; for(var count = 0; count < len; count++) { if (destList.options[count] != null) { if (srcList.options[i].text == destList.options[count].text) { found = true; break; } } } if (found != true) { destList.options[len] = new Option(srcList.options[i].text); len++; } } } } // Deletes from the destination list. function deleteFromList( list ) { var len = list.options.length; for(var i = (len-1); i >= 0; i--) { if ((list.options[i] != null) && (list.options[i].selected == true)) { list.options[i] = null; } } } </script> </head> <body text="#FFFFFF" link="#FFFFFF" vlink="000000" bgcolor="#7ACC7A"> <center> <form name="adderForm" method="POST"> <table bgcolor="#FFF4E5" > <tr> <font color ="white"> <td bgcolor="#006699" width="300" >""" + _("Available") + """</td> <td bgcolor="#006699" > </td> <td bgcolor="#006699" width="300" >""" + _("Selected") + """</td> </font> </tr> <tr> <td bgcolor="#7ACC7A" width="300"> <select size="12" style="width: 300px;height: 225px;font: 14px;" name="srcList" multiple> """ startingIndex = 1 if len(groups) > 0 : print """ <optgroup label=""" + '"' + _("Groups:") + """">""" + _("Groups:") + """</optgroup> """ for i in range(len(groups)): print """ <option value="%s">%s</option> """%( i+startingIndex, groups[i] ) startingIndex = i if len( sourlientNames ) > 0: if fileType == "tx": print """ <optgroup label=""" + '"'+ _("TX clients :") + """">""" + _("TX clients :") + """ </optgroup> """ elif fileType == "rx": print """ <optgroup label=""" + '"'+ _("RX sources :") + """">""" + _("RX sources :") + """ </optgroup> """ else: print """ <optgroup label=""" + '"'+ _("Sourlients :") + """">""" + _("Sourlients :") +""" </optgroup> """ for i in range(len(sourlientNames)): print """ <option value="%s">%s</option> """%( i+startingIndex, sourlientNames[i] ) print """ </select> </td> <td bgcolor="#FFF4E5" width="74" align="center"> <input type="button" value=" >> " style="font: 14px;" onClick="javascript:addSrcToDestList( document.forms['adderForm'].elements['srcList'], document.forms['adderForm'].elements['destList'] )"> <br><br> <input type="button" value=" << " style="font: 14px;" onclick="javascript:deleteFromList( document.forms['adderForm'].elements['destList'] );"> <br><br> <input type="button" value=""" + '"' + _("Done") + '"' + """ style="font: 14px;" onClick ="javascript:window.opener.copyLists(document.forms['adderForm'].elements['destList'], window.opener.document.forms['inputForm'].elements['sourlientList']);javascript:closeWindow();"> </td> <td bgcolor="#7ACC7A" width="300"> <select size="12" style="width: 300px;height: 225px;font: 14px;" name="destList" multiple> </select> </td> </tr> </table> </form> </body> </html> """ fileHandle.close() sys.stdout = oldStdOut #resets standard output