From 0511526d97cb6a74282c5f4dd334be1f5f355ea0 Mon Sep 17 00:00:00 2001 From: Dylan Baker Date: Wed, 25 Jan 2017 15:52:58 -0800 Subject: Implement a hook that runs periodically This creates a deferred that runs on a user configurable timer and is provided the ui. This deferred will re-fire after the number of seconds specified by the timer are eclipsed. By default this is 300 seconds. Fixes #522 --- NEWS | 1 + alot/defaults/alot.rc.spec | 3 +++ alot/ui.py | 11 ++++++++++- docs/source/configuration/alotrc_table | 10 ++++++++++ docs/source/configuration/hooks.rst | 7 +++++++ 5 files changed, 31 insertions(+), 1 deletion(-) diff --git a/NEWS b/NEWS index b5774584..ae67e361 100644 --- a/NEWS +++ b/NEWS @@ -5,6 +5,7 @@ next release: * enable variable interpolation in config file * Add encryption to CC addresses * Add bufferlist, tablist and pyshell subcommands to the command line interface +* Add hook that runs periodically. 0.4: * signal: refresh current buffer on SIGUSR1 diff --git a/alot/defaults/alot.rc.spec b/alot/defaults/alot.rc.spec index d2ee450f..cd72b39c 100644 --- a/alot/defaults/alot.rc.spec +++ b/alot/defaults/alot.rc.spec @@ -248,6 +248,9 @@ reply_account_header_priority = force_list(default=list(From,To,Cc,Envelope-To,X # history file might get *very* long. history_size = integer(default=50) +# The number of seconds to wait between calls to the loop_hook +periodic_hook_frequency = integer(default=300) + # Key bindings [bindings] __many__ = string(default=None) diff --git a/alot/ui.py b/alot/ui.py index 0208288c..ba708804 100644 --- a/alot/ui.py +++ b/alot/ui.py @@ -7,7 +7,7 @@ import logging import os import signal -from twisted.internet import reactor, defer +from twisted.internet import reactor, defer, task import urwid from .settings import settings @@ -103,6 +103,15 @@ class UI(object): unhandled_input=self._unhandled_input, input_filter=self._input_filter) + # Create a defered that calls the loop_hook + loop_hook = settings.get_hook('loop_hook') + if loop_hook: + loop = task.LoopingCall(loop_hook, ui=self) + loop_defered = loop.start(settings.get('periodic_hook_frequency')) + loop_defered.addErrback( + lambda e: logging.error('error in loop hook %s', + e.getErrorMessage())) + # set up colours colourmode = int(settings.get('colourmode')) logging.info('setup gui in %d colours', colourmode) diff --git a/docs/source/configuration/alotrc_table b/docs/source/configuration/alotrc_table index 9e27b206..6929a665 100644 --- a/docs/source/configuration/alotrc_table +++ b/docs/source/configuration/alotrc_table @@ -385,6 +385,16 @@ :default: 2 +.. _periodic-hook-frequency: + +.. describe:: periodic_hook_frequency + + The number of seconds to wait between calls to the loop_hook + + :type: integer + :default: 300 + + .. _prefer-plaintext: .. describe:: prefer_plaintext diff --git a/docs/source/configuration/hooks.rst b/docs/source/configuration/hooks.rst index 6e7162e8..352701c3 100644 --- a/docs/source/configuration/hooks.rst +++ b/docs/source/configuration/hooks.rst @@ -205,3 +205,10 @@ Apart from command pre- and posthooks, the following hooks will be interpreted: :type suffix: str :returns: tuple of `prefix` and `suffix` :rtype: (str, str) + +.. py:function:: loop_hook(ui=None) + + Run on a period controlled by :ref:`_periodic_hook_frequency ` + + :param ui: the main user interface + :type ui: :class:`alot.ui.UI` -- cgit v1.2.3