diff --git a/locale/fr/LC_MESSAGES/rougail_user_data_yaml.po b/locale/fr/LC_MESSAGES/rougail_user_data_yaml.po index 289cb5f..a30893d 100644 --- a/locale/fr/LC_MESSAGES/rougail_user_data_yaml.po +++ b/locale/fr/LC_MESSAGES/rougail_user_data_yaml.po @@ -5,8 +5,8 @@ msgid "" msgstr "" "Project-Id-Version: \n" -"POT-Creation-Date: 2025-05-11 10:23+0200\n" -"PO-Revision-Date: 2025-05-11 10:23+0200\n" +"POT-Creation-Date: 2025-05-14 08:28+0200\n" +"PO-Revision-Date: 2025-05-14 08:29+0200\n" "Last-Translator: \n" "Language-Team: \n" "Language: fr\n" @@ -16,32 +16,36 @@ msgstr "" "Generated-By: pygettext.py 1.5\n" "X-Generator: Poedit 3.5\n" -#: src/rougail/user_data_yaml/__init__.py:43 +#: src/rougail/user_data_yaml/__init__.py:46 msgid "yaml is not set in step.user_data" msgstr "\"yaml\" n'est pas défini dans step.user_data" -#: src/rougail/user_data_yaml/__init__.py:63 +#: src/rougail/user_data_yaml/__init__.py:72 msgid "cannot load \"{0}\", the root value is not a dict but \"{1}\"" msgstr "" "ne peut charger \"{0}\", la valeur racine n'est pas une dictionnaire mais " "\"{1}\"" -#: src/rougail/user_data_yaml/__init__.py:86 +#: src/rougail/user_data_yaml/__init__.py:95 msgid "the YAML file \"{0}\"" msgstr "le fichier YAML \"{0}\"" -#: src/rougail/user_data_yaml/__init__.py:117 +#: src/rougail/user_data_yaml/__init__.py:127 msgid "\"{0}\" in {1} has an unknown value" msgstr "\"{0}\" dans {1} a une valeur inconnue" -#: src/rougail/user_data_yaml/config.py:29 +#: src/rougail/user_data_yaml/config.py:30 msgid "Configuration rougail-user-data-yaml" msgstr "Configuration de rougail-user-data-yaml" -#: src/rougail/user_data_yaml/config.py:38 -msgid "Filename" -msgstr "Nom du fichier" +#: src/rougail/user_data_yaml/config.py:39 +msgid "File or directory name where user datas are stored" +msgstr "" +"Nom du fichier ou répertoire où sont enregistré les données utilisateur" -#: src/rougail/user_data_yaml/config.py:49 +#: src/rougail/user_data_yaml/config.py:48 msgid "File that may contain secrets" msgstr "Le fichier peut contenir des secrets" + +#~ msgid "Filename" +#~ msgstr "Nom du fichier" diff --git a/locale/rougail_user_data_yaml.pot b/locale/rougail_user_data_yaml.pot index e2d896f..7358ec7 100644 --- a/locale/rougail_user_data_yaml.pot +++ b/locale/rougail_user_data_yaml.pot @@ -5,7 +5,7 @@ msgid "" msgstr "" "Project-Id-Version: PACKAGE VERSION\n" -"POT-Creation-Date: 2025-05-12 08:39+0200\n" +"POT-Creation-Date: 2025-05-14 08:29+0200\n" "PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n" "Last-Translator: FULL NAME \n" "Language-Team: LANGUAGE \n" @@ -15,19 +15,19 @@ msgstr "" "Generated-By: pygettext.py 1.5\n" -#: src/rougail/user_data_yaml/__init__.py:45 +#: src/rougail/user_data_yaml/__init__.py:46 msgid "yaml is not set in step.user_data" msgstr "" -#: src/rougail/user_data_yaml/__init__.py:65 +#: src/rougail/user_data_yaml/__init__.py:72 msgid "cannot load \"{0}\", the root value is not a dict but \"{1}\"" msgstr "" -#: src/rougail/user_data_yaml/__init__.py:88 +#: src/rougail/user_data_yaml/__init__.py:95 msgid "the YAML file \"{0}\"" msgstr "" -#: src/rougail/user_data_yaml/__init__.py:120 +#: src/rougail/user_data_yaml/__init__.py:127 msgid "\"{0}\" in {1} has an unknown value" msgstr "" @@ -36,10 +36,10 @@ msgid "Configuration rougail-user-data-yaml" msgstr "" #: src/rougail/user_data_yaml/config.py:39 -msgid "Filename" +msgid "File or directory name where user datas are stored" msgstr "" -#: src/rougail/user_data_yaml/config.py:50 +#: src/rougail/user_data_yaml/config.py:48 msgid "File that may contain secrets" msgstr "" diff --git a/src/rougail/user_data_yaml/__init__.py b/src/rougail/user_data_yaml/__init__.py index 9822a29..ed5ff47 100644 --- a/src/rougail/user_data_yaml/__init__.py +++ b/src/rougail/user_data_yaml/__init__.py @@ -17,6 +17,7 @@ along with this program. If not, see . """ from ruamel.yaml import YAML +from pathlib import Path from rougail.error import ExtentionError from tiramisu.error import ValueOptionError, PropertiesOptionError, LeadershipError @@ -56,48 +57,54 @@ class RougailUserDataYaml: self.yaml = YAML() user_datas = [] for idx, filename in enumerate(self.filenames): - file_values = self.open(filename) - if not file_values: - continue - values = {} - if not isinstance(file_values, dict): - self.errors.append( - _( - 'cannot load "{0}", the root value is not a dict but "{1}"' - ).format(filename, file_values) - ) + filename = Path(filename) + if filename.is_file(): + filenames = [filename] else: - self.parse( - values, - "", - file_values, - filename, + filenames = list(filename.glob('*.yml')) + list(filename.glob('*.yaml')) + for filename in sorted(filenames): + file_values = self.open(filename) + if not file_values: + continue + values = {} + if not isinstance(file_values, dict): + self.errors.append( + _( + 'cannot load "{0}", the root value is not a dict but "{1}"' + ).format(filename, file_values) + ) + else: + self.parse( + values, + "", + file_values, + filename, + ) + if self.file_with_secrets == "none": + allow_secrets_variables = False + elif self.file_with_secrets == "first": + allow_secrets_variables = idx == 0 + elif self.file_with_secrets == "last": + if not idx: + last_filenames = len(self.filenames) - 1 + allow_secrets_variables = idx == last_filenames + else: + allow_secrets_variables = True + user_datas.append( + { + "source": _('the YAML file "{0}"').format(filename), + "errors": self.errors, + "warnings": self.warnings, + "values": values, + "options": { + "allow_secrets_variables": allow_secrets_variables, + }, + } ) - if self.file_with_secrets == "none": - allow_secrets_variables = False - elif self.file_with_secrets == "first": - allow_secrets_variables = idx == 0 - elif self.file_with_secrets == "last": - if not idx: - last_filenames = len(self.filenames) - 1 - allow_secrets_variables = idx == last_filenames - else: - allow_secrets_variables = True - user_datas.append( - { - "source": _('the YAML file "{0}"').format(filename), - "errors": self.errors, - "warnings": self.warnings, - "values": values, - "options": { - "allow_secrets_variables": allow_secrets_variables, - }, - } - ) return user_datas def open(self, filename: str) -> dict: - with open(filename) as fh_config: + with filename.open() as fh_config: return self.yaml.load(fh_config) def parse( diff --git a/src/rougail/user_data_yaml/config.py b/src/rougail/user_data_yaml/config.py index 584954c..0dc5a4d 100644 --- a/src/rougail/user_data_yaml/config.py +++ b/src/rougail/user_data_yaml/config.py @@ -36,15 +36,13 @@ yaml: {{% endif %}} filename: - description: {_("Filename")} + description: {_("File or directory name where user datas are stored")} alternative_name: ff type: unix_filename multi: true params: allow_relative: True test_existence: True - types: - - file file_with_secrets: description: {_("File that may contain secrets")} diff --git a/src/rougail/user_data_yaml/locale/fr/LC_MESSAGES/rougail_user_data_yaml.mo b/src/rougail/user_data_yaml/locale/fr/LC_MESSAGES/rougail_user_data_yaml.mo index ed9c7e4..8314ce5 100644 Binary files a/src/rougail/user_data_yaml/locale/fr/LC_MESSAGES/rougail_user_data_yaml.mo and b/src/rougail/user_data_yaml/locale/fr/LC_MESSAGES/rougail_user_data_yaml.mo differ diff --git a/tests/test_load.py b/tests/test_load.py index 898f6b0..f2d6389 100644 --- a/tests/test_load.py +++ b/tests/test_load.py @@ -163,11 +163,11 @@ def test_errors_2(): ####################################################################### -error_env = list((Path(__file__).parent / 'errors' / 'yaml').glob("*.yaml")) -error_env.sort() +error_yaml = list((Path(__file__).parent / 'errors' / 'yaml').glob("*.yaml")) +error_yaml.sort() -@fixture(scope="module", params=error_env, ids=idfn) +@fixture(scope="module", params=error_yaml, ids=idfn) def test_file_error(request): return request.param @@ -191,3 +191,26 @@ def test_dictionaries_error(test_file_error): with open(errors_file) as json_file: expected_errors = load(json_file) assert expected_errors == errors, errors_file + + +def test_dictionaries_directory(): + test_dir = Path(__file__).parent / 'directory' + rougailconfig = get_rougail_config(test_dir / 'structure') + ################################## + rougailconfig['step.user_data'] = ['yaml'] + rougailconfig['yaml.filename'] = [str(test_dir / 'yaml')] + rougail = Rougail(rougailconfig) + config = rougail.run() + ################################## + # loads variables in the tiramisu config + generated_user_data = RougailUserData(config, rougailconfig=rougailconfig).run() + rougail.user_datas(generated_user_data) + config.property.read_only() + data = dict(config_to_dict(config.value.get())) + data_file = test_dir / "result.txt" + if not data_file.is_file(): + with open(data_file, 'a') as json_file: + dump(data, json_file, indent=4) + with open(data_file) as json_file: + expected = load(json_file) + assert expected == data, data_file