Skip to content

Commit

Permalink
ci: Generate config and settings ref documentation (#223)
Browse files Browse the repository at this point in the history
Add documentation for config and settings with a generated reference.
  • Loading branch information
alexgarel authored Aug 19, 2024
1 parent ab75a96 commit 7d3a012
Show file tree
Hide file tree
Showing 18 changed files with 610 additions and 500 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ data/taxonomies
data/**/*.jsonl
products*.jsonl.gz
data/searchalicious-openapi.yml
data/searchalicious-config-schema.yml
data/searchalicious-settings-schema.yml

# Byte-compiled / optimized / DLL files
__pycache__/
Expand Down
8 changes: 8 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,14 @@ generate-custom-elements: _ensure_network
@echo "🔎 Generating custome-elements.json …"
${DOCKER_COMPOSE} run --rm search_nodejs npm run analyze

generate-config-schema: _ensure_network
@echo "🔎 Generating config-schema.yml …"
${DOCKER_COMPOSE} run --rm api python3 -m app export-config-schema /opt/search/data/searchalicious-config-schema.yml

generate-settings-schema: _ensure_network
@echo "🔎 Generating settings-schema.yml …"
${DOCKER_COMPOSE} run --rm api python3 -m app export-settings-schema /opt/search/data/searchalicious-settings-schema.yml

#-------#
# Tests #
#-------#
Expand Down
48 changes: 47 additions & 1 deletion app/cli/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -225,7 +225,7 @@ def export_openapi(
exists=None,
file_okay=True,
dir_okay=False,
help="Path of target_path the YAML or JSON data file",
help="Path of the YAML or JSON data file",
)
):
"""Export OpenAPI specification to a file."""
Expand All @@ -248,5 +248,51 @@ def export_openapi(
print(f"spec written to {target_path}")


def export_schema(
class_: type["app.config.Config"] | type["app.config.Settings"], target_path: Path
):
"""Export schema to a file."""
import json

import yaml

from app.config import ConfigGenerateJsonSchema

schema = class_.model_json_schema(schema_generator=ConfigGenerateJsonSchema)

print("writing json schema")
with open(target_path, "w") as f:
if str(target_path).endswith(".json"):
json.dump(schema, f, indent=2)
else:
yaml.safe_dump(schema, f, sort_keys=False)

print(f"schema written to {target_path}")


schema_target_path = typer.Argument(
exists=None,
file_okay=True,
dir_okay=False,
help="Path of the YAML or JSON data file",
)


@cli.command()
def export_config_schema(target_path: Path = schema_target_path):
"""Export Configuration json schema to a file."""
from app.config import Config

export_schema(Config, target_path)


@cli.command()
def export_settings_schema(target_path: Path = schema_target_path):
"""Export Configuration json schema to a file."""
from app.config import Settings

export_schema(Settings, target_path)


def main() -> None:
cli()
Loading

0 comments on commit 7d3a012

Please sign in to comment.