Skip to content

Commit

Permalink
Improve the class doc format
Browse files Browse the repository at this point in the history
Signed-off-by: SimFG <[email protected]>
  • Loading branch information
SimFG committed May 26, 2023
1 parent d0e27c9 commit 0c67584
Show file tree
Hide file tree
Showing 16 changed files with 182 additions and 206 deletions.
5 changes: 5 additions & 0 deletions docs/.readthedocs.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
version: 2

python:
version: 3.8
system_packages: true
157 changes: 0 additions & 157 deletions docs/_exts/docgen.py

This file was deleted.

106 changes: 106 additions & 0 deletions docs/_exts/docgen2.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
import os
import sys

from jinja2 import Environment, FileSystemLoader, select_autoescape

_default_skip_file_list = ["__init__.py"]
_default_skip_dir_list = ["__pycache__"]


class DocGen:
def __init__(self, lib_name="gptcache", source_dir="../gptcache", output_dir="references", skip_list=[]):
self.lib_name = lib_name
self.output_dir = os.path.abspath(output_dir)
self.source_dir = os.path.abspath(source_dir)
self.skip_list = skip_list

@staticmethod
def title_bar(input_str):
return "=" * len(input_str)

@staticmethod
def section_bar(input_str):
return "-" * len(input_str)

@staticmethod
def get_filename(input_str):
if input_str == "gptcache":
return input_str
input_str = os.path.splitext(input_str)[1][1:]
return input_str

@staticmethod
def cap(input_str):
input_str = DocGen.get_filename(input_str)
if input_str == "gptcache":
return "GPTCache"
return str.join(" ", [i.capitalize() for i in input_str.split("_")])

def model_name(self, input_str: str):
return self.lib_name + input_str[len(self.source_dir):].replace("/", ".")

def get_module_and_libs(self, module_dir, is_root):
module = self.model_name(module_dir)
libs = []
for file in os.listdir(module_dir):
if os.path.isfile(os.path.join(module_dir, file)) and file not in _default_skip_file_list:
libs.append(module + "." + os.path.splitext(file)[0])
if not is_root:
if os.path.isdir(os.path.join(module_dir, file)) and file not in _default_skip_dir_list:
_, child_libs = self.get_module_and_libs(os.path.join(module_dir, file), False)
libs.extend(child_libs)
if len(libs) > 0:
sorted(libs)
return module, libs
return "", []

def generate(self):
# Set the output directory
env = Environment(
loader=FileSystemLoader(os.path.join(self.output_dir, "../_templates")), autoescape=select_autoescape()
)

# Add custom filters
env.filters["title_bar"] = DocGen.title_bar
env.filters["section_bar"] = DocGen.section_bar
env.filters["cap"] = DocGen.cap

# Add the target path to the system path
sys.path.insert(0, os.path.abspath(".."))

# Load the modules
modules = []
libs = []

a, b = self.get_module_and_libs(self.source_dir, True)
if a:
modules.append(a)
libs.append(b)

for file in os.listdir(self.source_dir):
tmp_dir = os.path.join(self.source_dir, file)
if os.path.isdir(tmp_dir) and file not in _default_skip_dir_list:
a, b = self.get_module_and_libs(tmp_dir, False)
if a:
modules.append(a)
libs.append(b)

# Render the index templates and write rendered output to files
index_temp = env.get_template("index.rst")

with open(os.path.join(self.output_dir, "index.rst"), "w") as f:
t = index_temp.render({"modules": [DocGen.get_filename(module) for module in modules]})
f.write(t)

# Render the function templates and write rendered output to files
func_temp = env.get_template("function.rst")

for index, module in enumerate(modules):
with open(os.path.join(self.output_dir, f"{DocGen.get_filename(module)}.rst"), "w") as f:
t = func_temp.render({"module_name": module, "funcs": [(DocGen.get_filename(lib), lib) for lib in libs[index]]})
f.write(t)


# if __name__ == "__main__":
# gen = DocGen(source_dir="/Users/derek/fubang/gptcache/gptcache", output_dir="/Users/derek/fubang/gptcache/docs/references")
# gen.generate()
File renamed without changes.
10 changes: 6 additions & 4 deletions docs/_templates/function.rst
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,11 @@
.. contents:: Index

{% for func in funcs -%}
{{module_name}}.{{func[0]}}
{{ func[2] | section_bar }}
.. automodule:: {{func[2]}}
:members:
{{func[0]}}
{{ func[0] | section_bar }}
.. automodule:: {{func[1]}}
:members:
:undoc-members:
:show-inheritance:

{% endfor %}
2 changes: 1 addition & 1 deletion docs/_templates/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,5 @@
:caption: Contents:

{% for module in modules %}
{{ module[0] }}
{{ module }}
{%- endfor -%}
35 changes: 16 additions & 19 deletions docs/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@
sys.path.insert(0, os.path.abspath("."))
sys.path.insert(0, os.path.abspath(".."))
sys.path.insert(0, os.path.abspath("_exts"))
from _exts.docgen import DocGen
from _exts.indexCon import IndexCon
from _exts.index_con import IndexCon
from _exts.docgen2 import DocGen


# -- Project information -----------------------------------------------------
Expand Down Expand Up @@ -51,26 +51,26 @@
"sphinx_copybutton",
"sphinx_panels",
"sphinx_toolbox.collapse",
"sphinxcontrib.autodoc_pydantic",
]

intersphinx_mapping = {
"torch": ("https://pytorch.org/docs/stable/", None),
"numpy": ("https://numpy.org/devdocs/", None),
"python": ("https://docs.python.org/3", None),
}

autodoc_member_order = "bysource"
autodoc_mock_imports = ["httpx"]
autodoc_inherit_docstrings = False

autodoc_pydantic_model_show_json = False
autodoc_pydantic_field_list_validators = False
autodoc_pydantic_config_members = False
autodoc_pydantic_model_show_config_summary = False
autodoc_pydantic_model_show_validator_members = False
autodoc_pydantic_model_show_field_summary = False
autodoc_pydantic_model_members = False
autodoc_pydantic_model_undoc_members = False
autodoc_mock_imports = [
"httpx",
"protobuf",
"minigpt4",
"llama_cpp",
"transformers",
"diffusers",
"stability_sdk",
"paddle",
"paddlenlp",
]

source_suffix = {
".rst": "restructuredtext",
Expand All @@ -89,9 +89,6 @@
"_build",
"Thumbs.db",
".DS_Store",
"references/client*",
"references/config*",
"references/core*",
]


Expand Down Expand Up @@ -131,7 +128,7 @@
# -- Preactions --------------------------------------------------------------

# Prepare docs
docgen = DocGen(output_dir="references")
docgen.generate("gptcache")
docgen = DocGen()
docgen.generate()

IndexCon("../README.md", "index.rst")
13 changes: 4 additions & 9 deletions docs/references/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,9 @@


gptcache
adapter
client
config
core
processor
embedding
utils
adapter
manager
processor
report
session
similarity_evaluation
utils
similarity_evaluation
Loading

0 comments on commit 0c67584

Please sign in to comment.