From 3b3f4b7cadc48c40e54cc8d91d71153f8387668d Mon Sep 17 00:00:00 2001 From: LONGMANi Date: Tue, 4 Jun 2013 02:19:52 +0400 Subject: [PATCH] Added setting backup_per_day --- AutoBackups (Linux).sublime-settings | 21 ++--- AutoBackups (OSX).sublime-settings | 21 ++--- AutoBackups (Windows).sublime-settings | 21 ++--- AutoBackups.py | 2 +- autobackups/paths_helper.py | 104 +++++++++++++------------ autobackups/win_helper.py | 28 ------- packages.json | 6 +- 7 files changed, 94 insertions(+), 109 deletions(-) delete mode 100644 autobackups/win_helper.py diff --git a/AutoBackups (Linux).sublime-settings b/AutoBackups (Linux).sublime-settings index 0d1c636..b22c55e 100644 --- a/AutoBackups (Linux).sublime-settings +++ b/AutoBackups (Linux).sublime-settings @@ -1,15 +1,18 @@ { - // Don't make changes to this file directly as they can get wiped out when the - // plugin is updated. Instead transfer what you need to the 'Settings - User' file. + // Don't make changes to this file directly as they can get wiped out when the + // plugin is updated. Instead transfer what you need to the 'Settings - User' file. - // The directory where we'll keep our backups. If empty, we'll try to put them in - // ~/sublime_backups - "backup_dir": "~/sublime_backups", + // The directory where we'll keep our backups. If empty, we'll try to put them in + // ~/sublime_backups + "backup_dir": "~/sublime_backups", - // If true, also save a backup copy any time a file is opened (if backup file not exists) - "backup_on_open_file": true, + // If true, also save a backup copy any time a file is opened (if backup file not exists) + "backup_on_open_file": true, - // Files larger than this many bytes won't be backed up. - "max_backup_file_size_bytes": 262144 // = 256 KB + // If true, backups saved per day, in separate folders, for example ~/sublime_backups/2013-05-23 + "backup_per_day": false, + + // Files larger than this many bytes won't be backed up. + "max_backup_file_size_bytes": 262144 // = 256 KB } diff --git a/AutoBackups (OSX).sublime-settings b/AutoBackups (OSX).sublime-settings index 0d1c636..b22c55e 100644 --- a/AutoBackups (OSX).sublime-settings +++ b/AutoBackups (OSX).sublime-settings @@ -1,15 +1,18 @@ { - // Don't make changes to this file directly as they can get wiped out when the - // plugin is updated. Instead transfer what you need to the 'Settings - User' file. + // Don't make changes to this file directly as they can get wiped out when the + // plugin is updated. Instead transfer what you need to the 'Settings - User' file. - // The directory where we'll keep our backups. If empty, we'll try to put them in - // ~/sublime_backups - "backup_dir": "~/sublime_backups", + // The directory where we'll keep our backups. If empty, we'll try to put them in + // ~/sublime_backups + "backup_dir": "~/sublime_backups", - // If true, also save a backup copy any time a file is opened (if backup file not exists) - "backup_on_open_file": true, + // If true, also save a backup copy any time a file is opened (if backup file not exists) + "backup_on_open_file": true, - // Files larger than this many bytes won't be backed up. - "max_backup_file_size_bytes": 262144 // = 256 KB + // If true, backups saved per day, in separate folders, for example ~/sublime_backups/2013-05-23 + "backup_per_day": false, + + // Files larger than this many bytes won't be backed up. + "max_backup_file_size_bytes": 262144 // = 256 KB } diff --git a/AutoBackups (Windows).sublime-settings b/AutoBackups (Windows).sublime-settings index 94a1a07..34b02c8 100644 --- a/AutoBackups (Windows).sublime-settings +++ b/AutoBackups (Windows).sublime-settings @@ -1,15 +1,18 @@ { - // Don't make changes to this file directly as they can get wiped out when the - // plugin is updated. Instead transfer what you need to the 'Settings - User' file. + // Don't make changes to this file directly as they can get wiped out when the + // plugin is updated. Instead transfer what you need to the 'Settings - User' file. - // The directory where we'll keep our backups. If empty, we'll try to put them in - // D:/Sublime Text Backups - "backup_dir": "D:/Sublime Text Backups", + // The directory where we'll keep our backups. If empty, we'll try to put them in + // D:/Sublime Text Backups + "backup_dir": "D:/Sublime Text Backups", - // If true, also save a backup copy any time a file is opened (if backup file not exists) - "backup_on_open_file": true, + // If true, also save a backup copy any time a file is opened (if backup file not exists) + "backup_on_open_file": true, - // Files larger than this many bytes won't be backed up. - "max_backup_file_size_bytes": 262144 // = 256 KB + // If true, backups saved per day, in separate folders, for example D:/Sublime Text Backups/2013-05-23 + "backup_per_day": false, + + // Files larger than this many bytes won't be backed up. + "max_backup_file_size_bytes": 262144 // = 256 KB } diff --git a/AutoBackups.py b/AutoBackups.py index e07089d..d8657c4 100644 --- a/AutoBackups.py +++ b/AutoBackups.py @@ -70,7 +70,7 @@ def save_backup(self, view, on_load_event): newname = PathsHelper.get_backup_filepath(filename) if newname == None: return - + print(newname) # not create file backup if current file is backup if on_load_event & self.is_backup_file(filename): diff --git a/autobackups/paths_helper.py b/autobackups/paths_helper.py index ca96228..c247cf6 100644 --- a/autobackups/paths_helper.py +++ b/autobackups/paths_helper.py @@ -4,72 +4,76 @@ import os import re import sys - -if sublime.platform() == 'windows': - try: - from AutoBackups.autobackups.win_helper import WinHelper - except (ImportError): - from win_helper import WinHelper +import datetime class PathsHelper(object): - @staticmethod - def get_base_dir(): - platform = sublime.platform().title() - settings = sublime.load_settings('AutoBackups ('+platform+').sublime-settings') - # Configured setting - backup_dir = settings.get('backup_dir') + @staticmethod + def get_base_dir(): + platform = sublime.platform().title() + settings = sublime.load_settings('AutoBackups ('+platform+').sublime-settings') + # Configured setting + backup_dir = settings.get('backup_dir') + now_date = datetime.datetime.now() + date = str(now_date)[:10] + + backup_per_day = settings.get('backup_per_day') + if (backup_per_day): + backup_dir = backup_dir +'/'+ date + + + if backup_dir != '': + return os.path.expanduser(backup_dir) + # Windows: /My Documents/Sublime Text Backups + if (sublime.platform() == 'windows'): + backup_dir = 'D:/Sublime Text Backups' + if (backup_per_day): + backup_dir = backup_dir +'/'+ date + return backup_dir - if backup_dir != '': - return os.path.expanduser(backup_dir) + # Linux/OSX/other: ~/sublime_backups + backup_dir = '~/.sublime/backups' + if (backup_per_day): + backup_dir = backup_dir +'/'+ date + return os.path.expanduser(backup_dir) - # Windows: /My Documents/Sublime Text Backups - if sublime.platform() == 'windows': - return os.path.join( - WinHelper.get_shell_folder('Personal'), - 'Sublime Text Backups') + @staticmethod + def timestamp_file(filename): + (filepart, extensionpart) = os.path.splitext(filename) + return '%s%s' % (filepart, extensionpart,) - # Linux/OSX/other: ~/sublime_backups - return os.path.expanduser('~/.sublime/backups') + @staticmethod + def get_backup_path(filepath): + path = os.path.expanduser(os.path.split(filepath)[0]) + backup_base = PathsHelper.get_base_dir() + path = PathsHelper.normalise_path(path) + return os.path.join(backup_base, path) - @staticmethod - def timestamp_file(filename): + @staticmethod + def normalise_path(path): - (filepart, extensionpart) = os.path.splitext(filename) - return '%s%s' % ( - filepart, - extensionpart, - ) + if sublime.platform() != 'windows': + # remove any leading / before combining with backup_base + path = re.sub(r'^/', '', path) + return path - @staticmethod - def get_backup_path(filepath): - path = os.path.expanduser(os.path.split(filepath)[0]) - backup_base = PathsHelper.get_base_dir() - path = PathsHelper.normalise_path(path) - return os.path.join(backup_base, path) + path = path.replace('/', '\\') - @staticmethod - def normalise_path(path): - if sublime.platform() != 'windows': - # remove any leading / before combining with backup_base - path = re.sub(r'^/', '', path) - return path + # windows only: transform C: into just C + path = re.sub(r'^(\w):', r'\1', path) - path = path.replace('/', '\\') + # windows only: transform \\remotebox\share into network\remotebox\share + path = re.sub(r'^\\\\([\w\-]{2,})', r'network\\\1', path) + return path - # windows only: transform C: into just C - path = re.sub(r'^(\w):', r'\1', path) - # windows only: transform \\remotebox\share into network\remotebox\share - path = re.sub(r'^\\\\([\w\-]{2,})', r'network\\\1', path) - return path - @staticmethod - def get_backup_filepath(filepath): + @staticmethod + def get_backup_filepath(filepath): - filename = os.path.split(filepath)[1] + filename = os.path.split(filepath)[1] - return os.path.join(PathsHelper.get_backup_path(filepath), PathsHelper.timestamp_file(filename)) + return os.path.join(PathsHelper.get_backup_path(filepath), PathsHelper.timestamp_file(filename)) diff --git a/autobackups/win_helper.py b/autobackups/win_helper.py deleted file mode 100644 index 37a11e6..0000000 --- a/autobackups/win_helper.py +++ /dev/null @@ -1,28 +0,0 @@ -import os -import re -import sublime - - -if sublime.platform() == 'windows': - try: - import winreg - except ImportError: - import _winreg - -class WinHelper(object): - @staticmethod - def _substenv(m): - return os.environ.get(m.group(1), m.group(0)) - - @staticmethod - def get_shell_folder(name): - HKCU = winreg.HKEY_CURRENT_USER - USER_SHELL_FOLDERS = \ - r'Software\Microsoft\Windows\CurrentVersion\Explorer\User Shell Folders' - key = winreg.OpenKey(HKCU, USER_SHELL_FOLDERS) - ret = winreg.QueryValueEx(key, name) - key.Close() - if ret[1] == winreg.REG_EXPAND_SZ and '%' in ret[0]: - return re.compile(r'%([^|<>=^%]+)%').sub(_substenv, ret[0]) - else: - return ret[0] diff --git a/packages.json b/packages.json index 27cd21b..4263cfd 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-05-13 13:44", + "last_modified": "2013-06-04 02:17", "platforms": { "*": [ { - "version": "1.1", - "url": "https://nodeload.github.com/akalongman/sublimetext-autobackups/zip/1.1" + "version": "1.2", + "url": "https://nodeload.github.com/akalongman/sublimetext-autobackups/zip/1.2" } ] }