@@ -518,34 +518,6 @@ def matplotlib_fname():
518518 return fname
519519
520520
521- def validate_key (key , val , line , cnt , fname , fail_on_error ):
522- if key in _deprecated_map .keys ():
523- alt = _deprecated_map [key ]
524- warnings .warn ('%s is deprecated in matplotlibrc - use %s instead.' % (key , alt ))
525- key = alt
526-
527- if not defaultParams .has_key (key ):
528- print >> sys .stderr , """\
529- Bad key "%s" on line %d in
530- %s.
531- You probably need to get an updated matplotlibrc file from
532- http://matplotlib.sf.net/matplotlibrc or from the matplotlib source
533- distribution""" % (key , cnt , fname )
534- return None
535-
536- default , converter = defaultParams [key ]
537-
538- if fail_on_error :
539- return converter (val ) # try to convert to proper type or raise
540- else :
541- try : cval = converter (val ) # try to convert to proper type or raise
542- except Exception , msg :
543- warnings .warn ('Bad val "%s" on line #%d\n \t "%s"\n \t in file "%s"\n \t %s' % (
544- val , cnt , line , fname , msg ))
545- return None
546- else :
547- return cval
548-
549521_deprecated_map = {
550522 'text.fontstyle' : 'font.style' ,
551523 'text.fontangle' : 'font.style' ,
@@ -555,6 +527,31 @@ def validate_key(key, val, line, cnt, fname, fail_on_error):
555527 'tick.size' : 'tick.major.size' ,
556528 }
557529
530+
531+ class RcParams (dict ):
532+
533+ """A dictionary object including validation
534+ """
535+
536+ validate = dict ([ (key , converter ) for key , (default , converter ) in \
537+ defaultParams .iteritems () ])
538+
539+ fail_on_error = False
540+
541+ def __setitem__ (self , key , val ):
542+ try :
543+ if key in _deprecated_map .keys ():
544+ alt = _deprecated_map [key ]
545+ warnings .warn ('%s is deprecated in matplotlibrc. Use %s \
546+ instead.'% (key , alt ))
547+ key = alt
548+ cval = self .validate [key ](val )
549+ dict .__setitem__ (self , key , cval )
550+ except KeyError :
551+ raise KeyError ('%s is not a valid rc parameter.\
552+ See rcParams.keys() for a list of valid parameters.'% key )
553+
554+
558555def rc_params (fail_on_error = False ):
559556 'Return the default params updated from the values in the rc file'
560557
@@ -573,7 +570,8 @@ def rc_params(fail_on_error=False):
573570 if not strippedline : continue
574571 tup = strippedline .split (':' ,1 )
575572 if len (tup ) != 2 :
576- warnings .warn ('Illegal line #%d\n \t %s\n \t in file "%s"' % (cnt , line , fname ))
573+ warnings .warn ('Illegal line #%d\n \t %s\n \t in file "%s"' % \
574+ (cnt , line , fname ))
577575 continue
578576 key , val = tup
579577 key = key .strip ()
@@ -582,35 +580,53 @@ def rc_params(fail_on_error=False):
582580 warnings .warn ('Duplicate key in file "%s", line #%d' % (fname ,cnt ))
583581 rc_temp [key ] = (val , line , cnt )
584582
585- ret = dict ([ (key ,default ) for key , (default , converter ) in defaultParams .iteritems () ])
583+ ret = RcParams ([ (key , default ) for key , (default , converter ) in \
584+ defaultParams .iteritems () ])
586585
587586 for key in ('verbose.level' , 'verbose.fileo' ):
588587 if key in rc_temp :
589588 val , line , cnt = rc_temp .pop (key )
590- cval = validate_key (key , val , line , cnt , fname , fail_on_error )
591- if cval is not None :
592- ret [key ] = cval
589+ if fail_on_error :
590+ ret [key ] = val # try to convert to proper type or raise
591+ else :
592+ try : ret [key ] = val # try to convert to proper type or skip
593+ except Exception , msg :
594+ warnings .warn ('Bad val "%s" on line #%d\n \t "%s"\n \t in file \
595+ "%s"\n \t %s' % (val , cnt , line , fname , msg ))
593596
594597 verbose .set_level (ret ['verbose.level' ])
595598 verbose .set_fileo (ret ['verbose.fileo' ])
596599
597600 for key , (val , line , cnt ) in rc_temp .iteritems ():
598- cval = validate_key (key , val , line , cnt , fname , fail_on_error )
599- if cval is not None :
600- ret [key ] = cval
601+ if defaultParams .has_key (key ):
602+ if fail_on_error :
603+ ret [key ] = val # try to convert to proper type or raise
604+ else :
605+ try : ret [key ] = val # try to convert to proper type or skip
606+ except Exception , msg :
607+ warnings .warn ('Bad val "%s" on line #%d\n \t "%s"\n \t in file \
608+ "%s"\n \t %s' % (val , cnt , line , fname , msg ))
609+ else :
610+ print >> sys .stderr , """
611+ Bad key "%s" on line %d in
612+ %s.
613+ You probably need to get an updated matplotlibrc file from
614+ http://matplotlib.sf.net/matplotlibrc or from the matplotlib source
615+ distribution""" % (key , cnt , fname )
601616
602617 if ret ['datapath' ] is None :
603618 ret ['datapath' ] = get_data_path ()
604619
605620 verbose .report ('loaded rc file %s' % fname )
606-
621+
607622 return ret
608623
609624
610625# this is the instance used by the matplotlib classes
611626rcParams = rc_params ()
612627
613- rcParamsDefault = dict (rcParams .items ()) # a copy
628+ rcParamsDefault = RcParams ([ (key , default ) for key , (default , converter ) in \
629+ defaultParams .iteritems () ])
614630
615631rcParams ['ps.usedistiller' ] = checkdep_ps_distiller (rcParams ['ps.usedistiller' ])
616632rcParams ['text.usetex' ] = checkdep_usetex (rcParams ['text.usetex' ])
0 commit comments