citadel

My dotfiles, scripts and nix configs
git clone git://jb55.com/citadel
Log | Files | Refs | README | LICENSE

test-signal (1221B)


      1 #!/usr/bin/env python
      2 
      3 import dbus
      4 import datetime
      5 import gobject
      6 import os
      7 from dbus.mainloop.glib import DBusGMainLoop
      8 
      9 def start_work():
     10   print("starting work notifier")
     11   os.system("systemctl stop  --user home-email-notifier")
     12   os.system("systemctl start --user work-email-notifier")
     13 
     14 def start_home():
     15   print("starting home notifier")
     16   os.system("systemctl stop  --user work-email-notifier")
     17   os.system("systemctl start --user home-email-notifier")
     18 
     19 def check():
     20   now = datetime.datetime.now()
     21   if now.isoweekday() > 5:
     22     start_home()
     23   else:
     24     if now.hour > 17 or now.hour < 9:
     25       start_home()
     26     else:
     27       start_work()
     28 
     29 def handle_sleep_callback(sleeping):
     30   if not sleeping:
     31     # awoke from sleep
     32     check()
     33 
     34 DBusGMainLoop(set_as_default=True) # integrate into main loob
     35 bus = dbus.SystemBus()             # connect to dbus system wide
     36 bus.add_signal_receiver(           # defince the signal to listen to
     37     handle_sleep_callback,            # name of callback function
     38     'PrepareForSleep',                 # signal name
     39     'org.freedesktop.login1.Manager',   # interface
     40     'org.freedesktop.login1'            # bus name
     41 )
     42 
     43 loop = gobject.MainLoop()          # define mainloop
     44 loop.run()