|
| 1 | +"""Update Integrations Page""" |
| 2 | +import json |
| 3 | +from pathlib import Path |
| 4 | + |
| 5 | +import click |
| 6 | +import yaml |
| 7 | +from jinja2 import Environment, PackageLoader, select_autoescape, StrictUndefined |
| 8 | +from pydantic import BaseModel, ConfigDict, TypeAdapter |
| 9 | + |
| 10 | +from tools.update_di_reference import PluginDescription |
| 11 | + |
| 12 | +jinja_environment = Environment( |
| 13 | + loader=PackageLoader("tools"), |
| 14 | + autoescape=select_autoescape(), |
| 15 | + undefined=StrictUndefined |
| 16 | +) |
| 17 | + |
| 18 | +ListOfPlugins = TypeAdapter(dict[str, PluginDescription]) |
| 19 | + |
| 20 | +class AttrDict(dict): |
| 21 | + """Dict subclass that allows attribute access.""" |
| 22 | + |
| 23 | + def __getattr__(self, key): |
| 24 | + return self[key] |
| 25 | + |
| 26 | + def __setattr__(self, key, value): |
| 27 | + self[key] = value |
| 28 | + |
| 29 | + |
| 30 | +def get_plugin_placeholder(plugin_file: Path) -> object: |
| 31 | + """Get Plugin Placeholder to allow {{p.office365preadsheet}}""" |
| 32 | + placeholders = AttrDict() |
| 33 | + plugins = ListOfPlugins.validate_json(plugin_file.read_text()) |
| 34 | + for pluginId, plugin in plugins.items(): |
| 35 | + attribute_id = plugin.pluginId.replace("-", "_") |
| 36 | + attribute_id_ref = plugin.pluginId.replace("-", "_") + "_ref" |
| 37 | + plugin_link = f"../../build/reference/{plugin.pluginType}/{plugin.pluginId}.md" |
| 38 | + setattr(placeholders, attribute_id, f"[{plugin.title}]({plugin_link})") |
| 39 | + setattr(placeholders, attribute_id_ref, plugin_link) |
| 40 | + return placeholders |
| 41 | + |
| 42 | + |
| 43 | +class Integration(BaseModel): |
| 44 | + """Integration Description""" |
| 45 | + |
| 46 | + model_config = ConfigDict(extra="forbid") |
| 47 | + |
| 48 | + name: str |
| 49 | + icon: str |
| 50 | + content: str |
| 51 | + |
| 52 | +class IntegrationsFile(BaseModel): |
| 53 | + """Integration File Base Model""" |
| 54 | + |
| 55 | + model_config = ConfigDict(extra="forbid") |
| 56 | + |
| 57 | + integrations: list[Integration] |
| 58 | + |
| 59 | +def create_integrations_markdown(yaml_source: Path, markdown_output: Path, plugins_file: Path) -> None: |
| 60 | + """Create the integration markdown file.""" |
| 61 | + base_template = jinja_environment.get_template("integrations_base.md") |
| 62 | + integration_template = jinja_environment.get_template("integration.md") |
| 63 | + parsed_content = IntegrationsFile.model_validate(yaml.safe_load(yaml_source.read_text())) |
| 64 | + click.echo(f"Parsed {len(parsed_content.integrations)} integrations.") |
| 65 | + integrations = sorted(parsed_content.integrations, key=lambda i: i.name.lower()) |
| 66 | + plugin_placeholder = get_plugin_placeholder(plugin_file=plugins_file) |
| 67 | + items = "" |
| 68 | + for integration in integrations: |
| 69 | + content_template = jinja_environment.from_string(integration.content) |
| 70 | + integration.content = content_template.render(p=plugin_placeholder) + "\n\n" |
| 71 | + items += integration_template.render(integration=integration) |
| 72 | + markdown = base_template.render(items=items) |
| 73 | + markdown_output.write_text(markdown) |
| 74 | + |
| 75 | + |
| 76 | +@click.command() |
| 77 | +@click.option( |
| 78 | + "--input-file", "-i", |
| 79 | + type=click.Path(exists=False, dir_okay=False, file_okay=True), |
| 80 | + default="data/integrations.yml", |
| 81 | + help="Where to get the integration descriptions from?", |
| 82 | + show_default=True, |
| 83 | +) |
| 84 | +@click.option( |
| 85 | + "--output-file", "-o", |
| 86 | + type=click.Path(exists=False, dir_okay=False, file_okay=True), |
| 87 | + default="docs/build/integrations/index.md", |
| 88 | + help="Where to save the markdown file?", |
| 89 | + show_default=True, |
| 90 | +) |
| 91 | +@click.option( |
| 92 | + "--plugins-file", |
| 93 | + type=click.Path(exists=False, dir_okay=False, file_okay=True), |
| 94 | + default="data/plugins.json", |
| 95 | + help="Where to load the plugin information from?", |
| 96 | + show_default=True, |
| 97 | +) |
| 98 | +def update_integrations(input_file: str, output_file: str, plugins_file: str) -> None: |
| 99 | + """Update the integrations page by parsing the integrations.yaml""" |
| 100 | + click.echo(f"Creating Integrations Pages as {output_file}") |
| 101 | + create_integrations_markdown( |
| 102 | + yaml_source=Path(input_file), |
| 103 | + markdown_output=Path(output_file), |
| 104 | + plugins_file=Path(plugins_file) |
| 105 | + ) |
0 commit comments