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
|
# -*- coding: utf-8 -*-
# -----------------------------------------------------------------------------
# PyRoom - A clone of WriteRoom
# Copyright (c) 2007 Nicolas P. Rougier & NoWhereMan
# Copyright (c) 2008 The Pyroom Team - See AUTHORS file for more information
#
# This program is free software: you can redistribute it and/or modify it under
# the terms of the GNU General Public License as published by the Free Software
# Foundation, either version 3 of the License, or (at your option) any later
# version.
#
# This program is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
# FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
# details.
#
# You should have received a copy of the GNU General Public License along with
# this program. If not, see <http://www.gnu.org/licenses/>.
# -----------------------------------------------------------------------------
"""
provide autosave functions
"""
import gobject
from pyroom_error import PyroomError
import os
from globals import config
def start_autosave(edit_instance):
"""start the autosave timer"""
timeout_id = gobject.timeout_add(1000, autosave_timeout, edit_instance)
edit_instance.autosave_timeout_id = timeout_id
edit_instance.autosave_elapsed = 0
def stop_autosave(edit_instance):
"""stop the autosave timer and remove backup files"""
for buf in edit_instance.buffers:
autosave_fn = get_autosave_filename(buf.filename)
if not buf.filename == edit_instance.UNNAMED_FILENAME and \
os.path.isfile(autosave_fn):
os.remove(autosave_fn)
gobject.source_remove(edit_instance.autosave_timeout_id)
def autosave_timeout(edit_instance):
"""see if we have to autosave open files"""
if config.getint('editor', 'autosavetime'):
if edit_instance.autosave_elapsed >= \
config.getint('editor', 'autosavetime') * 60:
autosave(edit_instance)
edit_instance.autosave_elapsed = 0
else:
edit_instance.autosave_elapsed += 1
return True
def get_autosave_filename(filename):
"""get the filename autosave would happen to"""
SUFFIX = ".pyroom-autosave"
autosave_filename = os.path.join(
os.path.dirname(filename),
".%s%s" % (os.path.basename(filename), SUFFIX)
)
return autosave_filename
def autosave(edit_instance):
"""save all open files that have been saved before"""
for buf in edit_instance.buffers:
if not buf.filename == edit_instance.UNNAMED_FILENAME:
backup_file = open(
get_autosave_filename(buf.filename),
'w'
)
try:
try:
backup_file.write(
buf.get_text(buf.get_start_iter(), buf.get_end_iter())
)
except IOError:
raise PyroomError(_("Could not autosave file %s") %
buf.filename)
finally:
backup_file.close()
|