Exemplo n.º 1
0
    def validate(self, pedantic=False):
        """ a set of checks to validate data before we export the feed"""

        if not all([x in self.data.keys() for x in self.required]):
            missing_fields = ", ".join(
                set(self.required).difference(set(self.data.keys())))
            raise CbInvalidFeed("FeedInfo missing required field(s): %s" %
                                missing_fields)

        # validate shortname of this field is just a-z and 0-9, with at least one character
        if not self.data["name"].isalnum():
            raise CbInvalidFeed(
                "Feed name %s may only contain a-z, A-Z, 0-9 and must have one character"
                % self.data["name"])

        # if icon exists and points to a file, grab the bytes
        # and base64 them
        if "icon" in self.data and os.path.exists(self.data["icon"]):
            # TODO - enforce size restrictions? dimensions?  orientation?
            # raise CbIconError("...")

            icon_path = self.data.pop("icon")
            try:
                self.data["icon"] = base64.b64encode(
                    open(icon_path, "r").read())
            except Exception, err:
                raise CbIconError(
                    "Unknown error reading/encoding icon data: %s" % err)
Exemplo n.º 2
0
    def validate(self, pedantic=False):
        """ a set of checks to validate data before we export the feed"""

        if not all([x in self.data.keys() for x in self.required]):
            missing_fields = ", ".join(
                set(self.required).difference(set(self.data.keys())))
            raise CbInvalidFeed("FeedInfo missing required field(s): %s" %
                                missing_fields)

        # verify no non-supported keys are present
        for key in self.data.keys():
            if key not in self.required and key not in self.optional:
                raise CbInvalidFeed("FeedInfo includes extraneous key '%s'" %
                                    key)

        # check to see if icon_field can be base64 decoded
        for icon_field in ["icon", "icon_small"]:
            try:
                base64.b64decode(self.data[icon_field])
            except TypeError, err:
                raise CbIconError("Icon must either be path or base64 data.  \
                                        Path does not exist and base64 decode failed with: %s"
                                  % err)
            except KeyError as err:
                # we don't want to cause a ruckus if the icon is missing
                pass
Exemplo n.º 3
0
class CbFeedInfo(object):
    def __init__(self, **kwargs):
        # these fields are required in every feed descriptor
        self.required = [
            "name", "display_name", "version", "summary", "tech_data",
            "provider_url"
        ]
        self.data = kwargs
        self.data["version"] = 1

    def dump(self):
        self.validate()
        return self.data

    def validate(self, pedantic=False):
        """ a set of checks to validate data before we export the feed"""

        if not all([x in self.data.keys() for x in self.required]):
            missing_fields = ", ".join(
                set(self.required).difference(set(self.data.keys())))
            raise CbInvalidFeed("FeedInfo missing required field(s): %s" %
                                missing_fields)

        # validate shortname of this field is just a-z and 0-9, with at least one character
        if not self.data["name"].isalnum():
            raise CbInvalidFeed(
                "Feed name %s may only contain a-z, A-Z, 0-9 and must have one character"
                % self.data["name"])

        # if icon exists and points to a file, grab the bytes
        # and base64 them
        if "icon" in self.data and os.path.exists(self.data["icon"]):
            # TODO - enforce size restrictions? dimensions?  orientation?
            # raise CbIconError("...")

            icon_path = self.data.pop("icon")
            try:
                self.data["icon"] = base64.b64encode(
                    open(icon_path, "r").read())
            except Exception, err:
                raise CbIconError(
                    "Unknown error reading/encoding icon data: %s" % err)
        # otherwise, double-check it's valid base64
        elif "icon" in self.data:
            try:
                base64.b64decode(self.data["icon"])
            except TypeError, err:
                raise CbIconError("Icon must either be path or base64 data.  \
                                    Path does not exist and base64 decode failed with: %s"
                                  % err)
Exemplo n.º 4
0
    def validate(self, pedantic=False):
        """ a set of checks to validate data before we export the feed"""

        if not all([x in self.data.keys() for x in self.required]):
            missing_fields = ", ".join(
                set(self.required).difference(set(self.data.keys())))
            raise CbInvalidFeed("FeedInfo missing required field(s): %s" %
                                missing_fields)

        # verify no non-supported keys are present
        for key in self.data.keys():
            if key not in self.required and key not in self.optional:
                raise CbInvalidFeed("FeedInfo includes extraneous key '%s'" %
                                    key)

        # check to see if icon_field can be base64 decoded
        for icon_field in ["icon", "icon_small"]:
            try:
                base64.b64decode(self.data[icon_field])
            except TypeError as err:
                raise CbIconError("Icon must either be path or base64 data.  \
                                        Path does not exist and base64 decode failed with: %s"
                                  % err)
            except KeyError as err:
                # we don't want to cause a ruckus if the icon is missing
                pass

        # all fields in feedinfo must be strings
        for key in self.data.keys():
            if not isinstance(self.data[key], str):
                raise CbInvalidFeed(
                    "FeedInfo field %s must be of type %s, the field \
                                    %s is of type %s " %
                    (key, "unicode", key, type(self.data[key])))

        # certain fields, when present, must not be empty strings
        for key in self.data.keys():
            if key in self.noemptystrings and self.data[key] == "":
                raise CbInvalidFeed(
                    "The '%s' field must not be an empty string" % key)

        # validate shortname of this field is just a-z and 0-9, with at least one character
        if not self.data["name"].isalnum():
            raise CbInvalidFeed(
                "Feed name %s may only contain a-z, A-Z, 0-9 and must have one character"
                % self.data["name"])

        return True
Exemplo n.º 5
0
    def __init__(self, **kwargs):
        # these fields are required in every feed descriptor
        self.required = ["name", "display_name",
                         "summary", "tech_data", "provider_url"]
        self.optional = ["category", "icon", "version", "icon_small"]
        self.noemptystrings = ["name", "display_name", "summary", "tech_data", "category"]
        self.data = kwargs

        # if they are present, set the icon fields of the data to hold
        # the base64 encoded file data from their path
        for icon_field in ["icon", "icon_small"]:
            if icon_field in self.data and os.path.exists(self.data[icon_field]):
                icon_path = self.data.pop(icon_field)
                try:
                    self.data[icon_field] = base64.b64encode(open(icon_path, "rb").read()).decode("utf-8")
                except Exception as err:
                    raise CbIconError("Unknown error reading/encoding icon data: %s" % err)