urltree.py
changeset 50 e4fbf480fbee
parent 49 9b097385b463
child 51 a1da82870a6b
equal deleted inserted replaced
49:9b097385b463 50:e4fbf480fbee
   238         """
   238         """
   239             Match -> LabelValue
   239             Match -> LabelValue
   240         """
   240         """
   241         
   241         
   242         # default?
   242         # default?
   243         if value is None and self.default :
   243         if value is None and self.default is not None :
   244             return LabelValue(self, self.default)
   244             return LabelValue(self, self.default)
   245         
   245         
   246         # only non-empty values!
   246         # only non-empty values!
   247         elif value :
   247         elif value :
   248             # test
   248             # test
   320 class URLIntegerType (URLType) :
   320 class URLIntegerType (URLType) :
   321     """
   321     """
   322         A URLType for simple integers
   322         A URLType for simple integers
   323     """
   323     """
   324 
   324 
   325     def __init__ (self, negative=True, zero=True, max=None) :
   325     def __init__ (self, allow_negative=True, allow_zero=True, max=None) :
   326         """
   326         """
   327             Pass in negative=False to disallow negative numbers, zero=False to disallow zero, or non-zero max
   327             Pass in allow_negative=False to disallow negative numbers, allow_zero=False to disallow zero, or non-zero
   328             to specifiy maximum value
   328             max to specifiy maximum value
   329         """
   329         """
   330 
   330 
   331         self.negative = negative
   331         self.allow_negative = allow_negative
   332         self.zero = zero
   332         self.allow_zero = allow_zero
   333         self.max = max
   333         self.max = max
   334     
   334     
   335     def _validate (self, value) :
   335     def _validate (self, value) :
   336         """
   336         """
   337             Test to make sure value fits our criteria
   337             Test to make sure value fits our criteria
   338         """
   338         """
   339 
   339 
   340         # negative?
   340         # negative?
   341         if self.negative and value < 0 :
   341         if not self.allow_negative and value < 0 :
   342             raise ValueError("value is negative")
   342             raise ValueError("value is negative")
   343         
   343         
   344         # zero?
   344         # zero?
   345         if self.zero and value == 0 :
   345         if not self.allow_zero and value == 0 :
   346             raise ValueError("value is zero")
   346             raise ValueError("value is zero")
   347         
   347         
   348         # max?
   348         # max?
   349         if self.max is not None and value > max :
   349         if self.max is not None and value > max :
   350             raise ValueError("value is too large: %d" % value)
   350             raise ValueError("value is too large: %d" % value)