Esempio n. 1
0
    def validate_definition(cls, definition):
        errors = []
        if "media_type" not in definition:
            errors.append("is missing `media_type`")
        if definition["media_type"] not in MEDIA_TYPES:
            errors.append("has an invalid `media_type`")

        if "confidence" not in definition:
            errors.append("is missing `confidence`")
        if not isinstance(definition["confidence"], (int, float)):
            errors.append("must provide `confidence` as a number")

        if "perform" not in definition:
            errors.append("is missing `perform`")

        for perform in as_list(definition["perform"]):
            for key, options in perform.items():
                options["target"] = None

                if key in registry:
                    action = registry[key](options)
                    try:
                        action.validate_options()
                    except click.ClickException, e:
                        errors.append("error: %s" % e.message)
                else:
                    errors.append("references unknown perform: '%s'" % key)
Esempio n. 2
0
    def perform(self, dry_run):
        path = self.options["target"].path
        contents = [os.path.join(path, f) for f in os.listdir(path)]
        only = as_list(self.options.get("only", []))
        exclude = as_list(self.options.get("exclude", []))

        if only:
            logger.debug("Only: %s" % only)
        if exclude:
            logger.debug("Exclude: %s" % exclude)

        for f in contents:
            ext = os.path.splitext(f)[1]
            if ext.startswith("."):
                ext = ext[1:]

            if (only and ext in only) or (not only and exclude and ext not in exclude) or (not only and not exclude):
                self.copy_file_or_dir(f, dry_run=dry_run)

            else:
                logger.debug("%s did not pass extension filter" % f)
Esempio n. 3
0
    def from_config(cls, action_hash, classification):

        try:
            actions = [
                registry[action](options) for d in as_list(action_hash["perform"]) for action, options in d.items()
            ]

            for action in actions:
                action.options["target"] = classification
                action.validate_options()

            return actions
        except KeyError, e:
            msg = "Available actions: %s" % registry.keys()
            logger.error(e.message)
            logger.error(msg)
            raise click.ClickException(msg)