Skip to content

Commit a6da9d8

Browse files
committed
added parameters :)
1 parent 84464b2 commit a6da9d8

File tree

5 files changed

+94
-8
lines changed

5 files changed

+94
-8
lines changed

etc/example_session.yaml

+8-1
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,17 @@ common:
33
before_commands:
44
- fzirob change_environment # before commands can be specified global and per window
55
default_window: README # This window will be visible when first attaching to the session
6+
parameters:
7+
show_layouts: false
8+
replacement_param: world\!
69
windows:
710
- name: roscore
811
splits:
912
- commands:
1013
- roscore
1114
delay: 1 # Use this to wait before starting the next window
1215
- name: tiled
16+
if: show_layouts
1317
layout: tiled
1418
splits:
1519
- commands:
@@ -24,13 +28,15 @@ windows:
2428
- commands:
2529
- echo "4"
2630
- name: left-right
31+
if: show_layouts
2732
layout: even-vertical
2833
splits:
2934
- commands:
3035
- echo "left"
3136
- commands:
3237
- echo "right"
3338
- name: top-bottom
39+
if: show_layouts
3440
layout: even-horizontal
3541
splits:
3642
- commands:
@@ -42,8 +48,9 @@ windows:
4248
- clear && tput bold && tput setaf 6 && roscat catmux readme_tmux.txt
4349
- tput sgr0
4450
- name: no_split
51+
unless: show_layouts
4552
# If just one split is desired, just skip it
4653
commands:
47-
- echo "hello"
54+
- echo "hello ${replacement_param}"
4855

4956

script/create_session

+5-1
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,10 @@ def parse_arguments(debug=False):
4949
help="This config will be used for the tmux session")
5050
parser.add_argument('-d', '--detach', action='store_true',
5151
help="Start session in detached mode")
52+
parser.add_argument('--overwrite',
53+
help="Overwrite a parameter from the session config. Parameters can be "
54+
"specified using a comma-separated list such as '--overwrite "
55+
"param_a=1,param_b=2'.")
5256

5357
args = parser.parse_args()
5458
if debug:
@@ -60,7 +64,7 @@ def main():
6064
"""Creates a new tmux session if it does not yet exist"""
6165
args = parse_arguments()
6266

63-
session_config = CatmuxSession()
67+
session_config = CatmuxSession(args.overwrite)
6468
session_config.init_from_filepath(args.session_config)
6569

6670
try:

src/catmux/session.py

+80-3
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,9 @@
2121
"""Contains everything around the config file"""
2222
from __future__ import print_function
2323

24-
24+
import re
2525
import yaml
26+
2627
from window import Window
2728
import tmux_wrapper as tmux
2829

@@ -31,10 +32,12 @@ class Session(object):
3132

3233
"""Parser for a config yaml file"""
3334

34-
def __init__(self):
35+
def __init__(self, runtime_params=None):
3536
"""TODO: to be defined1. """
3637

3738
self._common = dict()
39+
self._parameters = dict()
40+
self._runtime_params = self._parse_overwrites(runtime_params)
3841
self._windows = list()
3942
self.__yaml_data = None
4043

@@ -54,9 +57,10 @@ def init_from_yaml(self, yaml_data):
5457

5558
self.__yaml_data = yaml_data
5659
self._parse_common()
60+
self._parse_parameters()
5761
self._parse_windows()
5862

59-
def run(self):
63+
def run(self, debug=False):
6064
"""Runs the loaded session"""
6165
if len(self._windows) == 0:
6266
print('No windows to run found')
@@ -65,6 +69,8 @@ def run(self):
6569
first = True
6670
for window in self._windows:
6771
window.create(first)
72+
if debug:
73+
window.debug()
6874
first = False
6975

7076
if 'default_window' in self._common:
@@ -79,13 +85,84 @@ def _parse_common(self):
7985
if 'common' in self.__yaml_data:
8086
self._common = self.__yaml_data['common']
8187

