Ejemplo n.º 1
0
    def __new__(cls, name, bases, attrs):
        attrs = attrs if attrs else {}

        # Set the options on the plugin by finding all the Options and
        # setting them. This also removes the original Option attributes.
        global_options = []
        groups = []

        option_parser = OptionParser()

        for key,val in attrs.items():
            if key == "_usage":
                option_parser.set_usage(val)
                del attrs[key]
            elif isinstance(val, Option):
                # We set the destination of the Option to always be the
                # attribute key...
                val.dest = key

                # Append it to the list of options and delete it from
                # the original attributes list
                global_options.append(val)
                del attrs[key]
            elif isinstance(val, OptionGroup):
                groups.append(val.get_option_group(option_parser))

        # Need to iterate through the bases in order to extract the
        # list of parent options, so we can inherit those.
        for base in bases:
            if hasattr(base, "_options"):
                global_options.extend(getattr(base, "_options"))

        for option in global_options:
            try:
                option_parser.add_option(option)
            except OptionConflictError:
                pass
        for group in groups:
            option_parser.add_option_group(group)

        # Store the option list and create the option parser
        attrs["_option_parser"] = option_parser
        attrs["_options"] = global_options

        # Create the class
        return super(PluginMeta, cls).__new__(cls, name, bases, attrs)