fix case 1: file was gone

--HG--
branch : pmacs2
This commit is contained in:
moculus 2009-04-16 15:19:56 +00:00
parent 0b7060c3c9
commit 317955c5d8
2 changed files with 35 additions and 34 deletions

View File

@ -22,8 +22,9 @@ def hasher(data):
m = md5.new(data) m = md5.new(data)
return m return m
class ReadOnlyError(Exception): class ReadOnlyError(Exception): pass
pass class FileChangedError(Exception): pass
class FileGoneError(Exception): pass
# used for undo/redo stacks when text will need to be added back # used for undo/redo stacks when text will need to be added back
class AddMove(object): class AddMove(object):
@ -79,8 +80,6 @@ class Buffer(object):
win_c = len(self.win_re.findall(data)) win_c = len(self.win_re.findall(data))
if (unix_c and mac_c) or (unix_c and win_c) or (mac_c and win_c): if (unix_c and mac_c) or (unix_c and win_c) or (mac_c and win_c):
# warn the user? # warn the user?
#raise Exception, 'inconsistent line endings %r' % \
# (data, [unix_c, mac_c, win_c])
pass pass
if unix_c >= win_c and unix_c >= mac_c: if unix_c >= win_c and unix_c >= mac_c:
@ -94,19 +93,19 @@ class Buffer(object):
def _open_file_r(self, path): def _open_file_r(self, path):
path = os.path.realpath(path) path = os.path.realpath(path)
if not os.path.isfile(path): if not os.path.isfile(path):
raise Exception, "Path '%s' does not exist" % (path) raise Exception("Path '%s' does not exist" % (path))
if not os.access(path, os.R_OK): if not os.access(path, os.R_OK):
raise Exception, "Path '%s' cannot be read" % (path) raise Exception("Path '%s' cannot be read" % (path))
f = open(path, 'r') f = open(path, 'r')
return f return f
def _open_file_w(self, path): def _open_file_w(self, path):
if os.path.isfile(path): if os.path.isfile(path):
raise Exception, "Path '%s' already exists" % (path) raise Exception("Path '%s' already exists" % (path))
d = os.path.dirname(path) d = os.path.dirname(path)
if not os.access(d, os.R_OK): if not os.access(d, os.R_OK):
raise Exception, "Dir '%s' cannot be read" % (path) raise Exception("Dir '%s' cannot be read" % (path))
if not os.access(d, os.W_OK): if not os.access(d, os.W_OK):
raise Exception, "Dir '%s' cannot be written" % (path) raise Exception("Dir '%s' cannot be written" % (path))
f = open(path, 'w') f = open(path, 'w')
return f return f
def _temp_path(self, path): def _temp_path(self, path):
@ -132,7 +131,7 @@ class Buffer(object):
self.undo_stack.append(move) self.undo_stack.append(move)
self._stack_trim(self.undo_stack) self._stack_trim(self.undo_stack)
else: else:
raise Exception, "Invalid act: %d" % (act) raise Exception("Invalid act: %d" % (act))
def undo(self): def undo(self):
if len(self.undo_stack): if len(self.undo_stack):
undo_id = self.undo_stack[-1].undo_id undo_id = self.undo_stack[-1].undo_id
@ -143,7 +142,7 @@ class Buffer(object):
pos = move.getpos() pos = move.getpos()
return pos return pos
else: else:
raise Exception, "Nothing to Undo!" raise Exception("Nothing to Undo!")
def redo(self): def redo(self):
if len(self.redo_stack): if len(self.redo_stack):
undo_id = self.redo_stack[-1].undo_id undo_id = self.redo_stack[-1].undo_id
@ -154,7 +153,7 @@ class Buffer(object):
pos = move.getpos() pos = move.getpos()
return pos return pos
else: else:
raise Exception, "Nothing to Redo!" raise Exception("Nothing to Redo!")
# window-buffer communication # window-buffer communication
def add_window(self, w): def add_window(self, w):
@ -237,7 +236,7 @@ class Buffer(object):
def changed_on_disk(self): def changed_on_disk(self):
return False return False
def reload(self): def reload(self):
raise Exception, "%s reload: Unimplemented" % (self.name()) raise Exception("%s reload: Unimplemented" % (self.name()))
def save_as(self, path, force=False): def save_as(self, path, force=False):
# check to see if the path exists, and if we're prepared to overwrite it # check to see if the path exists, and if we're prepared to overwrite it
# if yes to both, get its mode so we can preserve the path's permissions # if yes to both, get its mode so we can preserve the path's permissions
@ -246,7 +245,8 @@ class Buffer(object):
if force: if force:
mode = os.stat(self.path)[0] mode = os.stat(self.path)[0]
else: else:
raise Exception, "oh no! %r already exists" % path # XYZ
raise Exception("oh no! %r already exists" % path)
# create the string that we're going to write into the file # create the string that we're going to write into the file
data = self.write_filter(self.make_string()) data = self.write_filter(self.make_string())
@ -296,7 +296,7 @@ class Buffer(object):
# buffer set # buffer set
def set_lines(self, lines, force=False): def set_lines(self, lines, force=False):
if not force and self.readonly(): if not force and self.readonly():
raise Exception, "set_data: buffer is readonly" raise Exception("set_data: buffer is readonly")
start = self.get_buffer_start() start = self.get_buffer_start()
end = self.get_buffer_end() end = self.get_buffer_end()
self.delete(start, end, force=force) self.delete(start, end, force=force)
@ -464,7 +464,7 @@ class InterpreterBuffer(Buffer):
def get_env(self): def get_env(self):
return {} return {}
def get_cmd(self): def get_cmd(self):
raise Exception, 'unimplemented' raise Exception('unimplemented')
def pipe_readline(self): def pipe_readline(self):
if self.pipe.poll() is not None: if self.pipe.poll() is not None:
raise InterpreterPipeError('broken pipe') raise InterpreterPipeError('broken pipe')
@ -520,7 +520,7 @@ class IperlBuffer(InterpreterBuffer):
if parent.path.endswith('.pm'): if parent.path.endswith('.pm'):
return '*%s:%s*' % (cls._basename, parent.name()) return '*%s:%s*' % (cls._basename, parent.name())
else: else:
raise Exception, "not a perl module" raise Exception("not a perl module")
else: else:
return '*%s*' % cls._basename return '*%s*' % cls._basename
create_name = classmethod(create_name) create_name = classmethod(create_name)
@ -581,21 +581,21 @@ class FileBuffer(Buffer):
path = os.path.realpath(path) path = os.path.realpath(path)
self.path = path self.path = path
if not os.path.isfile(path): if not os.path.isfile(path):
raise Exception, "Path '%s' does not exist" % (path) raise Exception("Path '%s' does not exist" % (path))
if not os.access(path, os.R_OK): if not os.access(path, os.R_OK):
raise Exception, "Path '%s' cannot be read" % (path) raise Exception("Path '%s' cannot be read" % (path))
f = open(path, 'r') f = open(path, 'r')
return f return f
def _open_file_w(self, path=None, preserve=True): def _open_file_w(self, path=None, preserve=True):
if path is None: if path is None:
path = self.path path = self.path
if preserve and os.path.isfile(path): if preserve and os.path.isfile(path):
raise Exception, "Path '%s' already exists" % (path) raise Exception("Path '%s' already exists" % (path))
d = os.path.dirname(path) d = os.path.dirname(path)
if not os.access(d, os.R_OK): if not os.access(d, os.R_OK):
raise Exception, "Dir '%s' cannot be read" % (path) raise Exception("Dir '%s' cannot be read" % (path))
if not os.access(d, os.W_OK): if not os.access(d, os.W_OK):
raise Exception, "Dir '%s' cannot be written" % (path) raise Exception("Dir '%s' cannot be written" % (path))
f = open(path, 'w') f = open(path, 'w')
return f return f
def _temp_path(self, path=None): def _temp_path(self, path=None):
@ -610,8 +610,6 @@ class FileBuffer(Buffer):
def path_exists(self): def path_exists(self):
return os.path.exists(self.path) return os.path.exists(self.path)
def store_checksum(self, data): def store_checksum(self, data):
#self.checksum = md5.new(data)
#self.checksum = hashlib.md5(data)
self.checksum = hasher(data) self.checksum = hasher(data)
def read(self): def read(self):
if self.path_exists(): if self.path_exists():
@ -626,7 +624,7 @@ class FileBuffer(Buffer):
data = '' data = ''
if data.startswith('\xEF\xBB\xBF'): if data.startswith('\xEF\xBB\xBF'):
# utf-8 # utf-8 bytemark
self.bytemark = data[:3] self.bytemark = data[:3]
data = data[3:] data = data[3:]
@ -635,8 +633,7 @@ class FileBuffer(Buffer):
data = data.replace("\t", " ") data = data.replace("\t", " ")
for i in range(0, min(len(data), 128)): for i in range(0, min(len(data), 128)):
if data[i] not in string.printable: if data[i] not in string.printable:
raise BinaryDataException, "binary files are not supported" raise BinaryDataException("binary files are not supported")
#FIXME: this is horrible...but maybe not as horrible as using tabs??
return data return data
def open(self): def open(self):
data = self.read() data = self.read()
@ -649,8 +646,6 @@ class FileBuffer(Buffer):
f = open(self.path) f = open(self.path)
data = f.read() data = f.read()
f.close() f.close()
#m = md5.new(data)
#m = hashlib.md5(data)
m = hasher(data) m = hasher(data)
return self.checksum.digest() != m.digest() return self.checksum.digest() != m.digest()
def save(self, force=False): def save(self, force=False):
@ -661,9 +656,9 @@ class FileBuffer(Buffer):
# the file already existed and we took a checksum so make sure it's # the file already existed and we took a checksum so make sure it's
# still the same right now # still the same right now
if not self.path_exists(): if not self.path_exists():
raise Exception, "oh no! %r disappeared!" % self.path raise FileGoneError("oh no! %r disappeared!" % self.path)
if self.changed_on_disk(): if self.changed_on_disk():
raise Exception, "oh no! %r has changed on-disk!" % self.path raise FileChangedError("oh no! %r has changed on-disk!" % self.path)
exists = os.path.exists(self.path) exists = os.path.exists(self.path)
if exists: if exists:

View File

@ -115,11 +115,17 @@ class SaveBufferAs(Method):
class SaveBuffer(Method): class SaveBuffer(Method):
'''Save the contents of a buffer''' '''Save the contents of a buffer'''
def _execute(self, w, **vargs): def _execute(self, w, **vargs):
if w.buffer.changed(): if not w.buffer.changed():
w.set_error("(No changes need to be saved)")
return
try:
w.buffer.save() w.buffer.save()
w.set_error("Wrote %s" % (w.buffer.path)) w.set_error("Wrote %s" % (w.buffer.path))
else: except buffer.FileGoneError, e:
w.set_error("(No changes need to be saved)") w.buffer.save(force=True)
w.set_error("File had disappeared! Wrote %s" % (w.buffer.path))
except buffer.FileChangedError, e:
pass
class ToggleTabs(Method): class ToggleTabs(Method):
'''Toggle whether to write tabs out or not (defaults to false)''' '''Toggle whether to write tabs out or not (defaults to false)'''