From dd6c8976f877c289ffc7f785e5be265d9999c09c Mon Sep 17 00:00:00 2001 From: Alexandre Aubin Date: Fri, 6 May 2022 21:14:44 +0200 Subject: [PATCH] manifestv2: drafty implementation for notifications/doc inside the manifest --- src/app.py | 51 +++++++++++++++++++++++++++++++++++++++++++++ src/utils/system.py | 2 +- 2 files changed, 52 insertions(+), 1 deletion(-) diff --git a/src/app.py b/src/app.py index d8e0086a9..7d6efbc0d 100644 --- a/src/app.py +++ b/src/app.py @@ -23,6 +23,7 @@ Manage apps """ +import glob import os import toml import json @@ -174,6 +175,9 @@ def app_info(app, full=False, upgradable=False): ret["setting_path"] = setting_path ret["manifest"] = local_manifest + + # FIXME: maybe this is not needed ? default ask questions are + # already set during the _get_manifest_of_app earlier ? ret["manifest"]["install"] = _set_default_ask_questions( ret["manifest"].get("install", {}) ) @@ -181,6 +185,13 @@ def app_info(app, full=False, upgradable=False): ret["from_catalog"] = from_catalog + # Hydrate app notifications and doc + for pagename, pagecontent in ret["manifest"]["doc"].items(): + ret["manifest"]["doc"][pagename] = _hydrate_app_template(pagecontent, settings) + for step, notifications in ret["manifest"]["notifications"].items(): + for name, notification in notifications.items(): + notifications[name] = _hydrate_app_template(notification, settings) + ret["is_webapp"] = "domain" in settings and "path" in settings if ret["is_webapp"]: @@ -1954,9 +1965,49 @@ def _get_manifest_of_app(path): manifest = _convert_v1_manifest_to_v2(manifest) manifest["install"] = _set_default_ask_questions(manifest.get("install", {})) + manifest["doc"], manifest["notifications"] = _parse_app_doc_and_notifications(path) + return manifest +def _parse_app_doc_and_notifications(path): + + # FIXME: need to find a way to handle i18n in there (file named FOOBAR_fr.md etc.) + + doc = {} + + for pagename in glob.glob(os.path.join(path, "doc") + "/*.md"): + name = os.path.basename(pagename)[:-len('.md')] + doc[name] = read_file(pagename) + + notifications = {} + + for step in ["pre_install", "post_install", "pre_upgrade", "post_upgrade"]: + notifications[step] = {} + if os.path.exists(os.path.join(path, "doc", "notifications", f"{step}.md")): + notifications[step]["main"] = read_file(os.path.join(path, "doc", "notifications", f"{step}.md")) + else: + for notification in glob.glob(os.path.join(path, "doc", "notifications", f"{step}.d") + "/*.md"): + name = os.path.basename(notification)[:-len('.md')] + notifications[step][name].append(read_file(notification)) + + return doc, notifications + + +def _hydrate_app_template(template, data): + + stuff_to_replace = set(re.findall(r'__[A-Z0-9]+?[A-Z0-9_]*?[A-Z0-9]*?__', template)) + + for stuff in stuff_to_replace: + + varname = stuff.strip("_").lower() + + if varname in data: + template = template.replace(stuff, data[varname]) + + return template + + def _convert_v1_manifest_to_v2(manifest): manifest = copy.deepcopy(manifest) diff --git a/src/utils/system.py b/src/utils/system.py index f7d37cf1c..2aaf0fa35 100644 --- a/src/utils/system.py +++ b/src/utils/system.py @@ -57,7 +57,7 @@ def space_used_by_directory(dirpath, follow_symlinks=True): return int(du_output.split()[0]) stat = os.statvfs(dirpath) - return stat.f_frsize * stat.f_blocks + return stat.f_frsize * stat.f_blocks # FIXME : this doesnt do what the function name suggest this does ... def human_to_binary(size: str) -> int: