From 6ae05c082828c7c819b9413ff80ee8e106272d13 Mon Sep 17 00:00:00 2001 From: Ultrabug Date: Fri, 22 Feb 2013 12:25:55 +0100 Subject: add an integrated cache on user-class execution results --- README.md | 5 +++-- py3status/py3status.py | 34 +++++++++++++++++++++++----------- 2 files changed, 26 insertions(+), 13 deletions(-) diff --git a/README.md b/README.md index a52337e..4efd538 100644 --- a/README.md +++ b/README.md @@ -39,9 +39,10 @@ Usually you have your own i3status configuration, just point to it: You can see the help of py3status by issuing `py3status -h`: -c I3STATUS_CONF path to i3status config file - -n INTERVAL polling interval in seconds (default 1 sec) - -i INCLUDE_PATH user-based class include directory -d disable integrated transformations + -i INCLUDE_PATH user-based class include directory + -n INTERVAL polling interval in seconds (default 1 sec) + -t CACHE_TIMEOUT injection cache timeout in seconds (default 60 sec) ## Update Just pull on your local clone to get the latest py3status: diff --git a/py3status/py3status.py b/py3status/py3status.py index 9ec9361..d89a7d5 100755 --- a/py3status/py3status.py +++ b/py3status/py3status.py @@ -134,12 +134,21 @@ def inject(j): for my_class in USER_CLASSES.keys(): for my_method in USER_CLASSES[my_class]: try: - meth = getattr(my_class, my_method) - index, result = meth(j) - assert isinstance(result, dict), "user method didn't return a dict" - assert result.has_key('full_text'), "missing 'full_text' key" - assert result.has_key('name'), "missing 'name' key" - j.insert(index, result) + # handle a cache on user class methods results + try: + index, result = USER_CACHE[my_method] + if time() > result['cached_until']: + raise KeyError, 'cache timeout' + except KeyError: + meth = getattr(my_class, my_method) + index, result = meth(j) + result['cached_until'] = time() + CACHE_TIMEOUT + assert isinstance(result, dict), "user method didn't return a dict" + assert result.has_key('full_text'), "missing 'full_text' key" + assert result.has_key('name'), "missing 'name' key" + finally: + USER_CACHE[my_method] = (index, result) + j.insert(index, result) except Exception, err: syslog(LOG_ERR, "injection failed (%s)" % str(err)) return j @@ -182,22 +191,25 @@ if __name__ == '__main__': PARSER = argparse.ArgumentParser(add_help=True) PARSER = argparse.ArgumentParser(version='0.1') PARSER.add_argument('-c', action="store", dest="i3status_conf", type=str, default="/etc/i3status.conf", help="path to i3status config file") - PARSER.add_argument('-n', action="store", dest="interval", type=int, default=1, help="polling interval in seconds (default 1 sec)") - PARSER.add_argument('-i', action="store", dest="include_path", type=str, default='.i3/py3status', help="user-based class include directory") PARSER.add_argument('-d', action="store_true", dest="disable_transform", help="disable integrated transformations") + PARSER.add_argument('-i', action="store", dest="include_path", type=str, default='.i3/py3status', help="user-based class include directory") + PARSER.add_argument('-n', action="store", dest="interval", type=int, default=1, help="polling interval in seconds (default 1 sec)") + PARSER.add_argument('-t', action="store", dest="cache_timeout", type=int, default=60, help="injection cache timeout in seconds (default 60 sec)") OPTS = PARSER.parse_args() # globals - I3STATUS_CONFIG_FILE = OPTS.i3status_conf - INTERVAL = OPTS.interval + CACHE_TIMEOUT = OPTS.cache_timeout DISABLE_TRANSFORM = True if OPTS.disable_transform else False - INCLUDE_PATH = os.path.abspath( OPTS.include_path ) + '/' + I3STATUS_CONFIG_FILE = OPTS.i3status_conf I3STATUS_CONFIG = i3status_config_reader() + INCLUDE_PATH = os.path.abspath( OPTS.include_path ) + '/' + INTERVAL = OPTS.interval # py3status uses only the i3bar protocol assert I3STATUS_CONFIG['output_format'] == 'i3bar', 'unsupported output_format' # dynamic inclusion + USER_CACHE = {} USER_CLASSES = {} if INCLUDE_PATH and os.path.isdir(INCLUDE_PATH): for fn in os.listdir(INCLUDE_PATH): -- cgit v1.3