mirror of
https://github.com/YunoHost/moulinette.git
synced 2024-09-03 20:06:31 +02:00
[yolo] Add a write_to_yaml utility similar to write_to_json
This commit is contained in:
parent
ed3823b88e
commit
2e2e627894
1 changed files with 24 additions and 0 deletions
|
@ -204,6 +204,30 @@ def write_to_json(file_path, data):
|
|||
raise MoulinetteError('error_writing_file', file=file_path, error=str(e))
|
||||
|
||||
|
||||
def write_to_yaml(file_path, data):
|
||||
"""
|
||||
Write a dictionnary or a list to a yaml file
|
||||
|
||||
Keyword argument:
|
||||
file_path -- Path to the output yaml file
|
||||
data -- The data to write (must be a dict or a list)
|
||||
"""
|
||||
# Assumptions
|
||||
assert isinstance(file_path, basestring)
|
||||
assert isinstance(data, dict) or isinstance(data, list)
|
||||
assert not os.path.isdir(file_path)
|
||||
assert os.path.isdir(os.path.dirname(file_path))
|
||||
|
||||
# Write dict to file
|
||||
try:
|
||||
with open(file_path, "w") as f:
|
||||
yaml.safe_dump(data, f, default_flow_style=False)
|
||||
except IOError as e:
|
||||
raise MoulinetteError('cannot_write_file', file=file_path, error=str(e))
|
||||
except Exception as e:
|
||||
raise MoulinetteError('error_writing_file', file=file_path, error=str(e))
|
||||
|
||||
|
||||
def mkdir(path, mode=0o777, parents=False, uid=None, gid=None, force=False):
|
||||
"""Create a directory with optional features
|
||||
|
||||
|
|
Loading…
Add table
Reference in a new issue