4
4
import sys
5
5
def fn_name (): return sys ._getframe (1 ).f_code .co_name
6
6
7
-
8
7
import matplotlib
9
8
from matplotlib import verbose
10
9
from matplotlib .numerix import asarray , fromstring , UInt8 , zeros , \
@@ -25,7 +24,6 @@ def fn_name(): return sys._getframe(1).f_code.co_name
25
24
useMathText = False
26
25
else : useMathText = True
27
26
28
-
29
27
try :
30
28
import pygtk
31
29
if not matplotlib .FROZEN :
@@ -547,24 +545,6 @@ def set_linewidth(self, w):
547
545
self .gdkGC .line_width = max (1 , int (round (pixels )))
548
546
549
547
550
- def error_msg_gtk (msg , parent = None ):
551
- if parent : # find the toplevel gtk.Window
552
- parent = parent .get_toplevel ()
553
- if not parent .flags () & gtk .TOPLEVEL :
554
- parent = None
555
-
556
- if not is_string_like (msg ):
557
- msg = ',' .join (map (str ,msg ))
558
-
559
- dialog = gtk .MessageDialog (
560
- parent = parent ,
561
- type = gtk .MESSAGE_ERROR ,
562
- buttons = gtk .BUTTONS_OK ,
563
- message_format = msg )
564
- dialog .run ()
565
- dialog .destroy ()
566
-
567
-
568
548
def draw_if_interactive ():
569
549
"""
570
550
Is called after every matplotlib.matlab drawing command
@@ -701,8 +681,9 @@ def key_release_event(self, widget, event):
701
681
def mpl_connect (self , s , func ):
702
682
703
683
if s not in self .events :
704
- error_msg ('Can only connect events of type "%s"\n Do not know how to handle "%s"' % (', ' .join (self .events ), s ),
705
- parent = self )
684
+ raise MPLError ('Can only connect events of type "%s"\n Do not know how to handle "%s"' % (', ' .join (self .events ), s ))
685
+ #error_msg('Can only connect events of type "%s"\nDo not know how to handle "%s"' %(', '.join(self.events), s),
686
+ # parent=self)
706
687
707
688
def wrapper (widget , event ):
708
689
thisEvent = MplEvent (s , self )
@@ -851,20 +832,22 @@ def print_figure(self, filename, dpi=150, facecolor='w', edgecolor='w',
851
832
852
833
# pixbuf.save() recognises 'jpeg' not 'jpg'
853
834
if ext == 'jpg' : ext = 'jpeg'
854
- try : pixbuf .save (filename , ext )
855
- except gobject .GError , exc :
856
- error_msg ('Save figure failure:\n %s' % (exc ,), parent = self )
835
+ pixbuf .save (filename , ext )
836
+ #try: pixbuf.save(filename, ext)
837
+ #except gobject.GError, exc:
838
+ # error_msg('Save figure failure:\n%s' % (exc,), parent=self)
857
839
858
840
elif ext in ('eps' , 'ps' , 'svg' ,):
859
841
if ext == 'svg' :
860
842
from backend_svg import FigureCanvasSVG as FigureCanvas
861
843
else :
862
844
from backend_ps import FigureCanvasPS as FigureCanvas
863
845
864
- #try:
865
- # Testing - use uncaught exception handler
866
846
fc = self .switch_backends (FigureCanvas )
867
847
fc .print_figure (filename , dpi , facecolor , edgecolor , orientation )
848
+ #try:
849
+ # fc = self.switch_backends(FigureCanvas)
850
+ # fc.print_figure(filename, dpi, facecolor, edgecolor, orientation)
868
851
#except IOError, exc:
869
852
# error_msg("Save figure failure:\n%s: %s" %
870
853
# (exc.filename, exc.strerror), parent=self)
@@ -875,17 +858,21 @@ def print_figure(self, filename, dpi=150, facecolor='w', edgecolor='w',
875
858
try :
876
859
from backend_agg import FigureCanvasAgg as FigureCanvas
877
860
except :
878
- error_msg ('Save figure failure:\n '
879
- 'Agg must be loaded to save as bmp, raw and rgb' ,
880
- parent = self )
861
+ raise MPLError ('Save figure failure:\n '
862
+ 'Agg must be loaded to save as bmp, raw and rgb' )
863
+ #error_msg('Save figure failure:\n'
864
+ # 'Agg must be loaded to save as bmp, raw and rgb',
865
+ # parent=self)
881
866
else :
882
867
fc = self .switch_backends (FigureCanvas )
883
868
fc .print_figure (filename , dpi , facecolor , edgecolor , orientation )
884
869
885
870
else :
886
- error_msg ('Format "%s" is not supported.\n Supported formats are %s.' %
887
- (ext , ', ' .join (IMAGE_FORMAT )),
888
- parent = self )
871
+ raise MPLError ('Format "%s" is not supported.\n Supported formats are %s.' %
872
+ (ext , ', ' .join (IMAGE_FORMAT )))
873
+ #error_msg('Format "%s" is not supported.\nSupported formats are %s.' %
874
+ # (ext, ', '.join(IMAGE_FORMAT)),
875
+ # parent=self)
889
876
890
877
# restore figure settings
891
878
self .figure .dpi .set (origDPI )
@@ -1455,37 +1442,30 @@ def __init__ (self,
1455
1442
if path : self .path = path
1456
1443
else : self .path = os .getcwd () + os .sep
1457
1444
1458
- #self.IMAGE_FORMAT = matplotlib.backends.backend_mod.IMAGE_FORMAT
1459
- #self.IMAGE_FORMAT_DEFAULT = matplotlib.backends.backend_mod.IMAGE_FORMAT_DEFAULT
1460
- #self.IMAGE_FORMAT.sort()
1461
-
1462
- # later: change self.IMAGE_FORMAT to IMAGE_FORMAT
1463
- self .IMAGE_FORMAT = IMAGE_FORMAT
1464
- self .IMAGE_FORMAT_DEFAULT = IMAGE_FORMAT_DEFAULT
1465
- self .IMAGE_FORMAT .sort ()
1445
+ IMAGE_FORMAT .sort ()
1466
1446
1467
1447
# create an extra widget to list supported image formats
1468
1448
self .set_current_folder (self .path )
1469
- self .set_current_name ('image.' + self . IMAGE_FORMAT_DEFAULT )
1449
+ self .set_current_name ('image.' + IMAGE_FORMAT_DEFAULT )
1470
1450
1471
1451
hbox = gtk .HBox (spacing = 10 )
1472
1452
hbox .pack_start (gtk .Label ("Image Format:" ), expand = False )
1473
1453
1474
1454
self .cbox = gtk .combo_box_new_text ()
1475
1455
hbox .pack_start (self .cbox )
1476
1456
1477
- for item in self . IMAGE_FORMAT :
1457
+ for item in IMAGE_FORMAT :
1478
1458
self .cbox .append_text (item )
1479
- self .cbox .set_active (self . IMAGE_FORMAT .index (self . IMAGE_FORMAT_DEFAULT ))
1459
+ self .cbox .set_active (IMAGE_FORMAT .index (IMAGE_FORMAT_DEFAULT ))
1480
1460
1481
1461
def cb_cbox_changed (cbox , data = None ):
1482
1462
"""File extension changed"""
1483
1463
head , filename = os .path .split (self .get_filename ())
1484
1464
root , ext = os .path .splitext (filename )
1485
1465
ext = ext [1 :]
1486
- new_ext = self . IMAGE_FORMAT [cbox .get_active ()]
1466
+ new_ext = IMAGE_FORMAT [cbox .get_active ()]
1487
1467
1488
- if ext in self . IMAGE_FORMAT :
1468
+ if ext in IMAGE_FORMAT :
1489
1469
filename = filename .replace (ext , new_ext )
1490
1470
elif ext == '' :
1491
1471
filename = filename .rstrip ('.' ) + '.' + new_ext
@@ -1504,17 +1484,22 @@ def get_filename_from_user (self):
1504
1484
filename = None
1505
1485
break
1506
1486
filename = self .get_filename ()
1507
- menu_ext = self . IMAGE_FORMAT [self .cbox .get_active ()]
1487
+ menu_ext = IMAGE_FORMAT [self .cbox .get_active ()]
1508
1488
root , ext = os .path .splitext (filename )
1509
1489
ext = ext [1 :]
1510
1490
if ext == '' :
1511
1491
ext = menu_ext
1512
1492
filename += '.' + ext
1513
1493
1514
- if ext in self . IMAGE_FORMAT :
1494
+ if ext in IMAGE_FORMAT :
1515
1495
self .path = filename
1516
1496
break
1517
1497
else :
1498
+ # XXX exception causes method to terminate w/o closing window
1499
+ # but want to repeat loop until selection is made
1500
+ # so a popup IS more appropriate?
1501
+ #raise MPLError('Format "%s" is not supported.\nSupported formats are %s.' %
1502
+ # (ext, ', '.join(IMAGE_FORMAT)))
1518
1503
error_msg ('Image format "%s" is not supported' % ext ,
1519
1504
parent = self )
1520
1505
self .set_current_name (os .path .split (root )[1 ] + '.' + menu_ext )
@@ -1523,9 +1508,6 @@ def get_filename_from_user (self):
1523
1508
return filename
1524
1509
1525
1510
1526
- FigureManager = FigureManagerGTK
1527
- error_msg = error_msg_gtk
1528
-
1529
1511
# set icon used when windows are minimized, it requires
1530
1512
# gtk.pygtk_version >= (2,2,0) with a GDK pixbuf loader for SVG installed
1531
1513
try :
@@ -1535,22 +1517,54 @@ def get_filename_from_user (self):
1535
1517
verbose .report ('Could not load matplotlib icon: %s' % sys .exc_info ()[1 ])
1536
1518
1537
1519
1538
- # TESTING
1539
- import sys
1520
+ def error_msg_gtk (msg , parent = None ):
1521
+ if parent : # find the toplevel gtk.Window
1522
+ parent = parent .get_toplevel ()
1523
+ if not parent .flags () & gtk .TOPLEVEL :
1524
+ parent = None
1525
+
1526
+ if not is_string_like (msg ):
1527
+ msg = ',' .join (map (str ,msg ))
1528
+
1529
+ dialog = gtk .MessageDialog (
1530
+ parent = parent ,
1531
+ type = gtk .MESSAGE_ERROR ,
1532
+ buttons = gtk .BUTTONS_OK ,
1533
+ message_format = msg )
1534
+ dialog .run ()
1535
+ dialog .destroy ()
1536
+
1537
+
1538
+ class MPLError (Exception ):
1539
+ """Exception for Matplotlib errors
1540
+ Attributes:
1541
+ message -- explanation of the error
1542
+ """
1543
+ def __init__ (self , message ):
1544
+ self .message = message
1545
+
1546
+ def __str__ (self ):
1547
+ return self .message
1548
+
1540
1549
1541
1550
def exception_handler (type , value , tb ):
1542
- """Handle uncaught exceptions"""
1543
- # type is of format - exceptions.IOError
1551
+ """Handle uncaught exceptions
1552
+ It does not catch SystemExit
1553
+ """
1554
+ msg = ''
1544
1555
# get the filename attribute if available (for IOError)
1545
1556
if hasattr (value , 'filename' ) and value .filename != None :
1546
1557
msg = value .filename + ': '
1547
- else :
1548
- msg = ''
1549
1558
if hasattr (value , 'strerror' ) and value .strerror != None :
1550
1559
msg += value .strerror
1551
1560
else :
1552
- msg = value
1561
+ msg += str ( value )
1553
1562
1554
- if len (msg ): error_msg_gtk (msg )
1563
+ if len (msg ) : error_msg_gtk (msg )
1555
1564
1556
- sys .excepthook = exception_handler
1565
+ # override excepthook only if it has not already been overridden
1566
+ if sys .__excepthook__ is sys .excepthook :
1567
+ sys .excepthook = exception_handler
1568
+
1569
+ FigureManager = FigureManagerGTK
1570
+ error_msg = error_msg_gtk
0 commit comments