From dc8f51e2f80c2574579b0640372cf05b2c747fb3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?F=C3=A9lix=20Pi=C3=A9dallu?= Date: Thu, 14 Dec 2023 17:21:47 +0100 Subject: [PATCH 1/4] Add a new utils.algorithms file for code helpers. The first helper, recursive_apply, goes through recursive dict/lists to apply a function on non-recursable types --- src/utils/algorithms.py | 13 +++++++++++++ 1 file changed, 13 insertions(+) create mode 100644 src/utils/algorithms.py diff --git a/src/utils/algorithms.py b/src/utils/algorithms.py new file mode 100644 index 000000000..02e6205ee --- /dev/null +++ b/src/utils/algorithms.py @@ -0,0 +1,13 @@ +#!/usr/bin/env python3 + +from typing import Any, Callable + + +def recursive_apply(function: Callable, data: Any) -> Any: + if isinstance(data, dict): # FIXME: hashable? + return {key: recursive_apply(value, function) for key, value in data.items()} + + if isinstance(data, list): # FIXME: iterable? + return [recursive_apply(value, function) for value in data] + + return function(data) From 7981653c23e96f74bb31d03f10b36cf34909275a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?F=C3=A9lix=20Pi=C3=A9dallu?= Date: Thu, 14 Dec 2023 17:22:21 +0100 Subject: [PATCH 2/4] Apply __APP__ -> self.app recursively thanks to recursive_apply --- src/utils/resources.py | 18 ++++++++++++------ 1 file changed, 12 insertions(+), 6 deletions(-) diff --git a/src/utils/resources.py b/src/utils/resources.py index 324a0762c..7cbdfb893 100644 --- a/src/utils/resources.py +++ b/src/utils/resources.py @@ -34,6 +34,7 @@ from moulinette.utils.filesystem import ( ) from yunohost.utils.system import system_arch from yunohost.utils.error import YunohostError, YunohostValidationError +from yunohost.utils.algorithms import recursive_apply logger = getActionLogger("yunohost.app_resources") @@ -146,15 +147,20 @@ class AppResource: def __init__(self, properties: Dict[str, Any], app: str, manager=None): self.app = app self.manager = manager + properties = self.default_properties | properties - for key, value in self.default_properties.items(): - if isinstance(value, str): - value = value.replace("__APP__", self.app) - setattr(self, key, value) + replacements: dict[str, str] = { + "__APP__": self.app, + } + + def replace_tokens_in_strings(data: Any): + if not isinstance(data, str): + return + for token, replacement in replacements.items(): + data = data.replace(token, replacement) + recursive_apply(replace_tokens_in_strings, properties) for key, value in properties.items(): - if isinstance(value, str): - value = value.replace("__APP__", self.app) setattr(self, key, value) def get_setting(self, key): From fb2ca1f27ddfc6478c4a8d47c71f386f70569679 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?F=C3=A9lix=20Pi=C3=A9dallu?= Date: Thu, 14 Dec 2023 17:25:21 +0100 Subject: [PATCH 3/4] Add __ARCH__ and __DEBIAN_VERSION__ to available variables in manifest.toml --- src/utils/resources.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/utils/resources.py b/src/utils/resources.py index 7cbdfb893..a9d12cd2b 100644 --- a/src/utils/resources.py +++ b/src/utils/resources.py @@ -32,7 +32,7 @@ from moulinette.utils.filesystem import mkdir, chown, chmod, write_to_file from moulinette.utils.filesystem import ( rm, ) -from yunohost.utils.system import system_arch +from yunohost.utils.system import system_arch, debian_version from yunohost.utils.error import YunohostError, YunohostValidationError from yunohost.utils.algorithms import recursive_apply @@ -151,6 +151,8 @@ class AppResource: replacements: dict[str, str] = { "__APP__": self.app, + "__YNH_ARCH__": system_arch(), + "__YNH_DEBIAN_VERSION__": debian_version(), } def replace_tokens_in_strings(data: Any): From 0864014ea11fa6e7eb63461f1260cd45a3cd72ff Mon Sep 17 00:00:00 2001 From: Alexandre Aubin Date: Wed, 10 Apr 2024 20:59:54 +0200 Subject: [PATCH 4/4] Move recursive_apply directly where it's used, having a small algorithms.py just for this is too convoluted --- src/utils/algorithms.py | 13 ------------- src/utils/resources.py | 13 +++++++++++-- 2 files changed, 11 insertions(+), 15 deletions(-) delete mode 100644 src/utils/algorithms.py diff --git a/src/utils/algorithms.py b/src/utils/algorithms.py deleted file mode 100644 index 02e6205ee..000000000 --- a/src/utils/algorithms.py +++ /dev/null @@ -1,13 +0,0 @@ -#!/usr/bin/env python3 - -from typing import Any, Callable - - -def recursive_apply(function: Callable, data: Any) -> Any: - if isinstance(data, dict): # FIXME: hashable? - return {key: recursive_apply(value, function) for key, value in data.items()} - - if isinstance(data, list): # FIXME: iterable? - return [recursive_apply(value, function) for value in data] - - return function(data) diff --git a/src/utils/resources.py b/src/utils/resources.py index a9d12cd2b..bdc40d202 100644 --- a/src/utils/resources.py +++ b/src/utils/resources.py @@ -22,7 +22,7 @@ import shutil import random import tempfile import subprocess -from typing import Dict, Any, List, Union +from typing import Dict, Any, List, Union, Callable from moulinette import m18n from moulinette.utils.text import random_ascii @@ -34,7 +34,6 @@ from moulinette.utils.filesystem import ( ) from yunohost.utils.system import system_arch, debian_version from yunohost.utils.error import YunohostError, YunohostValidationError -from yunohost.utils.algorithms import recursive_apply logger = getActionLogger("yunohost.app_resources") @@ -155,11 +154,21 @@ class AppResource: "__YNH_DEBIAN_VERSION__": debian_version(), } + def recursive_apply(function: Callable, data: Any) -> Any: + if isinstance(data, dict): # FIXME: hashable? + return {key: recursive_apply(value, function) for key, value in data.items()} + + if isinstance(data, list): # FIXME: iterable? + return [recursive_apply(value, function) for value in data] + + return function(data) + def replace_tokens_in_strings(data: Any): if not isinstance(data, str): return for token, replacement in replacements.items(): data = data.replace(token, replacement) + recursive_apply(replace_tokens_in_strings, properties) for key, value in properties.items():