Mon, 29 Jan 2018 14:02:55 +0000
Make dreamtime module not throw up on non-macOS systems
borgend/dreamtime.py | file | annotate | diff | comparison | revisions |
--- a/borgend/dreamtime.py Mon Jan 29 10:46:01 2018 +0000 +++ b/borgend/dreamtime.py Mon Jan 29 14:02:55 2018 +0000 @@ -4,8 +4,6 @@ # This file implements system wake/sleep detection for scheduling adjustments. # -import Foundation -import AppKit import platform import time import threading @@ -125,73 +123,77 @@ def _now(): return dreamtime() -# -# Wake up / sleep handling -# +if platform.system()=='Darwin': + + import Foundation + import AppKit -class SleepHandler(Foundation.NSObject): - """ Handle wake/sleep notifications """ + # + # Wake up / sleep handling + # + + class SleepHandler(Foundation.NSObject): + """ Handle wake/sleep notifications """ - def init(self): - self.__sleeptime=None - self.__slept=0 - self.__epoch=time.monotonic() - self.__lock=threading.Lock() - self.__callbacks=weakref.WeakKeyDictionary() - - return self + def init(self): + self.__sleeptime=None + self.__slept=0 + self.__epoch=time.monotonic() + self.__lock=threading.Lock() + self.__callbacks=weakref.WeakKeyDictionary() - def handleSleepNotification_(self, aNotification): - logger.info("System going to sleep") - now=time.monotonic() - with self.__lock: - self.__sleeptime=now + return self - def handleWakeNotification_(self, aNotification): - logger.info("System waking up from sleep") - try: + def handleSleepNotification_(self, aNotification): + logger.info("System going to sleep") now=time.monotonic() with self.__lock: - if self.__sleeptime: - slept=max(0, now-self.__sleeptime) - logger.info("Slept %f seconds" % slept) - self.__slept=self.__slept+slept - self.__sleeptime=None - callbacks=self.__callbacks.copy() - except: - logger.exception("Bug in wakeup handler") + self.__sleeptime=now - for callback in callbacks.values(): + def handleWakeNotification_(self, aNotification): + logger.info("System waking up from sleep") try: - callback() - except Exception: - logger.exception("Error in wake notification callback") + now=time.monotonic() + with self.__lock: + if self.__sleeptime: + slept=max(0, now-self.__sleeptime) + logger.info("Slept %f seconds" % slept) + self.__slept=self.__slept+slept + self.__sleeptime=None + callbacks=self.__callbacks.copy() + except: + logger.exception("Bug in wakeup handler") + + for callback in callbacks.values(): + try: + callback() + except Exception: + logger.exception("Error in wake notification callback") - # Return difference to time.monotonic() - def diff(self): - with self.__lock: - diff=self.__epoch+self.__slept - return diff + # Return difference to time.monotonic() + def diff(self): + with self.__lock: + diff=self.__epoch+self.__slept + return diff - # Weirdo (Py)ObjC naming to stop it form choking up - def addForObj_aCallback_(self, obj, callback): - with self.__lock: - self.__callbacks[obj]=callback + # Weirdo (Py)ObjC naming to stop it form choking up + def addForObj_aCallback_(self, obj, callback): + with self.__lock: + self.__callbacks[obj]=callback -# obj is to use a a key in a weak key dictionary -def add_callback(obj, callback): - global _dreamtime_monitor + # obj is to use a a key in a weak key dictionary + def add_callback(obj, callback): + global _dreamtime_monitor - monitor=_dreamtime_monitor - if not monitor: - raise Exception("Dreamtime monitor not started") - else: - monitor.addForObj_aCallback_(obj, callback) + monitor=_dreamtime_monitor + if not monitor: + raise Exception("Dreamtime monitor not started") + else: + monitor.addForObj_aCallback_(obj, callback) -def start_monitoring(): - global _dreamtime_monitor + def start_monitoring(): + global _dreamtime_monitor - if platform.system()=='Darwin': logger.debug("Starting to monitor system sleep") workspace = AppKit.NSWorkspace.sharedWorkspace() notification_center = workspace.notificationCenter() @@ -208,7 +210,13 @@ "handleWakeNotification:", AppKit.NSWorkspaceDidWakeNotification, None) - else: + +else: # Not on macOS + + def add_callback(obj, callback): + pass + + def start_monitoring(): logger.warning(("No system sleep monitor implemented for '%s'" % platform.system()))