Advertisement
rezasurmar

Untitled

Oct 11th, 2019
133
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.91 KB | None | 0 0
  1. #!/usr/bin/env python
  2. from datetime import datetime
  3. import os
  4. import pwd
  5. import subprocess
  6. import time
  7.  
  8. LOG_FILE = os.path.expanduser('~/hours_log.csv')
  9.  
  10.  
  11. cmd = subprocess.Popen(["dbus-monitor \"type='signal',interface="
  12.                         "'org.freedesktop.ScreenSaver'\""], shell=True,
  13.                        stdout=subprocess.PIPE)
  14.  
  15. running = 0
  16. while 1:
  17.     time.sleep(0.1)
  18.     if running:
  19.         output = cmd.stdout.readline()
  20.         status = 'unlocked' if 'true' in output else 'locked'
  21.         new_line = "{time} {user} {status} the screen\n".format(
  22.             time=datetime.now().ctime(),
  23.             user=pwd.getpwuid(os.getuid())[0],
  24.             status=status
  25.         )
  26.         with open(LOG_FILE, 'a') as f:
  27.             f.write(new_line)
  28.  
  29.         running = 0
  30.     line = cmd.stdout.readline()
  31.     if "ActiveChange" in line and 'org.freedesktop.ScreenSaver' in line:
  32.         running = 1
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement