Refactored file plugin

This commit is contained in:
Fabio Manganiello 2018-11-02 13:57:24 +00:00
parent c05fc9ee3f
commit e2ff62f15d

View file

@ -1,52 +1,58 @@
from platypush.plugins import Plugin, action import os
from platypush.plugins import Plugin, action
class FilePlugin(Plugin):
"""
A plugin for general-purpose file methods class FilePlugin(Plugin):
""" """
A plugin for general-purpose file methods
@action """
def get(self, filename):
""" @classmethod
Gets the content of a file def _get_path(cls, filename):
return os.path.abspath(os.path.expanduser(filename))
:param filename: Path of the file
:type filename: str @action
""" def get(self, filename):
"""
with open(filename, 'r') as f: Gets the content of a file
return f.read()
:param filename: Path of the file
@action :type filename: str
def write(self, filename, content): """
"""
Writes content to a specified filename. Previous content will be truncated. with open(self._get_path(filename), 'r') as f:
return f.read()
:param filename: Path of the file
:type filename: str @action
def write(self, filename, content):
:param content: Content to write """
:type content: str Writes content to a specified filename. Previous content will be truncated.
"""
:param filename: Path of the file
with open(filename, 'w') as f: :type filename: str
f.write(content)
:param content: Content to write
@action :type content: str
def append(self, filename, content): """
"""
Append content to a specified filename with open(self._get_path(filename), 'w') as f:
f.write(content)
:param filename: Path of the file
:type filename: str @action
def append(self, filename, content):
:param content: Content to write """
:type content: str Append content to a specified filename
"""
:param filename: Path of the file
with open(filename, 'a') as f: :type filename: str
f.write(content)
:param content: Content to write
# vim:sw=4:ts=4:et: :type content: str
"""
with open(self._get_path(filename), 'a') as f:
f.write(content)
# vim:sw=4:ts=4:et: