def updateImg( self, strMessage, strFilenameImg = None, strLegend = None ): "add an image and a sentence to a blog" "strFilenameImg: if None, take a picture using camera" "strLegend: if different of None or strLegend, add a legend below the image" if( strFilenameImg == None ): print( "INF: Blog.updateImg: Taking picture...\n" ); camToolBox = naoqitools.myGetProxy( "ALVisionToolbox" ); camToolBox.halfPress(); strFilenameImg = "/home/nao/" + filetools.getFilenameFromTime(); camToolBox.post.takePictureRegularly( 5.0, strFilenameImg, True, "jpg", 2 ); # 2=> VGA time.sleep( 2.2 ); # 1 second is not sufficient ! camToolBox.stopTPR( strFilenameImg, "jpg" ); strFilenameImg = strFilenameImg + '.jpg'; # coz takePictureRegularly add '.jpg' automatically print( "INF: Blog.updateImg: Taking picture, file goes to '%s'\n" % strFilenameImg ); strExt = os.path.basename( strFilenameImg ); strExt = strExt.split( '.' ); strExt = strExt.pop(); strFilename = filetools.getFilenameFromTime() + '.' + strExt; strDest = self.strImgFolderAbs + strFilename; filetools.copyFile( strFilenameImg, strDest ); strTxt = "<table cellpadding=2><tr><td>%s<br></td></tr><tr><td><IMG SRC='%s' HEIGHT=192></td></tr>" % ( strMessage, self.strImgFolderLocal + strFilename ); if( strLegend != None and strLegend != "" ): strTxt += "<tr><td><font size=-2><center>- %s -</center></font></td></tr>" % strLegend; strTxt += "</table>"; self.updateBlog( strTxt );
def updateImg(self, strMessage, strFilenameImg=None, strLegend=None): "add an image and a sentence to a blog" "strFilenameImg: if None, take a picture using camera" "strLegend: if different of None or strLegend, add a legend below the image" if (strFilenameImg == None): print("INF: Blog.updateImg: Taking picture...\n") camToolBox = naoqitools.myGetProxy("ALVisionToolbox") camToolBox.halfPress() strFilenameImg = "/home/nao/" + filetools.getFilenameFromTime() camToolBox.post.takePictureRegularly(5.0, strFilenameImg, True, "jpg", 2) # 2=> VGA time.sleep(2.2) # 1 second is not sufficient ! camToolBox.stopTPR(strFilenameImg, "jpg") strFilenameImg = strFilenameImg + '.jpg' # coz takePictureRegularly add '.jpg' automatically print("INF: Blog.updateImg: Taking picture, file goes to '%s'\n" % strFilenameImg) strExt = os.path.basename(strFilenameImg) strExt = strExt.split('.') strExt = strExt.pop() strFilename = filetools.getFilenameFromTime() + '.' + strExt strDest = self.strImgFolderAbs + strFilename filetools.copyFile(strFilenameImg, strDest) strTxt = "<table cellpadding=2><tr><td>%s<br></td></tr><tr><td><IMG SRC='%s' HEIGHT=192></td></tr>" % ( strMessage, self.strImgFolderLocal + strFilename) if (strLegend != None and strLegend != ""): strTxt += "<tr><td><font size=-2><center>- %s -</center></font></td></tr>" % strLegend strTxt += "</table>" self.updateBlog(strTxt)
def logToFile( strMessage, strSpecificFileName = "" ): "add a message to the current debug log file" import filetools import pathtools global global_strAltools_LogToFile; global global_mutex_LogToFile; global global_timeLogToFile_lastLog; timeNow = time.time(); rDurationSec = timeNow - global_timeLogToFile_lastLog; global_timeLogToFile_lastLog = timeNow; while( global_mutex_LogToFile.testandset() == False ): time.sleep( 0.02 ); if( global_strAltools_LogToFile == None ): try: os.makedirs( pathtools.getCachePath() ); except: pass global_strAltools_LogToFile = pathtools.getCachePath() + filetools.getFilenameFromTime() + ".log"; print( "altools.logToFile: logging to '%s'" % global_strAltools_LogToFile ); if( strSpecificFileName == '' ): strFileName = global_strAltools_LogToFile; else: strFileName = pathtools.getCachePath() + strSpecificFileName + '_' + getNaoqiStartupTimeStamp() + ".log"; # print( "logToFile: logging to %s" % strFileName ); try: file = open( strFileName, "at" ); file.write( "%s (%5.2fs): %s\n" % ( getHumanTimeStamp(), rDurationSec, strMessage ) ); finally: if( file ): file.close(); global_mutex_LogToFile.unlock();
def executeAndGrabOutput( strCommand, bLimitToFirstLine = False ): "execute a command in system shell and return grabbed output" "WARNING: it's a 'not efficient' function!" strTimeStamp = filetools.getFilenameFromTime(); strOutputFilename = pathtools.getVolatilePath() + "grab_output_" + strTimeStamp+ ".tmp"; # generate a different file each call for multithreading capabilities mySystemCall( strCommand + " > " + strOutputFilename ); if( bLimitToFirstLine ): strBufferRead = filetools.getFileFirstLine( strOutputFilename ); else: strBufferRead = filetools.getFileContents( strOutputFilename ); try: os.unlink( strOutputFilename ); except: pass debug.debug( "executeAndGrabOutput: '%s'" % strBufferRead ); return strBufferRead;
def logToFile(strMessage, strSpecificFileName=""): "add a message to the current debug log file" import filetools import pathtools global global_strAltools_LogToFile global global_mutex_LogToFile global global_timeLogToFile_lastLog timeNow = time.time() rDurationSec = timeNow - global_timeLogToFile_lastLog global_timeLogToFile_lastLog = timeNow while (global_mutex_LogToFile.testandset() == False): time.sleep(0.02) if (global_strAltools_LogToFile == None): try: os.makedirs(pathtools.getCachePath()) except: pass global_strAltools_LogToFile = pathtools.getCachePath( ) + filetools.getFilenameFromTime() + ".log" print("altools.logToFile: logging to '%s'" % global_strAltools_LogToFile) if (strSpecificFileName == ''): strFileName = global_strAltools_LogToFile else: strFileName = pathtools.getCachePath( ) + strSpecificFileName + '_' + getNaoqiStartupTimeStamp() + ".log" # print( "logToFile: logging to %s" % strFileName ); try: file = open(strFileName, "at") file.write("%s (%5.2fs): %s\n" % (getHumanTimeStamp(), rDurationSec, strMessage)) finally: if (file): file.close() global_mutex_LogToFile.unlock()
bRet = usage.post.getWebFile( strHost, strFolder + strPageName, strSaveAs, rTimeOutForAnswerInSec ); return ""; strPageContents = usage.getWebPage( strHost, strFolder + strPageName, rTimeOutForAnswerInSec ); if( strPageContents != "error" or ( rTimeOutForAnswerInSec > 0. and rTimeOutForAnswerInSec < 10.0 ) ): # if we put a short timeout, that's possible to have an empty response! return strPageContents; # else, we will use the normal method else: print( "WRN: getHtmlPage: CPP method error: return empty, trying other method" ); except BaseException, err: print( "WRN: getHtmlPage: CPP method error: %s" % str( err ) ); pass # use oldies version print( "WRN: nettools.getHtmlPage: using old one using fork and shell!" ); # not very efficient: should store it in var/volatile (but less os independent) debug.debug( "getHtmlPage( %s, bWaitAnswer = %d, rTimeOutForAnswerInSec = %d )" % ( strHtmlAdress, bWaitAnswer, rTimeOutForAnswerInSec ) ); strRandomFilename = pathtools.getVolatilePath() + "getHtmlPage_%s.html" % filetools.getFilenameFromTime(); # sous windows wget peut geler, donc on va l'appeller avec un timeout (qui ne fonctionne pas, c'est drole...) # threadWGet = system.mySystemCall( "wget %s --output-document=%s --tries=16 --timeout=60 --cache=off -q" % ( strHtmlAdress, strRandomFilename ), False, True ); # commenter cette ligne pour avoir toujours le meme fichier # en fait plein d'options n'existe pas sur Nao, donc on ne laisse que celle ci: if( strSaveAs != None ): strRandomFilename = strSaveAs; threadWGet = system.mySystemCall( "wget \"%s\" --output-document=%s -q" % ( strHtmlAdress, strRandomFilename ), bWaitEnd = False, bStoppable = True ); # commenter cette ligne pour avoir toujours le meme fichier if( not bWaitAnswer ): debug.debug( "getHtmlPage( %s, %d ) - direct return" % ( strHtmlAdress, bWaitAnswer ) ); return ""; timeBegin = time.time(); timeElapsed = 0.0; time.sleep( 1.0 ); # time for the process to be created !