Browse code

Add error dialogs when a file cannot be parsed.

Anyone know how to set the window title?

From: Marius Gedminas <marius@gedmin.as>

Jose.R.Fonseca authored on 13/07/2008 03:22:33
Showing 1 changed files

  • xdot.py index 5a34658..be23387 100755
... ...
@@ -562,6 +562,10 @@ class XDotAttrParser:
562 562
         return self.parser.transform(x, y)
563 563
 
564 564
 
565
+class GraphParseError(Exception):
566
+    pass
567
+
568
+
565 569
 class XDotParser:
566 570
 
567 571
     def __init__(self, xdotcode):
... ...
@@ -571,7 +575,7 @@ class XDotParser:
571 575
         graph = pydot.graph_from_dot_data(self.xdotcode)
572 576
 
573 577
         if graph.bb is None:
574
-            return Graph()
578
+            return GraphParseError()
575 579
 
576 580
         xmin, ymin, xmax, ymax = map(int, graph.bb.split(","))
577 581
 
... ...
@@ -876,7 +880,7 @@ class DotWidget(gtk.DrawingArea):
876 880
         self.presstime = None
877 881
         self.highlight = None
878 882
 
879
-    def set_dotcode(self, dotcode):
883
+    def set_dotcode(self, dotcode, filename='<stdin>'):
880 884
         p = subprocess.Popen(
881 885
             ['dot', '-Txdot'],
882 886
             stdin=subprocess.PIPE,
... ...
@@ -885,7 +889,15 @@ class DotWidget(gtk.DrawingArea):
885 889
             universal_newlines=True
886 890
         )
887 891
         xdotcode = p.communicate(dotcode)[0]
888
-        self.set_xdotcode(xdotcode)
892
+        try:
893
+            self.set_xdotcode(xdotcode)
894
+        except GraphParseError, e:
895
+            msg = "Could not parse %s, is it a valid dot file?" % filename
896
+            error_dlg = gtk.MessageDialog(type=gtk.MESSAGE_ERROR,
897
+                                          message_format=msg,
898
+                                          buttons=gtk.BUTTONS_OK)
899
+            error_dlg.run()
900
+            error_dlg.destroy()
889 901
 
890 902
     def set_xdotcode(self, xdotcode):
891 903
         #print xdotcode
... ...
@@ -1143,7 +1155,7 @@ class DotWindow(gtk.Window):
1143 1155
         # Add the actiongroup to the uimanager
1144 1156
         uimanager.insert_action_group(actiongroup, 0)
1145 1157
 
1146
-        # Add a UI description
1158
+        # Add a UI descrption
1147 1159
         uimanager.add_ui_from_string(self.ui)
1148 1160
 
1149 1161
         # Create a Toolbar
... ...
@@ -1156,13 +1168,13 @@ class DotWindow(gtk.Window):
1156 1168
 
1157 1169
         self.show_all()
1158 1170
 
1159
-    def set_dotcode(self, dotcode):
1160
-        self.widget.set_dotcode(dotcode)
1171
+    def set_dotcode(self, dotcode, filename='<stdin>'):
1172
+        self.widget.set_dotcode(dotcode, filename)
1161 1173
 
1162 1174
     def open_file(self, filename):
1163 1175
         try:
1164 1176
             fp = file(filename, 'rt')
1165
-            self.set_dotcode(fp.read())
1177
+            self.set_dotcode(fp.read(), filename)
1166 1178
             fp.close()
1167 1179
         except IOError:
1168 1180
             # TODO: show an error message