-
Notifications
You must be signed in to change notification settings - Fork 15
/
AutomaticBackups.py
117 lines (88 loc) · 3.69 KB
/
AutomaticBackups.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
# Sublime Text event listeners and commands interface for automatic backups.
import sublime
import sublime_plugin
import os
import shutil
from .backup_paths import get_backup_filepath
from .backups_navigator import BackupsNavigator
from .settings import get_settings
nav = BackupsNavigator() # our backup navigator state manager
class AutomaticBackupsEventListener(sublime_plugin.EventListener):
"""Creates an automatic backup of every file you save. This
gives you a rudimentary mechanism for making sure you don't lose
information while working."""
def on_post_save(self, view):
"""When a file is saved, put a copy of the file into the
backup directory."""
self.save_view_to_backup(view)
def on_activated(self, view):
"""Reinit backups navigator on view activation, just in case
file was modified outside of ST."""
if view.file_name() != nav.current_file:
nav.reinit()
def on_load(self, view):
"""When a file is opened, put a copy of the file into the
backup directory if backup_on_open_file setting is true."""
settings = get_settings()
if settings.get('backup_on_open_file', False):
self.save_view_to_backup(view)
def save_view_to_backup(self, view):
"""When a file is opened, put a copy of the file into the
backup directory."""
settings = get_settings()
# don't save files above configured size
if view.size() > settings.get("max_backup_file_size_bytes", 131072):
print('Backup not saved, file too large (%d bytes)' % view.size())
return
filename = view.file_name()
newname = get_backup_filepath(filename)
if newname == None:
return
(backup_dir, file_to_write) = os.path.split(newname)
# make sure that we have a directory to write into
if os.access(backup_dir, os.F_OK) == False:
os.makedirs(backup_dir)
shutil.copy(filename, newname)
print('Backup saved to:', newname)
nav.reinit()
class AutomaticBackupsCommand(sublime_plugin.TextCommand):
"""Wires up received commands from keybindings to our BackupsNavigator
instance."""
def is_enabled(self, **kwargs):
return self.view.file_name() is not None
def run(self, edit, **kwargs):
settings = get_settings()
command = kwargs['command']
if command in ('jump', 'step'):
forward = kwargs['forward']
if nav.index is None:
nav.find_backups(self.view)
if command == 'step': # move 1 step forward or backward in history
if forward:
nav.nav_forwards()
else:
nav.nav_backwards()
elif command == 'jump': # jump to beginning/end of history
if forward:
nav.nav_end()
else:
nav.nav_start()
if not nav.found_backup_files:
sublime.status_message('No automatic backups found of this file')
return
if nav.at_last_backup():
if command == 'merge':
sublime.error_message('You are viewing the current version of this file. Navigate to a backup version before merging.')
return
if nav.just_reverted:
sublime.status_message('Showing current version')
else:
nav.revert(self.view)
return
nav.backup = nav.found_backup_files[nav.index]
nav.backup_full_path = os.path.join(nav.backup_path,
nav.backup)
if command == 'merge':
nav.merge(self.view)
else:
nav.load_backup_to_view(self.view, edit)