Commits

Nicholas Dudfield committed fdc28b5

Added in @gpfsmurf's on_pre_save addition: `When a file is saved, and there is no backup of it yet, create a backup of the original file before saving the new, modified version.`

  • Participants
  • Parent commits b50292b

Comments (0)

Files changed (1)

automaticbackups.py

     """Creates an automatic backup of every file you save. This
     gives you a rudimentary mechanism for making sure you don't lose
     information while working."""
+    
+    
+    def on_pre_save(self, view):
+      """When a file is saved, and there is no backup of it yet, create a
+      backup of the original file before saving the new, modified version."""
 
+      buffer_file_name = view.file_name()
+
+      # if buffer_file_name.endswith('.lnk'): return
+      backup_dir = backups.backup_file_path(view, True)
+      if backup_dir == None:
+        return
+
+      filepart, extensionpart = os.path.splitext(os.path.split(buffer_file_name)[1])
+      newname = os.path.join(backup_dir, filepart + "-0000-00-00-00-00-00" + extensionpart)
+      
+      if os.path.isfile(newname):
+        # Backup of original file already exists
+        return
+
+      # make sure that we have a directory to write into
+      if (os.access(backup_dir, os.F_OK) == False):
+        os.makedirs(backup_dir)
+
+      backups.log("backing up original version to " + newname)
+      shutil.copy(buffer_file_name, newname)
+    
     def on_post_save(self, view):
         """When a file is saved, put a copy of the file into the
         backup directory"""