generate_tools.py 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. from pathlib import Path
  2. import os
  3. def underline(title: str, character: str = "=") -> str:
  4. return f"{title}\n{character * len(title)}"
  5. def generate_title(filename: str) -> str:
  6. # Turn filename into a title
  7. title = filename.replace("_", " ").title()
  8. # Handle acronyms and names
  9. # title = fix_case(title)
  10. # # Underline title
  11. title = underline(title)
  12. return title
  13. def get_root_dir(starting_directory=None):
  14. if starting_directory is None:
  15. starting_directory = os.getcwd()
  16. current_directory = starting_directory
  17. while True:
  18. if os.path.isdir(os.path.join(current_directory, '.git')):
  19. return current_directory
  20. parent_directory = os.path.dirname(current_directory)
  21. if parent_directory == current_directory:
  22. raise FileNotFoundError("No .git directory found in any parent directories")
  23. current_directory = parent_directory
  24. def generate_tools():
  25. root_dir = Path(__file__).parent.parent.parent.resolve()
  26. # Source paths
  27. script_dir = root_dir / "pyopenagi/tools"
  28. script_paths = sorted(script_dir.glob("*/*.py"))
  29. # Destination paths
  30. doc_dir = root_dir / "docs/source/agent_developer/external_tools"
  31. doc_paths = [doc_dir / f"{path.stem}.rst" for path in script_paths]
  32. # Generate the example docs for each example script
  33. for script_path, doc_path in zip(script_paths, doc_paths):
  34. script_url = f"https://github.com/agiresearch/AIOS/blob/main/{str(script_path.relative_to(root_dir))}"
  35. # Make script_path relative to doc_path and call it include_path
  36. include_path = '../../../..' / script_path.relative_to(root_dir)
  37. content = (f"{generate_title(doc_path.stem)}\n\n"
  38. f"Source code at {script_url}.\n\n"
  39. f".. literalinclude:: {include_path}\n"
  40. " :language: python\n"
  41. )
  42. with open(doc_path, "w") as f:
  43. f.write(content)
  44. # Generate the toctree for the example scripts
  45. with open(doc_dir / "tool_index.template.rst") as f:
  46. examples_index = f.read()
  47. with open(doc_dir / "tool_index.rst", "w") as f:
  48. example_docs = "\n ".join(path.stem for path in script_paths)
  49. f.write(examples_index.replace(r"%tool_example%", example_docs))