Skip to content

Commit

Permalink
Merge pull request #36 from Abelarm/master
Browse files Browse the repository at this point in the history
#30 Added feature for "Add setting to modify the prefix or suffix of the backup files"
  • Loading branch information
akalongman authored Oct 2, 2017
2 parents f57b7a2 + 1819f83 commit 63dbaca
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 6 deletions.
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))

0 comments on commit 63dbaca

Please sign in to comment.