# 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]classMkdir:""" A class to encapsulate the functionality of mkdir in Unix-like operating systems. Attributes: path (str): The directory path to create. **Examples:** .. code:: python yield Mkdir(path='/home/user/hfs') 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"Mkdir(path={self.path!r})"@staticmethodasyncdef_mkdir_callback(host_info,global_info,command,cp,caller):"""Static callback method for directory creation"""# Validate host_info with proper error messagesrequired_keys=['host','username','password']missing_keys=[]invalid_keys=[]forkeyinrequired_keys:ifkeynotinhost_info:missing_keys.append(key)elifhost_info[key]isNone:invalid_keys.append(key)ifmissing_keys:raiseValueError(f"Missing required keys in host_info: {missing_keys}")ifinvalid_keys:raiseValueError(f"None values for keys in host_info: {invalid_keys}")# Validate caller attributesifcaller.pathisNone:raiseValueError("The 'path' attribute of the caller cannot be None.")# Clean host_info by removing None values and keys that asyncssh doesn't expectclean_host_info={'host':host_info['host'],'username':host_info['username'],'password':host_info['password']}# Add optional parameters only if they exist and are not Noneoptional_keys=['port','known_hosts','client_keys','passphrase']forkeyinoptional_keys:ifkeyinhost_infoandhost_info[key]isnotNone:clean_host_info[key]=host_info[key]try:asyncwithasyncssh.connect(**clean_host_info)asconn:asyncwithconn.start_sftp_client()assftp:# Create the remote directoryawaitsftp.mkdir(caller.path)returnf"Successfully created directory {caller.path} on {host_info['host']}"except(OSError,asyncssh.Error)asexc:# Provide more detailed error informationraiseException(f"Failed to create directory {caller.path} on {host_info['host']}: {str(exc)}")defexecute(self):r=yieldCommand(f"{self}",local=True,callback=self._mkdir_callback,caller=self)r.executed=True# Directory creation is inherently a changing operationr.changed=Truereturnr