Author: esr
Date: Mon Oct 13 14:39:08 2008
New Revision: 30112

URL: http://svn.gna.org/viewcvs/wesnoth?rev=30112&view=rev
Log:
trackplacer: handle file read errors.

Modified:
    trunk/data/tools/trackplacer

Modified: trunk/data/tools/trackplacer
URL: 
http://svn.gna.org/viewcvs/wesnoth/trunk/data/tools/trackplacer?rev=30112&r1=30111&r2=30112&view=diff
==============================================================================
--- trunk/data/tools/trackplacer (original)
+++ trunk/data/tools/trackplacer Mon Oct 13 14:39:08 2008
@@ -41,8 +41,7 @@
 # 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 @@
 
 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 @@
         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 @@
 
     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 @@
         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()


_______________________________________________
Wesnoth-commits mailing list
[email protected]
https://mail.gna.org/listinfo/wesnoth-commits

Reply via email to