From 1563eddb55664578911ba76064919c79f186fd1e Mon Sep 17 00:00:00 2001 From: Max Carrara Date: Fri, 22 Mar 2024 19:06:23 +0100 Subject: [PATCH] listvms: run formatter This commit formats the script using `black -l 80` [0], even though we don't have an official style guide for Python. [0]: https://github.com/psf/black Signed-off-by: Max Carrara --- listvms.py | 30 +++++++++++++++++++++++------- 1 file changed, 23 insertions(+), 7 deletions(-) diff --git a/listvms.py b/listvms.py index 58b09df..90b27bf 100755 --- a/listvms.py +++ b/listvms.py @@ -170,22 +170,29 @@ def list_vms(service_instance: vim.ServiceInstance) -> list[vim.VirtualMachine]: vm_view.Destroy() return vms + def parse_file_path(path) -> tuple[str, Path]: """Parse a path of the form '[datastore] file/path'""" - datastore_name, relative_path = path.split('] ', 1) - datastore_name = datastore_name.strip('[') + datastore_name, relative_path = path.split("] ", 1) + datastore_name = datastore_name.strip("[") return (datastore_name, Path(relative_path)) + def get_vm_vmx_info(vm: vim.VirtualMachine) -> VmVmxInfo: """Extract VMX file path and checksum from a VM object.""" - datastore_name, relative_vmx_path = parse_file_path(vm.config.files.vmPathName) + datastore_name, relative_vmx_path = parse_file_path( + vm.config.files.vmPathName + ) return VmVmxInfo( datastore=datastore_name, path=relative_vmx_path, - checksum=vm.config.vmxConfigChecksum.hex() if vm.config.vmxConfigChecksum else 'N/A' + checksum=vm.config.vmxConfigChecksum.hex() + if vm.config.vmxConfigChecksum + else "N/A", ) + def get_vm_disk_info(vm: vim.VirtualMachine) -> list[VmDiskInfo]: disks = [] for device in vm.config.hardware.device: @@ -196,13 +203,22 @@ def get_vm_disk_info(vm: vim.VirtualMachine) -> list[VmDiskInfo]: disks.append(VmDiskInfo(datastore, path, capacity)) except Exception as err: # if we can't figure out the disk stuff that's fine... - print("failed to get disk information for esxi vm: ", err, file=sys.stderr) + print( + "failed to get disk information for esxi vm: ", + err, + file=sys.stderr, + ) return disks -def get_all_datacenters(service_instance: vim.ServiceInstance) -> list[vim.Datacenter]: + +def get_all_datacenters( + service_instance: vim.ServiceInstance, +) -> list[vim.Datacenter]: """Retrieve all datacenters from the ESXi/vCenter server.""" content = service_instance.content - dc_view = content.viewManager.CreateContainerView(content.rootFolder, [vim.Datacenter], True) + dc_view: Any = content.viewManager.CreateContainerView( + content.rootFolder, [vim.Datacenter], True + ) datacenters = dc_view.view dc_view.Destroy() return datacenters