Commit a69d3ff2 authored by dobbymoodge's avatar dobbymoodge Committed by Sébastien Helleu 3: support python 3.x and fixes

This commit replaces a `dict.iteritems()` call with the
py2/3-compatible `dict.items()` call. It also adds code to try the
`text=True` arg to the `subprocess.check_output` call so the output
can be handled as a `str` in python 3, falling back to the python
2-compatible call without the `text` arg if it catches and exception.
The updated args are stored in a global so we don't burn cycles
raising the exception every time `check_output` is called.

Other changes include minor formatting tweaks and replacing a
`+`-style string concatenation with inline string formatting.
parent 49c7640c
......@@ -21,17 +21,23 @@ History:
2014-02-03 Aron Griffis <[email protected]>
version 2: python 2.6 compatible subprocess.check_output()
2020-01-03 dobbymoodge <john.w.lamb [at] gmail . com> ( )
version 3: python 3.x compatibility
from __future__ import absolute_import, unicode_literals
import weechat as w
import fnmatch
import os
import subprocess
if not hasattr(subprocess, 'check_output'):
def check_output(*popenargs, **kwargs):
process = subprocess.Popen(stdout=subprocess.PIPE, *popenargs, **kwargs)
process = subprocess.Popen(stdout=subprocess.PIPE,
*popenargs, **kwargs)
output, unused_err = process.communicate()
retcode = process.poll()
if retcode:
......@@ -43,13 +49,11 @@ if not hasattr(subprocess, 'check_output'):
subprocess.check_output = check_output
del check_output
import weechat as w
SCRIPT_NAME = "tmux_env"
SCRIPT_AUTHOR = "Aron Griffis <[email protected]>"
SCRIPT_NAME = "tmux_env"
SCRIPT_AUTHOR = "Aron Griffis <[email protected]>"
SCRIPT_DESC = "Update weechat environment from tmux"
SCRIPT_DESC = "Update weechat environment from tmux"
settings = {
'interval': '30', # How often in seconds to check for updates
......@@ -58,7 +62,7 @@ settings = {
# environment updates, not removals. For removals, include the variable
# name prefixed by a minus sign. For example, to add/remove exclusively
# the DISPLAY variable, include="DISPLAY,-DISPLAY"
# Globs are also accepted, so you can ignore all variable removals with
# exclude="-*"
......@@ -66,8 +70,11 @@ settings = {
'exclude': '', # Env vars to exclude, default all
check_output_kwargs = {'text': True}
TIMER = None
def set_timer():
"""Update timer hook with new interval"""
......@@ -75,7 +82,8 @@ def set_timer():
TIMER = w.hook_timer(int(w.config_get_plugin('interval')) * 1000,
0, 0, 'timer_cb', '')
0, 0, 'timer_cb', '')
def config_cb(data, option, value):
"""Reset timer when interval option is updated"""
......@@ -84,18 +92,31 @@ def config_cb(data, option, value):
return w.WEECHAT_RC_OK
def timer_cb(buffer, args):
"""Check if tmux is attached, update environment"""
attached = os.access(SOCK, os.X_OK) # X bit indicates attached
attached = os.access(SOCK, os.X_OK) # X bit indicates attached
if attached:
return w.WEECHAT_RC_OK
def do_check_output(command_args):
"""Wrap `subprocess.check_output` to detect and account for
python2/python3 variations"""
global check_output_kwargs
return subprocess.check_output(command_args, **check_output_kwargs)
except TypeError:
return subprocess.check_output(command_args, **check_output_kwargs)
def update_environment():
"""Updates environment from tmux showenv"""
env = subprocess.check_output(['tmux', 'showenv'])
env = subprocess.check_output(['tmux', 'showenv'], text=True)
for line in env.splitlines():
name = line.split('=', 1)[0]
if check_include(name) and not check_exclude(name):
......@@ -104,50 +125,56 @@ def update_environment():
add_env(name, line.split('=', 1)[1])
def check_include(name):
globs = comma_split_config('include')
return check_match(name, globs)
def check_exclude(name):
globs = comma_split_config('exclude')
return check_match(name, globs)
def check_match(name, globs):
for g in globs:
if fnmatch.fnmatch(name, g):
return True
def comma_split_config(name):
config = w.config_get_plugin(name)
return filter(None, (s.strip() for s in config.split(',')))
def add_env(name, value):
old = os.environ.get(name)
if old != value:
w.prnt("", "%s: add %s=%r (was %r)" % (SCRIPT_NAME, name, value, old))
os.environ[name] = value
def remove_env(name):
old = os.environ.get(name)
if old is not None:
w.prnt("", "%s: remove %s (was %r)" % (SCRIPT_NAME, name, old))
del os.environ[name]
SCRIPT_DESC, '', ''):
for option, default_value in settings.iteritems():
SCRIPT_DESC, '', ''):
for option, default_value in settings.items():
if not w.config_is_set_plugin(option):
w.config_set_plugin(option, default_value)
global SOCK
SOCK = None
if 'TMUX' in os.environ.keys():
if 'TMUX' in os.environ:
# We are running under tmux
socket_data = os.environ['TMUX']
SOCK = socket_data.rsplit(',',2)[0]
SOCK = socket_data.rsplit(',', 2)[0]
if SOCK:
w.hook_config("plugins.var.python." + SCRIPT_NAME + ".*",
"config_cb", "")
w.hook_config("plugins.var.python.{}.*".format(SCRIPT_NAME), "config_cb", "")
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment