Esempio n. 1
0
    def validate(self, configuration, val=None):
        if val is None:
            # If not passed in by subclass
            val = configuration.get(self.name, None)
        if val is None:
            # Not provided.
            if self.required:
                raise getmailConfigurationError(
                    '%s: missing required configuration parameter' % self.name
                )
            # Use default.
            return self.default
        if not isinstance(val,self.dtype) and val != self.default:
            # Got value, but not of expected type.  Try to convert.
            if self.securevalue:
                self.log.debug('converting %s to type %s\n'
                               % (self.name, self.dtype))
            else:
                self.log.debug('converting %s (%s) to type %s\n'
                               % (self.name, val, self.dtype))

            try:
                if self.dtype == bool:
                    val = eval_bool(val)
                else:
                    val = self.dtype(eval(val))
            except (ValueError, SyntaxError, TypeError) as o:
                raise getmailConfigurationError(
                    '%s: configuration value (%s) not of required type %s (%s)'
                    % (self.name, val, self.dtype, o)
                )
        return val
 def validate(self, configuration, val=None):
     if val is None:
         # If not passed in by subclass
         val = configuration.get(self.name, None)
     if val is None:
         # Not provided.
         if self.required:
             raise getmailConfigurationError(
                 '%s: missing required configuration parameter' % self.name
             )
         # Use default.
         return self.default
     if type(val) is not self.dtype and val != self.default:
         # Got value, but not of expected type.  Try to convert.
         if self.securevalue:
             self.log.debug('converting %s to type %s\n'
                 % (self.name, self.dtype))
         else:
             self.log.debug('converting %s (%s) to type %s\n'
                 % (self.name, val, self.dtype))
         
         try:
             if self.dtype == bool:
                 val = eval_bool(val)
             else:
                 val = self.dtype(eval(val))
         except (ValueError, SyntaxError, TypeError), o:
             raise getmailConfigurationError(
                 '%s: configuration value (%s) not of required type %s (%s)'
                 % (self.name, val, self.dtype, o)
             )