Skip to content

Commit

Permalink
Fixed bug: open backuped files listing
Browse files Browse the repository at this point in the history
  • Loading branch information
akalongman committed Jun 6, 2013
1 parent 3b3f4b7 commit 594426a
Show file tree
Hide file tree
Showing 5 changed files with 64 additions and 20 deletions.
56 changes: 49 additions & 7 deletions AutoBackups.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import sys
import os
import shutil
import re

st_version = 2
if sublime.version() == '' or int(sublime.version()) > 3000:
Expand Down Expand Up @@ -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]
Expand All @@ -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!')
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
18 changes: 10 additions & 8 deletions autobackups/paths_helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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


Expand All @@ -28,13 +28,13 @@ def get_base_dir():
# Windows: <user folder>/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)

Expand All @@ -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
Expand All @@ -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))

2 changes: 1 addition & 1 deletion package-metadata.json
Original file line number Diff line number Diff line change
@@ -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)"}
{"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)"}
6 changes: 3 additions & 3 deletions packages.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
]
}
Expand Down

0 comments on commit 594426a

Please sign in to comment.