def _parse_request_syntax(self, input_string: str) -> None: if "**Request Syntax**" not in input_string: return request_syntax_index = input_string.index("**Request Syntax**") while request_syntax_index > 0 and input_string[request_syntax_index - 1] == " ": request_syntax_index = request_syntax_index - 1 request_syntax_string = get_line_with_indented( input_string[request_syntax_index:], True) SyntaxGrammar.reset() SyntaxGrammar.enable_packrat() try: match = SyntaxGrammar.request_syntax.parseString( request_syntax_string) except ParseException as e: self.logger.warning( f"Cannot parse request syntax for {self.prefix}") self.logger.debug(e) return argument_groups = match.asDict().get("arguments", []) for argument_dict in argument_groups: argument_name = argument_dict["name"] argument_prefix = self.prefix + get_class_prefix(argument_name) argument_value = TypeValue(self.service_name, argument_prefix, argument_dict["value"]) argument_type = argument_value.get_type() argument = self._find_argument_or_append(argument_name) argument.type = argument_type
def _parse_response_structure(self, input_string: str) -> Optional[TypeDocLine]: if "**Response Structure**" not in input_string: return None response_structure_index = input_string.index("**Response Structure**") while response_structure_index > 0 and input_string[ response_structure_index - 1] == " ": response_structure_index -= 1 response_structure_string = get_line_with_indented( input_string[response_structure_index:], True) response_structure_string = textwrap.dedent(response_structure_string) TypeDocGrammar.reset() try: match = TypeDocGrammar.response_structure.parseString( response_structure_string) except ParseException as e: self.logger.warning( f"Cannot parse response structure for {self.prefix}") self.logger.debug(e) return None return TypeDocLine(**match.asDict())
def _parse_response_syntax(self, input_string: str) -> Optional[FakeAnnotation]: if "**Response Syntax**" not in input_string: return None response_syntax_index = input_string.index("**Response Syntax**") while response_syntax_index > 0 and input_string[response_syntax_index - 1] == " ": response_syntax_index -= 1 response_syntax_string = get_line_with_indented( input_string[response_syntax_index:], True) SyntaxGrammar.reset() SyntaxGrammar.enable_packrat() try: match = SyntaxGrammar.response_syntax.parseString( response_syntax_string) except ParseException as e: self.logger.warning( f"Cannot parse response syntax for {self.prefix}") self.logger.debug(e) return None value = match.asDict()["value"] return TypeValue(self.service_name, f"{self.prefix}Response", value).get_type()
def _parse_params(self, input_string: str) -> None: if ":param " not in input_string: return for re_match in self.RE_PARAM.finditer(input_string): start_index = re_match.start() param_string = get_line_with_indented(input_string[start_index + 1:]) TypeDocGrammar.reset() try: match = TypeDocGrammar.param_definition.parseString( param_string) except ParseException as e: self.logger.warning( f"Cannot parse param definition {param_string} for {self.prefix}" ) self.logger.debug(e) continue argument_line = TypeDocLine(**match.asDict()) if not argument_line.name: continue argument_name = argument_line.name argument = self._find_argument_or_append(argument_name) if argument_line.required: argument.default = None if not argument.type: continue self._fix_keys(argument.type, argument_line)
def test_get_line_with_indented(self) -> None: assert get_line_with_indented("a\n\nb\nc") == "a" assert get_line_with_indented( "a\n\n b\n c\n\n d\ne") == "a\n\n b\n c\n\n d" assert get_line_with_indented(" a\n b\n c\n d") == " a\n b\n c" assert get_line_with_indented("") == "" assert get_line_with_indented("a\n\nb\n c\nd", True) == "a\n\nb\n c" assert get_line_with_indented(" a\n\n b\n c\n d\ne", True) == " a\n\n b\n c\n d"
def test_get_line_with_indented(self) -> None: self.assertEqual(get_line_with_indented("a\n\nb\nc"), "a") self.assertEqual( get_line_with_indented("a\n\n b\n c\n\n d\ne"), "a\n\n b\n c\n\n d" ) self.assertEqual(get_line_with_indented(" a\n b\n c\n d"), " a\n b\n c") self.assertEqual(get_line_with_indented(""), "") self.assertEqual(get_line_with_indented("a\n\nb\n c\nd", True), "a\n\nb\n c") self.assertEqual( get_line_with_indented(" a\n\n b\n c\n d\ne", True), " a\n\n b\n c\n d", )