Proposal to use `wmi` PyPi module instead of Powershell
kernel-dev opened this issue · 1 comments
kernel-dev commented
Hi,
I'm wondering if it's a good idea to transform the entire application to use the wmi package instead of executing Powershell commands via subprocess
.
It's an implementation of the WMI API using pywintypes
, accommodated for Python's internal types, of course. I believe it is far more efficient, in its entirety, compared to running Powershell via subprocess
.
For example, the current method to obtain the CPU goes as follows:
@property
def cpu(self):
""" Get the current CPU you got """
ps = self.powershell("Get-WmiObject -Class Win32_Processor -ComputerName. | Select-Object -Property name")
find_cpu = re.compile(r"\rname\r[-]{1,}\r(.*?)\r").search(ps)
return find_cpu.group(1) if find_cpu else "Not found...??"
By using the WMI module, we could possibly simplify it to:
# Where:
# import wmi
# ...
# self.wmi = wmi.WMI()
@property
def cpu(self):
""" Get the current CPU """
# Obtain the first CPU identity
try:
# The CPU model
return self.wmi.instances("Win32_Processor")[0].wmi_property("Name").value
except Exception:
return "Not found...??"
AlexFlipnote commented