]> git.rmz.io Git - dotfiles.git/commitdiff
weechat: update plugins
authorSamir Benmendil <samir.benmendil@ultrahaptics.com>
Wed, 1 May 2019 08:48:04 +0000 (09:48 +0100)
committerSamir Benmendil <samir.benmendil@ultrahaptics.com>
Wed, 1 May 2019 08:48:04 +0000 (09:48 +0100)
weechat/autosort.conf
weechat/plugins.conf
weechat/python/autosort.py
weechat/python/screen_away.py
weechat/python/text_item.py

index 923dff9f1cb330563f60ed19ce8236fd0b144cef..cd11d47490edabdec92ad3bd5bdcf193eb1bd67b 100644 (file)
 
 [sorting]
 case_sensitive = off
 
 [sorting]
 case_sensitive = off
+debug_log = off
 replacements = ""
 rules = ""
 signal_delay = 5
 signals = "buffer_opened buffer_merged buffer_unmerged buffer_renamed"
 replacements = ""
 rules = ""
 signal_delay = 5
 signals = "buffer_opened buffer_merged buffer_unmerged buffer_renamed"
+sort_limit = 100
 sort_on_config_change = on
 
 [v3]
 sort_on_config_change = on
 
 [v3]
index fbb23eabb3fa99d4b42d0a8609277430abdb1250..0dc184f51ed6a8d021a2df5ca819e71f8f4ccaa7 100644 (file)
@@ -71,6 +71,7 @@ python.screen_away.interval = "5"
 python.screen_away.message = "Detached from tmux"
 python.screen_away.no_output = "off"
 python.screen_away.set_away = "on"
 python.screen_away.message = "Detached from tmux"
 python.screen_away.no_output = "off"
 python.screen_away.set_away = "on"
+python.screen_away.socket_file = ""
 python.screen_away.time_format = "since %Y-%m-%d %H:%M:%S%z"
 python.slack.auto_open_threads = "false"
 python.slack.background_load_all_history = "false"
 python.screen_away.time_format = "since %Y-%m-%d %H:%M:%S%z"
 python.slack.auto_open_threads = "false"
 python.slack.background_load_all_history = "false"
@@ -172,6 +173,7 @@ python.screen_away.interval = "How often in seconds to check screen status"
 python.screen_away.message = "Away message"
 python.screen_away.no_output = "no detach/attach information will be displayed in buffer"
 python.screen_away.set_away = "Set user as away."
 python.screen_away.message = "Away message"
 python.screen_away.no_output = "no detach/attach information will be displayed in buffer"
 python.screen_away.set_away = "Set user as away."
+python.screen_away.socket_file = "Socket file to use (leave blank to auto-detect)"
 python.screen_away.time_format = "time format append to away message"
 python.slack.auto_open_threads = "Automatically open threads when mentioned or inresponse to own messages."
 python.slack.background_load_all_history = "Load history for each channel in the background as soon as it opens, rather than waiting for the user to look at it."
 python.screen_away.time_format = "time format append to away message"
 python.slack.auto_open_threads = "Automatically open threads when mentioned or inresponse to own messages."
 python.slack.background_load_all_history = "Load history for each channel in the background as soon as it opens, rather than waiting for the user to look at it."
index 08a6c5ba677b4afc6e4401605dccccd9958216ae..46a840c34a4d0573a5c007c05da8323728b3c629 100644 (file)
 
 #
 # Changelog:
 
 #
 # Changelog:
+# 3.4:
+#   * Fix rate-limit of sorting to prevent high CPU load and lock-ups.
+#   * Fix bug in parsing empty arguments for info hooks.
+#   * Add debug_log option to aid with debugging.
+#   * Correct a few typos.
 # 3.3:
 #   * Fix the /autosort debug command for unicode.
 #   * Update the default rules to work better with Slack.
 # 3.3:
 #   * Fix the /autosort debug command for unicode.
 #   * Update the default rules to work better with Slack.
@@ -71,14 +76,17 @@ import weechat
 
 SCRIPT_NAME     = 'autosort'
 SCRIPT_AUTHOR   = 'Maarten de Vries <maarten@de-vri.es>'
 
 SCRIPT_NAME     = 'autosort'
 SCRIPT_AUTHOR   = 'Maarten de Vries <maarten@de-vri.es>'
-SCRIPT_VERSION  = '3.3'
+SCRIPT_VERSION  = '3.4'
 SCRIPT_LICENSE  = 'GPL3'
 SCRIPT_DESC     = 'Flexible automatic (or manual) buffer sorting based on eval expressions.'
 
 
 SCRIPT_LICENSE  = 'GPL3'
 SCRIPT_DESC     = 'Flexible automatic (or manual) buffer sorting based on eval expressions.'
 
 
-config = None
-hooks  = []
-timer  = None
+config             = None
+hooks              = []
+signal_delay_timer = None
+sort_limit_timer   = None
+sort_queued        = False
+
 
 # Make sure that unicode, bytes and str are always available in python2 and 3.
 # For python 2, str == bytes
 
 # Make sure that unicode, bytes and str are always available in python2 and 3.
 # For python 2, str == bytes
@@ -147,12 +155,12 @@ def decode_rules(blob):
 def decode_helpers(blob):
        parsed = json.loads(blob)
        if not isinstance(parsed, dict):
 def decode_helpers(blob):
        parsed = json.loads(blob)
        if not isinstance(parsed, dict):
-               log('Malformed helpers, expected a JSON encoded dictonary but got a {0}. No helpers have been loaded. Please fix the setting manually.'.format(type(parsed)))
+               log('Malformed helpers, expected a JSON encoded dictionary but got a {0}. No helpers have been loaded. Please fix the setting manually.'.format(type(parsed)))
                return {}
 
        for key, value in parsed.items():
                if not isinstance(value, (str, unicode)):
                return {}
 
        for key, value in parsed.items():
                if not isinstance(value, (str, unicode)):
-                       log('Helper "{0}" is not a string but a {1}. No helpers have been loaded. Please fix seting manually.'.format(key, type(value)))
+                       log('Helper "{0}" is not a string but a {1}. No helpers have been loaded. Please fix setting manually.'.format(key, type(value)))
                        return {}
        return parsed
 
                        return {}
        return parsed
 
@@ -180,6 +188,7 @@ class Config:
        })
 
        default_signal_delay = 5
        })
 
        default_signal_delay = 5
+       default_sort_limit   = 100
 
        default_signals = 'buffer_opened buffer_merged buffer_unmerged buffer_renamed'
 
 
        default_signals = 'buffer_opened buffer_merged buffer_unmerged buffer_renamed'
 
@@ -196,14 +205,18 @@ class Config:
                self.helpers          = {}
                self.signals          = []
                self.signal_delay     = Config.default_signal_delay,
                self.helpers          = {}
                self.signals          = []
                self.signal_delay     = Config.default_signal_delay,
+               self.sort_limit       = Config.default_sort_limit,
                self.sort_on_config   = True
                self.sort_on_config   = True
+               self.debug_log        = False
 
                self.__case_sensitive = None
                self.__rules          = None
                self.__helpers        = None
                self.__signals        = None
                self.__signal_delay   = None
 
                self.__case_sensitive = None
                self.__rules          = None
                self.__helpers        = None
                self.__signals        = None
                self.__signal_delay   = None
+               self.__sort_limit     = None
                self.__sort_on_config = None
                self.__sort_on_config = None
+               self.__debug_log      = None
 
                if not self.config_file:
                        log('Failed to initialize configuration file "{0}".'.format(self.filename))
 
                if not self.config_file:
                        log('Failed to initialize configuration file "{0}".'.format(self.filename))
@@ -273,6 +286,14 @@ class Config:
                        '', '', '', '', '', ''
                )
 
                        '', '', '', '', '', ''
                )
 
+               self.__sort_limit = weechat.config_new_option(
+                       self.config_file, self.sorting_section,
+                       'sort_limit', 'integer',
+                       'Minimum delay in milliseconds to wait after sorting before signals can trigger a sort again. This is effectively a rate limit on sorting. Keeping signal_delay low while setting this higher can reduce excessive sorting without a long initial delay.',
+                       '', 0, 1000, str(Config.default_sort_limit), str(Config.default_sort_limit), 0,
+                       '', '', '', '', '', ''
+               )
+
                self.__sort_on_config = weechat.config_new_option(
                        self.config_file, self.sorting_section,
                        'sort_on_config_change', 'boolean',
                self.__sort_on_config = weechat.config_new_option(
                        self.config_file, self.sorting_section,
                        'sort_on_config_change', 'boolean',
@@ -281,6 +302,14 @@ class Config:
                        '', '', '', '', '', ''
                )
 
                        '', '', '', '', '', ''
                )
 
+               self.__debug_log = weechat.config_new_option(
+                       self.config_file, self.sorting_section,
+                       'debug_log', 'boolean',
+                       'If enabled, print more debug messages. Not recommended for normal usage.',
+                       '', 0, 0, 'off', 'off', 0,
+                       '', '', '', '', '', ''
+               )
+
                if weechat.config_read(self.config_file) != weechat.WEECHAT_RC_OK:
                        log('Failed to load configuration file.')
 
                if weechat.config_read(self.config_file) != weechat.WEECHAT_RC_OK:
                        log('Failed to load configuration file.')
 
@@ -302,7 +331,9 @@ class Config:
                self.helpers        = decode_helpers(helpers_blob)
                self.signals        = signals_blob.split()
                self.signal_delay   = weechat.config_integer(self.__signal_delay)
                self.helpers        = decode_helpers(helpers_blob)
                self.signals        = signals_blob.split()
                self.signal_delay   = weechat.config_integer(self.__signal_delay)
+               self.sort_limit     = weechat.config_integer(self.__sort_limit)
                self.sort_on_config = weechat.config_boolean(self.__sort_on_config)
                self.sort_on_config = weechat.config_boolean(self.__sort_on_config)
+               self.debug_log      = weechat.config_boolean(self.__debug_log)
 
        def save_rules(self, run_callback = True):
                ''' Save the current rules to the configuration. '''
 
        def save_rules(self, run_callback = True):
                ''' Save the current rules to the configuration. '''
@@ -317,10 +348,12 @@ def pad(sequence, length, padding = None):
        ''' Pad a list until is has a certain length. '''
        return sequence + [padding] * max(0, (length - len(sequence)))
 
        ''' Pad a list until is has a certain length. '''
        return sequence + [padding] * max(0, (length - len(sequence)))
 
-
 def log(message, buffer = 'NULL'):
        weechat.prnt(buffer, 'autosort: {0}'.format(message))
 
 def log(message, buffer = 'NULL'):
        weechat.prnt(buffer, 'autosort: {0}'.format(message))
 
+def debug(message, buffer = 'NULL'):
+       if config.debug_log:
+               weechat.prnt(buffer, 'autosort: debug: {0}'.format(message))
 
 def get_buffers():
        ''' Get a list of all the buffers in weechat. '''
 
 def get_buffers():
        ''' Get a list of all the buffers in weechat. '''
@@ -396,18 +429,23 @@ def split_args(args, expected, optional = 0):
                raise HumanReadableError('Expected at least {0} arguments, got {1}.'.format(expected, len(split)))
        return split[:-1] + pad(split[-1].split(' ', optional), optional + 1, '')
 
                raise HumanReadableError('Expected at least {0} arguments, got {1}.'.format(expected, len(split)))
        return split[:-1] + pad(split[-1].split(' ', optional), optional + 1, '')
 
-def do_sort():
+def do_sort(verbose = False):
+       start = perf_counter()
+
        hdata, buffers = get_buffers()
        buffers = merge_buffer_list(buffers)
        buffers = sort_buffers(hdata, buffers, config.rules, config.helpers, config.case_sensitive)
        apply_buffer_order(buffers)
 
        hdata, buffers = get_buffers()
        buffers = merge_buffer_list(buffers)
        buffers = sort_buffers(hdata, buffers, config.rules, config.helpers, config.case_sensitive)
        apply_buffer_order(buffers)
 
+       elapsed = perf_counter() - start
+       if verbose:
+               log("Finished sorting buffers in {0:.4f} seconds.".format(elapsed))
+       else:
+               debug("Finished sorting buffers in {0:.4f} seconds.".format(elapsed))
+
 def command_sort(buffer, command, args):
        ''' Sort the buffers and print a confirmation. '''
 def command_sort(buffer, command, args):
        ''' Sort the buffers and print a confirmation. '''
-       start = perf_counter()
-       do_sort()
-       elapsed = perf_counter() - start
-       log("Finished sorting buffers in {0:.4f} seconds.".format(elapsed))
+       do_sort(True)
        return weechat.WEECHAT_RC_OK
 
 def command_debug(buffer, command, args):
        return weechat.WEECHAT_RC_OK
 
 def command_debug(buffer, command, args):
@@ -429,7 +467,7 @@ def command_debug(buffer, command, args):
                fullname = ensure_str(fullname)
                result = [ensure_str(x) for x in result]
                log('{0}: {1}'.format(fullname, result))
                fullname = ensure_str(fullname)
                result = [ensure_str(x) for x in result]
                log('{0}: {1}'.format(fullname, result))
-       log('Computing evalutaion results took {0:.4f} seconds.'.format(elapsed))
+       log('Computing evaluation results took {0:.4f} seconds.'.format(elapsed))
 
        return weechat.WEECHAT_RC_OK
 
 
        return weechat.WEECHAT_RC_OK
 
@@ -574,7 +612,7 @@ def command_helper_swap(buffer, command, args):
        return weechat.WEECHAT_RC_OK
 
 def call_command(buffer, command, args, subcommands):
        return weechat.WEECHAT_RC_OK
 
 def call_command(buffer, command, args, subcommands):
-       ''' Call a subccommand from a dictionary. '''
+       ''' Call a subcommand from a dictionary. '''
        subcommand, tail = pad(args.split(' ', 1), 2, '')
        subcommand = subcommand.strip()
        if (subcommand == ''):
        subcommand, tail = pad(args.split(' ', 1), 2, '')
        subcommand = subcommand.strip()
        if (subcommand == ''):
@@ -591,21 +629,78 @@ def call_command(buffer, command, args, subcommands):
        log('{0}: command not found'.format(' '.join(command)))
        return weechat.WEECHAT_RC_ERROR
 
        log('{0}: command not found'.format(' '.join(command)))
        return weechat.WEECHAT_RC_ERROR
 
-def on_signal(*args, **kwargs):
-       global timer
-       ''' Called whenever the buffer list changes. '''
-       if timer is not None:
-               weechat.unhook(timer)
-               timer = None
-       weechat.hook_timer(config.signal_delay, 0, 1, "on_timeout", "")
+def on_signal(data, signal, signal_data):
+       global signal_delay_timer
+       global sort_queued
+
+       # If the sort limit timeout is started, we're in the hold-off time after sorting, just queue a sort.
+       if sort_limit_timer is not None:
+               if sort_queued:
+                       debug('Signal {0} ignored, sort limit timeout is active and sort is already queued.'.format(signal))
+               else:
+                       debug('Signal {0} received but sort limit timeout is active, sort is now queued.'.format(signal))
+               sort_queued = True
+               return weechat.WEECHAT_RC_OK
+
+       # If the signal delay timeout is started, a signal was recently received, so ignore this signal.
+       if signal_delay_timer is not None:
+               debug('Signal {0} ignored, signal delay timeout active.'.format(signal))
+               return weechat.WEECHAT_RC_OK
+
+       # Otherwise, start the signal delay timeout.
+       debug('Signal {0} received, starting signal delay timeout of {1} ms.'.format(signal, config.signal_delay))
+       weechat.hook_timer(config.signal_delay, 0, 1, "on_signal_delay_timeout", "")
        return weechat.WEECHAT_RC_OK
 
        return weechat.WEECHAT_RC_OK
 
