diff --git a/AutoBackups.py b/AutoBackups.py index d8657c4..0f63a9e 100644 --- a/AutoBackups.py +++ b/AutoBackups.py @@ -5,6 +5,7 @@ import sys import os import shutil +import re st_version = 2 if sublime.version() == '' or int(sublime.version()) > 3000: @@ -94,7 +95,7 @@ def save_backup(self, view, on_load_event): def is_backup_file(self, path): path = PathsHelper.normalise_path(path) - base_dir = PathsHelper.get_base_dir() + base_dir = PathsHelper.get_base_dir(False) base_dir = PathsHelper.normalise_path(base_dir) backup_dir_len = len(base_dir) sub = path[0:backup_dir_len] @@ -111,12 +112,53 @@ def console(self, text): class AutoBackupsOpenBackupCommand(sublime_plugin.TextCommand): def run(self, edit): + platform = sublime.platform().title() + settings = sublime.load_settings('AutoBackups ('+platform+').sublime-settings') + backup_per_day = settings.get('backup_per_day') + + if (not backup_per_day): + window = sublime.active_window() + view = sublime.Window.active_view(window) + filepath = view.file_name() + newname = PathsHelper.get_backup_filepath(filepath) + if os.path.isfile(newname): + window.open_file(newname) + else: + sublime.error_message('Backup for ' + filepath + ' not exists!') + else: + f_files = self.getFiles() + + if not f_files: + sublime.error_message('Backups for this file not exists!') + return + + f_files.reverse() + self.view.window().show_quick_panel(f_files, self.open) + return + + def getFiles(self): + filename = PathsHelper.normalise_path(self.view.file_name(), True) + basedir = PathsHelper.get_base_dir(True) + + f_files = [] + for folder in os.listdir(basedir): + fl = basedir+'/'+folder+'/'+filename + match = re.search(r"[0-9+]{4}-[0-9+]{2}-[0-9+]{2}", folder) + if os.path.isfile(fl) and match is not None: + folder_name, file_name = os.path.split(fl) + f_file = [] + f_file.append(folder+' - '+file_name) + f_file.append(fl) + f_files.append(f_file) + return f_files + + def open(self, file): + if (file == -1): + return + # open file + f_files = self.getFiles() + filename = f_files[file][1] window = sublime.active_window() view = sublime.Window.active_view(window) - filepath = view.file_name() - newname = PathsHelper.get_backup_filepath(filepath) + window.open_file(filename) - if os.path.isfile(newname): - window.open_file(newname) - else: - sublime.error_message('Backup for ' + filepath + ' not exists!') diff --git a/README.md b/README.md index d4a1d89..2bfec7f 100644 --- a/README.md +++ b/README.md @@ -5,7 +5,7 @@ Sublime Text 2/3 Auto backups plugin AutoBackups is a Sublime Text 2/3 plugin, which automatically save a backup copy every time you save or open (if backup file not exists) a file. (Like DreamWeaver) -When you edit text files (scripts, prose, whatever) you often find yourself wishing for an last version. Ever accidentally deleted a chunk from an important configuration file, or wished you could roll back a document a few hours? This plugin takes a copy of file you open/save and copies it into a backup directory structure, ensuring that you never lose an old version of a file. +When you edit text files (scripts, prose, whatever) you often find yourself wishing for an last version. Ever accidentally deleted a chunk from an important configuration file, or wished you could roll back a document a few hours? This plugin takes a copy of file you open/save and copies it into a backup directory structure, ensuring that you never lose an old version of a file. If enabled setting backup_per_day backups will be saved for each day. ## Installation diff --git a/autobackups/paths_helper.py b/autobackups/paths_helper.py index c247cf6..0a9559c 100644 --- a/autobackups/paths_helper.py +++ b/autobackups/paths_helper.py @@ -9,7 +9,7 @@ class PathsHelper(object): @staticmethod - def get_base_dir(): + def get_base_dir(only_base): platform = sublime.platform().title() settings = sublime.load_settings('AutoBackups ('+platform+').sublime-settings') # Configured setting @@ -18,7 +18,7 @@ def get_base_dir(): date = str(now_date)[:10] backup_per_day = settings.get('backup_per_day') - if (backup_per_day): + if (backup_per_day and not only_base): backup_dir = backup_dir +'/'+ date @@ -28,13 +28,13 @@ def get_base_dir(): # Windows: /My Documents/Sublime Text Backups if (sublime.platform() == 'windows'): backup_dir = 'D:/Sublime Text Backups' - if (backup_per_day): + if (backup_per_day and not only_base): backup_dir = backup_dir +'/'+ date return backup_dir # Linux/OSX/other: ~/sublime_backups backup_dir = '~/.sublime/backups' - if (backup_per_day): + if (backup_per_day and not only_base): backup_dir = backup_dir +'/'+ date return os.path.expanduser(backup_dir) @@ -46,12 +46,12 @@ def timestamp_file(filename): @staticmethod def get_backup_path(filepath): path = os.path.expanduser(os.path.split(filepath)[0]) - backup_base = PathsHelper.get_base_dir() + backup_base = PathsHelper.get_base_dir(False) path = PathsHelper.normalise_path(path) return os.path.join(backup_base, path) @staticmethod - def normalise_path(path): + def normalise_path(path, slashes = False): if sublime.platform() != 'windows': # remove any leading / before combining with backup_base @@ -66,14 +66,16 @@ def normalise_path(path): # windows only: transform \\remotebox\share into network\remotebox\share path = re.sub(r'^\\\\([\w\-]{2,})', r'network\\\1', path) + + if slashes: + path = path.replace('\\', '/') + return path @staticmethod def get_backup_filepath(filepath): - filename = os.path.split(filepath)[1] - return os.path.join(PathsHelper.get_backup_path(filepath), PathsHelper.timestamp_file(filename)) diff --git a/package-metadata.json b/package-metadata.json index 49869c3..ecc8f26 100644 --- a/package-metadata.json +++ b/package-metadata.json @@ -1 +1 @@ -{"url": "https://github.com/akalongman/sublimetext-autobackups", "version": "1.0.4", "description": "AutoBackups is a Sublime Text 2/3 plugin, which automatically save a backup copy every time you save or open (if backup file not exists) a file. (Like DreamWeaver)"} \ No newline at end of file +{"url": "https://github.com/akalongman/sublimetext-autobackups", "version": "1.2", "description": "AutoBackups is a Sublime Text 2/3 plugin, which automatically save a backup copy every time you save or open (if backup file not exists) a file. (Like DreamWeaver)"} \ No newline at end of file diff --git a/packages.json b/packages.json index 4263cfd..f8674a7 100644 --- a/packages.json +++ b/packages.json @@ -6,12 +6,12 @@ "description": "AutoBackups is a Sublime Text 2/3 plugin, which automatically save a backup copy every time you save or open (if backup file not exists) a file. (Like DreamWeaver)", "author": "Avtandil Kikabidze", "homepage": "https://github.com/akalongman/sublimetext-autobackups", - "last_modified": "2013-06-04 02:17", + "last_modified": "2013-06-06 11:48", "platforms": { "*": [ { - "version": "1.2", - "url": "https://nodeload.github.com/akalongman/sublimetext-autobackups/zip/1.2" + "version": "1.3", + "url": "https://nodeload.github.com/akalongman/sublimetext-autobackups/zip/1.3" } ] }