Ejemplo n.º 1
0
    def expand(self, dest, model_type, group_name=None, patches=None):
        # TODO:
        # two privates symbols are imported here. they should be made public or this utility class
        # should be moved into azure.cli.core
        from knack.introspection import extract_args_from_signature, option_descriptions

        self._check_stale()
        if not self._applicable():
            return

        if not patches:
            patches = dict()

        # fetch the documentation for model parameters first. for models, which are the classes
        # derive from msrest.serialization.Model and used in the SDK API to carry parameters, the
        # document of their properties are attached to the classes instead of constructors.
        parameter_docs = option_descriptions(model_type)

        def get_complex_argument_processor(expanded_arguments, assigned_arg,
                                           model_type):
            """
            Return a validator which will aggregate multiple arguments to one complex argument.
            """
            def _expansion_validator_impl(namespace):
                """
                The validator create a argument of a given type from a specific set of arguments from CLI
                command.
                :param namespace: The argparse namespace represents the CLI arguments.
                :return: The argument of specific type.
                """
                ns = vars(namespace)
                kwargs = dict(
                    (k, ns[k]) for k in ns if k in set(expanded_arguments))

                setattr(namespace, assigned_arg, model_type(**kwargs))

            return _expansion_validator_impl

        expanded_arguments = []
        for name, arg in extract_args_from_signature(
                model_type.__init__, excluded_params=EXCLUDED_PARAMS):
            arg = arg.type
            if name in parameter_docs:
                arg.settings['help'] = parameter_docs[name]

            if group_name:
                arg.settings['arg_group'] = group_name

            if name in patches:
                patches[name](arg)

            self.extra(name, arg_type=arg)
            expanded_arguments.append(name)

        dest_option = ['--__{}'.format(dest.upper())]
        self.argument(dest,
                      arg_type=ignore_type,
                      options_list=dest_option,
                      validator=get_complex_argument_processor(
                          expanded_arguments, dest, model_type))
Ejemplo n.º 2
0
    def expand(self, dest, model_type, group_name=None, patches=None):
        # TODO:
        # two privates symbols are imported here. they should be made public or this utility class
        # should be moved into azure.cli.core
        from knack.introspection import extract_args_from_signature, option_descriptions

        self._check_stale()
        if not self._applicable():
            return

        if not patches:
            patches = dict()

        # fetch the documentation for model parameters first. for models, which are the classes
        # derive from msrest.serialization.Model and used in the SDK API to carry parameters, the
        # document of their properties are attached to the classes instead of constructors.
        parameter_docs = option_descriptions(model_type)

        def get_complex_argument_processor(expanded_arguments, assigned_arg, model_type):
            """
            Return a validator which will aggregate multiple arguments to one complex argument.
            """

            def _expansion_validator_impl(namespace):
                """
                The validator create a argument of a given type from a specific set of arguments from CLI
                command.
                :param namespace: The argparse namespace represents the CLI arguments.
                :return: The argument of specific type.
                """
                ns = vars(namespace)
                kwargs = dict((k, ns[k]) for k in ns if k in set(expanded_arguments))

                setattr(namespace, assigned_arg, model_type(**kwargs))

            return _expansion_validator_impl

        expanded_arguments = []
        for name, arg in extract_args_from_signature(model_type.__init__, excluded_params=EXCLUDED_PARAMS):
            arg = arg.type
            if name in parameter_docs:
                arg.settings['help'] = parameter_docs[name]

            if group_name:
                arg.settings['arg_group'] = group_name

            if name in patches:
                patches[name](arg)

            self.extra(name, arg_type=arg)
            expanded_arguments.append(name)

        dest_option = ['--__{}'.format(dest.upper())]
        self.argument(dest,
                      arg_type=ignore_type,
                      options_list=dest_option,
                      validator=get_complex_argument_processor(expanded_arguments, dest, model_type))
Ejemplo n.º 3
0
 def test_option_desc_simple(self):
     option_descs = option_descriptions(op1)
     self.assertIn('arg3', option_descs)
     self.assertEqual(option_descs['arg3'], 'This is an arg for a test.')
