mirror of
https://github.com/invoke-ai/InvokeAI
synced 2024-08-30 20:32:17 +00:00
scripts: add script to update config docstring
- Add script to call config docstring helper function and write the docstring to the file directly - Add `make` target for this script
This commit is contained in:
parent
eba1fc1355
commit
c41e87160a
5
Makefile
5
Makefile
@ -11,6 +11,7 @@ help:
|
|||||||
@echo "mypy Run mypy using the config in pyproject.toml to identify type mismatches and other coding errors"
|
@echo "mypy Run mypy using the config in pyproject.toml to identify type mismatches and other coding errors"
|
||||||
@echo "mypy-all Run mypy ignoring the config in pyproject.tom but still ignoring missing imports"
|
@echo "mypy-all Run mypy ignoring the config in pyproject.tom but still ignoring missing imports"
|
||||||
@echo "test Run the unit tests."
|
@echo "test Run the unit tests."
|
||||||
|
@echo "update-config-docstring Update the app's config docstring so mkdocs can autogenerate it correctly."
|
||||||
@echo "frontend-install Install the pnpm modules needed for the front end"
|
@echo "frontend-install Install the pnpm modules needed for the front end"
|
||||||
@echo "frontend-build Build the frontend in order to run on localhost:9090"
|
@echo "frontend-build Build the frontend in order to run on localhost:9090"
|
||||||
@echo "frontend-dev Run the frontend in developer mode on localhost:5173"
|
@echo "frontend-dev Run the frontend in developer mode on localhost:5173"
|
||||||
@ -41,6 +42,10 @@ mypy-all:
|
|||||||
test:
|
test:
|
||||||
pytest ./tests
|
pytest ./tests
|
||||||
|
|
||||||
|
# Update config docstring
|
||||||
|
update-config-docstring:
|
||||||
|
python scripts/update_config_docstring.py
|
||||||
|
|
||||||
# Install the pnpm modules needed for the front end
|
# Install the pnpm modules needed for the front end
|
||||||
frontend-install:
|
frontend-install:
|
||||||
rm -rf invokeai/frontend/web/node_modules
|
rm -rf invokeai/frontend/web/node_modules
|
||||||
|
46
scripts/update_config_docstring.py
Normal file
46
scripts/update_config_docstring.py
Normal file
@ -0,0 +1,46 @@
|
|||||||
|
import os
|
||||||
|
|
||||||
|
# The pydantic app config can be documented automatically using mkdocs, but this requires that the docstring
|
||||||
|
# for the class is kept up to date. We use a pydantic model for the app config. Each config setting is a field
|
||||||
|
# with a `description` parameter. It is tedious to update both the description _and_ the docstring for the class.
|
||||||
|
#
|
||||||
|
# This script parses the pydantic model, generates a valid docstring and replaces the existing docstring in the file,
|
||||||
|
# so you don't need to worry about keeping the docstring up to date.
|
||||||
|
#
|
||||||
|
# A test is provided to ensure that the docstring is up to date. If the test fails, run this script.
|
||||||
|
|
||||||
|
|
||||||
|
def main():
|
||||||
|
# Change working directory to the repo root
|
||||||
|
os.chdir(os.path.abspath(os.path.join(os.path.dirname(__file__), "..")))
|
||||||
|
|
||||||
|
from invokeai.app.services.config.config_default import InvokeAIAppConfig
|
||||||
|
|
||||||
|
docstring = InvokeAIAppConfig.generate_docstrings()
|
||||||
|
|
||||||
|
# Replace the docstring in the file
|
||||||
|
with open("invokeai/app/services/config/config_default.py", "r") as f:
|
||||||
|
lines = f.readlines()
|
||||||
|
|
||||||
|
# Find the class definition line
|
||||||
|
class_def_index = next(i for i, line in enumerate(lines) if "class InvokeAIAppConfig" in line)
|
||||||
|
|
||||||
|
# Find the existing docstring start and end lines
|
||||||
|
docstring_start_index = next(i for i, line in enumerate(lines[class_def_index:]) if '"""' in line) + class_def_index
|
||||||
|
docstring_end_index = (
|
||||||
|
next(i for i, line in enumerate(lines[docstring_start_index + 1 :]) if '"""' in line)
|
||||||
|
+ docstring_start_index
|
||||||
|
+ 1
|
||||||
|
)
|
||||||
|
|
||||||
|
# Replace the existing docstring with the new one, plus some line breaks in between. This _should_ result in a
|
||||||
|
# correctly-formatted file with no syntax errors.
|
||||||
|
lines = lines[:docstring_start_index] + [docstring, "\n"] + lines[docstring_end_index + 1 :]
|
||||||
|
|
||||||
|
# Write the modified lines back to the file
|
||||||
|
with open("invokeai/app/services/config/config_default.py", "w") as f:
|
||||||
|
f.writelines(lines)
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
main()
|
Loading…
Reference in New Issue
Block a user