From 6fdb3bd78b6db51ccf53f44283c4ba61b57c5eb2 Mon Sep 17 00:00:00 2001 From: Abelarm Date: Mon, 2 Oct 2017 19:18:00 +0200 Subject: [PATCH 1/3] [add] feature for saving the backup files with a prefix or a suffix --- AutoBackups (Linux).sublime-settings | 6 ++++++ AutoBackups (OSX).sublime-settings | 6 ++++++ AutoBackups (Windows).sublime-settings | 6 ++++++ AutoBackups.py | 9 ++++++++- autobackups/paths_helper.py | 19 ++++++++++++++++--- 5 files changed, 42 insertions(+), 4 deletions(-) diff --git a/AutoBackups (Linux).sublime-settings b/AutoBackups (Linux).sublime-settings index d8d45ef..6d972a1 100644 --- a/AutoBackups (Linux).sublime-settings +++ b/AutoBackups (Linux).sublime-settings @@ -19,6 +19,12 @@ // to use this feature, you must have enabled backup_per_day setting "backup_per_time": "file", + // If, set will change the name of the file according to mode: + // prefix: "auto-save.myfile_095034.php" + // suffix: myfile_095034.auto-save.php + // if None or false won't change the name + "backup_name_mode":"prefix", + // 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 d8d45ef..6d972a1 100644 --- a/AutoBackups (OSX).sublime-settings +++ b/AutoBackups (OSX).sublime-settings @@ -19,6 +19,12 @@ // to use this feature, you must have enabled backup_per_day setting "backup_per_time": "file", + // If, set will change the name of the file according to mode: + // prefix: "auto-save.myfile_095034.php" + // suffix: myfile_095034.auto-save.php + // if None or false won't change the name + "backup_name_mode":"prefix", + // 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 dc313df..8ad0da9 100644 --- a/AutoBackups (Windows).sublime-settings +++ b/AutoBackups (Windows).sublime-settings @@ -19,6 +19,12 @@ // to use this feature, you must have enabled backup_per_day setting "backup_per_time": "file", + // If, set will change the name of the file according to mode: + // prefix: "auto-save.myfile_095034.php" + // suffix: myfile_095034.auto-save.php + // if None or false won't change the name + "backup_name_mode":"prefix", + // 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 353eda3..13d306e 100644 --- a/AutoBackups.py +++ b/AutoBackups.py @@ -60,9 +60,14 @@ def plugin_loaded(): backup_dir = settings.get('backup_dir') backup_per_day = settings.get('backup_per_day') backup_per_time = settings.get('backup_per_time') + backup_name_mode = settings.get('backup_name_mode') - PathsHelper.initialize(platform, backup_dir, backup_per_day, backup_per_time) + PathsHelper.initialize(platform, backup_dir, backup_per_day, backup_per_time, backup_name_mode) cprint('AutoBackups: Plugin Initialized') + cprint('With: backup_dir: {}\n\ + backup_per_day: {}\n\ + backup_per_time: {}\n\ + backup_name_mode: {}'.format(backup_dir, backup_per_day, backup_per_time, backup_name_mode)) sublime.set_timeout(gc, 10000) @@ -142,6 +147,8 @@ def save_backup(self, view, on_load_event): if newname == None: return + self.console(newname) + buffer_id = view.buffer_id() content = filename+view.substr(sublime.Region(0, view_size)) content = self.encode(content) diff --git a/autobackups/paths_helper.py b/autobackups/paths_helper.py index c07f4ba..8b5a68a 100644 --- a/autobackups/paths_helper.py +++ b/autobackups/paths_helper.py @@ -13,6 +13,9 @@ import sys import datetime + +backup_name_mode_text = 'auto-save' + class PathsHelper(object): platform = False backup_dir = False @@ -20,11 +23,12 @@ class PathsHelper(object): backup_per_time = False @staticmethod - def initialize(pl, backup_dir, backup_per_day, backup_per_time): + def initialize(pl, backup_dir, backup_per_day, backup_per_time, backup_name_mode): PathsHelper.platform = pl PathsHelper.backup_dir = backup_dir PathsHelper.backup_per_day = backup_per_day PathsHelper.backup_per_time = backup_per_time + PathsHelper.backup_name_mode = backup_name_mode @staticmethod @@ -60,9 +64,13 @@ def get_base_dir(only_base): return os.path.expanduser(backup_dir) @staticmethod - def timestamp_file(filename): + def create_name_file(filename): (filepart, extensionpart) = os.path.splitext(filename) + if PathsHelper.backup_name_mode not in (False, None, ) \ + and PathsHelper.backup_name_mode.lower() == 'prefix': + filepart = '%s.%s' % (backup_name_mode_text, filepart) + backup_per_day = PathsHelper.backup_per_day backup_per_time = PathsHelper.backup_per_time if (backup_per_day and backup_per_time == 'file'): @@ -71,6 +79,11 @@ def timestamp_file(filename): name = '%s_%s%s' % (filepart, time, extensionpart,) else: name = '%s%s' % (filepart, extensionpart,) + + (filepart, extensionpart) = os.path.splitext(name) + if PathsHelper.backup_name_mode not in (False, None, ) \ + and PathsHelper.backup_name_mode.lower() == 'suffix': + name = '%s.%s%s' % (filepart, backup_name_mode_text, extensionpart) return name @staticmethod @@ -109,5 +122,5 @@ def normalise_path(path, slashes = False): @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)) + return os.path.join(PathsHelper.get_backup_path(filepath), PathsHelper.create_name_file(filename)) From 38d4f1fe356dae24c5fd5b29832348868fa0c005 Mon Sep 17 00:00:00 2001 From: Abelarm Date: Mon, 2 Oct 2017 19:18:00 +0200 Subject: [PATCH 2/3] [add] feature for saving the backup files with a prefix or a suffix --- AutoBackups (Linux).sublime-settings | 6 ++++++ AutoBackups (OSX).sublime-settings | 6 ++++++ AutoBackups (Windows).sublime-settings | 6 ++++++ AutoBackups.py | 9 ++++++--- autobackups/paths_helper.py | 19 ++++++++++++++++--- 5 files changed, 40 insertions(+), 6 deletions(-) diff --git a/AutoBackups (Linux).sublime-settings b/AutoBackups (Linux).sublime-settings index d8d45ef..6d972a1 100644 --- a/AutoBackups (Linux).sublime-settings +++ b/AutoBackups (Linux).sublime-settings @@ -19,6 +19,12 @@ // to use this feature, you must have enabled backup_per_day setting "backup_per_time": "file", + // If, set will change the name of the file according to mode: + // prefix: "auto-save.myfile_095034.php" + // suffix: myfile_095034.auto-save.php + // if None or false won't change the name + "backup_name_mode":"prefix", + // 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 d8d45ef..6d972a1 100644 --- a/AutoBackups (OSX).sublime-settings +++ b/AutoBackups (OSX).sublime-settings @@ -19,6 +19,12 @@ // to use this feature, you must have enabled backup_per_day setting "backup_per_time": "file", + // If, set will change the name of the file according to mode: + // prefix: "auto-save.myfile_095034.php" + // suffix: myfile_095034.auto-save.php + // if None or false won't change the name + "backup_name_mode":"prefix", + // 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 dc313df..8ad0da9 100644 --- a/AutoBackups (Windows).sublime-settings +++ b/AutoBackups (Windows).sublime-settings @@ -19,6 +19,12 @@ // to use this feature, you must have enabled backup_per_day setting "backup_per_time": "file", + // If, set will change the name of the file according to mode: + // prefix: "auto-save.myfile_095034.php" + // suffix: myfile_095034.auto-save.php + // if None or false won't change the name + "backup_name_mode":"prefix", + // 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 353eda3..c10918e 100644 --- a/AutoBackups.py +++ b/AutoBackups.py @@ -60,9 +60,14 @@ def plugin_loaded(): backup_dir = settings.get('backup_dir') backup_per_day = settings.get('backup_per_day') backup_per_time = settings.get('backup_per_time') + backup_name_mode = settings.get('backup_name_mode') - PathsHelper.initialize(platform, backup_dir, backup_per_day, backup_per_time) + PathsHelper.initialize(platform, backup_dir, backup_per_day, backup_per_time, backup_name_mode) cprint('AutoBackups: Plugin Initialized') + cprint('With: backup_dir: {}\n\ + backup_per_day: {}\n\ + backup_per_time: {}\n\ + backup_name_mode: {}'.format(backup_dir, backup_per_day, backup_per_time, backup_name_mode)) sublime.set_timeout(gc, 10000) @@ -131,8 +136,6 @@ def save_backup(self, view, on_load_event): #cprint("AutoBackups: " + filename + " is excluded"); return - - # not create file backup if current file is backup if on_load_event & self.is_backup_file(filename): return diff --git a/autobackups/paths_helper.py b/autobackups/paths_helper.py index c07f4ba..8b5a68a 100644 --- a/autobackups/paths_helper.py +++ b/autobackups/paths_helper.py @@ -13,6 +13,9 @@ import sys import datetime + +backup_name_mode_text = 'auto-save' + class PathsHelper(object): platform = False backup_dir = False @@ -20,11 +23,12 @@ class PathsHelper(object): backup_per_time = False @staticmethod - def initialize(pl, backup_dir, backup_per_day, backup_per_time): + def initialize(pl, backup_dir, backup_per_day, backup_per_time, backup_name_mode): PathsHelper.platform = pl PathsHelper.backup_dir = backup_dir PathsHelper.backup_per_day = backup_per_day PathsHelper.backup_per_time = backup_per_time + PathsHelper.backup_name_mode = backup_name_mode @staticmethod @@ -60,9 +64,13 @@ def get_base_dir(only_base): return os.path.expanduser(backup_dir) @staticmethod - def timestamp_file(filename): + def create_name_file(filename): (filepart, extensionpart) = os.path.splitext(filename) + if PathsHelper.backup_name_mode not in (False, None, ) \ + and PathsHelper.backup_name_mode.lower() == 'prefix': + filepart = '%s.%s' % (backup_name_mode_text, filepart) + backup_per_day = PathsHelper.backup_per_day backup_per_time = PathsHelper.backup_per_time if (backup_per_day and backup_per_time == 'file'): @@ -71,6 +79,11 @@ def timestamp_file(filename): name = '%s_%s%s' % (filepart, time, extensionpart,) else: name = '%s%s' % (filepart, extensionpart,) + + (filepart, extensionpart) = os.path.splitext(name) + if PathsHelper.backup_name_mode not in (False, None, ) \ + and PathsHelper.backup_name_mode.lower() == 'suffix': + name = '%s.%s%s' % (filepart, backup_name_mode_text, extensionpart) return name @staticmethod @@ -109,5 +122,5 @@ def normalise_path(path, slashes = False): @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)) + return os.path.join(PathsHelper.get_backup_path(filepath), PathsHelper.create_name_file(filename)) From 1819f83319a90f83135123e8ebda067da92987cd Mon Sep 17 00:00:00 2001 From: Abelarm Date: Mon, 2 Oct 2017 22:58:59 +0200 Subject: [PATCH 3/3] [fix] default false for backup_name_mode --- autobackups/paths_helper.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/autobackups/paths_helper.py b/autobackups/paths_helper.py index 8b5a68a..8f02bf8 100644 --- a/autobackups/paths_helper.py +++ b/autobackups/paths_helper.py @@ -21,6 +21,8 @@ class PathsHelper(object): backup_dir = False backup_per_day = False backup_per_time = False + backup_name_mode = False + @staticmethod def initialize(pl, backup_dir, backup_per_day, backup_per_time, backup_name_mode):