Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 351475a

Browse files
committed
validate rcParams
svn path=/trunk/matplotlib/; revision=3552
1 parent b1b57fa commit 351475a

3 files changed

Lines changed: 62 additions & 41 deletions

File tree

CHANGELOG

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
2007-07-17 added validation to setting and changing rcParams - DSD
2+
13
2007-07-17 bugfix segfault in transforms module. Thanks Ben North for
24
the patch. - ADS
35

lib/matplotlib/__init__.py

Lines changed: 54 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -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\tin 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+
558555
def 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\tin file "%s"' % (cnt, line, fname))
573+
warnings.warn('Illegal line #%d\n\t%s\n\tin 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\tin 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\tin 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
611626
rcParams = rc_params()
612627

613-
rcParamsDefault = dict(rcParams.items()) # a copy
628+
rcParamsDefault = RcParams([ (key, default) for key, (default, converter) in \
629+
defaultParams.iteritems() ])
614630

615631
rcParams['ps.usedistiller'] = checkdep_ps_distiller(rcParams['ps.usedistiller'])
616632
rcParams['text.usetex'] = checkdep_usetex(rcParams['text.usetex'])

lib/matplotlib/rcsetup.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,8 @@ def validate_bool(b):
3636
'Convert b to a boolean or raise'
3737
if type(b) is str:
3838
b = b.lower()
39-
if b in ('t', 'y', 'yes', 'true', '1', 1, True): return True
40-
elif b in ('f', 'n', 'no', 'false', '0', 0, False): return False
39+
if b in ('t', 'y', 'yes', 'on', 'true', '1', 1, True): return True
40+
elif b in ('f', 'n', 'no', 'off', 'false', '0', 0, False): return False
4141
else:
4242
raise ValueError('Could not convert "%s" to boolean' % b)
4343

@@ -142,12 +142,15 @@ def validate_color(s):
142142

143143
if len(s)==6 and s.isalnum(): # looks like hex
144144
return '#' + s
145+
146+
if len(s)==7 and s.startswith('#') and s[1:].isalnum():
147+
return s
145148

146149
if s.isalpha():
147150
#assuming a color name, hold on
148151
return s
149152

150-
raise ValueError('"s" does not look like color arg')
153+
raise ValueError('%s does not look like color arg'%s)
151154

152155
def validate_stringlist(s):
153156
'return a list'

0 commit comments

Comments
 (0)