From 317955c5d8f0bb9059a76eba0861c5e4391a2f54 Mon Sep 17 00:00:00 2001 From: moculus Date: Thu, 16 Apr 2009 15:19:56 +0000 Subject: [PATCH] fix case 1: file was gone --HG-- branch : pmacs2 --- buffer/__init__.py | 57 +++++++++++++++++++++------------------------- method/buffers.py | 12 +++++++--- 2 files changed, 35 insertions(+), 34 deletions(-) diff --git a/buffer/__init__.py b/buffer/__init__.py index b7bc53a..3aa3e2b 100644 --- a/buffer/__init__.py +++ b/buffer/__init__.py @@ -22,8 +22,9 @@ def hasher(data): m = md5.new(data) return m -class ReadOnlyError(Exception): - pass +class ReadOnlyError(Exception): pass +class FileChangedError(Exception): pass +class FileGoneError(Exception): pass # used for undo/redo stacks when text will need to be added back class AddMove(object): @@ -79,8 +80,6 @@ class Buffer(object): 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): # warn the user? - #raise Exception, 'inconsistent line endings %r' % \ - # (data, [unix_c, mac_c, win_c]) pass if unix_c >= win_c and unix_c >= mac_c: @@ -94,19 +93,19 @@ class Buffer(object): def _open_file_r(self, path): path = os.path.realpath(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): - raise Exception, "Path '%s' cannot be read" % (path) + raise Exception("Path '%s' cannot be read" % (path)) f = open(path, 'r') return f def _open_file_w(self, 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) 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): - raise Exception, "Dir '%s' cannot be written" % (path) + raise Exception("Dir '%s' cannot be written" % (path)) f = open(path, 'w') return f def _temp_path(self, path): @@ -132,7 +131,7 @@ class Buffer(object): self.undo_stack.append(move) self._stack_trim(self.undo_stack) else: - raise Exception, "Invalid act: %d" % (act) + raise Exception("Invalid act: %d" % (act)) def undo(self): if len(self.undo_stack): undo_id = self.undo_stack[-1].undo_id @@ -143,7 +142,7 @@ class Buffer(object): pos = move.getpos() return pos else: - raise Exception, "Nothing to Undo!" + raise Exception("Nothing to Undo!") def redo(self): if len(self.redo_stack): undo_id = self.redo_stack[-1].undo_id @@ -154,7 +153,7 @@ class Buffer(object): pos = move.getpos() return pos else: - raise Exception, "Nothing to Redo!" + raise Exception("Nothing to Redo!") # window-buffer communication def add_window(self, w): @@ -237,7 +236,7 @@ class Buffer(object): def changed_on_disk(self): return False def reload(self): - raise Exception, "%s reload: Unimplemented" % (self.name()) + raise Exception("%s reload: Unimplemented" % (self.name())) def save_as(self, path, force=False): # 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 @@ -246,7 +245,8 @@ class Buffer(object): if force: mode = os.stat(self.path)[0] 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 data = self.write_filter(self.make_string()) @@ -296,7 +296,7 @@ class Buffer(object): # buffer set def set_lines(self, lines, force=False): 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() end = self.get_buffer_end() self.delete(start, end, force=force) @@ -464,7 +464,7 @@ class InterpreterBuffer(Buffer): def get_env(self): return {} def get_cmd(self): - raise Exception, 'unimplemented' + raise Exception('unimplemented') def pipe_readline(self): if self.pipe.poll() is not None: raise InterpreterPipeError('broken pipe') @@ -520,7 +520,7 @@ class IperlBuffer(InterpreterBuffer): if parent.path.endswith('.pm'): return '*%s:%s*' % (cls._basename, parent.name()) else: - raise Exception, "not a perl module" + raise Exception("not a perl module") else: return '*%s*' % cls._basename create_name = classmethod(create_name) @@ -581,21 +581,21 @@ class FileBuffer(Buffer): path = os.path.realpath(path) self.path = 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): - raise Exception, "Path '%s' cannot be read" % (path) + raise Exception("Path '%s' cannot be read" % (path)) f = open(path, 'r') return f def _open_file_w(self, path=None, preserve=True): if path is None: path = self.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) 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): - raise Exception, "Dir '%s' cannot be written" % (path) + raise Exception("Dir '%s' cannot be written" % (path)) f = open(path, 'w') return f def _temp_path(self, path=None): @@ -610,8 +610,6 @@ class FileBuffer(Buffer): def path_exists(self): return os.path.exists(self.path) def store_checksum(self, data): - #self.checksum = md5.new(data) - #self.checksum = hashlib.md5(data) self.checksum = hasher(data) def read(self): if self.path_exists(): @@ -626,7 +624,7 @@ class FileBuffer(Buffer): data = '' if data.startswith('\xEF\xBB\xBF'): - # utf-8 + # utf-8 bytemark self.bytemark = data[:3] data = data[3:] @@ -635,8 +633,7 @@ class FileBuffer(Buffer): data = data.replace("\t", " ") for i in range(0, min(len(data), 128)): if data[i] not in string.printable: - raise BinaryDataException, "binary files are not supported" - #FIXME: this is horrible...but maybe not as horrible as using tabs?? + raise BinaryDataException("binary files are not supported") return data def open(self): data = self.read() @@ -649,8 +646,6 @@ class FileBuffer(Buffer): f = open(self.path) data = f.read() f.close() - #m = md5.new(data) - #m = hashlib.md5(data) m = hasher(data) return self.checksum.digest() != m.digest() 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 # still the same right now 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(): - 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) if exists: diff --git a/method/buffers.py b/method/buffers.py index b99e1cf..d34886c 100644 --- a/method/buffers.py +++ b/method/buffers.py @@ -115,11 +115,17 @@ class SaveBufferAs(Method): class SaveBuffer(Method): '''Save the contents of a buffer''' 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.set_error("Wrote %s" % (w.buffer.path)) - else: - w.set_error("(No changes need to be saved)") + except buffer.FileGoneError, e: + w.buffer.save(force=True) + w.set_error("File had disappeared! Wrote %s" % (w.buffer.path)) + except buffer.FileChangedError, e: + pass class ToggleTabs(Method): '''Toggle whether to write tabs out or not (defaults to false)'''