Пример #1
0
    def test_pub_prefix(self):
        """Verify that misc.valid_pub_prefix returns True or False as
                expected."""

        self.assertFalse(misc.valid_pub_prefix(None))
        self.assertFalse(misc.valid_pub_prefix(""))
        self.assertFalse(misc.valid_pub_prefix("!@#$%^&*(*)"))
        self.assertTrue(misc.valid_pub_prefix("a0bc.def-ghi"))
Пример #2
0
        def test_pub_prefix(self):
                """Verify that misc.valid_pub_prefix returns True or False as
                expected."""

                self.assertFalse(misc.valid_pub_prefix(None))
                self.assertFalse(misc.valid_pub_prefix(""))
                self.assertFalse(misc.valid_pub_prefix("!@#$%^&*(*)"))
                self.assertTrue(misc.valid_pub_prefix(
                    "a0bc.def-ghi"))
Пример #3
0
 def __set_prefix(self, prefix):
     if not misc.valid_pub_prefix(prefix):
         raise api_errors.BadPublisherPrefix(prefix)
     self.__prefix = prefix
Пример #4
0
    def is_valid_attribute_value(cls, section, attr, value, raise_error=False):
        """Returns a boolean indicating whether the given attribute
                value is valid for the specified section and attribute.

                This function will raise an exception instead of returning a
                boolean is raise_error=True is specified.
                """
        def validate_uri(uri):
            try:
                valid = misc.valid_pub_url(uri)
            except KeyboardInterrupt:
                raise
            except:
                valid = False

            if not valid:
                raise ValueError()

        if cls.is_valid_attribute(section, attr, raise_error=raise_error):
            atype = cls.get_attribute_type(section, attr)
            # If the type is string, we always assume it is valid.
            # For all other types, we attempt a forced conversion
            # of the value; if it fails, we know the value isn't
            # valid for the given type.
            try:
                if atype == ATTR_TYPE_STR:
                    return True
                elif atype == ATTR_TYPE_INT:
                    int(value)
                elif atype == ATTR_TYPE_FLOAT:
                    float(value)
                elif atype == ATTR_TYPE_BOOL:
                    if str(value) not in ("True", "False"):
                        raise TypeError
                elif atype == ATTR_TYPE_UUID:
                    # None is valid for configuration
                    # purposes, even though UUID would
                    # fail.
                    if value is not None:
                        uuid.UUID(hex=str(value))
                elif atype == ATTR_TYPE_URI:
                    if value in (None, ""):
                        return True
                    validate_uri(value)
                elif atype == ATTR_TYPE_URI_LIST:
                    if not isinstance(value, list):
                        raise TypeError
                    for u in value:
                        validate_uri(u)
                elif atype in (ATTR_TYPE_PUB_ALIAS, ATTR_TYPE_PUB_PREFIX):
                    # For now, values are not required.
                    if value in (None, ""):
                        return True

                    # The same rules that apply to publisher
                    # prefixes also apply to aliases (for
                    # now).
                    if not misc.valid_pub_prefix(value):
                        raise ValueError()
                elif atype == ATTR_TYPE_REPO_COLL_TYPE:
                    if str(value) not in ("core", "supplemental"):
                        raise TypeError
                else:
                    raise RuntimeError(
                        "Unknown attribute type: %s" % \
                        atype)
            except (TypeError, ValueError, OverflowError):
                if raise_error:
                    raise InvalidAttributeValueError(
                        "Invalid value for %s.%s." % \
                        (section, attr))
                else:
                    return False
        else:
            return False
        return True
Пример #5
0
 def __set_prefix(self, prefix):
         if not misc.valid_pub_prefix(prefix):
                 raise api_errors.BadPublisherPrefix(prefix)
         self.__prefix = prefix
Пример #6
0
        def is_valid_attribute_value(cls, section, attr, value,
            raise_error=False):
                """Returns a boolean indicating whether the given attribute
                value is valid for the specified section and attribute.

                This function will raise an exception instead of returning a
                boolean is raise_error=True is specified.
                """

                def validate_uri(uri):
                        try:
                                valid = misc.valid_pub_url(uri)
                        except KeyboardInterrupt:
                                raise
                        except:
                                valid = False

                        if not valid:
                                raise ValueError()

                if cls.is_valid_attribute(section, attr,
                    raise_error=raise_error):
                        atype = cls.get_attribute_type(section, attr)
                        # If the type is string, we always assume it is valid.
                        # For all other types, we attempt a forced conversion
                        # of the value; if it fails, we know the value isn't
                        # valid for the given type.
                        try:
                                if atype == ATTR_TYPE_STR:
                                        return True
                                elif atype == ATTR_TYPE_INT:
                                        int(value)
                                elif atype == ATTR_TYPE_FLOAT:
                                        float(value)
                                elif atype == ATTR_TYPE_BOOL:
                                        if str(value) not in ("True", "False"):
                                                raise TypeError
                                elif atype == ATTR_TYPE_UUID:
                                        # None is valid for configuration
                                        # purposes, even though UUID would
                                        # fail.
                                        if value is not None:
                                                uuid.UUID(hex=str(value))
                                elif atype == ATTR_TYPE_URI:
                                        if value in (None, ""):
                                                return True
                                        validate_uri(value)
                                elif atype == ATTR_TYPE_URI_LIST:
                                        if not isinstance(value, list):
                                                raise TypeError
                                        for u in value:
                                                validate_uri(u)
                                elif atype in (ATTR_TYPE_PUB_ALIAS,
                                    ATTR_TYPE_PUB_PREFIX):
                                        # For now, values are not required.
                                        if value in (None, ""):
                                                return True

                                        # The same rules that apply to publisher
                                        # prefixes also apply to aliases (for
                                        # now).
                                        if not misc.valid_pub_prefix(value):
                                                raise ValueError()
                                elif atype == ATTR_TYPE_REPO_COLL_TYPE:
                                        if str(value) not in ("core",
                                            "supplemental"):
                                                raise TypeError
                                else:
                                        raise RuntimeError(
                                            "Unknown attribute type: %s" % \
                                            atype)
                        except (TypeError, ValueError, OverflowError):
                                if raise_error:
                                        raise InvalidAttributeValueError(
                                            "Invalid value for %s.%s." % \
                                            (section, attr))
                                else:
                                        return False
                else:
                        return False
                return True