From 7b65a3b6c297f67f3f400319eba19327be9faddb Mon Sep 17 00:00:00 2001 From: Laurent Peuch Date: Sun, 7 Jul 2019 03:34:43 +0200 Subject: [PATCH 1/4] [enh] add read_toml util --- doc/utils/filesystem.rst | 1 + moulinette/utils/filesystem.py | 25 +++++++++++++++++++++++++ 2 files changed, 26 insertions(+) diff --git a/doc/utils/filesystem.rst b/doc/utils/filesystem.rst index 6ae30928..a145e197 100644 --- a/doc/utils/filesystem.rst +++ b/doc/utils/filesystem.rst @@ -4,6 +4,7 @@ File system operation utils .. autofunction:: moulinette.utils.filesystem.read_file .. autofunction:: moulinette.utils.filesystem.read_json .. autofunction:: moulinette.utils.filesystem.read_yaml +.. autofunction:: moulinette.utils.filesystem.read_toml .. autofunction:: moulinette.utils.filesystem.write_to_file .. autofunction:: moulinette.utils.filesystem.append_to_file .. autofunction:: moulinette.utils.filesystem.write_to_json diff --git a/moulinette/utils/filesystem.py b/moulinette/utils/filesystem.py index 02066757..884c7afc 100644 --- a/moulinette/utils/filesystem.py +++ b/moulinette/utils/filesystem.py @@ -1,10 +1,13 @@ import os import yaml +import toml import errno import shutil import json import grp + from pwd import getpwnam +from collections import OrderedDict from moulinette import m18n from moulinette.core import MoulinetteError @@ -77,6 +80,28 @@ def read_yaml(file_path): return loaded_yaml +def read_toml(file_path): + """ + Safely read a toml file + + Keyword argument: + file_path -- Path to the toml file + """ + + # Read file + file_content = read_file(file_path) + + # Try to load toml to check if it's syntaxically correct + try: + loaded_toml = toml.loads(file_content, _dict=OrderedDict) + except Exception as e: + raise MoulinetteError(errno.EINVAL, + m18n.g('corrupted_toml', + ressource=file_path, error=str(e))) + + return loaded_toml + + def read_ldif(file_path, filtred_entries=[]): """ Safely read a LDIF file and create struct in the same style than From 9466c71a4594473c6ef89aa2fcff62be3d8775fe Mon Sep 17 00:00:00 2001 From: Laurent Peuch Date: Sun, 7 Jul 2019 03:35:52 +0200 Subject: [PATCH 2/4] [mod] depends on toml in debian/control --- debian/control | 1 + 1 file changed, 1 insertion(+) diff --git a/debian/control b/debian/control index 12a34020..0612a2ac 100644 --- a/debian/control +++ b/debian/control @@ -16,6 +16,7 @@ Depends: ${misc:Depends}, ${python:Depends}, python-gnupg, python-gevent-websocket, python-argcomplete, + python-toml, python-psutil, python-tz Replaces: yunohost-cli From d72340f536f46bc127ec0654f3d6675478b1d3df Mon Sep 17 00:00:00 2001 From: Bram Date: Sat, 20 Jul 2019 14:44:23 +0200 Subject: [PATCH 3/4] [mod] typo in comment Co-Authored-By: decentral1se --- moulinette/utils/filesystem.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/moulinette/utils/filesystem.py b/moulinette/utils/filesystem.py index 884c7afc..274c9342 100644 --- a/moulinette/utils/filesystem.py +++ b/moulinette/utils/filesystem.py @@ -91,7 +91,7 @@ def read_toml(file_path): # Read file file_content = read_file(file_path) - # Try to load toml to check if it's syntaxically correct + # Try to load toml to check if it's syntactically correct try: loaded_toml = toml.loads(file_content, _dict=OrderedDict) except Exception as e: From b1704654e12511ad6cc30ee79fa008ec9692962b Mon Sep 17 00:00:00 2001 From: Alexandre Aubin Date: Sun, 21 Jul 2019 13:32:34 +0200 Subject: [PATCH 4/4] Add the corrupted_toml string --- locales/en.json | 1 + 1 file changed, 1 insertion(+) diff --git a/locales/en.json b/locales/en.json index 0091b232..60e4d18b 100644 --- a/locales/en.json +++ b/locales/en.json @@ -43,6 +43,7 @@ "unknown_error_reading_file": "Unknown error while trying to read file {file:s}", "corrupted_json": "Corrupted json read from {ressource:s} (reason: {error:s})", "corrupted_yaml": "Corrupted yaml read from {ressource:s} (reason: {error:s})", + "corrupted_toml": "Corrupted toml read from {ressource:s} (reason: {error:s})", "error_writing_file": "Error when writing file {file:s}: {error:s}", "error_removing": "Error when removing {path:s}: {error:s}", "error_changing_file_permissions": "Error when changing permissions for {path:s}: {error:s}",