Esempio n. 1
0
def number(value, types=None, parser=None, allow_inf=False, min_=1,
           flag=None):
    """convert the given value to a number"""
    if value == "auto":
        return value

    # Internally used
    if isinstance(value, NUMERIC_TYPES):  # pragma: no cover
        return value

    elif value.lower() in INFINITE and allow_inf:
        return float("inf")

    elif value.lower() in INFINITE and not allow_inf:
        parser.error("%s does not allow an infinite value" % flag)

    try:
        value = convert.ston(value, types=types or NUMERIC_TYPES)
        if min_ is not None and value < min_:
            parser.error(
                "%s's value must be greater than %s" % (flag, min_))
        return value

    except SyntaxError:  # could not even parse the string as code
        parser.error(
            "%s failed to convert %s to a number" % (flag, repr(value)))

    except ValueError:  # it's a number, but not the right type
        parser.error(
            "%s, %s is not an instance of %s" % (flag, repr(value), types))
Esempio n. 2
0
def uidgid(value=None, flag=None,
           get_id=None, check_id=None, set_id=None,
           parser=None):  # pragma: no cover
        """
        Retrieves and validates the user or group id for a command line flag
        """
        # make sure the partial function is setting
        # the input values
        assert flag is not None
        assert get_id is not None
        assert check_id is not None
        assert set_id is not None

        if set_id is NotImplemented:
            logger.info("%s is ignored on %s" % (flag, OS.title()))
            return

        elif not value:
            return

        # convert the incoming argument to a number or fail
        try:
            value = convert.ston(value)
        except ValueError:
            parser.error("failed to convert %s to a number" % flag)

        # make sure the id actually exists
        try:
            check_id(value)
        except KeyError:
            parser.error(
                "%s %s does not seem to exist" % (flag, value))

        # get the uid/gid of the current process
        # so we can reset it after checking it
        original_id = get_id()

        # Try to set the uid/gid to the value requested, fail
        # otherwise.  We're doing this here because we'll be
        # able to stop the rest of the code from running faster
        # and provide a more useful error message.
        try:
            set_id(value)
        except OSError:
            parser.error(
                "Failed to set %s to %s, please make sure you have "
                "the necessary permissions to perform this action.  "
                "Typically you must be running as root." % (flag, value))

        # set the uid/gid back to the original value since
        # the id change should occur inside the form or right
        # before the agent is started
        try:
            set_id(original_id)
        except OSError:
            parser.error(
                "failed to set %s back to the original value" % flag)

        return value
Esempio n. 3
0
    def test_convert_ston_error(self):
        with self.assertRaises(TypeError):
            convert.ston(None)

        with self.assertRaises(ValueError):
            convert.ston("foo")

        with self.assertRaises(ValueError):
            convert.ston("[]")
Esempio n. 4
0
def port(value, parser=None, get_uid=None, flag=None):
    """convert and check to make sure the provided port is valid"""
    assert callable(get_uid)
    try:
        value = convert.ston(value)
    except (ValueError, SyntaxError):
        parser.error("%s requires a number" % flag)
    else:
        try:
            low_port = 1 if get_uid() == 0 else 49152
        except AttributeError:
            low_port = 49152

        high_port = 65535

        if low_port > value or value > high_port:
            parser.error(
                "valid port range is %s to %s" % (low_port, high_port))

        return value
Esempio n. 5
0
 def test_convert_ston(self):
     self.assertEqual(convert.ston(42), 42)
     self.assertEqual(convert.ston("42"), 42)