def save( self, backup ): """Save the files, and output the result.""" #Attempt to save if backup: printStuff( "Backing up script editor...", AutoSave.printPrefix ) else: printStuff( "Saving main script files...", AutoSave.printPrefix ) try: execute( saveScriptEditorFiles, backup, AutoSave().location() ) #If script editor window is closed (tabs don't exist) except RuntimeError: if not backup: printStuff( "Item doesn't exist (window is probably closed).", AutoSave.printPrefix ) #If file is read only except WindowsError: printStuff( "Failed to save scripts, one or more of the files is read only.", AutoSave.printPrefix ) #If saving is disabled except SaveScriptError: if backup: #Only write this once printStuff( "Saving script editor contents is disabled, enable it in the preferences to allow auto saving.", AutoSave.printPrefix ) #Other unknown error except Exception as e: printStuff( "Failed to save scripts for unknown reason ({}), tell me if you see this message.".format( e.message ), AutoSave.printPrefix ) #Successful save else: if not backup: printStuff( "Successfully saved scripts.", AutoSave.printPrefix )
def quickSave( self, interval=None ): """ Manually save the files. A little messy currently as it's just copied and pasted from the threaded version. """ scriptLocation = execute( self.location ) #Attempt to save onlySavedBackupFiles = False printStuff( "Saving scripts... Do not open or close the script editor during this time.", self.printPrefix ) execute( saveScriptEditorFiles, True, scriptLocation ) try: execute( saveScriptEditorFiles, True, scriptLocation ) #Backup files first onlySavedBackupFiles = True execute( saveScriptEditorFiles, False, scriptLocation ) #Overwrite main files onlySavedBackupFiles = False #If script editor window is closed (tabs don't exist) except RuntimeError: printMessage = "Item doesn't exist (window is probably closed)." if interval: printMessage = printMessage[:-1]+", trying again in {0} seconds.".format( interval ) printStuff( printMessage, self.printPrefix ) #This shouldn't ever happen, but just in case, save the backup files from deletion if onlySavedBackupFiles: MoveBackupScriptsToFolder( scriptLocation ) moveLocation = scriptLocation+'/scriptEditorTemp/{0}'.format( time.time() ) printStuff( "Normal files may have become corrupted so backup files have been moved into '{0}'.".format( moveLocation ), self.printPrefix ) #If file is read only except WindowsError: printStuff( "Failed to save scripts, one or more of the files is read only.", self.printPrefix ) #If saving is disabled except SaveScriptError: printStuff( "Saving to the script editor is not enabled, type 'pm.optionVar['saveActionsScriptEditor']=1' to enable.", self.printPrefix ) #Other unknown error except: #This also shouldn't happen printStuff( "Failed to save scripts for unknown reason, if it keeps happening please let me know.", self.printPrefix ) #Successful save else: printMessage = "Successfully saved scripts." if interval: printMessage = printMessage[:-1]+", saving again in {0} seconds.".format( interval ) printStuff( printMessage, self.printPrefix )
def __init__( self, wait=0 ): """Start self.run() as a thread. wait: How many seconds to wait before starting the timer, to be used if loading the script automatically when Maya starts. - Integer - Recommended 0 if manually activated, 10 if automatically loading """ #Set up values self.wait = wait self.location = execute( AutoSave().location ) self.enabled=True #Begin thread thread = threading.Thread(target=self.run, args=()) thread.daemon = True thread.start()
def run(self): #Wait a short while before starting the execution time.sleep( self.wait ) printStuff( "Started at intervals of {} seconds. Do not open or close the script editor during saving.".format( execute( AutoSave().interval ) ), AutoSave.printPrefix ) alreadySaidItsPaused = False #Loop until the running state becomes False while execute( AutoSave().enabled ): #Find if code is paused if not execute( AutoSave().paused ): #Update progress interval = execute( AutoSave().interval ) intervals = set( i for i in ( 15, 10, 5, 3, 2, 1 ) if i*2 <= interval ) totalCount = 0 #Print if just unpaused if alreadySaidItsPaused: printStuff( "Resuming backup, activating in {0} seconds.".format( interval ), AutoSave.printPrefix ) alreadySaidItsPaused = False #Count down from interval for i in xrange( interval, 0, -1 ): execute( AutoSave().progress, time.time() ) #Make sure interval hasn't been changed if i <= execute( AutoSave().interval ): #Check state hasn't been changed to paused or stopped runningState = execute( AutoSave().enabled ) if runningState > 0: #Only print progress if the time matches anything in the list if i in intervals: s = '' if i != 1: s = 's' printStuff( "Activating in {0} second{1}...".format( i, s ), AutoSave.printPrefix ) else: break time.sleep( 1 ) #Continue if state is still running if execute( AutoSave().enabled ) > 0: interval = execute( AutoSave().interval ) #Attempt to save onlySavedBackupFiles = False printStuff( "Saving scripts... Do not open or close the script editor during this time.", AutoSave.printPrefix ) try: execute( saveScriptEditorFiles, True, self.location ) #Backup files first onlySavedBackupFiles = True execute( saveScriptEditorFiles, False, self.location ) #Overwrite main files onlySavedBackupFiles = False #If script editor window is closed (tabs don't exist) except RuntimeError: printStuff( "Item doesn't exist (window is probably closed), trying again in {0} seconds.".format( interval ), AutoSave.printPrefix ) #This shouldn't ever happen, but just in case, save the backup files from deletion if onlySavedBackupFiles: MoveBackupScriptsToFolder( self.location ) moveLocation = self.location+'/scriptEditorTemp/{0}'.format( time.time() ) printStuff( "Normal files may have become corrupted so backup files have been moved into '{0}'.".format( moveLocation ), AutoSave.printPrefix ) #If file is read only except WindowsError: printStuff( "Failed to save scripts, one or more of the files is read only.", AutoSave.printPrefix ) #If saving is disabled except SaveScriptError: printStuff( "Saving to the script editor is not enabled, type 'pm.optionVar['saveActionsScriptEditor']=1' to enable.", AutoSave.printPrefix ) #Other unknown error except: #This also shouldn't happen printStuff( "Failed to save scripts for unknown reason, if it keeps happening please let me know.", AutoSave.printPrefix ) #Successful save else: printStuff( "Successfully saved scripts, saving again in {0} seconds.".format( interval ), AutoSave.printPrefix ) else: #Only print that the script has been paused once, but keep looping if not alreadySaidItsPaused: alreadySaidItsPaused = True printStuff( "Paused by user", AutoSave.printPrefix ) execute( AutoSave().progress, time.time() ) time.sleep(1) #Print confirmation that loop has been stopped execute( AutoSave().progress, False ) printStuff( "Cancelled by user", AutoSave.printPrefix )
def printStuff( stuff, prefix='', suffix='' ): """Print function to use the wrapper.""" if not pm.optionVar[AutoSave.printName]: currentTime = time.strftime('[%H:%M:%S] ', time.localtime(time.time())) execute( printWrapper, '// '+str( currentTime )+str( prefix )+str( stuff )+str( suffix ) )