Always convert content to string before writing it to a file

This commit is contained in:
Fabio Manganiello 2020-06-30 11:40:08 +02:00
parent 11709641d7
commit 3174db5776

View file

@ -1,3 +1,4 @@
import json
import os
import pathlib
@ -13,6 +14,13 @@ class FilePlugin(Plugin):
def _get_path(cls, filename):
return os.path.abspath(os.path.expanduser(filename))
@classmethod
def _to_string(cls, content):
try:
return json.dumps(content)
except:
return str(content)
@action
def read(self, file: str):
"""
@ -33,6 +41,9 @@ class FilePlugin(Plugin):
:param content: Content to write.
"""
if not isinstance(content, str):
content = self._to_string(content)
with open(self._get_path(file), 'w') as f:
f.write(content)
@ -45,6 +56,9 @@ class FilePlugin(Plugin):
:param content: Content to write.
"""
if not isinstance(content, str):
content = self._to_string(content)
with open(self._get_path(file), 'a') as f:
f.write(content)