platypush/platypush/plugins/file.py

53 lines
1.1 KiB
Python
Raw Normal View History

2018-10-16 09:01:22 +02:00
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
2018-10-17 09:02:22 +02:00
def write(self, filename, content):
2018-10-16 09:01:22 +02:00
"""
2018-10-17 09:02:22 +02:00
Writes content to a specified filename. Previous content will be truncated.
2018-10-16 09:01:22 +02:00
:param filename: Path of the file
:type filename: str
:param content: Content to write
:type content: str
2018-10-17 09:02:22 +02:00
"""
with open(filename, 'w') as f:
f.write(content)
@action
def append(self, filename, content):
"""
Append content to a specified filename
2018-10-16 09:01:22 +02:00
2018-10-17 09:02:22 +02:00
:param filename: Path of the file
:type filename: str
:param content: Content to write
:type content: str
2018-10-16 09:01:22 +02:00
"""
2018-10-17 09:02:22 +02:00
with open(filename, 'a') as f:
2018-10-16 09:01:22 +02:00
f.write(content)
# vim:sw=4:ts=4:et: