forked from platypush/platypush
Always convert content to string before writing it to a file
This commit is contained in:
parent
11709641d7
commit
3174db5776
1 changed files with 14 additions and 0 deletions
|
@ -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)
|
||||
|
||||
|
|
Loading…
Reference in a new issue