-def on_timeout(pointer, remaining_calls):
-       global timer
-       timer = None
+def on_signal_delay_timeout(pointer, remaining_calls):
+       """ Called when the signal_delay_timer triggers. """
+       global signal_delay_timer
+       global sort_limit_timer
+       global sort_queued
+
+       signal_delay_timer = None
+
+       # If the sort limit timeout was started, we're still in the no-sort period, so just queue a sort.
+       if sort_limit_timer is not None:
+               debug('Signal delay timeout expired, but sort limit timeout is active, sort is now queued.')
+               sort_queued = True
+               return weechat.WEECHAT_RC_OK
+
+       # Time to sort!
+       debug('Signal delay timeout expired, starting sort.')
        do_sort()
        do_sort()
+
+       # Start the sort limit timeout if not disabled.
+       if config.sort_limit > 0:
+               debug('Starting sort limit timeout of {0} ms.'.format(config.sort_limit))
+               sort_limit_timer = weechat.hook_timer(config.sort_limit, 0, 1, "on_sort_limit_timeout", "")
+
        return weechat.WEECHAT_RC_OK
 
        return weechat.WEECHAT_RC_OK
 
+def on_sort_limit_timeout(pointer, remainin_calls):
+       """ Called when de sort_limit_timer triggers. """
+       global sort_limit_timer
+       global sort_queued
+
+       # If no signal was received during the timeout, we're done.
+       if not sort_queued:
+               debug('Sort limit timeout expired without receiving a signal.')
+               sort_limit_timer = None
+               return weechat.WEECHAT_RC_OK
+
+       # Otherwise it's time to sort.
+       debug('Signal received during sort limit timeout, starting queued sort.')
+       do_sort()
+       sort_queued = False
+
+       # Start the sort limit timeout again if not disabled.
+       if config.sort_limit > 0:
+               debug('Starting sort limit timeout of {0} ms.'.format(config.sort_limit))
+               sort_limit_timer = weechat.hook_timer(config.sort_limit, 0, 1, "on_sort_limit_timeout", "")
+
+       return weechat.WEECHAT_RC_OK
+
+
 def apply_config():
        # Unhook all signals and hook the new ones.
        for hook in hooks:
 def apply_config():
        # Unhook all signals and hook the new ones.
        for hook in hooks:
@@ -614,6 +709,7 @@ def apply_config():
                hooks.append(weechat.hook_signal(signal, 'on_signal', ''))
 
        if config.sort_on_config:
                hooks.append(weechat.hook_signal(signal, 'on_signal', ''))
 
        if config.sort_on_config:
+               debug('Sorting because configuration changed.')
                do_sort()
 
 def on_config_changed(*args, **kwargs):
                do_sort()
 
 def on_config_changed(*args, **kwargs):
@@ -624,7 +720,7 @@ def on_config_changed(*args, **kwargs):
        return weechat.WEECHAT_RC_OK
 
 def parse_arg(args):
        return weechat.WEECHAT_RC_OK
 
 def parse_arg(args):
-       if not args: return None, None
+       if not args: return '', None
 
        result  = ''
        escaped = False
 
        result  = ''
        escaped = False
@@ -643,10 +739,11 @@ def parse_args(args, max = None):
        result = []
        i = 0
        while max is None or i < max:
        result = []
        i = 0
        while max is None or i < max:
+               i += 1
                arg, args = parse_arg(args)
                if arg is None: break
                result.append(arg)
                arg, args = parse_arg(args)
                if arg is None: break
                result.append(arg)
-               i += 1
+               if args is None: break
        return result, args
 
 def on_info_replace(pointer, name, arguments):
        return result, args
 
 def on_info_replace(pointer, name, arguments):
index ab17b810b2df6c6d4309767743eb7f2a33d37884..23fa206e0a9c580316bfe56da9ce3a4017586844 100644 (file)
@@ -24,6 +24,9 @@
 # (this script requires WeeChat 0.3.0 or newer)
 #
 # History:
 # (this script requires WeeChat 0.3.0 or newer)
 #
 # History:
+# 2019-03-04, Germain Z. <germanosz@gmail.com>
+#  version 0.16: add option "socket_file", for use with e.g. dtach
+#              : code reformatting for consistency/PEP8
 # 2017-11-20, Nils Görs <weechatter@arcor.de>
 #  version 0.15: make script python3 compatible
 #              : fix problem with empty "command_on_*" options
 # 2017-11-20, Nils Görs <weechatter@arcor.de>
 #  version 0.15: make script python3 compatible
 #              : fix problem with empty "command_on_*" options
 # 2009-11-27, xt <xt@bash.no>
 #  version 0.1: initial release
 
 # 2009-11-27, xt <xt@bash.no>
 #  version 0.1: initial release
 
-import weechat as w
-import re
 import os
 import os
-import datetime, time
-
-SCRIPT_NAME    = "screen_away"
-SCRIPT_AUTHOR  = "xt <xt@bash.no>"
-SCRIPT_VERSION = "0.15"
-SCRIPT_LICENSE = "GPL3"
-SCRIPT_DESC    = "Set away status on screen detach"
-
-settings = {
-        'message': ('Detached from screen', 'Away message'),
-        'time_format': ('since %Y-%m-%d %H:%M:%S%z', 'time format append to away message'),
-        'interval': ('5', 'How often in seconds to check screen status'),
-        'away_suffix': ('', 'What to append to your nick when you\'re away.'),
-        'command_on_attach': ('', 'Commands to execute on attach, separated by semicolon'),
-        'command_on_detach': ('', 'Commands to execute on detach, separated by semicolon'),
-        'ignore': ('', 'Comma-separated list of servers to ignore.'),
-        'set_away': ('on', 'Set user as away.'),
-        'ignore_relays': ('off', 'Only check screen status and ignore relay interfaces'),
-        'no_output': ('off','no detach/attach information will be displayed in buffer'),
+import re
+import time
+import weechat as w
+
+
+SCRIPT_NAME = 'screen_away'
+SCRIPT_AUTHOR = 'xt <xt@bash.no>'
+SCRIPT_VERSION = '0.16'
+SCRIPT_LICENSE = 'GPL3'
+SCRIPT_DESC = 'Set away status on screen detach'
+
+SETTINGS = {
+    'message': ('Detached from screen', 'Away message'),
+    'time_format': ('since %Y-%m-%d %H:%M:%S%z',
+                    'time format append to away message'),
+    'interval': ('5', 'How often in seconds to check screen status'),
+    'away_suffix': ('', 'What to append to your nick when you\'re away.'),
+    'command_on_attach': ('',
+                          ('Commands to execute on attach, separated by '
+                           'semicolon')),
+    'command_on_detach': ('',
+                          ('Commands to execute on detach, separated by '
+                           'semicolon')),
+    'ignore': ('', 'Comma-separated list of servers to ignore.'),
+    'set_away': ('on', 'Set user as away.'),
+    'ignore_relays': ('off',
+                      'Only check screen status and ignore relay interfaces'),
+    'no_output': ('off',
+                  'no detach/attach information will be displayed in buffer'),
+    'socket_file': ('', 'Socket file to use (leave blank to auto-detect)'),
 }
 
 TIMER = None
 }
 
 TIMER = None
@@ -92,51 +104,62 @@ SOCK = None
 AWAY = False
 CONNECTED_RELAY = False
 
 AWAY = False
 CONNECTED_RELAY = False
 
-def set_timer():
-    '''Update timer hook with new interval'''
 
 
+def set_timer():
+    '''Update timer hook with new interval.'''
     global TIMER
     if TIMER:
         w.unhook(TIMER)
     global TIMER
     if TIMER:
         w.unhook(TIMER)
-    TIMER = w.hook_timer(int(w.config_get_plugin('interval')) * 1000,
-            0, 0, "screen_away_timer_cb", '')
+    TIMER = w.hook_timer(int(w.config_get_plugin('interval')) * 1000, 0, 0,
+                         'screen_away_timer_cb', '')
+
 
 def screen_away_config_cb(data, option, value):
 
 def screen_away_config_cb(data, option, value):
-    if option.endswith(".interval"):
+    '''Update timer / sock file on config changes.'''
+    global SOCK
+    if SOCK and option.endswith('.interval'):
         set_timer()
         set_timer()
+    elif option.endswith('.socket_file'):
+        SOCK = value
+        if not SOCK:
+            SOCK = get_sock()
+        if SOCK:
+            set_timer()
+        elif TIMER:
+            w.unhook(TIMER)
     return w.WEECHAT_RC_OK
 
     return w.WEECHAT_RC_OK
 
-def get_servers():
-    '''Get the servers that are not away, or were set away by this script'''
 
 
+def get_servers():
+    '''Get the servers that are not away, or were set away by this script.'''
     ignores = w.config_get_plugin('ignore').split(',')
     ignores = w.config_get_plugin('ignore').split(',')
-    infolist = w.infolist_get('irc_server','','')
+    infolist = w.infolist_get('irc_server', '', '')
     buffers = []
     while w.infolist_next(infolist):
     buffers = []
     while w.infolist_next(infolist):
-        if not w.infolist_integer(infolist, 'is_connected') == 1 or \
-               w.infolist_string(infolist, 'name') in ignores:
+        if (not w.infolist_integer(infolist, 'is_connected') == 1 or
+                w.infolist_string(infolist, 'name') in ignores):
             continue
             continue
-        if not w.config_string_to_boolean(w.config_get_plugin('set_away')) or \
-                not w.infolist_integer(infolist, 'is_away') or \
-                    w.config_get_plugin('message') in w.infolist_string(infolist, 'away_message'):
-#                    w.infolist_string(infolist, 'away_message') == \
-#                    w.config_get_plugin('message'):
+        if (not w.config_string_to_boolean(w.config_get_plugin('set_away')) or
+                not w.infolist_integer(infolist, 'is_away') or
+                w.config_get_plugin('message') in w.infolist_string(
+                    infolist, 'away_message')):
             buffers.append((w.infolist_pointer(infolist, 'buffer'),
             buffers.append((w.infolist_pointer(infolist, 'buffer'),
-                w.infolist_string(infolist, 'nick')))
+                            w.infolist_string(infolist, 'nick')))
     w.infolist_free(infolist)
     return buffers
 
     w.infolist_free(infolist)
     return buffers
 
-def screen_away_timer_cb(buffer, args):
-    '''Check if screen is attached, update awayness'''
 
 
+def screen_away_timer_cb(buffer, args):
+    '''Check if screen is attached and update awayness.'''
     global AWAY, SOCK, CONNECTED_RELAY
 
     set_away = w.config_string_to_boolean(w.config_get_plugin('set_away'))
     global AWAY, SOCK, CONNECTED_RELAY
 
     set_away = w.config_string_to_boolean(w.config_get_plugin('set_away'))
-    check_relays = not w.config_string_to_boolean(w.config_get_plugin('ignore_relays'))
+    check_relays = not w.config_string_to_boolean(
+        w.config_get_plugin('ignore_relays'))
     suffix = w.config_get_plugin('away_suffix')
     suffix = w.config_get_plugin('away_suffix')
-    attached = os.access(SOCK, os.X_OK) # X bit indicates attached
+    attached = os.access(SOCK, os.X_OK)  # X bit indicates attached.
 
 
-    # Check wether a client is connected on relay or not
+    # Check wether a client is connected on relay or not.
     CONNECTED_RELAY = False
     if check_relays:
         infolist = w.infolist_get('relay', '', '')
     CONNECTED_RELAY = False
     if check_relays:
         infolist = w.infolist_get('relay', '', '')
@@ -148,59 +171,74 @@ def screen_away_timer_cb(buffer, args):
                     break
             w.infolist_free(infolist)
 
                     break
             w.infolist_free(infolist)
 
-    if (attached and AWAY) or (check_relays and CONNECTED_RELAY and not attached and AWAY):
+    if ((attached and AWAY) or
+            (check_relays and CONNECTED_RELAY and not attached and AWAY)):
         if not w.config_string_to_boolean(w.config_get_plugin('no_output')):
         if not w.config_string_to_boolean(w.config_get_plugin('no_output')):
-            w.prnt('', '%s: Screen attached. Clearing away status' % SCRIPT_NAME)
+            w.prnt('', '{}: Screen attached. Clearing away status'.format(
+                SCRIPT_NAME))
         for server, nick in get_servers():
             if set_away:
         for server, nick in get_servers():
             if set_away:
-                w.command(server,  "/away")
+                w.command(server, '/away')
             if suffix and nick.endswith(suffix):
                 nick = nick[:-len(suffix)]
             if suffix and nick.endswith(suffix):
                 nick = nick[:-len(suffix)]
-                w.command(server,  "/nick %s" % nick)
+                w.command(server, '/nick {}'.format(nick))
         AWAY = False
         AWAY = False
-        if w.config_get_plugin("command_on_attach"):
-            for cmd in w.config_get_plugin("command_on_attach").split(";"):
-                w.command("", cmd)
+        if w.config_get_plugin('command_on_attach'):
+            for cmd in w.config_get_plugin('command_on_attach').split(';'):
+                w.command('', cmd)
 
     elif not attached and not AWAY:
         if not CONNECTED_RELAY:
 
     elif not attached and not AWAY:
         if not CONNECTED_RELAY:
-            if not w.config_string_to_boolean(w.config_get_plugin('no_output')):
-                w.prnt('', '%s: Screen detached. Setting away status' % SCRIPT_NAME)
+            if (not w.config_string_to_boolean(
+                    w.config_get_plugin('no_output'))):
+                w.prnt('', '{}: Screen detached. Setting away status'.format(
+                    SCRIPT_NAME))
             for server, nick in get_servers():
                 if suffix and not nick.endswith(suffix):
             for server, nick in get_servers():
                 if suffix and not nick.endswith(suffix):
-                    w.command(server, "/nick %s%s" % (nick, suffix));
+                    w.command(server, '/nick {}{}'.format(nick, suffix))
                 if set_away:
                 if set_away:
-                    w.command(server, "/away %s %s" % (w.config_get_plugin('message'), time.strftime(w.config_get_plugin('time_format'))))
+                    w.command(server, '/away {} {}'.format(
+                        w.config_get_plugin('message'),
+                        time.strftime(w.config_get_plugin('time_format'))))
             AWAY = True
             AWAY = True
-            if w.config_get_plugin("command_on_detach"):
-                for cmd in w.config_get_plugin("command_on_detach").split(";"):
-                    w.command("", cmd)
+            if w.config_get_plugin('command_on_detach'):
+                for cmd in w.config_get_plugin('command_on_detach').split(';'):
+                    w.command('', cmd)
 
     return w.WEECHAT_RC_OK
 
 
 
     return w.WEECHAT_RC_OK
 
 
+def get_sock():
+    '''Try to get the appropriate sock file for screen/tmux.'''
+    sock = None
+    if 'STY' in os.environ.keys():
+        # We are running under screen.
+        cmd_output = os.popen('env LC_ALL=C screen -ls').read()
+        match = re.search(r'Sockets? in (/.+)\.', cmd_output)
+        if match:
+            sock = os.path.join(match.group(1), os.environ['STY'])
+
+    if not sock and 'TMUX' in os.environ.keys():
+        # We are running under tmux.
+        socket_data = os.environ['TMUX']
+        sock = socket_data.rsplit(',', 2)[0]
+    return sock
+
+
 if w.register(SCRIPT_NAME, SCRIPT_AUTHOR, SCRIPT_VERSION, SCRIPT_LICENSE,
 if w.register(SCRIPT_NAME, SCRIPT_AUTHOR, SCRIPT_VERSION, SCRIPT_LICENSE,
-                    SCRIPT_DESC, "", ""):
+              SCRIPT_DESC, '', ''):
     version = w.info_get('version_number', '') or 0
     version = w.info_get('version_number', '') or 0
-    for option, default_desc in settings.items():
+    for option, default_desc in SETTINGS.items():
         if not w.config_is_set_plugin(option):
             w.config_set_plugin(option, default_desc[0])
         if int(version) >= 0x00030500:
             w.config_set_desc_plugin(option, default_desc[1])
 
         if not w.config_is_set_plugin(option):
             w.config_set_plugin(option, default_desc[0])
         if int(version) >= 0x00030500:
             w.config_set_desc_plugin(option, default_desc[1])
 
-    if 'STY' in os.environ.keys():
-        # We are running under screen
-        cmd_output = os.popen('env LC_ALL=C screen -ls').read()
-        match = re.search(r'Sockets? in (/.+)\.', cmd_output)
-        if match:
-            SOCK = os.path.join(match.group(1), os.environ['STY'])
-
-    if not SOCK and 'TMUX' in os.environ.keys():
-        # We are running under tmux
-        socket_data = os.environ['TMUX']
-        SOCK = socket_data.rsplit(',',2)[0]
+    SOCK = w.config_get_plugin('socket_file')
+    if not SOCK:
+        SOCK = get_sock()
 
     if SOCK:
         set_timer()
 
     if SOCK:
         set_timer()
-        w.hook_config("plugins.var.python." + SCRIPT_NAME + ".*",
-            "screen_away_config_cb", "")
+    w.hook_config('plugins.var.python.{}.*'.format(SCRIPT_NAME),
+                  'screen_away_config_cb', '')
index 47b284fb16a69ce5519bb1e11ba9c3770f74140f..08c1536e6e266553b84e5e4035eb0a2972276c3f 100644 (file)
@@ -1,6 +1,6 @@
 # -*- coding: utf-8 -*-
 #
 # -*- coding: utf-8 -*-
 #
-# Copyright (c) 2012-2017 by nils_2 <weechatter@arcor.de>
+# Copyright (c) 2012-2018 by nils_2 <weechatter@arcor.de>
 #
 # add a plain text or evaluated content to item bar
 #
 #
 # add a plain text or evaluated content to item bar
 #
@@ -17,6 +17,9 @@
 # You should have received a copy of the GNU General Public License
 # along with this program.  If not, see <http://www.gnu.org/licenses/>.
 #
 # You should have received a copy of the GNU General Public License
 # along with this program.  If not, see <http://www.gnu.org/licenses/>.
 #
+# 2018-08-18: nils_2, (freenode.#weechat)
+#       0.8 : add new option "interval"
+#
 # 2017-08-23: nils_2, (freenode.#weechat)
 #     0.7.1 : improve /help text
 #
 # 2017-08-23: nils_2, (freenode.#weechat)
 #     0.7.1 : improve /help text
 #
@@ -56,7 +59,7 @@ except Exception:
 
 SCRIPT_NAME     = "text_item"
 SCRIPT_AUTHOR   = "nils_2 <weechatter@arcor.de>"
 
 SCRIPT_NAME     = "text_item"
 SCRIPT_AUTHOR   = "nils_2 <weechatter@arcor.de>"
-SCRIPT_VERSION  = "0.7.1"
+SCRIPT_VERSION  = "0.8"
 SCRIPT_LICENSE  = "GPL"
 SCRIPT_DESC     = "add a plain text or evaluated content to item bar"
 
 SCRIPT_LICENSE  = "GPL"
 SCRIPT_DESC     = "add a plain text or evaluated content to item bar"
 
@@ -64,6 +67,11 @@ SCRIPT_DESC     = "add a plain text or evaluated content to item bar"
 regex_color=re.compile('\$\{([^\{\}]+)\}')
 
 hooks = {}
 regex_color=re.compile('\$\{([^\{\}]+)\}')
 
 hooks = {}
+TIMER = None
+
+settings = {
+        'interval': ('0', 'How often (in seconds) to force an update of all items. 0 means deactivated'),
+}
 
 # ================================[ hooks ]===============================
 def add_hook(signal, item):
 
 # ================================[ hooks ]===============================
 def add_hook(signal, item):
@@ -71,7 +79,7 @@ def add_hook(signal, item):
     # signal already exists?
     if signal in hooks:
         return
     # signal already exists?
     if signal in hooks:
         return
-    hooks[item] = weechat.hook_signal(signal, "bar_item_update", "")
+    hooks[item] = weechat.hook_signal(signal, "bar_item_update_cb", "")
 
 def unhook(hook):
     global hooks
 
 def unhook(hook):
     global hooks
@@ -79,9 +87,14 @@ def unhook(hook):
         weechat.unhook(hooks[hook])
         del hooks[hook]
 
         weechat.unhook(hooks[hook])
         del hooks[hook]
 
-def toggle_refresh(pointer, name, value):
+def toggle_refresh_cb(pointer, name, value):
     option_name = name[len('plugins.var.python.' + SCRIPT_NAME + '.'):]      # get optionname
 
     option_name = name[len('plugins.var.python.' + SCRIPT_NAME + '.'):]      # get optionname
 
+    # check for timer hook
+    if name.endswith(".interval"):
+        set_timer()
+        return weechat.WEECHAT_RC_OK
+
     # option was removed? remove bar_item from struct
     if not weechat.config_get_plugin(option_name):
         ptr_bar = weechat.bar_item_search(option_name)
     # option was removed? remove bar_item from struct
     if not weechat.config_get_plugin(option_name):
         ptr_bar = weechat.bar_item_search(option_name)
