From b14bb44c1087df48a6a3617b3546c03da7395eec Mon Sep 17 00:00:00 2001 From: Fabio Manganiello Date: Tue, 16 Oct 2018 08:49:55 +0200 Subject: [PATCH] Added utils actions to handle files --- platypush/plugins/utils.py | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/platypush/plugins/utils.py b/platypush/plugins/utils.py index f16277906..c774aa9f2 100644 --- a/platypush/plugins/utils.py +++ b/platypush/plugins/utils.py @@ -19,6 +19,36 @@ class UtilsPlugin(Plugin): time.sleep(seconds) + @action + def file_get(self, filename): + """ + Gets the content of a file + + :param filename: Path of the file + :type filename: str + """ + + with open(filename, 'r') as f: + return f.read() + + @action + def file_write(self, filename, content, append=False): + """ + Writes content to a specified filename + + :param filename: Path of the file + :type filename: str + + :param content: Content to write + :type content: str + + :param append: If set, the content will be appended to the file (default: False, truncate file upon write) + :type append: bool + """ + + mode = 'a' if append else 'w' + with open(filename, mode) as f: + f.write(content) # vim:sw=4:ts=4:et: