Example #1
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)
Example #2
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)
Example #3
0
    def setUp(self):
        """ Set up data used in tests """

        self.config = mock.Mock()

        def get(*args, **kwargs):
            if args[0] == 'app_path':
                return 'tests/latte/'
            elif args[0] == 'sleep_time':
                return 5
            elif args[0] == 'stats_path':
                return 'stats/'
            elif args[0] == 'ignore_keywords':
                return ['ignore', 'keyw']

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

        engine = create_engine('sqlite:///:memory:')
        self.session = sessionmaker(bind=engine)
        Base.metadata.create_all(engine)

        self.timetracker = TimeTracker(self.config, session=self.session())
Example #4
0
    def setUp(self):
        """ Set up data used in tests """

        self.config = mock.Mock()

        def get(*args, **kwargs):
            if args[0] == 'app_path':
                return 'tests/latte/'
            elif args[0] == 'sleep_time':
                return 5
            elif args[0] == 'stats_path':
                return 'stats/'
            elif args[0] == 'ignore_keywords':
                return ['ignore', 'keyw']
        self.config.get = get
        self.config.getint = get

        engine = create_engine('sqlite:///:memory:')
        self.session = sessionmaker(bind=engine)
        Base.metadata.create_all(engine)

        self.timetracker = TimeTracker(self.config, session=self.session())
Example #5
0
class testTimeTracker(unittest.TestCase):
    """ A test class for the TimeTracker class """

    sleepTime = 0
    timetracker = None

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

        self.config = mock.Mock()

        def get(*args, **kwargs):
            if args[0] == 'app_path':
                return 'tests/latte/'
            elif args[0] == 'sleep_time':
                return 5
            elif args[0] == 'stats_path':
                return 'stats/'
            elif args[0] == 'ignore_keywords':
                return ['ignore', 'keyw']
        self.config.get = get
        self.config.getint = get

        engine = create_engine('sqlite:///:memory:')
        self.session = sessionmaker(bind=engine)
        Base.metadata.create_all(engine)

        self.timetracker = TimeTracker(self.config, session=self.session())

    def tearDown(self):
        self.session().rollback()

    def testGettingEmptyLog(self):
        """ Tests if getWindowTime with empty log returns None """

        self.assertEqual(self.timetracker.get_window_time(u'Bogus'), None)

    def testAddTimeToNonExistingWindows(self):
        """ Test adding time to non existing window titles """

        window = u'Non existing window 1'
        window_class = u'New class'
        window_instance = u'New instance'
        self.timetracker.log(window, window_class, window_instance)
        self.assertEqual(self.timetracker.get_window_time(window), self.config.get('sleep_time'))

    def testAddTimeToExistingWindows(self):
        window = u'Testing Window 1'
        window_class = u'Window class 1'
        window_instance = u'Instance 1'
        self.timetracker.log(window, window_class, window_instance)
        self.timetracker.log(window, window_class, window_instance)

        self.assertEqual(self.timetracker.get_window_time(window), self.config.get('sleep_time') * 2)

    def testGetWindowStats(self):
        window = u'Some window'
        window_class = u'Some class'
        window_instance = u'Some instance'
        self.timetracker.log(window, window_class, window_instance)

        data = self.timetracker.get_window_stats(window)
        self.assertIs(type(self.timetracker.get_window_stats(window)), Log)

    def testContainsIgnoredKeywords(self):
        window = u'Some string with ignored keywords'
        self.assertTrue(self.timetracker.contains_ignored_keywords(window))
        window2 = u'Doesn\'t contain bad words'
        self.assertFalse(self.timetracker.contains_ignored_keywords(window2))

    def testAddLogWithIgnoredKeywords(self):
        window = u'Some string with ignored keywords'
        window_class = u'Window class'
        window_instance = u'Window instance'
        self.assertFalse(self.timetracker.log(window, window_class, window_instance))
Example #6
0
class testTimeTracker(unittest.TestCase):
    """ A test class for the TimeTracker class """

    sleepTime = 0
    timetracker = None

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

        self.config = mock.Mock()

        def get(*args, **kwargs):
            if args[0] == 'app_path':
                return 'tests/latte/'
            elif args[0] == 'sleep_time':
                return 5
            elif args[0] == 'stats_path':
                return 'stats/'
            elif args[0] == 'ignore_keywords':
                return ['ignore', 'keyw']

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

        engine = create_engine('sqlite:///:memory:')
        self.session = sessionmaker(bind=engine)
        Base.metadata.create_all(engine)

        self.timetracker = TimeTracker(self.config, session=self.session())

    def tearDown(self):
        self.session().rollback()

    def testGettingEmptyLog(self):
        """ Tests if getWindowTime with empty log returns None """

        self.assertEqual(self.timetracker.get_window_time('Bogus'), None)

    def testAddTimeToNonExistingWindows(self):
        """ Test adding time to non existing window titles """

        window = 'Non existing window 1'
        window_class = 'New class'
        window_instance = 'New instance'
        self.timetracker.log(window, window_class, window_instance)
        self.assertEqual(self.timetracker.get_window_time(window),
                         self.config.get('sleep_time'))

    def testAddTimeToExistingWindows(self):
        window = 'Testing Window 1'
        window_class = 'Window class 1'
        window_instance = 'Instance 1'
        self.timetracker.log(window, window_class, window_instance)
        self.timetracker.log(window, window_class, window_instance)

        self.assertEqual(self.timetracker.get_window_time(window),
                         self.config.get('sleep_time') * 2)

    def testGetWindowStats(self):
        window = 'Some window'
        window_class = 'Some class'
        window_instance = 'Some instance'
        self.timetracker.log(window, window_class, window_instance)

        data = self.timetracker.get_window_stats(window)
        self.assertIs(type(self.timetracker.get_window_stats(window)), Log)

    def testContainsIgnoredKeywords(self):
        window = 'Some string with ignored keywords'
        self.assertTrue(self.timetracker.contains_ignored_keywords(window))
        window2 = 'Doesn\'t contain bad words'
        self.assertFalse(self.timetracker.contains_ignored_keywords(window2))

    def testAddLogWithIgnoredKeywords(self):
        window = 'Some string with ignored keywords'
        window_class = 'Window class'
        window_instance = 'Window instance'
        self.assertFalse(
            self.timetracker.log(window, window_class, window_instance))