Example #1
0
 def get_param_as_int(
     self,
     name: str,
     default: Union[object, int] = MISSING,
     min: Optional[int] = None,
     max: Optional[int] = None,
 ) -> int:
     if name not in self._params:
         if default is not MISSING:
             return cast(int, default)
         raise errors.MissingRequiredParameterError(
             "Required parameter %r is missing." % name)
     value = self._params[name]
     try:
         value = int(value)
         if min is not None and value < min:
             raise errors.InvalidParameterError(
                 "Parameter %r must be at least %r." % (name, min))
         if max is not None and value > max:
             raise errors.InvalidParameterError(
                 "Parameter %r may not exceed %r." % (name, max))
         return value
     except (ValueError, TypeError):
         pass
     raise errors.InvalidParameterError(
         "Parameter %r must be an integer value." % name)
Example #2
0
 def get_param_as_int(self,
                      name,
                      required=False,
                      min=None,
                      max=None,
                      default=None):
     if name in self.input:
         val = self.input[name]
         try:
             val = int(val)
         except (ValueError, TypeError):
             raise errors.InvalidParameterError(
                 'Parameter %r is invalid: the value must be an integer.' %
                 name)
         if min is not None and val < min:
             raise errors.InvalidParameterError(
                 'Parameter %r is invalid: the value must be at least %r.' %
                 (name, min))
         if max is not None and val > max:
             raise errors.InvalidParameterError(
                 'Parameter %r is invalid: the value may not exceed %r.' %
                 (name, max))
         return val
     if not required:
         return default
     raise errors.MissingRequiredParameterError(
         'Required parameter %r is missing.' % name)
Example #3
0
 def get_param_as_list(self, name, required=False, default=None):
     if name in self.input:
         param = self.input[name]
         if not isinstance(param, list):
             return [param]
         return param
     if not required:
         return default
     raise errors.MissingRequiredParameterError(
         'Required paramter %r is missing.' % name)
Example #4
0
 def get_param_as_string(self, name, required=False, default=None):
     if name in self.input:
         param = self.input[name]
         if isinstance(param, list):
             try:
                 param = ','.join(param)
             except:
                 raise errors.InvalidParameterError(
                     'Parameter %r is invalid - expected simple string.' %
                     name)
         return param
     if not required:
         return default
     raise errors.MissingRequiredParameterError(
         'Required paramter %r is missing.' % name)
Example #5
0
 def get_param_as_list(
         self,
         name: str,
         default: Union[object, List[Any]] = MISSING) -> List[Any]:
     if name not in self._params:
         if default is not MISSING:
             return cast(List[Any], default)
         raise errors.MissingRequiredParameterError(
             'Required parameter %r is missing.' % name)
     value = self._params[name]
     if type(value) is str:
         if ',' in value:
             return value.split(',')
         return [value]
     if type(value) is list:
         return value
     raise errors.InvalidParameterError('Parameter %r must be a list.' %
                                        name)
Example #6
0
 def get_param_as_bool(self,
                       name: str,
                       default: Union[object, bool] = MISSING) -> bool:
     if name not in self._params:
         if default is not MISSING:
             return cast(bool, default)
         raise errors.MissingRequiredParameterError(
             'Required parameter %r is missing.' % name)
     value = self._params[name]
     try:
         value = str(value).lower()
     except TypeError:
         pass
     if value in ['1', 'y', 'yes', 'yeah', 'yep', 'yup', 't', 'true']:
         return True
     if value in ['0', 'n', 'no', 'nope', 'f', 'false']:
         return False
     raise errors.InvalidParameterError(
         'Parameter %r must be a boolean value.' % name)
Example #7
0
 def get_param_as_bool(self,
                       name: str,
                       default: Union[object, bool] = MISSING) -> bool:
     if name not in self._params:
         if default is not MISSING:
             return cast(bool, default)
         raise errors.MissingRequiredParameterError(
             "Required parameter %r is missing." % name)
     value = self._params[name]
     try:
         value = str(value).lower()
     except TypeError:
         pass
     if value in ["1", "y", "yes", "yeah", "yep", "yup", "t", "true"]:
         return True
     if value in ["0", "n", "no", "nope", "f", "false"]:
         return False
     raise errors.InvalidParameterError(
         "Parameter %r must be a boolean value." % name)
Example #8
0
 def get_param_as_string(self,
                         name: str,
                         default: Union[object, str] = MISSING) -> str:
     if name not in self._params:
         if default is not MISSING:
             return cast(str, default)
         raise errors.MissingRequiredParameterError(
             'Required parameter %r is missing.' % name)
     value = self._params[name]
     try:
         if value is None:
             return ''
         if type(value) is list:
             return ','.join(value)
         if type(value) is int or type(value) is float:
             return str(value)
         if type(value) is str:
             return value
     except TypeError:
         pass
     raise errors.InvalidParameterError(
         'Parameter %r must be a string value.' % name)