Ejemplo n.º 1
0
    def cast(self, data):
        if data is None: return

        values = {'on': 1, 'off': 0, 'true': 1, 'false': 0, 'yes': 1, 'no': 0}

        # If this is from the command-line, increment or decrement
        if self.source & COMMANDLINE:
            initdata = self.data
            if self.data is None:
                initdata = 0
            if data:
                return max(0, initdata + 1)
            else:
                return max(0, initdata - 1)

        # If this is from any other source, set explicitly
        try:
            return max(0, int(data))
        except:
            try:
                return values[str(data).lower()]
            except:
                pass
        name = self.name
        if self.actual: name = self.actual
        raise InvalidOptionError(name, data, type='counted')
Ejemplo n.º 2
0
 def cast(self, data):
     name = self.name
     if self.actual: name = self.actual
     if data is None: return
     try:
         return float(data)
     except:
         raise InvalidOptionError(name, data, type='float')
Ejemplo n.º 3
0
   def cast(self, data):
      if data is None: return

      values = {'on':1,'off':0,'true':1,'false':0,'yes':1,'no':0}

      try:
         bool = int(data)
         if bool in [0, 1]:
             return bool
      except:
         try:
            bool = values[str(data).lower()]
            if bool in [0, 1]:
                return bool
         except: pass
      name = self.name
      if self.actual: name = self.actual
      raise InvalidOptionError(name, data, type='boolean')
Ejemplo n.º 4
0
    def checkValues(self, value):
        """ Check the value against the possible valid values """
        # All values are valid
        if self.values is None: return value

        name = self.name
        if self.actual: name = self.actual

        # Check to see if the value is within the valid range
        if isinstance(self.values, list):
            if not self.values:
                pass
            elif value >= self.values[0] and value <= self.values[-1]:
                return value

            if self.error:
                raise InvalidOptionError(name,
                                         value,
                                         msg=self.error % self.names())
            else:
                raise InvalidOptionError(
                    name,
                    value,
                    msg='Given value is not within the valid range (%s, %s)' %
                    (self.values[0], self.values[-1]))

        # Check to see if the value is within the list of valid values
        elif isinstance(self.values, tuple):
            # Look for literal values
            for option in [
                    x for x in self.values if not (isinstance(x, RegexType))
            ]:
                if isinstance(option, str) and option.lower() == value.lower():
                    return value
                elif option == value:
                    return value

            # Look for regular expressions in the list
            for regex in [x for x in self.values if isinstance(x, RegexType)]:
                if regex.search(value):
                    return value

            if self.error:
                raise InvalidOptionError(name,
                                         value,
                                         msg=self.error % self.names())
            else:
                raise InvalidOptionError(name, value,
                      msg='Given value is not a valid value. Expecting (%s)' % \
                          (', '.join(map(str, self.values))))

        # Check to see if the value is equal to the only valid value
        elif isinstance(self.values, (str, int, float)):
            if value == self.values:
                return value

            if self.error:
                raise InvalidOptionError(name,
                                         value,
                                         msg=self.error % self.names())
            else:
                raise InvalidOptionError(name, value,
                      msg='Given value is not a valid value. Expecting (%s)' % \
                           self.values)

        # Check to see if the value is valid using a regex
        elif isinstance(self.values, RegexType):
            if not isinstance(value, str):
                pass
            elif self.values.search(value):
                return value

            if self.error:
                raise InvalidOptionError(name,
                                         value,
                                         msg=self.error % self.names())
            else:
                raise InvalidOptionError(
                    name, value, msg='Given value is not a valid value')

        # Use the user defined validation function
        elif isinstance(self.values, collections.abc.Callable):
            if self.values(value):
                return value

            if self.error:
                raise InvalidOptionError(name,
                                         value,
                                         msg=self.error % self.names())
            else:
                raise InvalidOptionError(
                    name, value, msg='Given value is not a valid value')

        # Bail out if we don't know what this is
        raise ValueError('Unknown valid values type')