~ubuntu-branches/ubuntu/trusty/plainbox-provider-checkbox/trusty

« back to all changes in this revision

Viewing changes to bin/lock_screen_watcher

  • Committer: Package Import Robot
  • Author(s): Zygmunt Krynicki
  • Date: 2014-04-07 19:00:31 UTC
  • mfrom: (3.1.1 sid)
  • Revision ID: package-import@ubuntu.com-20140407190031-rf836grml6oilfyt
Tags: 0.4-1
* New upstream release. List of bugfixes:
  https://launchpad.net/plainbox-provider-checkbox/14.04/0.4
* debian/watch: look for new releases on launchpad
* debian/rules: stop using pybuild and use manage.py
  {i18n,build,install,validate} instead. This also drops dependency on
  python3-distutils-extra and replaces that with intltool
* debian/control: drop X-Python3-Version
* debian/control: make plainbox-provider-checkbox depend on python and
  python2.7 (for some scripts) rather than suggesting them.
* debian/upstream/signing-key.asc: Use armoured gpg keys to avoid having to
  keep binary files in Debian packaging. Also, replace that with my key
  since I made the 0.3 release upstream.
* debian/source/lintian-overrides: add an override for warning about no
  source for flash movie with reference to a bug report that discusses that
  issue.
* debian/source/include-binaries: drop (no longer needed)
* debian/patches: drop (no longer needed)
* debian/plainbox-provider-checkbox.lintian-overrides: drop (no longer
  needed)
* Stop being a python3 module, move to from DPMT to PAPT

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#!/usr/bin/env python3
 
2
import argparse
 
3
import sys
 
4
import subprocess
 
5
 
 
6
from gi.repository import GObject
 
7
 
 
8
from checkbox_support.dbus import connect_to_system_bus
 
9
 
 
10
import threading
 
11
import time
 
12
 
 
13
GObject.threads_init()
 
14
 
 
15
class SceenSaverStatusHelper(threading.Thread):
 
16
 
 
17
    def __init__(self, loop):
 
18
        super(SceenSaverStatusHelper, self).__init__()
 
19
        self._loop = loop
 
20
        self.quit = False
 
21
 
 
22
    def query(self):
 
23
        p = subprocess.Popen(["gnome-screensaver-command", "-q"], stdout=subprocess.PIPE)
 
24
        stdout, stderr = p.communicate()
 
25
        # parse the stdout string from the command "gnome-screensaver-command -q"
 
26
        # the result should be "active" or "inactive"
 
27
        if "active" == stdout.decode().split(" ")[-1][0:-1] :
 
28
            print("the screensaver is active")
 
29
            self._loop.quit()
 
30
 
 
31
    def run(self):
 
32
        while not self.quit:
 
33
            GObject.idle_add(self.query)
 
34
            time.sleep(1)
 
35
 
 
36
 
 
37
class HotkeyFunctionListener:
 
38
 
 
39
    def __init__(self, system_bus, loop):
 
40
        self._bus = system_bus
 
41
        self._loop = loop
 
42
        # Assume the test passes, this is changed when timeout expires
 
43
        self._error = False
 
44
 
 
45
    def _on_timeout_expired(self):
 
46
        """
 
47
        Internal function called when the timer expires.
 
48
 
 
49
        Basically it's just here to tell the user the test failed or that the
 
50
        user was unable to pressed the hot key during the allowed time.
 
51
        """
 
52
        print("You have failed to perform the required manipulation in time")
 
53
        # Fail the test when the timeout was reached
 
54
        self._error = True
 
55
        # Stop the loop now
 
56
        self._loop.quit()
 
57
 
 
58
    def check(self, timeout):
 
59
        """
 
60
        Run the configured test and return the result
 
61
 
 
62
        The result is False if the test has failed.  The timeout, when
 
63
        non-zero, will make the test fail after the specified seconds have
 
64
        elapsed without conclusive result.
 
65
        """
 
66
        # Setup a timeout if requested
 
67
        if timeout > 0:
 
68
            GObject.timeout_add_seconds(timeout, self._on_timeout_expired)
 
69
 
 
70
        # helper to listen the functionality is triggered or not
 
71
        query_thread = SceenSaverStatusHelper(self._loop)
 
72
        query_thread.start()
 
73
 
 
74
        self._loop.run()
 
75
        query_thread.quit = True
 
76
        # Return the outcome of the test
 
77
        return self._error        
 
78
 
 
79
def main():
 
80
    description = "Wait for the specified hotkey to be pressed."
 
81
    parser = argparse.ArgumentParser(description=description)
 
82
    parser.add_argument('--timeout', type=int, default=30)
 
83
 
 
84
    args = parser.parse_args()
 
85
 
 
86
    # Connect to the system bus, we also get the event
 
87
    # loop as we need it to start listening for signals.
 
88
    system_bus, loop = connect_to_system_bus()
 
89
 
 
90
    listener = HotkeyFunctionListener(system_bus, loop)
 
91
 
 
92
    # Run the actual listener and wait till it either times out or discovers
 
93
    # the specific hot key pressed.
 
94
    try:
 
95
        return listener.check(args.timeout)
 
96
    except KeyboardInterrupt:
 
97
        return 1
 
98
 
 
99
if __name__ == "__main__":
 
100
    sys.exit(main())
 
101