Exemplo n.º 1
0
def register_cli_commands(parser):
    """Register the CLI's meta-subcommands on `parser`."""
    for name, command in commands.items():
        help_title, help_body = parse_docstring(command)
        command_parser = parser.subparsers.add_parser(
            safe_name(name), help=help_title, description=help_title,
            epilog=help_body)
        command_parser.set_defaults(execute=command(command_parser))

    # Setup the snap commands into the maascli if in a snap and command exists.
    if 'SNAP' in os.environ:
        # Only import snappy if running under the snap.
        from maascli import snappy
        for name, command in [
                ('init', snappy.cmd_init),
                ('config', snappy.cmd_config),
                ('status', snappy.cmd_status),
                ('migrate', snappy.cmd_migrate)]:
            help_title, help_body = parse_docstring(command)
            command_parser = parser.subparsers.add_parser(
                safe_name(name), help=help_title, description=help_title,
                epilog=help_body)
            command_parser.set_defaults(execute=command(command_parser))

    # Setup and the allowed django commands into the maascli.
    management = get_django_management()
    if management is not None and is_maasserver_available():
        os.environ.setdefault(
            "DJANGO_SETTINGS_MODULE", "maasserver.djangosettings.settings")
        from django import setup as django_setup
        django_setup()
        load_regiond_commands(management, parser)
Exemplo n.º 2
0
 def test_normalises_whitespace(self):
     # parse_docstring can parse CRLF/CR/LF text, but always emits LF (\n,
     # new-line) separated text.
     self.assertEqual(("long title", ""),
                      utils.parse_docstring("long\r\ntitle"))
     self.assertEqual(("title", "body1\n\nbody2"),
                      utils.parse_docstring("title\n\nbody1\r\rbody2"))
Exemplo n.º 3
0
 def test_basic(self):
     self.assertEqual(("Title", "Body"),
                      utils.parse_docstring("Title\n\nBody"))
     self.assertEqual(
         ("A longer title", "A longer body"),
         utils.parse_docstring("A longer title\n\nA longer body"),
     )
Exemplo n.º 4
0
 def test_no_body(self):
     # parse_docstring returns an empty string when there's no body.
     self.assertEqual(
         ("Title", ""),
         utils.parse_docstring("Title\n\n"))
     self.assertEqual(
         ("Title", ""),
         utils.parse_docstring("Title"))
Exemplo n.º 5
0
 def test_basic(self):
     self.assertEqual(
         ("Title", "Body"),
         utils.parse_docstring("Title\n\nBody"))
     self.assertEqual(
         ("A longer title", "A longer body"),
         utils.parse_docstring(
             "A longer title\n\nA longer body"))
Exemplo n.º 6
0
 def test_normalises_whitespace(self):
     # parse_docstring can parse CRLF/CR/LF text, but always emits LF (\n,
     # new-line) separated text.
     self.assertEqual(
         ("long title", ""),
         utils.parse_docstring("long\r\ntitle"))
     self.assertEqual(
         ("title", "body1\n\nbody2"),
         utils.parse_docstring("title\n\nbody1\r\rbody2"))
Exemplo n.º 7
0
def register_cli_commands(parser):
    """Register the CLI's meta-subcommands on `parser`."""
    for name, command in commands.items():
        help_title, help_body = parse_docstring(command)
        command_parser = parser.subparsers.add_parser(
            safe_name(name), help=help_title, description=help_title,
            epilog=help_body)
        command_parser.set_defaults(execute=command(command_parser))
Exemplo n.º 8
0
Arquivo: api.py Projeto: ocni-dtu/maas
def register_handler(profile, handler, parser):
    """Register a resource's handler."""
    help_title, help_body = parse_docstring(handler["doc"])
    handler_name = handler_command_name(handler["name"])
    handler_parser = parser.subparsers.add_parser(
        handler_name, help=help_title, description=help_title, epilog=help_body
    )
    register_actions(profile, handler, handler_parser)
Exemplo n.º 9
0
def register_handler(profile, handler, parser):
    """Register a resource's handler."""
    help_title, help_body = parse_docstring(handler["doc"])
    handler_name = handler_command_name(handler["name"])
    handler_parser = parser.subparsers.add_parser(
        handler_name, help=help_title, description=help_title,
        epilog=help_body)
    register_actions(profile, handler, handler_parser)
Exemplo n.º 10
0
def prepare_parser(argv):
    """Create and populate an arguments parser for the maascli command."""
    help_title, help_body = parse_docstring(api)
    parser = ArgumentParser(description=help_body,
                            prog=argv[0],
                            epilog="http://maas.ubuntu.com/")
    register_cli_commands(parser)
    api.register_api_commands(parser)
    return parser
Exemplo n.º 11
0
def register_cli_commands(parser):
    """Register the CLI's meta-subcommands on `parser`."""
    for name, command in commands.items():
        help_title, help_body = parse_docstring(command)
        command_parser = parser.subparsers.add_parser(safe_name(name),
                                                      help=help_title,
                                                      description=help_title,
                                                      epilog=help_body)
        command_parser.set_defaults(execute=command(command_parser))
