Source code for ts_ids_core.mapping_table.locate

import re
from dataclasses import dataclass
from typing import List, Tuple


[docs] @dataclass class Pattern: """Dataclass to store a pattern's name and regex""" name: str regex: str
TABLE_SEPARATOR_PATTERN = Pattern( regex=r"^(\|? *:?-+:? *\|?)+\s*$", name="Table Separator" ) NEW_LINE_PATTERN = Pattern(regex=r"^\n$", name="New Line")
[docs] class MissingLineError(Exception): """Error class for missing line errors"""
[docs] def get_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 """ for idx, line in enumerate(lines): if re.search(pattern.regex, line): return idx raise MissingLineError( f"Could not find a line which matches the pattern:\n" f"Name: {pattern.name}\n" f"Regex: {pattern.regex}" )
[docs] def get_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 - 1 return header_line_index
[docs] def get_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 line body_end_index = ( get_line_index_with_pattern( NEW_LINE_PATTERN, markdown_lines[header_line_index:] ) + header_line_index ) return header_line_index, body_end_index
[docs] def get_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 ) return markdown_lines[header_line_index:body_end_index]
[docs] def get_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 later field_paths = [path] if path and schema.get("type") != "array" else [] # Recursively visit objects (containing properties) and arrays (containing items) if "properties" in schema: for property_name, property_schema in schema["properties"].items(): field_paths.extend( get_field_paths( schema=property_schema, path=f"{path}{'.' if path else ''}{property_name}", ) ) elif "items" in schema: # [*] added to array paths heres, including multi-dimensional arrays field_paths.extend(get_field_paths(schema["items"], path=f"{path}[*]")) return field_paths