Skip to content

Commit 2f3d9d0

Browse files
committed
tmuxp freeze
1 parent 7a5b978 commit 2f3d9d0

File tree

3 files changed

+124
-43
lines changed

3 files changed

+124
-43
lines changed

tmuxp/cli.py

Lines changed: 82 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,13 @@
1818
import argparse
1919
import argcomplete
2020
import logging
21+
import pkg_resources
2122
import kaptan
22-
from . import config
23-
from distutils.util import strtobool
24-
from . import log, util, exc, WorkspaceBuilder, Server
23+
from . import log, util, exc, WorkspaceBuilder, Server, config
2524
from .util import ascii_lowercase, input
26-
import pkg_resources
25+
from .workspacebuilder import freeze
26+
from distutils.util import strtobool
27+
2728

2829
__version__ = pkg_resources.require("tmuxp")[0].version
2930

@@ -281,6 +282,70 @@ def load_workspace(config_file, args):
281282
sys.exit()
282283

283284

285+
def command_freeze(args):
286+
""" Import teamocil config to tmuxp format. """
287+
288+
t = Server(
289+
socket_name=args.socket_name,
290+
socket_path=args.socket_path
291+
)
292+
293+
logger.error(args)
294+
session = t.findWhere({
295+
'session_name': args.session_name
296+
})
297+
298+
sconf = freeze(session)
299+
configparser = kaptan.Kaptan()
300+
newconfig = config.inline(sconf)
301+
configparser.import_config(newconfig)
302+
config_format = prompt_choices('Convert to', choices=[
303+
'yaml', 'json'], default='yaml')
304+
305+
if config_format == 'yaml':
306+
newconfig = configparser.export(
307+
'yaml', indent=2, default_flow_style=False, safe=True
308+
)
309+
elif config_format == 'json':
310+
newconfig = configparser.export('json', indent=2)
311+
else:
312+
sys.exit('Unknown config format.')
313+
314+
print(newconfig)
315+
print(
316+
'---------------------------------------------------------------')
317+
print(
318+
'Configuration import does its best to convert teamocil files.\n')
319+
if prompt_yes_no(
320+
'The new config *WILL* require adjusting afterwards. Save config?'
321+
):
322+
dest = None
323+
while not dest:
324+
dest_prompt = prompt('Save to: ', os.path.abspath(
325+
os.path.join(config_dir, 'myimport.%s' % config_format)))
326+
if os.path.exists(dest_prompt):
327+
print('%s exists. Pick a new filename.' % dest_prompt)
328+
continue
329+
330+
dest = dest_prompt
331+
332+
dest = os.path.abspath(os.path.relpath(dest))
333+
if prompt_yes_no('Write to %s?' % dest):
334+
buf = open(dest, 'w')
335+
buf.write(newconfig)
336+
buf.close()
337+
338+
print('Saved to %s.' % dest)
339+
else:
340+
print(
341+
'tmuxp has examples in JSON and YAML format at <http://tmuxp.readthedocs.org/en/latest/examples.html>\n'
342+
'View tmuxp docs at <http://tmuxp.readthedocs.org/>'
343+
)
344+
sys.exit()
345+
346+
347+
348+
284349
def command_load(args):
285350
""" Load a session from a tmuxp session file. """
286351
if args.list:
@@ -542,6 +607,9 @@ def command_convert(args):
542607
print('written new config to <%s>.' % (newfile))
543608

544609

610+
611+
612+
545613
def command_attach_session(args):
546614
""" Command to attach / switch client to a tmux session."""
547615
commands = []
@@ -624,6 +692,14 @@ def get_parser():
624692
type=str,
625693
).completer = SessionCompleter
626694

695+
freeze = subparsers.add_parser('freeze')
696+
freeze.set_defaults(callback=command_freeze)
697+
698+
freeze.add_argument(
699+
dest='session_name',
700+
type=str,
701+
).completer = SessionCompleter
702+
627703
load = subparsers.add_parser('load')
628704

629705
loadgroup = load.add_mutually_exclusive_group(required=True)
@@ -758,6 +834,8 @@ def main():
758834
command_import_teamocil(args)
759835
elif args.callback is command_import_tmuxinator:
760836
command_import_tmuxinator(args)
837+
elif args.callback is command_freeze:
838+
command_freeze(args)
761839
elif args.callback is command_attach_session:
762840
command_attach_session(args)
763841
elif args.callback is command_kill_session:

