|
| 1 | + |
| 2 | +class EnvironmentMixin(object): |
| 3 | + |
| 4 | + """Mixin class for managing session and server level environment |
| 5 | + variables in tmux. |
| 6 | +
|
| 7 | + """ |
| 8 | + |
| 9 | + _add_option = None |
| 10 | + |
| 11 | + def __init__(self, add_option=None): |
| 12 | + self._add_option = add_option |
| 13 | + |
| 14 | + def set_environment(self, name, value): |
| 15 | + """Set environment ``$ tmux set-environment <name> <value>``. |
| 16 | +
|
| 17 | + :param name: the environment variable name. such as 'PATH'. |
| 18 | + :type option: string |
| 19 | + :param value: environment value. |
| 20 | + :type value: string |
| 21 | +
|
| 22 | + """ |
| 23 | + |
| 24 | + args = ['set-environment'] |
| 25 | + if self._add_option: |
| 26 | + args += [self._add_option] |
| 27 | + |
| 28 | + args += [name, value] |
| 29 | + |
| 30 | + proc = self.cmd(*args) |
| 31 | + |
| 32 | + if proc.stderr: |
| 33 | + if isinstance(proc.stderr, list) and len(proc.stderr) == int(1): |
| 34 | + proc.stderr = proc.stderr[0] |
| 35 | + raise ValueError('tmux set-environment stderr: %s' % proc.stderr) |
| 36 | + |
| 37 | + def unset_environment(self, name): |
| 38 | + """Unset environment variable ``$ tmux set-environment -u <name>``. |
| 39 | +
|
| 40 | + :param name: the environment variable name. such as 'PATH'. |
| 41 | + :type option: string |
| 42 | + """ |
| 43 | + |
| 44 | + args = ['set-environment'] |
| 45 | + if self._add_option: |
| 46 | + args += [self._add_option] |
| 47 | + args += ['-u', name] |
| 48 | + |
| 49 | + proc = self.cmd(*args) |
| 50 | + |
| 51 | + if proc.stderr: |
| 52 | + if isinstance(proc.stderr, list) and len(proc.stderr) == int(1): |
| 53 | + proc.stderr = proc.stderr[0] |
| 54 | + raise ValueError('tmux set-environment stderr: %s' % proc.stderr) |
| 55 | + |
| 56 | + def remove_environment(self, name): |
| 57 | + """Remove environment variable ``$ tmux set-environment -r <name>``. |
| 58 | +
|
| 59 | + :param name: the environment variable name. such as 'PATH'. |
| 60 | + :type option: string |
| 61 | + """ |
| 62 | + |
| 63 | + args = ['set-environment'] |
| 64 | + if self._add_option: |
| 65 | + args += [self._add_option] |
| 66 | + args += ['-r', name] |
| 67 | + |
| 68 | + proc = self.cmd(*args) |
| 69 | + |
| 70 | + if proc.stderr: |
| 71 | + if isinstance(proc.stderr, list) and len(proc.stderr) == int(1): |
| 72 | + proc.stderr = proc.stderr[0] |
| 73 | + raise ValueError('tmux set-environment stderr: %s' % proc.stderr) |
| 74 | + |
| 75 | + def show_environment(self, name=None): |
| 76 | + """Show environment ``$tmux show-environment -t [session] <name>``. |
| 77 | +
|
| 78 | + Return dict of environment variables for the session or the value of a |
| 79 | + specific variable if the name is specified. |
| 80 | +
|
| 81 | + :param name: the environment variable name. such as 'PATH'. |
| 82 | + :type option: string |
| 83 | + """ |
| 84 | + tmux_args = ['show-environment'] |
| 85 | + if self._add_option: |
| 86 | + tmux_args += [self._add_option] |
| 87 | + if name: |
| 88 | + tmux_args += [name] |
| 89 | + vars = self.cmd(*tmux_args).stdout |
| 90 | + vars = [tuple(item.split('=', 1)) for item in vars] |
| 91 | + vars_dict = {} |
| 92 | + for t in vars: |
| 93 | + if len(t) == 2: |
| 94 | + vars_dict[t[0]] = t[1] |
| 95 | + elif len(t) == 1: |
| 96 | + vars_dict[t[0]] = True |
| 97 | + else: |
| 98 | + raise ValueError('unexpected variable %s', t) |
| 99 | + |
| 100 | + if name: |
| 101 | + return vars_dict.get(name) |
| 102 | + |
| 103 | + return vars_dict |
0 commit comments