From: <ds...@us...> - 2007-07-17 15:35:52
|
Revision: 3552 http://matplotlib.svn.sourceforge.net/matplotlib/?rev=3552&view=rev Author: dsdale Date: 2007-07-17 08:35:41 -0700 (Tue, 17 Jul 2007) Log Message: ----------- validate rcParams Modified Paths: -------------- trunk/matplotlib/CHANGELOG trunk/matplotlib/lib/matplotlib/__init__.py trunk/matplotlib/lib/matplotlib/rcsetup.py Modified: trunk/matplotlib/CHANGELOG =================================================================== --- trunk/matplotlib/CHANGELOG 2007-07-17 13:40:56 UTC (rev 3551) +++ trunk/matplotlib/CHANGELOG 2007-07-17 15:35:41 UTC (rev 3552) @@ -1,3 +1,5 @@ +2007-07-17 added validation to setting and changing rcParams - DSD + 2007-07-17 bugfix segfault in transforms module. Thanks Ben North for the patch. - ADS Modified: trunk/matplotlib/lib/matplotlib/__init__.py =================================================================== --- trunk/matplotlib/lib/matplotlib/__init__.py 2007-07-17 13:40:56 UTC (rev 3551) +++ trunk/matplotlib/lib/matplotlib/__init__.py 2007-07-17 15:35:41 UTC (rev 3552) @@ -518,34 +518,6 @@ return fname -def validate_key(key, val, line, cnt, fname, fail_on_error): - if key in _deprecated_map.keys(): - alt = _deprecated_map[key] - warnings.warn('%s is deprecated in matplotlibrc - use %s instead.' % (key, alt)) - key = alt - - if not defaultParams.has_key(key): - print >> sys.stderr, """\ -Bad key "%s" on line %d in -%s. -You probably need to get an updated matplotlibrc file from -http://matplotlib.sf.net/matplotlibrc or from the matplotlib source -distribution""" % (key, cnt, fname) - return None - - default, converter = defaultParams[key] - - if fail_on_error: - return converter(val) # try to convert to proper type or raise - else: - try: cval = converter(val) # try to convert to proper type or raise - except Exception, msg: - warnings.warn('Bad val "%s" on line #%d\n\t"%s"\n\tin file "%s"\n\t%s' % ( - val, cnt, line, fname, msg)) - return None - else: - return cval - _deprecated_map = { 'text.fontstyle': 'font.style', 'text.fontangle': 'font.style', @@ -555,6 +527,31 @@ 'tick.size' : 'tick.major.size', } + +class RcParams(dict): + + """A dictionary object including validation + """ + + validate = dict([ (key, converter) for key, (default, converter) in \ + defaultParams.iteritems() ]) + + fail_on_error = False + + def __setitem__(self, key, val): + try: + if key in _deprecated_map.keys(): + alt = _deprecated_map[key] + warnings.warn('%s is deprecated in matplotlibrc. Use %s \ +instead.'% (key, alt)) + key = alt + cval = self.validate[key](val) + dict.__setitem__(self, key, cval) + except KeyError: + raise KeyError('%s is not a valid rc parameter.\ +See rcParams.keys() for a list of valid parameters.'%key) + + def rc_params(fail_on_error=False): 'Return the default params updated from the values in the rc file' @@ -573,7 +570,8 @@ if not strippedline: continue tup = strippedline.split(':',1) if len(tup) !=2: - warnings.warn('Illegal line #%d\n\t%s\n\tin file "%s"' % (cnt, line, fname)) + warnings.warn('Illegal line #%d\n\t%s\n\tin file "%s"'%\ + (cnt, line, fname)) continue key, val = tup key = key.strip() @@ -582,35 +580,53 @@ warnings.warn('Duplicate key in file "%s", line #%d'%(fname,cnt)) rc_temp[key] = (val, line, cnt) - ret = dict([ (key,default) for key, (default, converter) in defaultParams.iteritems() ]) + ret = RcParams([ (key, default) for key, (default, converter) in \ + defaultParams.iteritems() ]) for key in ('verbose.level', 'verbose.fileo'): if key in rc_temp: val, line, cnt = rc_temp.pop(key) - cval = validate_key(key, val, line, cnt, fname, fail_on_error) - if cval is not None: - ret[key] = cval + if fail_on_error: + ret[key] = val # try to convert to proper type or raise + else: + try: ret[key] = val # try to convert to proper type or skip + except Exception, msg: + warnings.warn('Bad val "%s" on line #%d\n\t"%s"\n\tin file \ +"%s"\n\t%s' % (val, cnt, line, fname, msg)) verbose.set_level(ret['verbose.level']) verbose.set_fileo(ret['verbose.fileo']) for key, (val, line, cnt) in rc_temp.iteritems(): - cval = validate_key(key, val, line, cnt, fname, fail_on_error) - if cval is not None: - ret[key] = cval + if defaultParams.has_key(key): + if fail_on_error: + ret[key] = val # try to convert to proper type or raise + else: + try: ret[key] = val # try to convert to proper type or skip + except Exception, msg: + warnings.warn('Bad val "%s" on line #%d\n\t"%s"\n\tin file \ +"%s"\n\t%s' % (val, cnt, line, fname, msg)) + else: + print >> sys.stderr, """ +Bad key "%s" on line %d in +%s. +You probably need to get an updated matplotlibrc file from +http://matplotlib.sf.net/matplotlibrc or from the matplotlib source +distribution""" % (key, cnt, fname) if ret['datapath'] is None: ret['datapath'] = get_data_path() verbose.report('loaded rc file %s'%fname) - + return ret # this is the instance used by the matplotlib classes rcParams = rc_params() -rcParamsDefault = dict(rcParams.items()) # a copy +rcParamsDefault = RcParams([ (key, default) for key, (default, converter) in \ + defaultParams.iteritems() ]) rcParams['ps.usedistiller'] = checkdep_ps_distiller(rcParams['ps.usedistiller']) rcParams['text.usetex'] = checkdep_usetex(rcParams['text.usetex']) Modified: trunk/matplotlib/lib/matplotlib/rcsetup.py =================================================================== --- trunk/matplotlib/lib/matplotlib/rcsetup.py 2007-07-17 13:40:56 UTC (rev 3551) +++ trunk/matplotlib/lib/matplotlib/rcsetup.py 2007-07-17 15:35:41 UTC (rev 3552) @@ -36,8 +36,8 @@ 'Convert b to a boolean or raise' if type(b) is str: b = b.lower() - if b in ('t', 'y', 'yes', 'true', '1', 1, True): return True - elif b in ('f', 'n', 'no', 'false', '0', 0, False): return False + if b in ('t', 'y', 'yes', 'on', 'true', '1', 1, True): return True + elif b in ('f', 'n', 'no', 'off', 'false', '0', 0, False): return False else: raise ValueError('Could not convert "%s" to boolean' % b) @@ -142,12 +142,15 @@ if len(s)==6 and s.isalnum(): # looks like hex return '#' + s + + if len(s)==7 and s.startswith('#') and s[1:].isalnum(): + return s if s.isalpha(): #assuming a color name, hold on return s - raise ValueError('"s" does not look like color arg') + raise ValueError('%s does not look like color arg'%s) def validate_stringlist(s): 'return a list' This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |