Beispiel #1
0
    def test_get_arguments(self) -> None:
        input_string = """
        :type name: string
        :param name: The Bucket's name identifier. This **must** be set.

        **Request Syntax**::
        ::

            response = client.abort_multipart_upload(
                name='string'
            )

        """
        service_name_mock = MagicMock()
        docstring_parser = DocstringParser(service_name_mock, "ClassName",
                                           "method_name", [])
        result = docstring_parser.get_arguments(input_string)
        assert len(result) == 1
        assert result[0].name == "name"
        assert result[0].type_annotation == Type.str

        input_string = """
        :type name string
        :param name The Bucket's name identifier. This **must** be set.

        **Request Syntax**::
        ::

            response = client.abort_multipart_upload(
                name='string'

        """
        assert docstring_parser.get_arguments(input_string)
Beispiel #2
0
def parse_method(parent_name: str, name: str, method: FunctionType,
                 service_name: ServiceName) -> Method:
    """
    Parse method to a structure.

    Arguments:
        parent_name -- Parent class name.
        method -- Inspect method.

    Returns:
        Method structure.
    """
    logger = get_logger()
    docstring = textwrap.dedent(inspect.getdoc(method) or "")
    method_name = f"{parent_name}.{name}"

    logger.debug(f"Slow parsing of {method_name}: {len(docstring)} chars")
    prefix = f"{get_class_prefix(parent_name)}{get_class_prefix(name)}"
    arg_spec_parser = ArgSpecParser(prefix, service_name)

    arguments = get_method_arguments_stub(service_name, parent_name, name)
    if arguments is None:
        arguments = arg_spec_parser.get_arguments(parent_name, name, method)
        docstring_parser = DocstringParser(service_name, parent_name, name,
                                           arguments)
        arguments = docstring_parser.get_arguments(docstring)

    return_type = arg_spec_parser.get_return_type(parent_name, name)
    if return_type is None:
        return_type = DocstringParser(service_name, parent_name, name,
                                      []).get_return_type(docstring)

    return Method(name=name, arguments=arguments, return_type=return_type)
Beispiel #3
0
 def test_get_arguments(self) -> None:
     input_string = """
     :type name: string
     :param name: The Bucket's name identifier. This **must** be set.
     """
     service_name_mock = MagicMock()
     docstring_parser = DocstringParser(service_name_mock, "ClassName",
                                        "method_name", [])
     result = docstring_parser.get_arguments(input_string)
     assert len(result) == 1
     assert result[0].name == "name"
     assert result[0].type_annotation == Type.str
 def test_get_arguments(self) -> None:
     input_string = """
     :type name: string
     :param name: The Bucket's name identifier. This **must** be set.
     """
     service_name_mock = MagicMock()
     docstring_parser = DocstringParser(
         service_name_mock, "ClassName", "method_name", []
     )
     result = docstring_parser.get_arguments(input_string)
     self.assertEqual(len(result), 1)
     self.assertEqual(result[0].name, "name")
     self.assertEqual(result[0].type, Type.str)
def parse_method(
    parent_name: str, name: str, method: MethodType, service_name: ServiceName
) -> Method:
    """
    Parse method to a structure.

    Arguments:
        parent_name -- Parent class name.
        method -- Inspect method.

    Returns:
        Method structure.
    """
    logger = get_logger()
    docstring = textwrap.dedent(inspect.getdoc(method) or "")
    method_name = f"{parent_name}.{name}"

    logger.debug(f"Slow parsing of {method_name}: {len(docstring)} chars")
    prefix = f"{get_class_prefix(parent_name)}{get_class_prefix(name)}"
    arg_spec_parser = ArgSpecParser(prefix, service_name)

    arguments = get_method_arguments_stub(service_name, parent_name, name)
    if arguments is None:
        arguments = arg_spec_parser.get_arguments(parent_name, name, method)
        docstring_parser = DocstringParser(service_name, parent_name, name, arguments)
        arguments = docstring_parser.get_arguments(docstring)

    # do not add kwonly flag to resource generators
    if len(arguments) > 1 and not name[0].isupper():
        arguments.insert(1, Argument.kwflag())

    return_type = arg_spec_parser.get_return_type(parent_name, name)
    if return_type is None:
        return_type = DocstringParser(service_name, parent_name, name, []).get_return_type(
            docstring
        )

    result = Method(name=name, arguments=arguments, return_type=return_type)
    result.request_type_annotation = result.get_request_type_annotation(
        f"{parent_name}{get_class_prefix(name)}RequestTypeDef"
    )
    return result
Beispiel #6
0
 def test_init(self) -> None:
     argument_mock = MagicMock()
     argument_mock.name = "argument_name"
     argument_mock.prefix = "*"
     service_name_mock = MagicMock()
     docstring_parser = DocstringParser(service_name_mock, "ClassName",
                                        "method_name", [argument_mock])
     assert docstring_parser.service_name == service_name_mock
     assert docstring_parser.prefix == "ClassNameMethodName"
     assert docstring_parser.class_name == "ClassName"
     assert docstring_parser.method_name == "method_name"
     assert docstring_parser.arguments_map == {}
 def test_init(self) -> None:
     argument_mock = MagicMock()
     argument_mock.name = "argument_name"
     argument_mock.prefix = "*"
     service_name_mock = MagicMock()
     docstring_parser = DocstringParser(
         service_name_mock, "ClassName", "method_name", [argument_mock]
     )
     self.assertEqual(docstring_parser.service_name, service_name_mock)
     self.assertEqual(docstring_parser.prefix, "ClassNameMethodName")
     self.assertEqual(docstring_parser.class_name, "ClassName")
     self.assertEqual(docstring_parser.method_name, "method_name")
     self.assertEqual(
         docstring_parser.arguments_map, {"*argument_name": argument_mock,}
     )
Beispiel #8
0
    def test_get_return_type(self) -> None:
        input_string = """
        :type name: string
        :param name: The Bucket's name identifier. This **must** be set.

        :rtype: :py:class:`S3.Bucket`
        :returns: A Bucket resource
        """
        service_name_mock = MagicMock()
        docstring_parser = DocstringParser(service_name_mock, "ClassName",
                                           "method_name", [])
        result = docstring_parser.get_return_type(input_string)
        assert result.name == "Bucket"

        input_string = """
        :type name: string
        :param name: The Bucket's name identifier. This **must** be set.

        :rtype: dict
        :returns: 

        **Response Syntax** 

        ::

            {
                'RequestCharged': 'requester'
            }
        **Response Structure** 

        - *(dict) --* 

            - **RequestCharged** *(string) --* 

            If present, indicates that the requester was successfully charged for the request.
        """

        result = docstring_parser.get_return_type(input_string)
        assert result.name == "ClassNameMethodNameResponseTypeDef"

        input_string = ":returns: None"
        result = docstring_parser.get_return_type(input_string)
        assert result == Type.none

        input_string = ":returns: Always returns False"
        result = docstring_parser.get_return_type(input_string)
        assert result == Type.bool

        input_string = ":returns: unknown"
        result = docstring_parser.get_return_type(input_string)
        assert result == Type.none

        input_string = """
        :returninvalid
        :returns: real
        """
        result = docstring_parser.get_return_type(input_string)
        assert result == Type.none

        input_string = ""
        result = docstring_parser.get_return_type(input_string)
        assert result == Type.none