Skip to content

Commit

Permalink
add doc starting bloc
Browse files Browse the repository at this point in the history
  • Loading branch information
ElNiak committed Jun 3, 2024
1 parent 645e38f commit e4acc0c
Show file tree
Hide file tree
Showing 21 changed files with 1,380 additions and 0 deletions.
23 changes: 23 additions & 0 deletions .github/dependabot.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# To get started with Dependabot version updates, you'll need to specify which
# package ecosystems to update and where the package manifests are located.
# Please see the documentation for all configuration options:
# https://docs.github.com/code-security/dependabot/dependabot-version-updates/configuration-options-for-the-dependabot.yml-file

version: 2
updates:
- package-ecosystem: "pip" # See documentation for possible values
directory: "sema-toolchain/sema_scdg/requirement.txt" # Location of package manifests
schedule:
interval: "weekly"
- package-ecosystem: "docker" # See documentation for possible values
directory: "sema-toolchain/sema_scdg/Dockerfile." # Location of package manifests
schedule:
interval: "weekly"
- package-ecosystem: "pip" # See documentation for possible values
directory: "sema-toolchain/sema_classifier/requirement.txt" # Location of package manifests
schedule:
interval: "weekly"
- package-ecosystem: "docker" # See documentation for possible values
directory: "sema-toolchain/sema_classifier/Dockerfile." # Location of package manifests
schedule:
interval: "weekly"
File renamed without changes.
28 changes: 28 additions & 0 deletions .github/workflows/pr-generate-docs.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
name: Documentation Generation
on:
pull_request:
branches:
- "production"
paths:
- "**.py"

permissions:
contents: write

jobs:
doc-generation:
name: Documentation Generation
runs-on: "ubuntu-latest"
steps:
- name: "Checkout code"
uses: "actions/checkout@v3"
- name: "Generate docs"
run: |
pip install -r requirements.txt
make gen-docs
mkdocs gh-deploy --force --clean
mv sources docs
- name: "Commit generated documentation"
uses: "stefanzweifel/git-auto-commit-action@v4"
with:
commit_message: "Updating documentation"
7 changes: 7 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
gen-docs:
python3 ./doc/generate_doc.py
gendocs --config doc/mkgendocs.yaml

mkdocs:
make gen-docs
mkdocs serve
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
```

[![Documentation Built by gendocs](https://img.shields.io/badge/docs%20by-gendocs-blue.svg)](https://gendocs.readthedocs.io/en/latest/)

### Toolchain architecture
<a name="toolchain-architecture"></a>
Expand Down
119 changes: 119 additions & 0 deletions doc/generate_doc.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
#!/usr/bin/env python3
"""Generate mkgendocs.yaml from python files by iterating over files and functions."""

import os

import yaml
import re


def get_mkgendocs_config():
"""
Get the mkgendocs configuration file.
Raises:
FileNotFoundError: If mkgendocs.yaml is not found
YAMLError: If there is an error parsing mkgendocs.yaml
Returns:
mkgendocs configuration
"""
try:
with open("doc/mkgendocs.yaml", encoding="UTF-8") as mkgendocs_config:
return yaml.safe_load(mkgendocs_config)
except FileNotFoundError as error_message:
print("mkgendocs.yaml not found")
raise FileNotFoundError from error_message
except yaml.YAMLError as error_message:
raise yaml.YAMLError() from error_message


# Function to get a list of python files in a directory
def get_python_files(directory):
"""
Get a list of python files in a directory.
Args:
directory: Directory to search
Returns:
List of python files
"""
python_files = []
for root, dirs, files in os.walk(directory):
for file in files:
if file.endswith(".py") and (
not root.startswith("./tests")
and not file.startswith("_")
and not root.startswith("./build")
):
python_files.append(os.path.join(root, file))
return python_files


def get_file_functions(filename):
"""
Get the functions in a file.
Args:
filename: The name of the file to parse for functions.
Returns:
List of functions
"""
if "ivy" in filename:
print("No ivy documentation generated")
return []
if "submodules" in filename:
print("No submodules documentation generated")
return []
with open(filename, encoding="UTF-8") as file:
content = file.read()
if "print " in content:
print(f"Python 2 {filename} skipping")
return []
with open(filename, encoding="UTF-8") as file:
lines = file.readlines()
functions = []
# TODO fix
# for line in lines:
# if re.match(r"^\s*def\s+\w+\s*\(", line):
# function_name = re.search(r"def\s+(\w+)\s*\(", line).group(1)
# functions.append(function_name)
for line in lines:
if line.startswith("def"):
functions.append(line.split(" ")[1].split("(")[0])
return functions

def main():
"""Generate configuration for mkgendocs to build documentation."""
print("Starting doc generation")
mkgendocs_config = get_mkgendocs_config()
new_pages = []

print("Getting a list of functions in python files..")
python_files = get_python_files(".")
for python_file in python_files:
functions = get_file_functions(python_file)
if len(functions) > 0:
print("Functions found, adding " + python_file)
new_pages.append(
{
"page": python_file.replace(".py", ".md").replace("./", ""),
"source": python_file,
"functions": functions,
}
)
else:
print("No functions found, skipping " + python_file)

mkgendocs_config["pages"] = new_pages

with open("doc/mkgendocs.yaml", "w", encoding="UTF-8") as mkgendocs_config_file:
yaml.dump(mkgendocs_config, mkgendocs_config_file)


if __name__ == "__main__":
main()
Loading

0 comments on commit e4acc0c

Please sign in to comment.