trackplacer: handle file read errors.

This commit is contained in:
Eric S. Raymond 2008-10-13 12:39:08 +00:00
parent 79f71984fa
commit 9aedec1cee

View file

@ -41,8 +41,7 @@ The Help button displays this message.
# TODO:
# 1. Test reading and writing of track files
# 2. Write track-to-macro tool.
# 3. Reject .cfg files selected for read.
# 4. Implement save-handler stub.
# 3. Implement save-handler stub.
import sys, os, exceptions, getopt
@ -75,7 +74,7 @@ vision_distance = 12
class ReadException(exceptions.Exception):
"Exception thrown while reading a track file."
def __init__(self, message, filename, lineno):
def __init__(self, message, filename, lineno=None):
self.message = message
self.filename = filename
self.lineno = lineno
@ -97,32 +96,32 @@ class JourneyTrack:
if type(fp) == type(""):
try:
fp = open(fp)
except FileError:
raise ReadException("cannot read", fp)
except IOError:
raise ReadException("Cannot read file.", fp)
if self.track:
raise ReadException("reading with track nonempty", fp.name, 1)
raise ReadException("Reading with track nonempty.", fp.name)
if fp.name.endswith(".png") or fp.name.endswith(".jpg"):
self.filename = fp.name
return
if not fp.name.endswith(".trk"):
raise ReadException("cannot read this filetype", fp.name, 0)
raise ReadException("Cannot read this filetype.", fp.name)
header = fp.readline().split()
if header[0] != 'FILE':
raise ReadException("missing FILE element", fp.name, 1)
raise ReadException("Missing FILE element.", fp.name, 1)
else:
self.filename = header[1]
while (i, line) in enumerate(fp):
fields = line.split()
if len(fields) != 3:
raise ReadException("ill-formed line", fp.name, i+1)
raise ReadException("Ill-formed track file line.", fp.name, i+1)
(tag, x, y) = fields
if tag not in ("JOURNEY", "BATTLE", "REST"):
raise ReadException("invalid tag field", fp.name, i+1)
raise ReadException("Invalid tag field on track file line.", fp.name, i+1)
try:
x = int(x)
y = int(y)
except ValuError:
raise ReadException("invalid coordinate field", fp.name, i+1)
raise ReadException("Invalid coordinate field.", fp.name, i+1)
self.initial_track.append((tag, x, y))
self.track = self.initial_track
def has_unsaved_changes(self):
@ -442,7 +441,7 @@ class TrackEditor:
def help_handler(self, w):
"Display help."
w = gtk.MessageDialog(type=gtk.MESSAGE_ERROR,
w = gtk.MessageDialog(type=gtk.MESSAGE_INFO,
flags=gtk.DIALOG_DESTROY_WITH_PARENT,
buttons=gtk.BUTTONS_OK)
w.set_markup(gui_help)
@ -483,8 +482,22 @@ if __name__ == "__main__":
TrackEditor(filename=arguments[0], verbose=verbose)
else:
while True:
selector = ModalFileSelector(default=default_map,
legend="Track or map file to read")
if not selector.filename:
break
TrackEditor(selector.filename, verbose=verbose)
try:
selector = ModalFileSelector(default=default_map,
legend="Track or map file to read")
if not selector.filename:
break
TrackEditor(selector.filename, verbose=verbose)
except ReadException, e:
w = gtk.MessageDialog(type=gtk.MESSAGE_ERROR,
flags=gtk.DIALOG_DESTROY_WITH_PARENT,
buttons=gtk.BUTTONS_OK)
if e.lineno:
errloc = '"%s", line %d:' % (e.filename, e.lineno)
# Emacs friendliness
sys.stderr.write(errloc + " " + e.message + "\n")
else:
errloc = e.filename + ":"
w.set_markup(errloc + "\n\n" + e.message)
w.run()
w.destroy()