From e2ff62f15dc60eb06e0fb46e6dffe1617a6bad9a Mon Sep 17 00:00:00 2001 From: Fabio Manganiello Date: Fri, 2 Nov 2018 13:57:24 +0000 Subject: [PATCH] Refactored file plugin --- platypush/plugins/file.py | 110 ++++++++++++++++++++------------------ 1 file changed, 58 insertions(+), 52 deletions(-) diff --git a/platypush/plugins/file.py b/platypush/plugins/file.py index c6dbd148ea..49950c8cac 100644 --- a/platypush/plugins/file.py +++ b/platypush/plugins/file.py @@ -1,52 +1,58 @@ -from platypush.plugins import Plugin, action - - -class FilePlugin(Plugin): - """ - A plugin for general-purpose file methods - """ - - @action - def 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 write(self, filename, content): - """ - Writes content to a specified filename. Previous content will be truncated. - - :param filename: Path of the file - :type filename: str - - :param content: Content to write - :type content: str - """ - - with open(filename, 'w') as f: - f.write(content) - - @action - def append(self, filename, content): - """ - Append content to a specified filename - - :param filename: Path of the file - :type filename: str - - :param content: Content to write - :type content: str - """ - - with open(filename, 'a') as f: - f.write(content) - -# vim:sw=4:ts=4:et: - +import os + +from platypush.plugins import Plugin, action + + +class FilePlugin(Plugin): + """ + A plugin for general-purpose file methods + """ + + @classmethod + def _get_path(cls, filename): + return os.path.abspath(os.path.expanduser(filename)) + + @action + def get(self, filename): + """ + Gets the content of a file + + :param filename: Path of the file + :type filename: str + """ + + with open(self._get_path(filename), 'r') as f: + return f.read() + + @action + def write(self, filename, content): + """ + Writes content to a specified filename. Previous content will be truncated. + + :param filename: Path of the file + :type filename: str + + :param content: Content to write + :type content: str + """ + + with open(self._get_path(filename), 'w') as f: + f.write(content) + + @action + def append(self, filename, content): + """ + Append content to a specified filename + + :param filename: Path of the file + :type filename: str + + :param content: Content to write + :type content: str + """ + + with open(self._get_path(filename), 'a') as f: + f.write(content) + +# vim:sw=4:ts=4:et: +