Ejemplo n.º 4
0
def create_args_for_complex_type(arg_ctx, dest, model_type, arguments):
    '''
    Creates args that will be combined into an object by an arg validator.
    '''

    from knack.arguments import ignore_type
    from knack.introspection import option_descriptions

    def get_complex_argument_processor(expanded_arguments, assigned_arg,
                                       model_type):
        '''
        Return a validator which will aggregate multiple arguments to one complex argument.
        '''
        def _expansion_validator_impl(namespace):
            '''
            The validator create a argument of a given type from a specific set of arguments from CLI
            command.
            :param namespace: The argparse namespace represents the CLI arguments.
            :return: The argument of specific type.
            '''
            ns = vars(namespace)
            kwargs = dict(
                (k, ns[k]) for k in ns if k in set(expanded_arguments))
            setattr(namespace, assigned_arg, model_type(**kwargs))

        return _expansion_validator_impl

    # Fetch the documentation for model parameters first. for models, which are the classes
    # derive from msrest.serialization.Model and used in the SDK API to carry parameters, the
    # document of their properties are attached to the classes instead of constructors.
    parameter_docs = option_descriptions(model_type)

    for name in arguments:
        # Get the validation map from the model type in order to determine
        # whether the argument should be required
        validation = model_type._validation.get(name, None)  # pylint: disable=protected-access
        required = validation.get('required', False) if validation else False

        # Generate the command line argument name from the property name
        options_list = ['--' + name.replace('_', '-')]

        # Get the help text from the model type
        help_text = parameter_docs.get(name, None)

        # Create the additional command line argument
        arg_ctx.extra(name,
                      required=required,
                      options_list=options_list,
                      help=help_text)

    # Rename the original command line argument and ignore it (i.e. make invisible)
    # so that it does not show up on command line and does not conflict with any other
    # arguments.
    dest_option = ['--__{}'.format(dest.upper())]

    arg_ctx.argument(
        dest,
        arg_type=ignore_type,
        options_list=dest_option,
        # The argument is hidden from the command line, but its value
        # will be populated by this validator.
        validator=get_complex_argument_processor(arguments, dest, model_type))
Ejemplo n.º 5
0
def create_args_for_complex_type(arg_ctx, dest, model_type, arguments):
    '''
    Creates args that will be combined into an object by an arg validator.
    '''

    from knack.arguments import ignore_type
    from knack.introspection import option_descriptions

    def get_complex_argument_processor(expanded_arguments, assigned_arg, model_type):
        '''
        Return a validator which will aggregate multiple arguments to one complex argument.
        '''

        def _expansion_validator_impl(namespace):
            '''
            The validator create a argument of a given type from a specific set of arguments from CLI
            command.
            :param namespace: The argparse namespace represents the CLI arguments.
            :return: The argument of specific type.
            '''
            ns = vars(namespace)
            kwargs = dict((k, ns[k]) for k in ns if k in set(expanded_arguments))
            setattr(namespace, assigned_arg, model_type(**kwargs))

        return _expansion_validator_impl

    # Fetch the documentation for model parameters first. for models, which are the classes
    # derive from msrest.serialization.Model and used in the SDK API to carry parameters, the
    # document of their properties are attached to the classes instead of constructors.
    parameter_docs = option_descriptions(model_type)

    for name in arguments:
        # Get the validation map from the model type in order to determine
        # whether the argument should be required
        validation = model_type._validation.get(name, None)  # pylint: disable=protected-access
        required = validation.get('required', False) if validation else False

        # Generate the command line argument name from the property name
        options_list = ['--' + name.replace('_', '-')]

        # Get the help text from the model type
        help_text = parameter_docs.get(name, None)

        # Create the additional command line argument
        arg_ctx.extra(
            name,
            required=required,
            options_list=options_list,
            help=help_text)

    # Rename the original command line argument and ignore it (i.e. make invisible)
    # so that it does not show up on command line and does not conflict with any other
    # arguments.
    dest_option = ['--__{}'.format(dest.upper())]

    arg_ctx.argument(dest,
                     arg_type=ignore_type,
                     options_list=dest_option,
                     # The argument is hidden from the command line, but its value
                     # will be populated by this validator.
                     validator=get_complex_argument_processor(arguments, dest, model_type))