From 3174db5776c516874dcd453040d8bf6e21ff154c Mon Sep 17 00:00:00 2001 From: Fabio Manganiello Date: Tue, 30 Jun 2020 11:40:08 +0200 Subject: [PATCH] Always convert content to string before writing it to a file --- platypush/plugins/file.py | 14 ++++++++++++++ 1 file changed, 14 insertions(+) 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)