/usr/lib/python2.7/dist-packages/tmuxp/config.py is in python-tmuxp 1.3.5-2.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 | # -*- coding: utf-8 -*-
"""Configuration parsing and export for tmuxp.
tmuxp.config
~~~~~~~~~~~~
"""
from __future__ import (absolute_import, division, print_function,
unicode_literals, with_statement)
import copy
import logging
import os
from . import exc
from ._compat import string_types
logger = logging.getLogger(__name__)
def validate_schema(sconf):
"""Return True if config schema is correct.
:param sconf: session configuration
:type sconf: dict
:rtype: bool
"""
# verify session_name
if 'session_name' not in sconf:
raise exc.ConfigError('config requires "session_name"')
if 'windows' not in sconf:
raise exc.ConfigError('config requires list of "windows"')
for window in sconf['windows']:
if 'window_name' not in window:
raise exc.ConfigError('config window is missing "window_name"')
if 'panes' not in window:
raise exc.ConfigError(
'config window %s requires list of panes' %
window['window_name']
)
return True
def is_config_file(filename, extensions=['.yml', '.yaml', '.json']):
"""Return True if file has a valid config file type.
:param filename: filename to check (e.g. ``mysession.json``).
:type filename: str
:param extensions: filetypes to check (e.g. ``['.yaml', '.json']``).
:type extensions: list or str
:rtype: bool
"""
extensions = [extensions] if isinstance(
extensions, string_types) else extensions
return any(filename.endswith(e) for e in extensions)
def in_dir(
config_dir=os.path.expanduser('~/.tmuxp'),
extensions=['.yml', '.yaml', '.json']
):
"""Return a list of configs in ``config_dir``.
:param config_dir: directory to search
:type config_dir: str
:param extensions: filetypes to check (e.g. ``['.yaml', '.json']``).
:type extensions: list
:rtype: list
"""
configs = []
for filename in os.listdir(config_dir):
if is_config_file(filename, extensions) and \
not filename.startswith('.'):
configs.append(filename)
return configs
def in_cwd():
"""Return list of configs in current working directory.
If filename is ``.tmuxp.py``, ``.tmuxp.json``, ``.tmuxp.yaml``.
:rtype: list
"""
configs = []
for filename in os.listdir(os.getcwd()):
if filename.startswith('.tmuxp') and is_config_file(filename):
configs.append(filename)
return configs
def expandshell(_path):
"""Return expanded path based on user's ``$HOME`` and ``env``.
:py:func:`os.path.expanduser` and :py:func:`os.path.expandvars`
:param _path: path to expand
:type _path: str
:returns: expanded path
:rtype: str
"""
return os.path.expandvars(os.path.expanduser(_path))
def inline(sconf):
""" Return config in inline form, opposite of :meth:`config.expand`.
:param sconf: unexpanded config file
:type sconf: dict
:rtype: dict
"""
if (
'shell_command' in sconf and
isinstance(sconf['shell_command'], list) and
len(sconf['shell_command']) == 1
):
sconf['shell_command'] = sconf['shell_command'][0]
if len(sconf.keys()) == int(1):
sconf = sconf['shell_command']
if (
'shell_command_before' in sconf and
isinstance(sconf['shell_command_before'], list) and
len(sconf['shell_command_before']) == 1
):
sconf['shell_command_before'] = sconf['shell_command_before'][0]
# recurse into window and pane config items
if 'windows' in sconf:
sconf['windows'] = [
inline(window) for window in sconf['windows']
]
if 'panes' in sconf:
sconf['panes'] = [inline(pane) for pane in sconf['panes']]
return sconf
def expand(sconf, cwd=None, parent=None):
"""Return config with shorthand and inline properties expanded.
This is necessary to keep the code in the :class:`WorkspaceBuilder` clean
and also allow for neat, short-hand configurations.
As a simple example, internally, tmuxp expects that config options
like ``shell_command`` are a list (array)::
'shell_command': ['htop']
tmuxp configs allow for it to be simply a string::
'shell_command': 'htop'
Kaptan will load JSON/YAML files into python dicts for you.
:param sconf: the configuration for the session
:type sconf: dict
:param cwd: directory to expand relative paths against. should be the dir
of the config directory.
:type cwd: str
:param parent: (used on recursive entries) start_directory of parent window
or session object.
:type parent: str
:rtype: dict
"""
# Note: cli.py will expand configs relative to project's config directory
# for the first cwd argument.
if not cwd:
cwd = os.getcwd()
if 'session_name' in sconf:
sconf['session_name'] = expandshell(sconf['session_name'])
if 'window_name' in sconf:
sconf['window_name'] = expandshell(sconf['window_name'])
if 'environment' in sconf:
for key in sconf['environment']:
val = sconf['environment'][key]
val = expandshell(val)
if any(val.startswith(a) for a in ['.', './']):
val = os.path.normpath(os.path.join(cwd, val))
sconf['environment'][key] = val
if 'global_options' in sconf:
for key in sconf['global_options']:
val = sconf['global_options'][key]
if isinstance(val, string_types):
val = expandshell(val)
if any(val.startswith(a) for a in ['.', './']):
val = os.path.normpath(os.path.join(cwd, val))
sconf['global_options'][key] = val
if 'options' in sconf:
for key in sconf['options']:
val = sconf['options'][key]
if isinstance(val, string_types):
val = expandshell(val)
if any(val.startswith(a) for a in ['.', './']):
val = os.path.normpath(os.path.join(cwd, val))
sconf['options'][key] = val
# Any config section, session, window, pane that can contain the
# 'shell_command' value
if 'start_directory' in sconf:
sconf['start_directory'] = expandshell(sconf['start_directory'])
start_path = sconf['start_directory']
if any(start_path.startswith(a) for a in ['.', './']):
# if window has a session, or pane has a window with a
# start_directory of . or ./, make sure the start_directory can be
# relative to the parent.
#
# This is for the case where you may be loading a config from
# outside your shell current directory.
if parent:
cwd = parent['start_directory']
start_path = os.path.normpath(os.path.join(cwd, start_path))
sconf['start_directory'] = start_path
if 'before_script' in sconf:
sconf['before_script'] = expandshell(sconf['before_script'])
if any(sconf['before_script'].startswith(a) for a in ['.', './']):
sconf['before_script'] = os.path.normpath(
os.path.join(cwd, sconf['before_script'])
)
if (
'shell_command' in sconf and
isinstance(sconf['shell_command'], string_types)
):
sconf['shell_command'] = [sconf['shell_command']]
if (
'shell_command_before' in sconf and
isinstance(sconf['shell_command_before'], string_types)
):
sconf['shell_command_before'] = [sconf['shell_command_before']]
if (
'shell_command_before' in sconf and
isinstance(sconf['shell_command_before'], list)
):
sconf['shell_command_before'] = [
expandshell(scmd) for scmd in sconf['shell_command_before']
]
# recurse into window and pane config items
if 'windows' in sconf:
sconf['windows'] = [
expand(window, parent=sconf) for window in sconf['windows']
]
elif 'panes' in sconf:
for pconf in sconf['panes']:
p_index = sconf['panes'].index(pconf)
p = copy.deepcopy(pconf)
pconf = sconf['panes'][p_index] = {}
if isinstance(p, string_types):
p = {
'shell_command': [p]
}
elif not p:
p = {
'shell_command': []
}
assert isinstance(p, dict)
if 'shell_command' in p:
cmd = p['shell_command']
if isinstance(p['shell_command'], string_types):
cmd = [cmd]
if not cmd or any(a == cmd for a in [None, 'blank', 'pane']):
cmd = []
if isinstance(cmd, list) and len(cmd) == int(1):
if any(a in cmd for a in [None, 'blank', 'pane']):
cmd = []
p['shell_command'] = cmd
else:
p['shell_command'] = []
pconf.update(p)
sconf['panes'] = [
expand(pane, parent=sconf) for pane in sconf['panes']
]
return sconf
def trickle(sconf):
"""Return a dict with "trickled down" / inherited config values.
This will only work if config has been expanded to full form with
:meth:`config.expand`.
tmuxp allows certain commands to be default at the session, window
level. shell_command_before trickles down and prepends the
``shell_command`` for the pane.
:param sconf: the session configuration
:type sconf: dict
:rtype: dict
"""
# prepends a pane's ``shell_command`` list with the window and sessions'
# ``shell_command_before``.
if 'start_directory' in sconf:
session_start_directory = sconf['start_directory']
else:
session_start_directory = None
if 'suppress_history' in sconf:
suppress_history = sconf['suppress_history']
else:
suppress_history = None
for windowconfig in sconf['windows']:
# Prepend start_directory to relative window commands
if session_start_directory:
if 'start_directory' not in windowconfig:
windowconfig['start_directory'] = session_start_directory
else:
if not any(
windowconfig['start_directory'].startswith(a)
for a in ['~', '/']
):
window_start_path = os.path.join(
session_start_directory,
windowconfig['start_directory']
)
windowconfig['start_directory'] = window_start_path
# We only need to trickle to the window, workspace builder checks wconf
if suppress_history is not None:
if 'suppress_history' not in windowconfig:
windowconfig['suppress_history'] = suppress_history
for paneconfig in windowconfig['panes']:
commands_before = []
# Prepend shell_command_before to commands
if 'shell_command_before' in sconf:
commands_before.extend(sconf['shell_command_before'])
if 'shell_command_before' in windowconfig:
commands_before.extend(windowconfig['shell_command_before'])
if 'shell_command_before' in paneconfig:
commands_before.extend(paneconfig['shell_command_before'])
if 'shell_command' in paneconfig:
commands_before.extend(paneconfig['shell_command'])
p_index = windowconfig['panes'].index(paneconfig)
windowconfig['panes'][p_index]['shell_command'] = commands_before
# paneconfig['shell_command'] = commands_before
return sconf
def import_tmuxinator(sconf):
"""Return tmuxp config from a `tmuxinator`_ yaml config.
.. _tmuxinator: https://github.com/aziz/tmuxinator
:param sconf: python dict for session configuration
:type sconf: dict
:rtype: dict
"""
tmuxp_config = {}
if 'project_name' in sconf:
tmuxp_config['session_name'] = sconf.pop('project_name')
elif 'name' in sconf:
tmuxp_config['session_name'] = sconf.pop('name')
else:
tmuxp_config['session_name'] = None
if 'project_root' in sconf:
tmuxp_config['start_directory'] = sconf.pop('project_root')
elif 'root' in sconf:
tmuxp_config['start_directory'] = sconf.pop('root')
if 'cli_args' in sconf:
tmuxp_config['config'] = sconf['cli_args']
if '-f' in tmuxp_config['config']:
tmuxp_config['config'] = tmuxp_config[
'config'
].replace('-f', '').strip()
elif 'tmux_options' in sconf:
tmuxp_config['config'] = sconf['tmux_options']
if '-f' in tmuxp_config['config']:
tmuxp_config['config'] = tmuxp_config[
'config'
].replace('-f', '').strip()
if 'socket_name' in sconf:
tmuxp_config['socket_name'] = sconf['socket_name']
tmuxp_config['windows'] = []
if 'tabs' in sconf:
sconf['windows'] = sconf.pop('tabs')
if 'pre' in sconf and 'pre_window' in sconf:
tmuxp_config['shell_command'] = sconf['pre']
if isinstance(sconf['pre'], string_types):
tmuxp_config['shell_command_before'] = [sconf['pre_window']]
else:
tmuxp_config['shell_command_before'] = sconf['pre_window']
elif 'pre' in sconf:
if isinstance(sconf['pre'], string_types):
tmuxp_config['shell_command_before'] = [sconf['pre']]
else:
tmuxp_config['shell_command_before'] = sconf['pre']
if 'rbenv' in sconf:
if 'shell_command_before' not in tmuxp_config:
tmuxp_config['shell_command_before'] = []
tmuxp_config['shell_command_before'].append(
'rbenv shell %s' % sconf['rbenv']
)
for w in sconf['windows']:
for k, v in w.items():
windowdict = {'window_name': k}
if isinstance(v, string_types) or v is None:
windowdict['panes'] = [v]
tmuxp_config['windows'].append(windowdict)
continue
elif isinstance(v, list):
windowdict['panes'] = v
tmuxp_config['windows'].append(windowdict)
continue
if 'pre' in v:
windowdict['shell_command_before'] = v['pre']
if 'panes' in v:
windowdict['panes'] = v['panes']
if 'root' in v:
windowdict['start_directory'] = v['root']
if 'layout' in v:
windowdict['layout'] = v['layout']
tmuxp_config['windows'].append(windowdict)
return tmuxp_config
def import_teamocil(sconf):
"""Return tmuxp config from a `teamocil`_ yaml config.
.. _teamocil: https://github.com/remiprev/teamocil
:todo: change 'root' to a cd or start_directory
:todo: width in pane -> main-pain-width
:todo: with_env_var
:todo: clear
:todo: cmd_separator
:param sconf: python dict for session configuration
:type sconf: dict
"""
tmuxp_config = {}
if 'session' in sconf:
sconf = sconf['session']
if 'name' in sconf:
tmuxp_config['session_name'] = sconf['name']
else:
tmuxp_config['session_name'] = None
if 'root' in sconf:
tmuxp_config['start_directory'] = sconf.pop('root')
tmuxp_config['windows'] = []
for w in sconf['windows']:
windowdict = {'window_name': w['name']}
if 'clear' in w:
windowdict['clear'] = w['clear']
if 'filters' in w:
if 'before' in w['filters']:
for b in w['filters']['before']:
windowdict['shell_command_before'] = w['filters']['before']
if 'after' in w['filters']:
for b in w['filters']['after']:
windowdict['shell_command_after'] = w['filters']['after']
if 'root' in w:
windowdict['start_directory'] = w.pop('root')
if 'splits' in w:
w['panes'] = w.pop('splits')
if 'panes' in w:
for p in w['panes']:
if 'cmd' in p:
p['shell_command'] = p.pop('cmd')
if 'width' in p:
# todo support for height/width
p.pop('width')
windowdict['panes'] = w['panes']
if 'layout' in w:
windowdict['layout'] = w['layout']
tmuxp_config['windows'].append(windowdict)
return tmuxp_config
|