[docs]classMissingLineError(Exception):"""Error class for missing line errors"""
[docs]defget_line_index_with_pattern(pattern:Pattern,lines:List[str])->int:"""Find a line matching a pattern in a list of lines Arguments: pattern -- Pattern instance lines -- List of lines to look in Raises: MissingLineError: could not find a line which matches the pattern """foridx,lineinenumerate(lines):ifre.search(pattern.regex,line):returnidxraiseMissingLineError(f"Could not find a line which matches the pattern:\n"f"Name: {pattern.name}\n"f"Regex: {pattern.regex}")
[docs]defget_header_line_index(markdown_lines:List[str],section_heading:str)->int:"""Find the index of a table header line in a list of lines Arguments: markdown_lines -- List of lines to look in heading -- The heading of the section containing the table """heading_pattern=Pattern(regex=rf"(?i)^\#+ *{re.escape(section_heading)}",name="Section Heading")section_index=get_line_index_with_pattern(heading_pattern,markdown_lines)table_separator_index=(get_line_index_with_pattern(TABLE_SEPARATOR_PATTERN,markdown_lines[section_index:])+section_index)header_line_index=table_separator_index-1returnheader_line_index
[docs]defget_table_indexes(markdown_lines:List[str],section_heading:str)->Tuple[int,int]:"""Find the indexes of the header and last row of a table in list of markdown lines Arguments: markdown_lines -- List of lines to look in section_heading -- The heading of the section containing the table """header_line_index=get_header_line_index(markdown_lines,section_heading)# The end of the table is marked by the first new linebody_end_index=(get_line_index_with_pattern(NEW_LINE_PATTERN,markdown_lines[header_line_index:])+header_line_index)returnheader_line_index,body_end_index
[docs]defget_table_lines(markdown_lines:List[str],section_heading:str)->List[str]:"""Get the table lines from a list of markdown lines Arguments: markdown_lines -- List of lines to look in section_heading -- The heading of the section containing the table """header_line_index,body_end_index=get_table_indexes(markdown_lines,section_heading)returnmarkdown_lines[header_line_index:body_end_index]
[docs]defget_field_paths(schema:dict,path:str="")->List[str]:"""Function for Depth First traversal of the dereferenced IDS dictionary"""# If the field type is an array, we only want to store the path for the items in the# array, which is handled laterfield_paths=[path]ifpathandschema.get("type")!="array"else[]# Recursively visit objects (containing properties) and arrays (containing items)if"properties"inschema:forproperty_name,property_schemainschema["properties"].items():field_paths.extend(get_field_paths(schema=property_schema,path=f"{path}{'.'ifpathelse''}{property_name}",))elif"items"inschema:# [*] added to array paths heres, including multi-dimensional arraysfield_paths.extend(get_field_paths(schema["items"],path=f"{path}[*]"))returnfield_paths