@@ -1000,9 +1000,6 @@ def validate_key(key, val, line, cnt, fname, fail_on_error):
1000
1000
1001
1001
default , converter = defaultParams [key ]
1002
1002
1003
- ind = val .find ('#' )
1004
- if ind >= 0 : val = val [:ind ] # ignore trailing comments
1005
- val = val .strip ()
1006
1003
if fail_on_error :
1007
1004
return converter (val ) # try to convert to proper type or raise
1008
1005
else :
@@ -1033,36 +1030,37 @@ def rc_params(fail_on_error=False):
1033
1030
warnings .warn (message )
1034
1031
return ret
1035
1032
1036
- lines = [line .strip () for line in file (fname )]
1037
1033
cnt = 0
1038
1034
rc_temp = {}
1039
- for line in lines :
1035
+ for line in file ( fname ) :
1040
1036
cnt += 1
1041
- if not len ( line ): continue
1042
- if line . startswith ( '#' ) : continue
1043
- tup = line .split (':' ,1 )
1037
+ strippedline = line . split ( '#' , 1 )[ 0 ]. strip ()
1038
+ if not strippedline : continue
1039
+ tup = strippedline .split (':' ,1 )
1044
1040
if len (tup ) != 2 :
1045
1041
warnings .warn ('Illegal line #%d\n \t %s\n \t in file "%s"' % (cnt , line , fname ))
1046
1042
continue
1047
1043
key , val = tup
1048
1044
key = key .strip ()
1045
+ val = val .strip ()
1046
+ if key in rc_temp :
1047
+ warnings .warn ('Duplicate key in file "%s", line #%d' % (fname ,cnt ))
1049
1048
rc_temp [key ] = (val , line , cnt )
1050
1049
1050
+ ret = dict ([ (key ,default ) for key , (default , converter ) in defaultParams .iteritems () ])
1051
+
1051
1052
for key in ('verbose.level' , 'verbose.fileo' ):
1052
- try : val , line , cnt = rc_temp .pop (key )
1053
- except KeyError : continue
1054
- else :
1053
+ if key in rc_temp :
1054
+ val , line , cnt = rc_temp .pop (key )
1055
1055
cval = validate_key (key , val , line , cnt , fname , fail_on_error )
1056
- if cval is not None : defaultParams [key ][0 ] = cval
1057
- while len (rc_temp ) > 0 :
1058
- key , (val , line , cnt ) = rc_temp .popitem ()
1056
+ if cval is not None :
1057
+ ret [key ] = cval
1059
1058
1059
+ for key , (val , line , cnt ) in rc_temp .iteritems ():
1060
1060
cval = validate_key (key , val , line , cnt , fname , fail_on_error )
1061
- if cval is not None : defaultParams [ key ][ 0 ] = cval
1062
- else : continue
1061
+ if cval is not None :
1062
+ ret [ key ] = cval
1063
1063
1064
- # strip the converter funcs and return
1065
- ret = dict ([ (key , tup [0 ]) for key , tup in defaultParams .items ()])
1066
1064
verbose .report ('loaded rc file %s' % fname )
1067
1065
1068
1066
return ret
0 commit comments