Exemplo n.º 12
0
def prepare_parser(argv):
    """Create and populate an arguments parser for the maascli command."""
    help_title, help_body = parse_docstring(api)
    parser = ArgumentParser(
        description=help_body, prog=argv[0],
        epilog="http://maas.ubuntu.com/")
    register_cli_commands(parser)
    api.register_api_commands(parser)
    return parser
Exemplo n.º 13
0
    def test_gets_docstring_from_function(self):
        # parse_docstring can extract the docstring when the argument passed
        # is not a string type.
        def example():
            """Title.

            Body.
            """

        self.assertEqual(("Title.", "Body."), utils.parse_docstring(example))
Exemplo n.º 14
0
    def test_gets_docstring_from_function(self):
        # parse_docstring can extract the docstring when the argument passed
        # is not a string type.
        def example():
            """Title.

            Body.
            """
        self.assertEqual(
            ("Title.", "Body."),
            utils.parse_docstring(example))
Exemplo n.º 15
0
 def add_command(name, command):
     help_title, help_body = parse_docstring(command)
     arg_name = safe_name(name)
     if command.hidden:
         command_parser = parser.subparsers.add_parser(arg_name)
     else:
         command_parser = parser.subparsers.add_parser(
             arg_name,
             help=help_title,
             description=help_title,
             epilog=help_body,
         )
     command_parser.set_defaults(execute=command(command_parser))
Exemplo n.º 16
0
def prepare_parser(argv):
    """Create and populate an arguments parser for the maascli command."""
    help_title, help_body = parse_docstring(api)
    parser = ArgumentParser(description=help_body,
                            prog=os.path.basename(argv[0]),
                            epilog="http://maas.io/")
    register_cli_commands(parser)
    api.register_api_commands(parser)
    parser.add_argument('--debug',
                        action='store_true',
                        default=False,
                        help=argparse.SUPPRESS)
    return parser
Exemplo n.º 17
0
    def test_unwrapping(self):
        # parse_docstring unwraps the title paragraph, and dedents the body
        # paragraphs.
        self.assertEqual(
            ("Title over two lines", "Paragraph over\ntwo lines\n\n"
             "Another paragraph\nover two lines"),
            utils.parse_docstring("""
                Title over
                two lines

                Paragraph over
                two lines

                Another paragraph
                over two lines
                """))
Exemplo n.º 18
0
def register_actions(profile, handler, parser):
    """Register a handler's actions."""
    for action in handler["actions"]:
        help_title, help_body = parse_docstring(action["doc"])
        action_name = safe_name(action["name"]).encode("ascii")
        action_bases = (Action, )
        action_ns = {
            "action": action,
            "handler": handler,
            "profile": profile,
        }
        action_class = type(action_name, action_bases, action_ns)
        action_parser = parser.subparsers.add_parser(action_name,
                                                     help=help_title,
                                                     description=help_title,
                                                     epilog=help_body)
        action_parser.set_defaults(execute=action_class(action_parser))
Exemplo n.º 19
0
def register_actions(profile, handler, parser):
    """Register a handler's actions."""
    for action in handler["actions"]:
        help_title, help_body = parse_docstring(action["doc"])
        action_name = safe_name(action["name"]).encode("ascii")
        action_bases = (Action,)
        action_ns = {
            "action": action,
            "handler": handler,
            "profile": profile,
            }
        action_class = type(action_name, action_bases, action_ns)
        action_parser = parser.subparsers.add_parser(
            action_name, help=help_title, description=help_title,
            epilog=help_body)
        action_parser.set_defaults(
            execute=action_class(action_parser))
Exemplo n.º 20
0
    def test_unwrapping(self):
        # parse_docstring unwraps the title paragraph, and dedents the body
        # paragraphs.
        self.assertEqual(
            ("Title over two lines",
             "Paragraph over\ntwo lines\n\n"
             "Another paragraph\nover two lines"),
            utils.parse_docstring("""
                Title over
                two lines

                Paragraph over
                two lines

                Another paragraph
                over two lines
                """))
Exemplo n.º 21
0
def register_actions(profile, handler, parser):
    """Register a handler's actions."""
    for action in handler["actions"]:
        help_title, help_body = parse_docstring(action["doc"])
        action_name = safe_name(action["name"])
        action_bases = get_action_class_bases(handler, action)
        action_ns = {
            "action": action,
            "handler": handler,
            "profile": profile,
            }
        action_class = type(action_name, action_bases, action_ns)
        action_parser = parser.subparsers.add_parser(
            action_name, help=help_title, description=help_title,
            epilog=help_body, add_help=False)
        action_parser.add_argument(
            '--help', '-h', action=ActionHelp, nargs=0,
            help="Show this help message and exit.")
        action_parser.set_defaults(execute=action_class(action_parser))
Exemplo n.º 22
0
def option_doc(option):
    """Return only the 'title' line from the option's docstring."""
    title, body = parse_docstring(option.__doc__)
    return title