1+ from __future__ import generators
12"""
23This is a matlab style functional interface the matplotlib.
34
113114the exception of those in mlab.py provided by matplotlib.
114115"""
115116
116- __version__ = '0.63.0 '
117+ __version__ = '0.63.4 '
117118__revision__ = '$Revision$'
118119__date__ = '$Date$'
119120
@@ -140,15 +141,89 @@ def warn(s):
140141
141142import sys , os
142143
144+
143145major , minor1 , minor2 , s , tmp = sys .version_info
144- if major < 2 or (major == 2 and minor1 < 3 ):
145- True = 1
146- False = 0
147- else :
148- True = True
149- False = False
146+ _python23 = major >= 2 and minor1 >= 3
147+
148+ _havemath = _python23
149+
150+ try :
151+ import datetime
152+ import dateutil
153+ import pytz
154+ except ImportError : _havedate = False
155+ else : _havedate = True
156+
157+ if not _python23 :
158+ def enumerate (seq ):
159+ for i in range (len (seq )):
160+ yield i , seq [i ]
161+
162+
163+ class Verbose :
164+ """
165+ A class to handle reporting. Set the fileo attribute to any file
166+ instance to handle the output. Default is sys.stdout
167+ """
168+ levels = ('silent' , 'helpful' , 'annoying' )
169+ vald = dict ( [(level , i ) for i ,level in enumerate (levels )])
170+
171+ def __init__ (self , level ):
172+ self .set_level (level )
173+ self .fileo = sys .stdout
174+
175+ def set_level (self , level ):
176+ 'set the verbosity to one of the Verbose.levels strings'
177+ assert level in self .levels
178+ self .level = level
179+
180+ def silent (self ):
181+ return self .level == 'silent'
150182
183+ def helpful (self ):
184+ return self .vald [self .level ]>= self .vald ['helpful' ]
151185
186+ def annoying (self ):
187+ return self .vald [self .level ]>= self .vald ['annoying' ]
188+
189+ def report (self , s , level = 'helpful' ):
190+ """
191+ print message s to self.fileo if self.level>=level. Return
192+ value indicates whether a message was issued
193+
194+ """
195+ if self .ge (level ):
196+ print >> self .fileo , s
197+ return True
198+ return False
199+
200+
201+ def wrap (self , fmt , func , level = 'helpful' , always = True ):
202+ """
203+ return a callable function that wraps func and reports it
204+ output through the verbose handler if current verbosity level
205+ is higher than level
206+
207+ if always is True, the report will occur on every function
208+ call; otherwise only on the first time the function is called
209+ """
210+ assert (callable , func )
211+ def wrapper (* args , ** kwargs ):
212+ ret = func (* args , ** kwargs )
213+
214+ if (always or not wrapper ._spoke ):
215+ spoke = self .report (fmt % ret , level )
216+ if not wrapper ._spoke : wrapper ._spoke = spoke
217+ return ret
218+ wrapper ._spoke = False
219+ wrapper .__doc__ = func .__doc__
220+ return wrapper
221+
222+ def ge (self , level ):
223+ 'return true if self.level is >= level'
224+ return self .vald [self .level ]>= self .vald [level ]
225+
226+ verbose = Verbose ('silent' ) # silent until rc is parsed
152227
153228def get_home ():
154229 """
@@ -168,8 +243,9 @@ def get_home():
168243
169244 return None
170245
171- def get_data_path ():
172246
247+ def _get_data_path ():
248+ 'get the path to matplotlib data'
173249 path = os .path .join (distutils .sysconfig .PREFIX , 'share' , 'matplotlib' )
174250 if os .path .isdir (path ): return path
175251
@@ -195,6 +271,8 @@ def get_data_path():
195271 return path
196272 raise RuntimeError ('Could not find the matplotlib data files' )
197273
274+ get_data_path = verbose .wrap ('matplotlib data path %s' , _get_data_path , always = False )
275+
198276
199277def validate_path_exists (s ):
200278 'If s is a path, return s, else False'
@@ -302,6 +380,26 @@ def validate_fontsize(s):
302380 raise ValueError ('not a valid font size' )
303381
304382
383+ def validate_verbose (s ):
384+ verbose .set_level (s )
385+ return verbose
386+
387+
388+ def validate_verbose_fileo (s ):
389+ d = {'sys.stdout' :sys .stdout ,
390+ 'sys.stderr' :sys .stderr ,
391+ }
392+ if d .has_key (s ): verbose .fileo = d [s ]
393+ else :
394+ try : fileo = file (s , 'w' )
395+ except IOError :
396+ raise ValueError ('Verbose object could not open log file "%s" for writing.\n Check your matplotlibrc verbose.fileo setting' % s )
397+
398+
399+ else :
400+ verbose .fileo = fileo
401+ return verbose .fileo
402+
305403# a map from key -> value, converter
306404defaultParams = {
307405 'backend' : ['GTK' , str ],
@@ -311,6 +409,10 @@ def validate_fontsize(s):
311409 'interactive' : [False , validate_bool ],
312410 'timezone' : ['UTC' , str ],
313411
412+ # the verbosity setting
413+ 'verbose.level' : ['silent' , validate_verbose ],
414+ 'verbose.fileo' : ['sys.stdout' , validate_verbose_fileo ],
415+
314416 # line props
315417 'lines.linewidth' : [0.5 , validate_float ], # line width in points
316418 'lines.linestyle' : ['-' , str ], # solid line
@@ -434,6 +536,8 @@ def matplotlib_fname():
434536 print >> sys .stderr , 'Could not find .matplotlibrc; using defaults'
435537 return fname
436538
539+
540+
437541def rc_params ():
438542 'Return the default params updated from the values in the rc file'
439543
@@ -448,8 +552,11 @@ def rc_params():
448552
449553 fname = matplotlib_fname ()
450554 if not os .path .exists (fname ):
451- return dict ([ (key , tup [0 ]) for key , tup in defaultParams .items ()])
452-
555+ message = 'could not find rc file; returning defaults'
556+ ret = dict ([ (key , tup [0 ]) for key , tup in defaultParams .items ()])
557+ verbose .report (message )
558+ return ret
559+
453560 cnt = 0
454561 for line in file (fname ):
455562 cnt += 1
@@ -488,7 +595,10 @@ def rc_params():
488595 defaultParams [key ][0 ] = cval
489596
490597 # strip the conveter funcs and return
491- return dict ([ (key , tup [0 ]) for key , tup in defaultParams .items ()])
598+ ret = dict ([ (key , tup [0 ]) for key , tup in defaultParams .items ()])
599+ verbose .report ('loaded rc file %s' % fname )
600+ return ret
601+
492602
493603# this is the instance used by the matplotlib classes
494604rcParams = rc_params ()
0 commit comments