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,3 +1,5 @@
import os
from platypush.plugins import Plugin, action from platypush.plugins import Plugin, action
@ -6,6 +8,10 @@ class FilePlugin(Plugin):
A plugin for general-purpose file methods A plugin for general-purpose file methods
""" """
@classmethod
def _get_path(cls, filename):
return os.path.abspath(os.path.expanduser(filename))
@action @action
def get(self, filename): def get(self, filename):
""" """
@ -15,7 +21,7 @@ class FilePlugin(Plugin):
:type filename: str :type filename: str
""" """
with open(filename, 'r') as f: with open(self._get_path(filename), 'r') as f:
return f.read() return f.read()
@action @action
@ -30,7 +36,7 @@ class FilePlugin(Plugin):
:type content: str :type content: str
""" """
with open(filename, 'w') as f: with open(self._get_path(filename), 'w') as f:
f.write(content) f.write(content)
@action @action
@ -45,7 +51,7 @@ class FilePlugin(Plugin):
:type content: str :type content: str
""" """
with open(filename, 'a') as f: with open(self._get_path(filename), 'a') as f:
f.write(content) f.write(content)
# vim:sw=4:ts=4:et: # vim:sw=4:ts=4:et: