示例#1
0
    def parse_date(self, arg):
        """ Parse a date argument """

        if arg:
            try:
                parameter = datetime.datetime.strptime(
                    arg, '%Y%m%d').date().isoformat()
                return parameter
            except Exception as e:
                raise exceptions.ParameterValueError(key=arg, value=arg,
                                                     message=e.message)
示例#2
0
    def parse_range(self, arg):
        """Parser for arguments that are numerical range types.

        Takes the argument to check as an input (e.g. revenue).
        Expect an argument of the format: n-N
        Returns lower and upper bounds. Negative values are permitted.
        Returns None if parsing failed."""

        arg_param = self.get_argument(arg, None)
        if arg_param:
            exp = "^([-]*[0-9]*)[-]([-]*[0-9]*)$"
            m = re.search(exp, arg_param)
            if not m:
                raise exceptions.ParameterValueError(key=arg, value=arg_param)

            lbound, ubound = None, None
            # Parse lower bound
            if m.group(1):
                try:
                    lbound = int(m.group(1))
                except ValueError:
                    raise exceptions.ParameterValueError(key=arg,
                                                         value=arg_param)

            # Parse upper bound
            if m.group(2):
                try:
                    ubound = int(m.group(2))
                except ValueError:
                    raise exceptions.ParameterValueError(key=arg,
                                                         value=arg_param)

            if lbound != None and ubound != None and lbound > ubound:
                raise exceptions.ParameterValueError(key=arg, value=arg_param)

            return lbound, ubound
        else:
            return None, None
示例#3
0
    def parse_boolean(self, arg):
        """Parse boolean argument types

        Returns True or False if argument is present, otherwise None."""

        arg_param = self.get_argument(arg, None)
        if not arg_param:
            return None

        arg_check_int = re.search("^[0-1]$", arg_param)
        arg_check_bool = re.search("^true|false", arg_param.lower())
        if arg_check_int:
            return bool(int(arg_param))
        elif arg_check_bool:
            return {"true": True, "false": False}[arg_param.lower()]
        else:
            raise exceptions.ParameterValueError(key=arg, value=arg_param)