Skip to content

Commit

Permalink
Memoize generation of plugin parameter docs
Browse files Browse the repository at this point in the history
  • Loading branch information
bjoluc committed Nov 21, 2022
1 parent 8a31f97 commit cc7d497
Show file tree
Hide file tree
Showing 6 changed files with 46 additions and 17 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,4 @@ dist.zip
packages/jspsych/README.md
.turbo
*.pyc
cache.db
12 changes: 8 additions & 4 deletions docs/__generator__/plugins.py
Original file line number Diff line number Diff line change
@@ -1,19 +1,23 @@
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",
"INT": "numeric",
}


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 = """
Expand All @@ -37,8 +41,8 @@ def generate_plugin_parameters_section(plugin_dir: str):
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"
Expand Down
22 changes: 15 additions & 7 deletions docs/__generator__/utils.py
Original file line number Diff line number Diff line change
@@ -1,27 +1,38 @@
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 = (
subprocess.list2cmdline(
[
"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",
]
),
)
Expand All @@ -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


Expand Down
13 changes: 8 additions & 5 deletions docs/__init__.py
Original file line number Diff line number Diff line change
@@ -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 changes: 13 additions & 1 deletion poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ authors = []

[tool.poetry.dependencies]
python = "^3.8"
diskcache = "^5.4.0"

[tool.poetry.dev-dependencies]
mkdocs = "^1.3.0"
Expand Down

0 comments on commit cc7d497

Please sign in to comment.