88+
def _parse_overwrites(self, data_string):
89+
"""Separates a comma-separated list of foo=val1,bar=val2 into a dictionary."""
90+
if data_string is None:
91+
return None
92+
93+
overwrites = dict()
94+
param_list = data_string.split(',')
95+
for param in param_list:
96+
key, value = param.split('=')
97+
overwrites[key] = value
98+
99+
return overwrites
100+
101+
def _parse_parameters(self):
102+
if self.__yaml_data is None:
103+
print('parse_parameters was called without yaml data loaded.')
104+
raise RuntimeError
105+
if 'parameters' in self.__yaml_data:
106+
self._parameters = self.__yaml_data['parameters']
107+
108+
print('Parameters found in session config:')
109+
print(' - ' + '\n - '.join('{} = {}'.format(key, value)
110+
for key, value in self._parameters.items()))
111+
if self._runtime_params:
112+
print('Parameters found during runtime (overwrites):')
113+
print(' - ' + '\n - '.join('{} = {}'.format(key, value)
114+
for key, value in self._runtime_params.items()))
115+
# Overwrite parameters given from command line
116+
self._parameters.update(self._runtime_params)
117+
118+
119+
self._replace_parameters(self.__yaml_data)
120+
121+
def _replace_parameters(self, data):
122+
if isinstance(data, dict):
123+
for key, value in data.items():
124+
data[key] = self._replace_parameters(value)
125+
elif isinstance(data, list):
126+
for index, item in enumerate(data):
127+
data[index] = self._replace_parameters(item)
128+
elif isinstance(data, str):
129+
for key, value in self._parameters.items():
130+
if isinstance(value, str):
131+
# print('replacing {} in {}'.format(key, data))
132+
data = re.sub(r"\${%s}"%(key), value, data)
133+
return data
134+
135+
82136
def _parse_windows(self):
83137
if self.__yaml_data is None:
84138
print('parse_windows was called without yaml data loaded.')
85139
raise RuntimeError
86140

87141
if 'windows' in self.__yaml_data:
88142
for window in self.__yaml_data['windows']:
143+
if 'if' in window:
144+
print('Detected of condition for window ' + window['name'])
145+
if window['if'] not in self._parameters:
146+
print('Skipping window ' + window['name'] + ' because parameter ' +
147+
window['if'] + ' was not found.')
148+
continue
149+
elif not self._parameters[window['if']]:
150+
print('Skipping window ' + window['name'] + ' because parameter ' +
151+
window['if'] + ' is switched off globally')
152+
continue
153+
else:
154+
print('condition fulfilled: {} == {}'
155+
.format(window['if'], self._parameters[window['if']]))
156+
if 'unless' in window:
157+
print('Detected unless condition for window ' + window['name'])
158+
if self._parameters[window['unless']]:
159+
print('Skipping window ' + window['name'] + ' because parameter ' +
160+
window['unless'] + ' is switched on globally')
161+
continue
162+
else:
163+
print('condition fulfilled: {} == {}'
164+
.format(window['unless'], self._parameters[window['unless']]))
165+
89166
kwargs = dict()
90167
if 'before_commands' in self._common:
91168
kwargs['before_commands'] = self._common['before_commands']

src/catmux/tmux_wrapper.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ def tmux_call(command_list):
3636
"""Executes a tmux command """
3737
tmux_cmd = ['tmux'] + command_list
3838

39-
print(' '.join(tmux_cmd))
39+
# print(' '.join(tmux_cmd))
4040
_safe_call(tmux_cmd)
4141

4242
def _safe_call(cmd_list):

src/catmux/window.py

-2
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,6 @@ def __init__(self, **kwargs):
4848
for key, value in kwargs.iteritems():
4949
setattr(self, key, value)
5050

51-
self.debug()
52-
5351

5452
def debug(self):
5553
"""Prints all information about this window"""

0 commit comments

Comments
 (0)