From 11709641d702aaf67752a6a6ca391a744a60c515 Mon Sep 17 00:00:00 2001 From: Fabio Manganiello Date: Mon, 29 Jun 2020 23:53:11 +0200 Subject: [PATCH] Extended file plugin with more util methods --- platypush/plugins/file.py | 133 +++++++++++++++++++++++++++++++------- 1 file changed, 108 insertions(+), 25 deletions(-) diff --git a/platypush/plugins/file.py b/platypush/plugins/file.py index 28b57e93d..06aa9560b 100644 --- a/platypush/plugins/file.py +++ b/platypush/plugins/file.py @@ -1,4 +1,5 @@ import os +import pathlib from platypush.plugins import Plugin, action @@ -13,53 +14,135 @@ class FilePlugin(Plugin): return os.path.abspath(os.path.expanduser(filename)) @action - def get(self, filename): + def read(self, file: str): """ - Gets the content of a file + Read and return the content of a (text) file. - :param filename: Path of the file - :type filename: str + :param file: Path of the file. """ - with open(self._get_path(filename), 'r') as f: + with open(self._get_path(file), 'r') as f: return f.read() @action - def write(self, filename, content): + def write(self, file: str, content: str): """ - Writes content to a specified filename. Previous content will be truncated. + Writes content to a specified (text) file. Previous content will be truncated. - :param filename: Path of the file - :type filename: str - - :param content: Content to write - :type content: str + :param file: Path of the file. + :param content: Content to write. """ - with open(self._get_path(filename), 'w') as f: + with open(self._get_path(file), 'w') as f: f.write(content) @action - def append(self, filename, content): + def append(self, file: str, content): """ - Append content to a specified filename + Append content to a specified (text) file. - :param filename: Path of the file - :type filename: str - - :param content: Content to write - :type content: str + :param file: Path of the file. + :param content: Content to write. """ - with open(self._get_path(filename), 'a') as f: + with open(self._get_path(file), 'a') as f: f.write(content) @action - def getsize(self, filename): + def getsize(self, file): """ - Get the size of the specified filename in bytes + Get the size of the specified file in bytes. + + :param file: File path. """ - return os.path.getsize(filename) + return os.path.getsize(self._get_path(file)) + + @action + def mkdir(self, directory: str, exist_ok=True, parents=True, mode=0o755): + """ + Create a directory. + + :param directory: Directory name/path. + :param exist_ok: If set and the directory already exist the method will + not return an error (default: True). + :param parents: If set and any of the parent directories in the path don't + exist they will be created (analogous to mkdir -p) (default: True). + :param mode: Access mode (default: 0755). + """ + pathlib.Path(self._get_path(directory)).mkdir(parents=parents, exist_ok=exist_ok, mode=mode) + + @action + def rmdir(self, directory: str): + """ + Remove a directory. The directory must be empty. + + :param directory: Directory name/path. + """ + pathlib.Path(self._get_path(directory)).rmdir() + + @action + def touch(self, file: str, mode=0o644): + """ + Create/touch a file. + + :param file: File name/path. + :param mode: File permissions (default: 0644). + """ + pathlib.Path(self._get_path(file)).touch(mode=mode) + + @action + def chmod(self, file: str, mode): + """ + Change the mode/permissions of a file. + + :param file: File name/path. + :param mode: New file permissions. + """ + pathlib.Path(self._get_path(file)).chmod(mode=mode) + + @action + def home(self) -> str: + """ + Returns the current user's home directory. + """ + return str(pathlib.Path.home()) + + @action + def rename(self, file: str, name: str): + """ + Rename/move a file. + + :param file: File to rename. + :param name: New file name. + """ + pathlib.Path(self._get_path(file)).rename(self._get_path(name)) + + @action + def link(self, file: str, target: str, symbolic=True): + """ + Create a link to a file. + + :param file: File to symlink. + :param target: Symlink path. + :param symbolic: If True, then the target link will be a symbolic link. Otherwise, + it will be a hard link (default: symbolic). + """ + file = pathlib.Path(self._get_path(file)) + target = self._get_path(target) + + if symbolic: + file.symlink_to(target) + else: + file.link_to(target) + + @action + def unlink(self, file: str): + """ + Remove a file or symbolic link. + + :param file: File/link to remove. + """ + pathlib.Path(self._get_path(file)).unlink() + # vim:sw=4:ts=4:et: -