Beispiel #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)
Beispiel #2
0
 def hibernate(cls, force=False, **kwargs):
     # The force parameter has no effect actually
     cls.adjust_privilege(win32con.SE_SHUTDOWN_NAME, True)
     try:
         win32api.SetSystemPowerState(False, force)
     finally:
         cls.adjust_privilege(win32con.SE_SHUTDOWN_NAME, False)
Beispiel #3
0
def win_standby():
    """ Standby Windows system, returns after wakeup """
    try:
        win_power_privileges()
        win32api.SetSystemPowerState(True, True)
    except:
        logging.error(T('Failed to standby system'))
        logging.info("Traceback: ", exc_info=True)
Beispiel #4
0
def win_hibernate():
    """ Hibernate Windows system, returns after wakeup """
    try:
        win_power_privileges()
        win32api.SetSystemPowerState(False, True)
    except:
        logging.error(T('Failed to hibernate system'))
        logging.info("Traceback: ", exc_info=True)
Beispiel #5
0
 def suspend(cls, force=False, disable_wake_event=True, **kwargs):
     # The force parameter has no effect actually
     cls.adjust_privilege(win32con.SE_SHUTDOWN_NAME, True)
     try:
         # SetSuspendState(False, force, disable_wake_event)
         win32api.SetSystemPowerState(True, force)
     finally:
         cls.adjust_privilege(win32con.SE_SHUTDOWN_NAME, False)