normalize value directly into is_valid_XXX

This commit is contained in:
Vincent Hanquez 2005-04-26 23:41:20 +00:00
parent 5ee1f2b29a
commit d415620b13
1 changed files with 25 additions and 18 deletions

View File

@ -217,18 +217,23 @@ class Config:
try: try:
ival = int(val) ival = int(val)
except: except:
return 0 return None
return 1 return ival
def is_valid_bool(self, val): def is_valid_bool(self, val):
if self.is_valid_int(val): if val == 'True':
if int(val) == 0 or int(val) == 1: return True
return 1 elif val == 'False':
return 0 return False
return 0 else:
ival = self.is_valid_int(val)
if ival:
return True
return False
return None
def is_valid_string(self, val): def is_valid_string(self, val):
return 1 return val
def is_valid(self, type, val): def is_valid(self, type, val):
if type[0] == 'boolean': if type[0] == 'boolean':
@ -238,21 +243,20 @@ class Config:
elif type[0] == 'string': elif type[0] == 'string':
return self.is_valid_string(val) return self.is_valid_string(val)
else: else:
return sre.match(type[1], val) if sre.match(type[1], val):
return val
else:
return None
def set(self, optname, value): def set(self, optname, value):
if not self.__options.has_key(optname): if not self.__options.has_key(optname):
# print 'error: option %s doesn\'t exist' % (optname) # print 'error: option %s doesn\'t exist' % (optname)
return -1 return -1
opt = self.__options[optname] opt = self.__options[optname]
if opt[OPT_TYPE][0] == 'boolean': value = self.is_valid(opt[OPT_TYPE], value)
if value == 'True': if value == None:
value = True
elif value == 'False':
value = False
if not self.is_valid(opt[OPT_TYPE], value):
return -1 return -1
opt[OPT_VAL] = value opt[OPT_VAL] = value
return 0 return 0
@ -269,8 +273,10 @@ class Config:
return -1 return -1
opt = self.__options_per_key[typename] opt = self.__options_per_key[typename]
if opt[1].has_key(name):
return -2
opt[1][name] = copy.deepcopy(opt[0]) opt[1][name] = copy.deepcopy(opt[0])
return 0
def del_per(self, typename, name): # per_group_of_option def del_per(self, typename, name): # per_group_of_option
if not self.__options_per_key.has_key(typename): if not self.__options_per_key.has_key(typename):
@ -291,7 +297,8 @@ class Config:
if not obj.has_key(subname): if not obj.has_key(subname):
return -1 return -1
subobj = obj[subname] subobj = obj[subname]
if not self.is_valid(subobj[OPT_TYPE], value): value = self.is_valid(subobj[OPT_TYPE], value)
if value == None:
return -1 return -1
subobj[OPT_VAL] = value subobj[OPT_VAL] = value
return 0 return 0