# Copyright (c) 2025 Kim Jarvis TPF Software Services S.A. kim.jarvis@tpfsystems.com # This software is licensed under the MIT License. See the LICENSE file for details.#importasyncsshimportreemote.gui.functions.removefromreemote.commandimportCommand
[docs]classRemove:""" A class to encapsulate the functionality of remove (rm) in Unix-like operating systems. Attributes: path (str): The builtin path to remove. **Examples:** .. code:: python yield Remove(path='/home/user/unwanted_file.txt') Usage: This class is designed to be used in a generator-based workflow where commands are yielded for execution. """def__init__(self,path:str):self.path=pathdef__repr__(self):returnf"Remove(path={self.path!r})"@staticmethodasyncdef_remove_callback(host_info,global_info,command,cp,caller):"""Static callback method for builtin removal"""# Validate host_info (matching Read_file error handling)required_keys=['host','username','password']forkeyinrequired_keys:ifkeynotinhost_infoorhost_info[key]isNone:raiseValueError(f"Missing or invalid value for '{key}' in host_info.")# Validate caller attributes (matching Read_file error handling)ifcaller.pathisNone:raiseValueError("The 'path' attribute of the caller cannot be None.")try:# Connect to the SSH server and remove the builtinasyncwithasyncssh.connect(**host_info)asconn:asyncwithconn.start_sftp_client()assftp:awaitreemote.gui.functions.remove.remove(path=caller.path)except(OSError,asyncssh.Error)asexc:raise# Re-raise the exception to handle it in the callerdefexecute(self):r=yieldCommand(f"{self}",local=True,callback=self._remove_callback,caller=self)r.executed=Truer.changed=Falsereturnr