From 18a987b2907ac24e6a34b28f88d963b568fe4a93 Mon Sep 17 00:00:00 2001 From: Stephen Boddy Date: Thu, 26 Feb 2015 00:01:45 +0100 Subject: [PATCH] Merge Activity Watcher plugin improvements from Joseph Crosland (with additional fixup) --- terminatorlib/config.py | 28 +++++++++++++++----- terminatorlib/plugins/activitywatch.py | 36 ++++++++++++++++---------- 2 files changed, 43 insertions(+), 21 deletions(-) diff --git a/terminatorlib/config.py b/terminatorlib/config.py index 801dc9c0..302cb0c2 100755 --- a/terminatorlib/config.py +++ b/terminatorlib/config.py @@ -44,6 +44,16 @@ Classes relating to configuration {'foo': 'bar'} >>> config.plugin_get('testplugin', 'foo') 'bar' +>>> config.plugin_get('testplugin', 'foo', 'new') +'bar' +>>> config.plugin_get('testplugin', 'algo') +Traceback (most recent call last): +... +KeyError: 'ConfigBase::get_item: unknown key algo' +>>> config.plugin_get('testplugin', 'algo', 1) +1 +>>> config.plugin_get('anothertestplugin', 'algo', 500) +500 >>> config.get_profile() 'default' >>> config.set_profile('my_first_new_testing_profile') @@ -263,9 +273,9 @@ class Config(object): self.set_profile(profile) self.inhibited = False - def __getitem__(self, key): + def __getitem__(self, key, default=None): """Look up a configuration item""" - return(self.base.get_item(key, self.profile)) + return(self.base.get_item(key, self.profile, default=default)) def __setitem__(self, key, value): """Set a particular configuration item""" @@ -400,9 +410,11 @@ class Config(object): """Get the command line options""" return(self.base.command_line_options) - def plugin_get(self, pluginname, key): - """Get a plugin config value""" - return(self.base.get_item(key, plugin=pluginname)) + def plugin_get(self, pluginname, key, default=None): + """Get a plugin config value, if doesn't exist + return default if specified + """ + return(self.base.get_item(key, plugin=pluginname, default=default)) def plugin_set(self, pluginname, key, value): """Set a plugin config value""" @@ -663,7 +675,7 @@ class ConfigBase(Borg): except Exception, ex: err('ConfigBase::save: Unable to save config: %s' % ex) - def get_item(self, key, profile='default', plugin=None): + def get_item(self, key, profile='default', plugin=None, default=None): """Look up a configuration item""" if not self.profiles.has_key(profile): # Hitting this generally implies a bug @@ -679,10 +691,12 @@ class ConfigBase(Borg): return(self.profiles[profile][key]) elif key == 'keybindings': return(self.keybindings) - elif plugin is not None and self.plugins[plugin].has_key(key): + elif plugin and plugin in self.plugins and key in self.plugins[plugin]: dbg('ConfigBase::get_item: %s found in plugin %s: %s' % ( key, plugin, self.plugins[plugin][key])) return(self.plugins[plugin][key]) + elif default: + return default else: raise KeyError('ConfigBase::get_item: unknown key %s' % key) diff --git a/terminatorlib/plugins/activitywatch.py b/terminatorlib/plugins/activitywatch.py index 88a162e2..a1441255 100755 --- a/terminatorlib/plugins/activitywatch.py +++ b/terminatorlib/plugins/activitywatch.py @@ -7,6 +7,7 @@ import time import gtk import gobject +from terminatorlib.config import Config import terminatorlib.plugin as plugin from terminatorlib.translation import _ from terminatorlib.util import err, dbg @@ -21,6 +22,12 @@ try: except ImportError: err(_('ActivityWatch plugin unavailable: please install python-notify')) +config = Config() +inactive_period = float(config.plugin_get('ActivityWatch', 'inactive_period', + 10.0)) +watch_interval = int(config.plugin_get('ActivityWatch', 'watch_interval', + 5000)) + class ActivityWatch(plugin.MenuItem): """Add custom commands to the terminal menu""" capabilities = ['terminal_menu'] @@ -40,14 +47,15 @@ class ActivityWatch(plugin.MenuItem): pynotify.init(APP_NAME.capitalize()) def callback(self, menuitems, menu, terminal): - """Add our menu items to the menu""" - if not self.watches.has_key(terminal): - item = gtk.MenuItem(_('Watch for activity')) - item.connect("activate", self.watch, terminal) - else: - item = gtk.MenuItem(_('Stop watching for activity')) + """Add our menu item to the menu""" + item = gtk.CheckMenuItem(_('Watch for activity')) + item.set_active(self.watches.has_key(terminal)) + if item.get_active(): item.connect("activate", self.unwatch, terminal) + else: + item.connect("activate", self.watch, terminal) menuitems.append(item) + dbg('Menu item appended') def watch(self, _widget, terminal): """Watch a terminal""" @@ -105,13 +113,13 @@ class InactivityWatch(plugin.MenuItem): pynotify.init(APP_NAME.capitalize()) def callback(self, menuitems, menu, terminal): - """Add our menu items to the menu""" - if not self.watches.has_key(terminal): - item = gtk.MenuItem(_("Watch for silence")) - item.connect("activate", self.watch, terminal) - else: - item = gtk.MenuItem(_("Stop watching for silence")) + """Add our menu item to the menu""" + item = gtk.CheckMenuItem(_("Watch for silence")) + item.set_active(self.watches.has_key(terminal)) + if item.get_active(): item.connect("activate", self.unwatch, terminal) + else: + item.connect("activate", self.watch, terminal) menuitems.append(item) dbg('Menu items appended') @@ -120,7 +128,7 @@ class InactivityWatch(plugin.MenuItem): vte = terminal.get_vte() self.watches[terminal] = vte.connect('contents-changed', self.reset_timer, terminal) - timeout_id = gobject.timeout_add(5000, self.check_times, terminal) + timeout_id = gobject.timeout_add(watch_interval, self.check_times, terminal) self.timers[terminal] = timeout_id dbg('timer %s added for %s' %(timeout_id, terminal)) @@ -146,7 +154,7 @@ class InactivityWatch(plugin.MenuItem): return True dbg('seconds since last activity: %f (%s)' % (time_now - self.last_activities[terminal], terminal)) - if time_now - self.last_activities[terminal] >= 10.0: + if time_now - self.last_activities[terminal] >= inactive_period: del(self.last_activities[terminal]) note = pynotify.Notification('Terminator', 'Silence in: %s' % terminal.get_window_title(), 'terminator')