diff --git a/platypush/plugins/file.py b/platypush/plugins/file.py index 06aa9560..d2bc6553 100644 --- a/platypush/plugins/file.py +++ b/platypush/plugins/file.py @@ -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)