tmuxp/testsuite/test_workspacefreezer.py

Lines changed: 8 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,13 @@
22
from __future__ import absolute_import, division, print_function, with_statement
33

44
import os
5+
import sys
56
import unittest
67
import logging
78
import time
89
import kaptan
910
from .. import Window, config, exc
10-
from ..workspacebuilder import WorkspaceBuilder
11+
from ..workspacebuilder import WorkspaceBuilder, freeze
1112
from .helpers import TmuxTestCase
1213

1314
logger = logging.getLogger(__name__)
@@ -16,41 +17,6 @@
1617
example_dir = os.path.abspath(os.path.join(current_dir, '..', '..'))
1718

1819

19-
def freeza(session):
20-
sconf = {}
21-
22-
sconf['session_name'] = session['session_name']
23-
24-
sconf['windows'] = []
25-
for w in session.windows:
26-
wconf = {}
27-
wconf['options'] = w.show_window_options()
28-
wconf['window_name'] = w.get('window_name')
29-
wconf['panes'] = []
30-
logger.error(w)
31-
logger.error(dict(w))
32-
33-
for p in w.panes:
34-
pconf = {}
35-
pconf['shell_command'] = []
36-
pconf['shell_command'].append('cd ' + p.get('pane_current_path'))
37-
pconf['shell_command'].append(p.get('pane_current_command'))
38-
wconf['panes'].append(pconf)
39-
logger.error(p)
40-
logger.error(dict(p))
41-
42-
43-
sconf['windows'].append(wconf)
44-
45-
logger.error(sconf)
46-
47-
return sconf
48-
49-
50-
51-
52-
53-
5420
class FreezeTest(TmuxTestCase):
5521

5622
yaml_config = '''
@@ -77,7 +43,11 @@ class FreezeTest(TmuxTestCase):
7743
- htop
7844
'''
7945

80-
def test_split_windows(self):
46+
def test_focus(self):
47+
# assure the built yaml config has focus
48+
pass
49+
50+
def test_freeze_config(self):
8151
sconfig = kaptan.Kaptan(handler='yaml')
8252
sconfig = sconfig.import_config(self.yaml_config).get()
8353

@@ -89,7 +59,7 @@ def test_split_windows(self):
8959
time.sleep(1)
9060

9161
session = self.session
92-
sconf = freeza(session)
62+
sconf = freeze(session)
9363

9464
config.check_consistency(sconf)
9565

@@ -102,7 +72,6 @@ def test_split_windows(self):
10272
yaml = kaptanconf.export(
10373
'yaml', indent=2, default_flow_style=False, safe=True)
10474

105-
10675
logger.error(json)
10776
logger.error(yaml)
10877

tmuxp/workspacebuilder.py

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -215,3 +215,37 @@ def iter_create_panes(self, w, wconf):
215215
w.server._update_panes()
216216

217217
yield p
218+
219+
220+
def freeze(session):
221+
sconf = {}
222+
223+
sconf['session_name'] = session['session_name']
224+
225+
sconf['windows'] = []
226+
for w in session.windows:
227+
wconf = {}
228+
wconf['options'] = w.show_window_options()
229+
wconf['window_name'] = w.get('window_name')
230+
wconf['panes'] = []
231+
logger.error(w)
232+
logger.error(dict(w))
233+
234+
if all(w.panes[0].get('pane_current_path') == p.get('pane_current_path') for p in w.panes):
235+
wconf['shell_command_before'] = w.panes[0].get('pane_current_path')
236+
237+
for p in w.panes:
238+
pconf = {}
239+
pconf['shell_command'] = []
240+
if 'shell_command_before' not in wconf:
241+
pconf['shell_command'].append('cd ' + p.get('pane_current_path'))
242+
pconf['shell_command'].append(p.get('pane_current_command'))
243+
wconf['panes'].append(pconf)
244+
logger.error(p)
245+
logger.error(dict(p))
246+
247+
248+
sconf['windows'].append(wconf)
249+
250+
return sconf
251+

0 commit comments

Comments
 (0)