Esempio n. 1
0
def suspend(hibernate=False):
	"""Puts Windows to Suspend/Sleep/Standby or Hibernate.
	Parameters
	hibernate: bool, default False
		If False (default), system will enter Suspend/Sleep/Standby state.
		If True, system will Hibernate, but only if Hibernate is enabled in the
		system settings. If it's not, system will Sleep.
	"""
	# Enable the SeShutdown privilege (which must be present in your
	# token in the first place)
	priv_flags = (
		win32security.TOKEN_ADJUST_PRIVILEGES | win32security.TOKEN_QUERY)
	hToken = win32security.OpenProcessToken(
		win32api.GetCurrentProcess(), priv_flags)
	priv_id = win32security.LookupPrivilegeValue(
		None, win32security.SE_SHUTDOWN_NAME)
	old_privs = win32security.AdjustTokenPrivileges(
		hToken, 0, [(priv_id, win32security.SE_PRIVILEGE_ENABLED)])
	if (not win32api.GetPwrCapabilities()['HiberFilePresent'] and hibernate):
		import warnings
		# Translators: message to user to report hibernate mode is not available.
		warnings.warn(_("Hibernate isn't available. Sleep mode is activated."))
	try:
		ctypes.windll.powrprof.SetSuspendState(not hibernate, True, False)
	except:  # noqa:E722
		# True=> Standby; False=> Hibernate
		# https://msdn.microsoft.com/pt-br/library/windows/desktop/aa373206(v=vs.85).aspx
		# says the second parameter has no effect.
		win32api.SetSystemPowerState(not hibernate, True)
	# Restore previous privileges
	win32security.AdjustTokenPrivileges(hToken, 0, old_privs)
Esempio n. 2
0
 def test_has_battery(self):
     if win32api.GetPwrCapabilities()['SystemBatteriesPresent']:
         self.assertIsNotNone(psutil.sensors_battery())
     else:
         self.assertIsNone(psutil.sensors_battery())