Memoize generation of plugin parameter docs

This commit is contained in:
bjoluc 2022-11-21 21:06:17 +01:00
parent 8a31f97cd5
commit cc7d497cd7
6 changed files with 46 additions and 17 deletions

1
.gitignore vendored
View File

@ -12,3 +12,4 @@ dist.zip
packages/jspsych/README.md
.turbo
*.pyc
cache.db

View File

@ -1,11 +1,14 @@
from pathlib import Path
from docs.__generator__.utils import (
cache,
get_plugin_description,
get_value_by_path,
get_values_by_path,
jsdoc_to_markdown,
)
parameter_type_mapping = {
PARAMETER_TYPE_MAPPING = {
"HTML_STRING": "HTML string",
"KEYS": "array of strings",
"BOOL": "boolean",
@ -13,7 +16,8 @@ parameter_type_mapping = {
}
def generate_plugin_parameters_section(plugin_dir: str):
@cache.memoize(expire=60 * 60 * 24 * 30) # 1 month in seconds
def generate_plugin_parameters_section(plugin_dir: Path, source_hash: str):
description = get_plugin_description(plugin_dir)
output = """
@ -37,8 +41,8 @@ specified. Other parameters can be left unspecified if the default value is acce
parameter_type = get_value_by_path(
parameter, "$.type.declaration.children[?name = type].type.name"
)
if parameter_type in parameter_type_mapping:
parameter_type = parameter_type_mapping[parameter_type]
if parameter_type in PARAMETER_TYPE_MAPPING:
parameter_type = PARAMETER_TYPE_MAPPING[parameter_type]
is_array = get_value_by_path(
parameter, "$.type.declaration.children[?name = array].type.value"

View File

@ -1,14 +1,25 @@
import json
from logging import getLogger
import subprocess
from hashlib import md5
from pathlib import Path
from tempfile import NamedTemporaryFile
from typing import Any, List
from diskcache import Cache
from jsonpath_ng.ext import parse
cache = Cache(Path(__file__).parent)
logger = getLogger("mkdocs")
def get_plugin_description(plugin_dir: str):
package_path = f"packages/plugin-{plugin_dir}"
def hash_file(path: Path):
with path.open() as file:
return md5(file.read().encode("utf8")).hexdigest()
def get_plugin_description(plugin_dir: Path):
logger.info(f"Collecting parameter infos for {plugin_dir}...")
with NamedTemporaryFile() as json_file:
typedoc_command = (
@ -16,12 +27,12 @@ def get_plugin_description(plugin_dir: str):
[
"node_modules/.bin/typedoc",
"--tsconfig",
f"{package_path}/tsconfig.json",
plugin_dir / "tsconfig.json",
"--json",
f"{json_file.name}",
"--sort",
"source-order",
f"{package_path}/src/index.ts",
plugin_dir / "src/index.ts",
]
),
)
@ -36,9 +47,6 @@ def get_plugin_description(plugin_dir: str):
description = json.load(json_file)
# with Path("tmp.json").open("w") as file:
# json.dump(description, file)
return description

View File

@ -1,12 +1,15 @@
from logging import getLogger
from pathlib import Path
from docs.__generator__.plugins import generate_plugin_parameters_section
from docs.__generator__.utils import hash_file
logger = getLogger("mkdocs")
# https://mkdocs-macros-plugin.readthedocs.io/en/latest/macros/
def define_env(env):
@env.macro
def plugin_parameters(plugin_dir: str):
logger.info(f"Collecting parameter infos for plugin {plugin_dir}...")
return generate_plugin_parameters_section(plugin_dir)
def plugin_parameters(plugin: str):
plugin_dir = Path(f"packages/plugin-{plugin}")
return generate_plugin_parameters_section(
plugin_dir, hash_file(plugin_dir / "src/index.ts")
)

14
poetry.lock generated
View File

@ -47,6 +47,14 @@ category = "dev"
optional = false
python-versions = ">=3.5"
[[package]]
name = "diskcache"
version = "5.4.0"
description = "Disk Cache -- Disk and file backed persistent cache."
category = "main"
optional = false
python-versions = ">=3"
[[package]]
name = "ghp-import"
version = "2.1.0"
@ -410,7 +418,7 @@ testing = ["func-timeout", "jaraco.itertools", "pytest (>=6)", "pytest-black (>=
[metadata]
lock-version = "1.1"
python-versions = "^3.8"
content-hash = "0d2b29128da55beecf1c7ba4b16dabd738c91eff6e5e89ab4794cf12b247609c"
content-hash = "1c8d4dcf5a9244ea811f4e585311114536e369408066ebe6e7d1fb46e7f3b457"
[metadata.files]
black = [
@ -448,6 +456,10 @@ decorator = [
{file = "decorator-5.1.1-py3-none-any.whl", hash = "sha256:b8c3f85900b9dc423225913c5aace94729fe1fa9763b38939a95226f02d37186"},
{file = "decorator-5.1.1.tar.gz", hash = "sha256:637996211036b6385ef91435e4fae22989472f9d571faba8927ba8253acbc330"},
]
diskcache = [
{file = "diskcache-5.4.0-py3-none-any.whl", hash = "sha256:af3ec6d7f167bbef7b6c33d9ee22f86d3e8f2dd7131eb7c4703d8d91ccdc0cc4"},
{file = "diskcache-5.4.0.tar.gz", hash = "sha256:8879eb8c9b4a2509a5e633d2008634fb2b0b35c2b36192d89655dbde02419644"},
]
ghp-import = [
{file = "ghp-import-2.1.0.tar.gz", hash = "sha256:9c535c4c61193c2df8871222567d7fd7e5014d835f97dc7b7439069e2413d343"},
{file = "ghp_import-2.1.0-py3-none-any.whl", hash = "sha256:8337dd7b50877f163d4c0289bc1f1c7f127550241988d568c1db512c4324a619"},

View File

@ -6,6 +6,7 @@ authors = []
[tool.poetry.dependencies]
python = "^3.8"
diskcache = "^5.4.0"
[tool.poetry.dev-dependencies]
mkdocs = "^1.3.0"