Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#30 Added feature for "Add setting to modify the prefix or suffix of the backup files" #36

Merged
merged 4 commits into from
Oct 2, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions AutoBackups (Linux).sublime-settings
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
6 changes: 6 additions & 0 deletions AutoBackups (OSX).sublime-settings
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
6 changes: 6 additions & 0 deletions AutoBackups (Windows).sublime-settings
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
11 changes: 8 additions & 3 deletions AutoBackups.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)


Expand Down Expand Up @@ -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
Expand All @@ -142,6 +145,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)
Expand Down
21 changes: 18 additions & 3 deletions autobackups/paths_helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,18 +13,24 @@
import sys
import datetime


backup_name_mode_text = 'auto-save'

class PathsHelper(object):
platform = False
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):
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
Expand Down Expand Up @@ -60,9 +66,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'):
Expand All @@ -71,6 +81,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
Expand Down Expand Up @@ -109,5 +124,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))