from dataclasses import dataclass from pathlib import Path import inflection from bs4 import BeautifulSoup, ResultSet, Tag @dataclass class Description: note: str args: list[str] return_values: list[str] def scraping_regular_api( function_names: list[str], coppeliasim_dir: Path ) -> dict[str, Description]: """Expecting function_names like: [ 'loadScene','setJointMaxForce'], and the simulator dir""" regular_api_dir = get_regular_api_dir(coppeliasim_dir) function_descriptions = {} for function_name in function_names: description = scraping_regular_api_function(function_name, regular_api_dir) function_descriptions[function_name] = description return function_descriptions def scraping_regular_api_description(coppeliasim_dir: Path) -> str: en = get_regular_api_dir(coppeliasim_dir).parent index = en / "apiFunctions.htm" soup = BeautifulSoup(index.read_text(), "html.parser") description = _get_text_for_warning_box(soup) return description def scraping_sim_ik_api_description(coppeliasim_dir: Path) -> str: sim_ik_file = get_sim_ik_file(coppeliasim_dir) soup = BeautifulSoup(sim_ik_file.read_text(), "html.parser") description = _get_text_for_warning_box(soup) return description def _get_text_for_warning_box(soup: BeautifulSoup) -> str: p = soup.find("p", {"class": "warningBox"}) if p is None: raise Exception("Unable to find Regular API description") return p.text def scraping_sim_ik_api( function_names: list[str], coppeliasim_dir: Path ) -> dict[str, str]: function_descriptions = {} sim_ik_file = get_sim_ik_file(coppeliasim_dir) for name in function_names: description = scraping_sim_ik_api_function(name, sim_ik_file) function_descriptions[name] = description return function_descriptions ENGLISH_HELP_FILES_DIR = Path("manual") / "en" def get_regular_api_dir(coppeliasim_dir: Path) -> Path: return coppeliasim_dir / ENGLISH_HELP_FILES_DIR / "regularApi" def get_sim_ik_file(coppeliasim_dir: Path) -> Path: return coppeliasim_dir / ENGLISH_HELP_FILES_DIR / "simIK.htm" def scraping_regular_api_function( function_name: str, regular_api_dir: Path ) -> Description: name = inflection.camelize(function_name) reference_function_name = f"sim{name}" reference_file = regular_api_dir / f"{reference_function_name}.htm" if not reference_file.exists(): raise Exception( f"Unable to find {function_name} and your file: {reference_file}" ) html = reference_file.read_text() description = _scraping_sim_api_html(html) return description def scraping_sim_ik_api_function(function_name: str, sim_ik_file: Path) -> str: name = f"simIK.{function_name}" # id="simIK.addIkElement" soup = BeautifulSoup(sim_ik_file.read_text(), "html.parser") a_tag = soup.find("a", {"id": name}) if a_tag is None: raise Exception(f"Unable to find {function_name} in SimIK") # getting the next table of p """
Description | Adds a new IK element to an IK group. |