@@ -96,6 +109,19 @@ def toggle_refresh(pointer, name, value):
     weechat.bar_item_update(option_name)
     return weechat.WEECHAT_RC_OK
 
     weechat.bar_item_update(option_name)
     return weechat.WEECHAT_RC_OK
 
+def set_timer():
+    # Update timer hook with new interval. 0 means deactivated
+    global TIMER
+    if TIMER:
+        weechat.unhook(TIMER)
+    if int(weechat.config_get_plugin('interval')) >= 1:
+        TIMER = weechat.hook_timer(int(weechat.config_get_plugin('interval')) * 1000,0, 0, "timer_dummy_cb", '')
+
+def timer_dummy_cb(data, remaining_calls):
+    # hook_timer() has two arguments, hook_signal() needs three arguments
+    bar_item_update_cb("","","")
+    return weechat.WEECHAT_RC_OK
+
 # ================================[ items ]===============================
 def create_bar_items():
     ptr_infolist_option = weechat.infolist_get('option','','plugins.var.python.' + SCRIPT_NAME + '.*')
 # ================================[ items ]===============================
 def create_bar_items():
     ptr_infolist_option = weechat.infolist_get('option','','plugins.var.python.' + SCRIPT_NAME + '.*')
@@ -132,7 +158,7 @@ def update_item (data, item, window):
     return substitute_colors(value,window)
 
 # update item from weechat.hook_signal()
     return substitute_colors(value,window)
 
 # update item from weechat.hook_signal()
-def bar_item_update(signal, callback, callback_data):
+def bar_item_update_cb(signal, callback, callback_data):
     ptr_infolist_option = weechat.infolist_get('option','','plugins.var.python.' + SCRIPT_NAME + '.*')
 
     if not ptr_infolist_option:
     ptr_infolist_option = weechat.infolist_get('option','','plugins.var.python.' + SCRIPT_NAME + '.*')
 
     if not ptr_infolist_option:
@@ -141,12 +167,15 @@ def bar_item_update(signal, callback, callback_data):
     while weechat.infolist_next(ptr_infolist_option):
         option_full_name = weechat.infolist_string(ptr_infolist_option, 'full_name')
         option_name = option_full_name[len('plugins.var.python.' + SCRIPT_NAME + '.'):]      # get optionname
     while weechat.infolist_next(ptr_infolist_option):
         option_full_name = weechat.infolist_string(ptr_infolist_option, 'full_name')
         option_name = option_full_name[len('plugins.var.python.' + SCRIPT_NAME + '.'):]      # get optionname
+        if option_name == "interval":
+            continue
 
         # check if item exists in a bar and if we have a hook for it
         if weechat.bar_item_search(option_name) and option_name in hooks:
             weechat.bar_item_update(option_name)
 
     weechat.infolist_free(ptr_infolist_option)
 
         # check if item exists in a bar and if we have a hook for it
         if weechat.bar_item_search(option_name) and option_name in hooks:
             weechat.bar_item_update(option_name)
 
     weechat.infolist_free(ptr_infolist_option)
+
     return weechat.WEECHAT_RC_OK
 
 
     return weechat.WEECHAT_RC_OK
 
 
@@ -200,7 +229,11 @@ if __name__ == "__main__":
                         '   type : channel, server, private, all (all kind of buffers e.g. /color, /fset...) and !all (channel, server and private buffer)\n'
                         '   (see: /buffer localvar)\n\n'
                         '   signal (eg.): buffer_switch, buffer_closing, print, mouse_enabled\n'
                         '   type : channel, server, private, all (all kind of buffers e.g. /color, /fset...) and !all (channel, server and private buffer)\n'
                         '   (see: /buffer localvar)\n\n'
                         '   signal (eg.): buffer_switch, buffer_closing, print, mouse_enabled\n'
-                        '   (for a list of all possible signals, see API doc weechat_hook_signal())\n\n'
+                        '   (for a list of all possible signals, see API doc weechat_hook_signal())\n'
+                        '\n'
+                        'You can activate a timer hook() to force an upgrade of all items in a given period of time, for example using an item that have to be\n'
+                        'updated every second (e.g. watch)\n'
+                        '\n'
                         'Examples:\n'
                         'creates an option for a text item named "nick_text". The item will be created for "channel" buffers. '
                         'The text displayed in the status-bar is "Nicks:" (yellow colored!):\n'
                         'Examples:\n'
                         'creates an option for a text item named "nick_text". The item will be created for "channel" buffers. '
                         'The text displayed in the status-bar is "Nicks:" (yellow colored!):\n'
@@ -215,5 +248,14 @@ if __name__ == "__main__":
                         '',
                         '')
             version = weechat.info_get("version_number", "") or 0
                         '',
                         '')
             version = weechat.info_get("version_number", "") or 0
+
+            for option, default_desc in settings.items():
+                if not weechat.config_is_set_plugin(option):
+                    weechat.config_set_plugin(option, default_desc[0])
+                if int(version) >= 0x00030500:
+                    weechat.config_set_desc_plugin(option, default_desc[1])
+
+            set_timer()
             create_bar_items()
             create_bar_items()
-            weechat.hook_config( 'plugins.var.python.' + SCRIPT_NAME + '.*', 'toggle_refresh', '' )
+
+            weechat.hook_config( 'plugins.var.python.' + SCRIPT_NAME + '.*', 'toggle_refresh_cb', '' )