[enh] add read_toml util

This commit is contained in:
Laurent Peuch 2019-07-07 03:34:43 +02:00
parent 7740e3e49d
commit 7b65a3b6c2
2 changed files with 26 additions and 0 deletions

View file

@ -4,6 +4,7 @@ File system operation utils
.. autofunction:: moulinette.utils.filesystem.read_file .. autofunction:: moulinette.utils.filesystem.read_file
.. autofunction:: moulinette.utils.filesystem.read_json .. autofunction:: moulinette.utils.filesystem.read_json
.. autofunction:: moulinette.utils.filesystem.read_yaml .. autofunction:: moulinette.utils.filesystem.read_yaml
.. autofunction:: moulinette.utils.filesystem.read_toml
.. autofunction:: moulinette.utils.filesystem.write_to_file .. autofunction:: moulinette.utils.filesystem.write_to_file
.. autofunction:: moulinette.utils.filesystem.append_to_file .. autofunction:: moulinette.utils.filesystem.append_to_file
.. autofunction:: moulinette.utils.filesystem.write_to_json .. autofunction:: moulinette.utils.filesystem.write_to_json

View file

@ -1,10 +1,13 @@
import os import os
import yaml import yaml
import toml
import errno import errno
import shutil import shutil
import json import json
import grp import grp
from pwd import getpwnam from pwd import getpwnam
from collections import OrderedDict
from moulinette import m18n from moulinette import m18n
from moulinette.core import MoulinetteError from moulinette.core import MoulinetteError
@ -77,6 +80,28 @@ def read_yaml(file_path):
return loaded_yaml 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=[]): def read_ldif(file_path, filtred_entries=[]):
""" """
Safely read a LDIF file and create struct in the same style than Safely read a LDIF file and create struct in the same style than