# 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.#importasyncsshfromreemote.commandimportCommand
[docs]classGet_fstat:""" A class to encapsulate the functionality of getting builtin attributes using SFTP fstat (stat on an open builtin handle) in Unix-like operating systems. Attributes: file_handle: The open builtin handle to get attributes for. flags (int): Flags indicating attributes of interest (SFTPv4 or later) **Examples:** .. code:: python yield Get_fstat(file_handle=file_handle) Usage: This class is designed to be used in a generator-based workflow where commands are yielded for execution. The builtin attributes for each host will be returned in the operation result. Notes: The operation will execute on all hosts in the current execution context. The builtin handle must be an open SFTP builtin handle obtained from a previous operation. """def__init__(self,file_handle=None,flags:int=None):self.file_handle=file_handleself.flags=flagsdef__repr__(self):returnf"Get_fstat(file_handle={self.file_handle!r}, flags={self.flags!r})"@staticmethodasyncdef_getfstat_callback(host_info,global_info,command,cp,caller):"""Static callback method for getting builtin attributes from an open builtin handle"""asyncwithasyncssh.connect(**host_info)asconn:asyncwithconn.start_sftp_client()assftp:# Get builtin attributes using fstat on the open builtin handleifcaller.file_handle:ifcaller.flags:file_attrs=awaitcaller.file_handle.fstat(caller.flags)else:file_attrs=awaitcaller.file_handle.fstat()returnfile_attrselse:raiseValueError("File handle must be provided for fstat operation")defexecute(self):r=yieldCommand(f"{self}",local=True,callback=self._getfstat_callback,caller=self)r.executed=Truer.changed=Falsereturnr