Exemple #1
0
    def setUp(self):
        """ Set up data used in tests """

        self.config = mock.Mock()

        def get(*args, **kwargs):
            if args[0] == 'user_inactive_threshold':
                return 10

        self.config.get = get
        self.config.getint = get

        self.time_tracker = mock.Mock()

        self.user_activity_tracker = UserActivityTracker(
            config=self.config, time_tracker=self.time_tracker)
Exemple #2
0
class testUserActivityTracker(unittest.TestCase):
    """ A test class for the UserActivityTracker class """

    user_activity_tracker = None

    def setUp(self):
        """ Set up data used in tests """

        self.config = mock.Mock()

        def get(*args, **kwargs):
            if args[0] == 'user_inactive_threshold':
                return 10

        self.config.get = get
        self.config.getint = get

        self.time_tracker = mock.Mock()

        self.user_activity_tracker = UserActivityTracker(
            config=self.config, time_tracker=self.time_tracker)

    def tearDown(self):
        self.user_activity_tracker = None

    def testUserIsActiveIfIsInactiveForLessThanInactivityThreshold(self):
        """ Tests if user is not inactive if inactivity duration is less than the threshold """
        self.user_activity_tracker.get_inactivity_time = lambda: 5
        self.assertFalse(self.user_activity_tracker.is_user_inactive())

    def testInactivityDurationIsSubtractedFromCurrentLog(self):
        """ Tests that the inactivity duration is subtracted from the log duration """
        inactivity_time = 15
        self.user_activity_tracker.get_inactivity_time = lambda: inactivity_time
        self.user_activity_tracker.is_user_inactive()
        self.time_tracker.reduce_time.assert_called_with(inactivity_time)

    def testUserIsInActiveIfIsInactiveForMoreThanInactivityThreshold(self):
        """ Tests if user is inactive if inactivity duration is more than the threshold """
        self.user_activity_tracker.get_inactivity_time = lambda: 15
        self.assertTrue(self.user_activity_tracker.is_user_inactive())
Exemple #3
0
    def __init__(self, silent=False):
        self.silent = silent
        self.config = Config()

        engine = create_engine(self.config.get('stats_db'))
        self.session = sessionmaker(bind=engine)
        Base.metadata.create_all(engine)

        self.screen = Screen()

        self.time_tracker = TimeTracker(config=self.config, session=self.session())
        self.activity_tracker = UserActivityTracker(time_tracker=self.time_tracker, config=self.config, screen=self.screen)
class testUserActivityTracker(unittest.TestCase):
    """ A test class for the UserActivityTracker class """

    user_activity_tracker = None

    def setUp(self):
        """ Set up data used in tests """

        self.config = mock.Mock()

        def get(*args, **kwargs):
            if args[0] == "user_inactive_threshold":
                return 10

        self.config.get = get
        self.config.getint = get

        self.time_tracker = mock.Mock()

        self.user_activity_tracker = UserActivityTracker(config=self.config, time_tracker=self.time_tracker)

    def tearDown(self):
        self.user_activity_tracker = None

    def testUserIsActiveIfIsInactiveForLessThanInactivityThreshold(self):
        """ Tests if user is not inactive if inactivity duration is less than the threshold """
        self.user_activity_tracker.get_inactivity_time = lambda: 5
        self.assertFalse(self.user_activity_tracker.is_user_inactive())

    def testInactivityDurationIsSubtractedFromCurrentLog(self):
        """ Tests that the inactivity duration is subtracted from the log duration """
        inactivity_time = 15
        self.user_activity_tracker.get_inactivity_time = lambda: inactivity_time
        self.user_activity_tracker.is_user_inactive()
        self.time_tracker.reduce_time.assert_called_with(inactivity_time)

    def testUserIsInActiveIfIsInactiveForMoreThanInactivityThreshold(self):
        """ Tests if user is inactive if inactivity duration is more than the threshold """
        self.user_activity_tracker.get_inactivity_time = lambda: 15
        self.assertTrue(self.user_activity_tracker.is_user_inactive())
Exemple #5
0
class Latte(object):
    """ Main application class. """

    def __init__(self, silent=False):
        self.silent = silent
        self.config = Config()

        engine = create_engine(self.config.get('stats_db'))
        self.session = sessionmaker(bind=engine)
        Base.metadata.create_all(engine)

        self.screen = Screen()

        self.time_tracker = TimeTracker(config=self.config, session=self.session())
        self.activity_tracker = UserActivityTracker(time_tracker=self.time_tracker, config=self.config, screen=self.screen)

    def run(self):
        if not self.screen.has_required_dependencies():
            self.output("Required dependencies were not found. Please make sure `xprop` is installed and is in your PATH. Exiting...")
            return
        if not self.screen.has_optional_dependencies():
            self.output("Optional dependencies were not found. Please make sure `libX11.so` and `libXss.so` are installed. Inactivity tracking will not work.")

        try:
            self.run_tracker()
        except KeyboardInterrupt:
            self.output('Exiting...')

    def run_tracker(self):
        while True:
            if self.activity_tracker.is_user_inactive():
                self.output("User inactive")
            else:
                self.log_user_activity()
            time.sleep(self.config.get('sleep_time'))

    def log_user_activity(self):
        [title, window_class, window_instance] = self.screen.get_active_window_data()
        self.time_tracker.log(title, window_class, window_instance)
        stats = self.time_tracker.current_log
        if stats:
            self.output("[%s] %s, %s" % (window_class, title, stats.duration))
        elif not stats:
            self.output("IGNORED")

    def get_session(self):
        return self.session()

    def output(self, text):
        if not self.silent:
            print(text)
    def setUp(self):
        """ Set up data used in tests """

        self.config = mock.Mock()

        def get(*args, **kwargs):
            if args[0] == "user_inactive_threshold":
                return 10

        self.config.get = get
        self.config.getint = get

        self.time_tracker = mock.Mock()

        self.user_activity_tracker = UserActivityTracker(config=self.config, time_tracker=self.time_tracker)
    def setUp(self):
        """ Set up data used in tests """

        self.config = mock.Mock()
        def get(*args, **kwargs):
            if args[0] == 'user_inactive_threshold':
                return 10
        self.config.get = get
        self.config.getint = get

        self.time_tracker = mock.Mock()
        self.screen = mock.Mock()
        self.screen.is_available.return_value = True

        self.user_activity_tracker = UserActivityTracker(config=self.config, time_tracker=self.time_tracker, screen=self.screen)