Exemplo n.º 1
0
    def generate_argument_parser(cls, tree, actions={}):
        """Generates argument parser for given assistant tree and actions.

        Args:
            tree: assistant tree as returned by
                  devassistant.assistant_base.AssistantBase.get_subassistant_tree
            actions: dict mapping actions (devassistant.actions.Action subclasses) to their
                     subaction dicts
        Returns:
            instance of devassistant_argparse.ArgumentParser (subclass of argparse.ArgumentParser)
        """
        cur_as, cur_subas = tree
        parser = devassistant_argparse.ArgumentParser(argument_default=argparse.SUPPRESS,
                                                      usage=argparse.SUPPRESS,
                                                      add_help=False)

        cls.add_default_arguments_to(parser)

        # add any arguments of the top assistant
        for arg in cur_as.args:
            arg.add_argument_to(parser)

        if cur_subas or actions:
            # then add the subassistants as arguments
            subparsers = cls._add_subparsers_required(parser,
                dest=settings.SUBASSISTANT_N_STRING.format('0'))
            for subas in sorted(cur_subas, key=lambda x: x[0].name):
                for alias in [subas[0].name] + getattr(subas[0], 'aliases', []):
                    cls.add_subassistants_to(subparsers, subas, level=1, alias=alias)

            for action, subactions in sorted(actions.items(), key=lambda x: x[0].name):
                cls.add_action_to(subparsers, action, subactions, level=1)

        return parser
Exemplo n.º 2
0
    def generate_argument_parser(cls, chain):
        cur_as, cur_subas = chain
        parser = devassistant_argparse.ArgumentParser(
            description=cur_as.description, argument_default=argparse.SUPPRESS)

        # add any arguments of the top assistant
        for arg in cur_as.args:
            arg.add_argument_to(parser)

        if cur_subas:
            # then add the subassistants as arguments
            subparsers = parser.add_subparsers(
                dest=settings.SUBASSISTANT_N_STRING.format('0'),
                title=cls.subassistants_string,
                description=cls.subparsers_description)
            # from Python 3.3, subparsers are optional by default => make them required
            subparsers.required = True
            for subas in cur_subas:
                cls.add_subparsers_to(subas, subparsers, level=1)

        return parser
Exemplo n.º 3
0
    def generate_argument_parser(cls, tree, actions={}):
        """Generates argument parser for given assistant tree and actions.

        Args:
            tree: assistant tree as returned by
                  devassistant.assistant_base.AssistantBase.get_subassistant_tree
            actions: dict mapping action names to devassistant.actions.Action subclasses
        Returns:
            instance of devassistant_argparse.ArgumentParser (subclass of argparse.ArgumentParser)
        """
        cur_as, cur_subas = tree
        parser = devassistant_argparse.ArgumentParser(argument_default=argparse.SUPPRESS,
                                                      usage=argparse.SUPPRESS,
                                                      add_help=False)

        # add --debug to the top parser (GUI does this completely differently)
        parser.add_argument('--debug',
                            help='Show debug output (may be a verbose a lot!).',
                            action='store_true',
                            dest='da_debug',
                            default=False)

        # add any arguments of the top assistant
        for arg in cur_as.args:
            arg.add_argument_to(parser)

        if cur_subas or actions:
            # then add the subassistants as arguments
            subparsers = parser.add_subparsers(dest=settings.SUBASSISTANT_N_STRING.format('0'))
            # from Python 3.3, subparsers are optional by default => make them required
            subparsers.required=True
            for subas in sorted(cur_subas, key=lambda x: x[0].name):
                cls.add_subassistants_to(subparsers, subas, level=1)

            for action_name, action in sorted(actions.items()):
                cls.add_action_to(subparsers, action)

        return parser