From e3364abb9364aff789cecbfc8d79c5633874975c Mon Sep 17 00:00:00 2001 From: Walter Purcaro Date: Wed, 29 Jul 2015 05:50:27 +0200 Subject: New plugin: AntiStandby Fix https://github.com/pyload/pyload/issues/1231 --- module/plugins/hooks/AntiStandby.py | 157 ++++++++++++++++++++++++++++++++++++ 1 file changed, 157 insertions(+) create mode 100644 module/plugins/hooks/AntiStandby.py (limited to 'module/plugins/hooks/AntiStandby.py') diff --git a/module/plugins/hooks/AntiStandby.py b/module/plugins/hooks/AntiStandby.py new file mode 100644 index 000000000..4a1155274 --- /dev/null +++ b/module/plugins/hooks/AntiStandby.py @@ -0,0 +1,157 @@ +# -*- coding: utf-8 -*- + +from __future__ import with_statement + +import os +import time +import subprocess +import sys + +try: + import caffeine +except ImportError: + pass + +from module.plugins.Addon import Addon, Expose + + +class Kernel32(object): + ES_AWAYMODE_REQUIRED = 0x00000040 + ES_CONTINUOUS = 0x80000000 + ES_DISPLAY_REQUIRED = 0x00000002 + ES_SYSTEM_REQUIRED = 0x00000001 + ES_USER_PRESENT = 0x00000004 + + +class AntiStandby(Addon): + __name__ = "AntiStandby" + __type__ = "hook" + __version__ = "0.01" + __status__ = "testing" + + __config__ = [("activated", "bool", "Activated" , True ), + ("hdd" , "bool", "Prevent HDD standby" , True ), + ("system" , "bool", "Prevent OS standby" , True ), + ("display" , "bool", "Prevent display standby" , False), + ("interval" , "int" , "HDD touching interval in seconds", 7 )] + + __description__ = """Prevent OS, HDD and display standby""" + __license__ = "GPLv3" + __authors__ = [("Walter Purcaro", "vuolter@gmail.com")] + + + TMP_FILE = ".antistandby" + MIN_INTERVAL = 5 + + + def setup(self): + self.mtime = 0 + + + def activate(self): + hdd = self.get_config('hdd') + system = self.get_config('system') + display = self.get_config('display') + + if hdd: + self.interval = max(self.get_config('interval'), self.MIN_INTERVAL) + self.init_periodical(self, threaded=True) + + if os.name == "nt": + self.win_standby(system, display) + + elif sys.platform == "darwin": + self.osx_standby(system, display) + + else: + self.linux_standby(system, display) + + + def deactivate(self): + try: + os.remove(self.TMP_FILE) + + except OSError: + pass + + if os.name == "nt": + self.win_standby(True) + + elif sys.platform == "darwin": + self.osx_standby(True) + + else: + self.linux_standby(True) + + + @Expose + def win_standby(system=True, display=True): + import ctypes + + set = ctypes.windll.kernel32.SetThreadExecutionState + + if system: + if display: + set(Kernel32.ES_CONTINUOUS) + else: + set(Kernel32.ES_CONTINUOUS | Kernel32.ES_DISPLAY_REQUIRED) + else: + if display: + set(Kernel32.ES_CONTINUOUS | Kernel32.ES_SYSTEM_REQUIRED) + else: + set(Kernel32.ES_CONTINUOUS | Kernel32.ES_SYSTEM_REQUIRED | Kernel32.ES_DISPLAY_REQUIRED) + + + @Expose + def osx_standby(system=True, display=True): + try: + if system: + caffeine.off() + else: + caffeine.on(display) + + except NameError: + self.log_warning(_("Unable to change power state"), + _("caffeine lib not found")) + + except Exception, e: + self.log_warning(_("Unable to change power state"), e) + + + @Expose + def linux_standby(system=True, display=True): + try: + if display: + subprocess.call(["xset", "+dpms", "s", "default"]) + else: + subprocess.call(["xset", "-dpms", "s", "off"]) + + except Exception, e: + self.log_warning(_("Unable to change power state"), e) + + + @Expose + def touch(self, path): + with open(path, 'w'): + os.utime(path, None) + + self.mtime = time.time() + + + @Expose + def max_mtime(self, path): + return max(os.path.getmtime(os.path.join(root, file)) + for file in files + for root, dirs, files in os.walk(path, topdown=False)) + + + def periodical(self): + if self.get_config('hdd') is False: + return + + if os.name != "nt": + path = self.pyload.config.get("general", "download_folder") + if (self.max_mtime(path) - self.mtime) < self.interval: + return + + self.touch(self.TMP_FILE) -- cgit v1.2.3 From 5181a04839f700f482fabed842b2ea0150b2c21d Mon Sep 17 00:00:00 2001 From: Walter Purcaro Date: Wed, 29 Jul 2015 06:01:58 +0200 Subject: [AntiStandby] Hotfix --- module/plugins/hooks/AntiStandby.py | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) (limited to 'module/plugins/hooks/AntiStandby.py') diff --git a/module/plugins/hooks/AntiStandby.py b/module/plugins/hooks/AntiStandby.py index 4a1155274..4a542953e 100644 --- a/module/plugins/hooks/AntiStandby.py +++ b/module/plugins/hooks/AntiStandby.py @@ -12,7 +12,7 @@ try: except ImportError: pass -from module.plugins.Addon import Addon, Expose +from module.plugins.internal.Addon import Addon, Expose class Kernel32(object): @@ -26,7 +26,7 @@ class Kernel32(object): class AntiStandby(Addon): __name__ = "AntiStandby" __type__ = "hook" - __version__ = "0.01" + __version__ = "0.02" __status__ = "testing" __config__ = [("activated", "bool", "Activated" , True ), @@ -50,12 +50,12 @@ class AntiStandby(Addon): def activate(self): hdd = self.get_config('hdd') - system = self.get_config('system') - display = self.get_config('display') + system = not self.get_config('system') + display = not self.get_config('display') if hdd: self.interval = max(self.get_config('interval'), self.MIN_INTERVAL) - self.init_periodical(self, threaded=True) + self.init_periodical(threaded=True) if os.name == "nt": self.win_standby(system, display) @@ -85,7 +85,7 @@ class AntiStandby(Addon): @Expose - def win_standby(system=True, display=True): + def win_standby(self, system=True, display=True): import ctypes set = ctypes.windll.kernel32.SetThreadExecutionState @@ -103,7 +103,7 @@ class AntiStandby(Addon): @Expose - def osx_standby(system=True, display=True): + def osx_standby(self, system=True, display=True): try: if system: caffeine.off() @@ -119,7 +119,7 @@ class AntiStandby(Addon): @Expose - def linux_standby(system=True, display=True): + def linux_standby(self, system=True, display=True): try: if display: subprocess.call(["xset", "+dpms", "s", "default"]) -- cgit v1.2.3 From bf6297fe115d0ca46ec39a4dbfd58b1878c6287d Mon Sep 17 00:00:00 2001 From: Walter Purcaro Date: Wed, 29 Jul 2015 08:26:05 +0200 Subject: Fix https://github.com/pyload/pyload/issues/1588 --- module/plugins/hooks/AntiStandby.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) (limited to 'module/plugins/hooks/AntiStandby.py') diff --git a/module/plugins/hooks/AntiStandby.py b/module/plugins/hooks/AntiStandby.py index 4a542953e..b0262ed33 100644 --- a/module/plugins/hooks/AntiStandby.py +++ b/module/plugins/hooks/AntiStandby.py @@ -26,14 +26,14 @@ class Kernel32(object): class AntiStandby(Addon): __name__ = "AntiStandby" __type__ = "hook" - __version__ = "0.02" + __version__ = "0.03" __status__ = "testing" __config__ = [("activated", "bool", "Activated" , True ), ("hdd" , "bool", "Prevent HDD standby" , True ), ("system" , "bool", "Prevent OS standby" , True ), ("display" , "bool", "Prevent display standby" , False), - ("interval" , "int" , "HDD touching interval in seconds", 7 )] + ("interval" , "int" , "HDD touching interval in seconds", 25 )] __description__ = """Prevent OS, HDD and display standby""" __license__ = "GPLv3" @@ -141,8 +141,8 @@ class AntiStandby(Addon): @Expose def max_mtime(self, path): return max(os.path.getmtime(os.path.join(root, file)) - for file in files - for root, dirs, files in os.walk(path, topdown=False)) + for root, dirs, files in os.walk(path, topdown=False) + for file in files) def periodical(self): -- cgit v1.2.3 From 5ced8cccb300b8971cec4326016df79670c4e7ad Mon Sep 17 00:00:00 2001 From: Walter Purcaro Date: Wed, 29 Jul 2015 12:32:28 +0200 Subject: Fix https://github.com/pyload/pyload/issues/1595 --- module/plugins/hooks/AntiStandby.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'module/plugins/hooks/AntiStandby.py') diff --git a/module/plugins/hooks/AntiStandby.py b/module/plugins/hooks/AntiStandby.py index b0262ed33..5c54b8c96 100644 --- a/module/plugins/hooks/AntiStandby.py +++ b/module/plugins/hooks/AntiStandby.py @@ -26,7 +26,7 @@ class Kernel32(object): class AntiStandby(Addon): __name__ = "AntiStandby" __type__ = "hook" - __version__ = "0.03" + __version__ = "0.04" __status__ = "testing" __config__ = [("activated", "bool", "Activated" , True ), @@ -44,7 +44,7 @@ class AntiStandby(Addon): MIN_INTERVAL = 5 - def setup(self): + def init(self): self.mtime = 0 -- cgit v1.2.3 From 2e5ad6d1cc7b352975df79a0885413ad157f0ba0 Mon Sep 17 00:00:00 2001 From: Walter Purcaro Date: Wed, 29 Jul 2015 18:49:49 +0200 Subject: Fix https://github.com/pyload/pyload/issues/1600 --- module/plugins/hooks/AntiStandby.py | 29 +++++++++++++++++++++-------- 1 file changed, 21 insertions(+), 8 deletions(-) (limited to 'module/plugins/hooks/AntiStandby.py') diff --git a/module/plugins/hooks/AntiStandby.py b/module/plugins/hooks/AntiStandby.py index 5c54b8c96..3667e378a 100644 --- a/module/plugins/hooks/AntiStandby.py +++ b/module/plugins/hooks/AntiStandby.py @@ -26,7 +26,7 @@ class Kernel32(object): class AntiStandby(Addon): __name__ = "AntiStandby" __type__ = "hook" - __version__ = "0.04" + __version__ = "0.05" __status__ = "testing" __config__ = [("activated", "bool", "Activated" , True ), @@ -45,6 +45,7 @@ class AntiStandby(Addon): def init(self): + self.pid = None self.mtime = 0 @@ -121,6 +122,13 @@ class AntiStandby(Addon): @Expose def linux_standby(self, system=True, display=True): try: + if system: + if self.pid: + self.pid.kill() + + elif not self.pid: + self.pid = subprocess.Popen(["caffeine"]) + if display: subprocess.call(["xset", "+dpms", "s", "default"]) else: @@ -140,18 +148,23 @@ class AntiStandby(Addon): @Expose def max_mtime(self, path): - return max(os.path.getmtime(os.path.join(root, file)) - for root, dirs, files in os.walk(path, topdown=False) - for file in files) + return max(0, 0, + *(os.path.getmtime(os.path.join(root, file)) + for root, dirs, files in os.walk(path, topdown=False) + for file in files)) def periodical(self): if self.get_config('hdd') is False: return - if os.name != "nt": - path = self.pyload.config.get("general", "download_folder") - if (self.max_mtime(path) - self.mtime) < self.interval: - return + if (self.pyfile.threadManager.pause or + not self.pyfile.api.isTimeDownload() or + not self.pyfile.threadManager.getActiveFiles()): + return + + path = self.pyload.config.get("general", "download_folder") + if (self.max_mtime(path) - self.mtime) < self.interval: + return self.touch(self.TMP_FILE) -- cgit v1.2.3 From 1142e6c4f2acef87dce9d87d6053f9679636953a Mon Sep 17 00:00:00 2001 From: Walter Purcaro Date: Wed, 29 Jul 2015 21:06:01 +0200 Subject: Fix https://github.com/pyload/pyload/issues/1600 (2) --- module/plugins/hooks/AntiStandby.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) (limited to 'module/plugins/hooks/AntiStandby.py') diff --git a/module/plugins/hooks/AntiStandby.py b/module/plugins/hooks/AntiStandby.py index 3667e378a..93eca09cc 100644 --- a/module/plugins/hooks/AntiStandby.py +++ b/module/plugins/hooks/AntiStandby.py @@ -26,7 +26,7 @@ class Kernel32(object): class AntiStandby(Addon): __name__ = "AntiStandby" __type__ = "hook" - __version__ = "0.05" + __version__ = "0.06" __status__ = "testing" __config__ = [("activated", "bool", "Activated" , True ), @@ -158,9 +158,9 @@ class AntiStandby(Addon): if self.get_config('hdd') is False: return - if (self.pyfile.threadManager.pause or - not self.pyfile.api.isTimeDownload() or - not self.pyfile.threadManager.getActiveFiles()): + if (self.pyload.threadManager.pause or + not self.pyload.api.isTimeDownload() or + not self.pyload.threadManager.getActiveFiles()): return path = self.pyload.config.get("general", "download_folder") -- cgit v1.2.3 From 3b6e1b55a0ab527af98c901cf2d855359500b14e Mon Sep 17 00:00:00 2001 From: Walter Purcaro Date: Thu, 30 Jul 2015 14:52:01 +0200 Subject: [AntiStandby] Fix max_mtime method --- module/plugins/hooks/AntiStandby.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) (limited to 'module/plugins/hooks/AntiStandby.py') diff --git a/module/plugins/hooks/AntiStandby.py b/module/plugins/hooks/AntiStandby.py index 93eca09cc..ba462e002 100644 --- a/module/plugins/hooks/AntiStandby.py +++ b/module/plugins/hooks/AntiStandby.py @@ -13,6 +13,7 @@ except ImportError: pass from module.plugins.internal.Addon import Addon, Expose +from module.utils import fs_join class Kernel32(object): @@ -26,7 +27,7 @@ class Kernel32(object): class AntiStandby(Addon): __name__ = "AntiStandby" __type__ = "hook" - __version__ = "0.06" + __version__ = "0.07" __status__ = "testing" __config__ = [("activated", "bool", "Activated" , True ), @@ -149,7 +150,7 @@ class AntiStandby(Addon): @Expose def max_mtime(self, path): return max(0, 0, - *(os.path.getmtime(os.path.join(root, file)) + *(os.path.getmtime(fs_join(root, file)) for root, dirs, files in os.walk(path, topdown=False) for file in files)) -- cgit v1.2.3 From bd9c692ab57f906383f264850bce95ff310fd61f Mon Sep 17 00:00:00 2001 From: Walter Purcaro Date: Thu, 30 Jul 2015 16:00:19 +0200 Subject: [AntiStandby] Fix import typo --- module/plugins/hooks/AntiStandby.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'module/plugins/hooks/AntiStandby.py') diff --git a/module/plugins/hooks/AntiStandby.py b/module/plugins/hooks/AntiStandby.py index ba462e002..e669ae4f3 100644 --- a/module/plugins/hooks/AntiStandby.py +++ b/module/plugins/hooks/AntiStandby.py @@ -13,7 +13,7 @@ except ImportError: pass from module.plugins.internal.Addon import Addon, Expose -from module.utils import fs_join +from module.utils import save_join as fs_join class Kernel32(object): @@ -27,7 +27,7 @@ class Kernel32(object): class AntiStandby(Addon): __name__ = "AntiStandby" __type__ = "hook" - __version__ = "0.07" + __version__ = "0.08" __status__ = "testing" __config__ = [("activated", "bool", "Activated" , True ), -- cgit v1.2.3 From 1c6457a71d11759ea8c62feac94d47d3becfc062 Mon Sep 17 00:00:00 2001 From: Walter Purcaro Date: Sun, 2 Aug 2015 07:16:01 +0200 Subject: [AntiStandby] Improve linux_standby --- module/plugins/hooks/AntiStandby.py | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) (limited to 'module/plugins/hooks/AntiStandby.py') diff --git a/module/plugins/hooks/AntiStandby.py b/module/plugins/hooks/AntiStandby.py index e669ae4f3..4b35c787a 100644 --- a/module/plugins/hooks/AntiStandby.py +++ b/module/plugins/hooks/AntiStandby.py @@ -27,7 +27,7 @@ class Kernel32(object): class AntiStandby(Addon): __name__ = "AntiStandby" __type__ = "hook" - __version__ = "0.08" + __version__ = "0.09" __status__ = "testing" __config__ = [("activated", "bool", "Activated" , True ), @@ -130,13 +130,17 @@ class AntiStandby(Addon): elif not self.pid: self.pid = subprocess.Popen(["caffeine"]) + except Exception, e: + self.log_warning(_("Unable to change system power state"), e) + + try: if display: subprocess.call(["xset", "+dpms", "s", "default"]) else: subprocess.call(["xset", "-dpms", "s", "off"]) except Exception, e: - self.log_warning(_("Unable to change power state"), e) + self.log_warning(_("Unable to change display power state"), e) @Expose -- cgit v1.2.3 From 1fbaa17324b8f16dcaf0eb78f2a8aa017ec57881 Mon Sep 17 00:00:00 2001 From: Walter Purcaro Date: Sun, 2 Aug 2015 10:34:44 +0200 Subject: Fix https://github.com/pyload/pyload/issues/1651 --- module/plugins/hooks/AntiStandby.py | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) (limited to 'module/plugins/hooks/AntiStandby.py') diff --git a/module/plugins/hooks/AntiStandby.py b/module/plugins/hooks/AntiStandby.py index 4b35c787a..699ecbbde 100644 --- a/module/plugins/hooks/AntiStandby.py +++ b/module/plugins/hooks/AntiStandby.py @@ -13,7 +13,7 @@ except ImportError: pass from module.plugins.internal.Addon import Addon, Expose -from module.utils import save_join as fs_join +from module.utils import save_join as fs_encode, fs_join class Kernel32(object): @@ -27,7 +27,7 @@ class Kernel32(object): class AntiStandby(Addon): __name__ = "AntiStandby" __type__ = "hook" - __version__ = "0.09" + __version__ = "0.10" __status__ = "testing" __config__ = [("activated", "bool", "Activated" , True ), @@ -155,7 +155,7 @@ class AntiStandby(Addon): def max_mtime(self, path): return max(0, 0, *(os.path.getmtime(fs_join(root, file)) - for root, dirs, files in os.walk(path, topdown=False) + for root, dirs, files in os.walk(fs_encode(path), topdown=False) for file in files)) @@ -168,8 +168,8 @@ class AntiStandby(Addon): not self.pyload.threadManager.getActiveFiles()): return - path = self.pyload.config.get("general", "download_folder") - if (self.max_mtime(path) - self.mtime) < self.interval: + download_folder = self.pyload.config.get("general", "download_folder") + if (self.max_mtime(download_folder) - self.mtime) < self.interval: return self.touch(self.TMP_FILE) -- cgit v1.2.3 From 0fbe60c3c0941f3c68ab688b1a9610115ee66133 Mon Sep 17 00:00:00 2001 From: GammaC0de Date: Mon, 3 Aug 2015 01:15:27 +0300 Subject: fix import error --- module/plugins/hooks/AntiStandby.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'module/plugins/hooks/AntiStandby.py') diff --git a/module/plugins/hooks/AntiStandby.py b/module/plugins/hooks/AntiStandby.py index 699ecbbde..2d7a165a6 100644 --- a/module/plugins/hooks/AntiStandby.py +++ b/module/plugins/hooks/AntiStandby.py @@ -13,7 +13,7 @@ except ImportError: pass from module.plugins.internal.Addon import Addon, Expose -from module.utils import save_join as fs_encode, fs_join +from module.utils import save_join as fs_join, fs_encode class Kernel32(object): @@ -27,7 +27,7 @@ class Kernel32(object): class AntiStandby(Addon): __name__ = "AntiStandby" __type__ = "hook" - __version__ = "0.10" + __version__ = "0.11" __status__ = "testing" __config__ = [("activated", "bool", "Activated" , True ), -- cgit v1.2.3 From 2cf928db10224b5327f918dceaa13273753620ac Mon Sep 17 00:00:00 2001 From: Walter Purcaro Date: Tue, 4 Aug 2015 18:06:42 +0200 Subject: Some fixes --- module/plugins/hooks/AntiStandby.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'module/plugins/hooks/AntiStandby.py') diff --git a/module/plugins/hooks/AntiStandby.py b/module/plugins/hooks/AntiStandby.py index 2d7a165a6..48b86fa55 100644 --- a/module/plugins/hooks/AntiStandby.py +++ b/module/plugins/hooks/AntiStandby.py @@ -13,7 +13,7 @@ except ImportError: pass from module.plugins.internal.Addon import Addon, Expose -from module.utils import save_join as fs_join, fs_encode +from module.utils import fs_encode, save_join as fs_join class Kernel32(object): -- cgit v1.2.3