1
0
Fork 0
ynh-apps_tools/readme_generator/make_readme.py

141 lines
5 KiB
Python
Raw Normal View History

#! /usr/bin/env python3
2021-05-10 17:23:40 +02:00
2021-04-28 20:11:42 +02:00
import argparse
2021-04-23 20:01:40 +02:00
import json
import os
from pathlib import Path
from copy import deepcopy
2021-04-23 20:01:40 +02:00
from typing import Dict, Optional, List, Tuple
2024-03-07 01:00:40 +01:00
import toml
2021-04-28 20:11:42 +02:00
from jinja2 import Environment, FileSystemLoader
2024-03-07 01:00:40 +01:00
def value_for_lang(values: Dict, lang: str):
2024-03-07 00:33:28 +01:00
if not isinstance(values, dict):
return values
if lang in values:
return values[lang]
elif "en" in values:
return values["en"]
else:
return list(values.values())[0]
2021-04-23 20:01:40 +02:00
2024-03-07 01:00:40 +01:00
def generate_READMEs(app_path: Path):
if not app_path.exists():
raise Exception("App path provided doesn't exists ?!")
2021-04-23 20:01:40 +02:00
2023-01-11 09:58:45 +01:00
if os.path.exists(app_path / "manifest.json"):
manifest = json.load(open(app_path / "manifest.json"))
else:
manifest = toml.load(open(app_path / "manifest.toml"))
2021-05-21 20:12:00 +02:00
upstream = manifest.get("upstream", {})
catalog = toml.load(
open(Path(os.path.abspath(__file__)).parent.parent.parent / "apps.toml")
)
from_catalog = catalog.get(manifest["id"], {})
antifeatures_list = toml.load(
open(Path(os.path.abspath(__file__)).parent.parent.parent / "antifeatures.toml")
)
2022-02-23 08:18:05 +01:00
if not upstream and not (app_path / "doc" / "DISCLAIMER.md").exists():
print(
"There's no 'upstream' key in the manifest, and doc/DISCLAIMER.md doesn't exists - therefore assuming that we shall not auto-update the README.md for this app yet."
)
2021-05-21 20:12:00 +02:00
return
env = Environment(loader=FileSystemLoader(Path(__file__).parent / "templates"))
2021-04-23 20:01:40 +02:00
screenshots: List[str]
screenshots = []
if (app_path / "doc" / "screenshots").exists():
# only pick files (no folder) on the root of 'screenshots'
for entry in os.scandir(os.path.join(app_path, "doc", "screenshots")):
if os.DirEntry.is_file(entry):
# ignore '.gitkeep' or any file whose name begins with a dot
if not entry.name.startswith("."):
screenshots.append(os.path.relpath(entry.path, app_path))
# parse available README template and generate a list in the form of:
# > [("en", ""), ("fr", "_fr"), ...]
available_langs: List[Tuple[str, str]] = [("en", "")]
for README_template in (Path(__file__).parent / "templates").iterdir():
# we only want README_{lang}.md.j2 files
if README_template.name == "README.md.j2":
continue
2024-03-11 17:34:33 +01:00
if not README_template.name.endswith(
".j2"
) or not README_template.name.startswith("README_"):
continue
language_code = README_template.name.split("_")[1].split(".")[0]
available_langs.append((language_code, "_" + language_code))
for lang, lang_suffix in available_langs:
template = env.get_template(f"README{lang_suffix}.md.j2")
2021-04-23 20:01:40 +02:00
2021-08-22 16:57:07 +02:00
if (app_path / "doc" / f"DESCRIPTION{lang_suffix}.md").exists():
description = (
app_path / "doc" / f"DESCRIPTION{lang_suffix}.md"
).read_text()
2021-08-22 16:57:07 +02:00
# Fallback to english if maintainer too lazy to translate the description
elif (app_path / "doc" / "DESCRIPTION.md").exists():
description = (app_path / "doc" / "DESCRIPTION.md").read_text()
else:
description = None
2024-03-07 01:00:40 +01:00
disclaimer: Optional[str]
if (app_path / "doc" / f"DISCLAIMER{lang_suffix}.md").exists():
disclaimer = (app_path / "doc" / f"DISCLAIMER{lang_suffix}.md").read_text()
# Fallback to english if maintainer too lazy to translate the disclaimer idk
elif (app_path / "doc" / "DISCLAIMER.md").exists():
disclaimer = (app_path / "doc" / "DISCLAIMER.md").read_text()
else:
disclaimer = None
2021-04-23 20:01:40 +02:00
2022-02-23 08:18:05 +01:00
# TODO: Add url to the documentation... and actually create that documentation :D
antifeatures = {
a: deepcopy(antifeatures_list[a])
for a in from_catalog.get("antifeatures", [])
}
2022-08-05 16:39:38 +02:00
for k, v in antifeatures.items():
antifeatures[k]["title"] = value_for_lang(v["title"], lang)
2022-08-05 16:39:38 +02:00
if manifest.get("antifeatures", {}).get(k, None):
antifeatures[k]["description"] = value_for_lang(
manifest.get("antifeatures", {}).get(k, None), lang
)
2022-05-30 13:59:54 +02:00
else:
antifeatures[k]["description"] = value_for_lang(
antifeatures[k]["description"], lang
)
2022-02-23 08:18:05 +01:00
2024-03-07 01:00:40 +01:00
out: str = template.render(
lang=lang,
upstream=upstream,
2021-08-22 16:57:07 +02:00
description=description,
screenshots=screenshots,
disclaimer=disclaimer,
2022-08-05 16:39:38 +02:00
antifeatures=antifeatures,
manifest=manifest,
)
(app_path / f"README{lang_suffix}.md").write_text(out)
if __name__ == "__main__":
parser = argparse.ArgumentParser(
description="Automatically (re)generate README for apps"
)
parser.add_argument(
"app_path", help="Path to the app to generate/update READMEs for"
)
args = parser.parse_args()
2024-03-07 01:00:40 +01:00
generate_READMEs(Path(args.app_path))