21
21
"""Contains everything around the config file"""
22
22
from __future__ import print_function
23
23
24
-
24
+ import re
25
25
import yaml
26
+
26
27
from window import Window
27
28
import tmux_wrapper as tmux
28
29
@@ -31,10 +32,12 @@ class Session(object):
31
32
32
33
"""Parser for a config yaml file"""
33
34
34
- def __init__ (self ):
35
+ def __init__ (self , runtime_params = None ):
35
36
"""TODO: to be defined1. """
36
37
37
38
self ._common = dict ()
39
+ self ._parameters = dict ()
40
+ self ._runtime_params = self ._parse_overwrites (runtime_params )
38
41
self ._windows = list ()
39
42
self .__yaml_data = None
40
43
@@ -54,9 +57,10 @@ def init_from_yaml(self, yaml_data):
54
57
55
58
self .__yaml_data = yaml_data
56
59
self ._parse_common ()
60
+ self ._parse_parameters ()
57
61
self ._parse_windows ()
58
62
59
- def run (self ):
63
+ def run (self , debug = False ):
60
64
"""Runs the loaded session"""
61
65
if len (self ._windows ) == 0 :
62
66
print ('No windows to run found' )
@@ -65,6 +69,8 @@ def run(self):
65
69
first = True
66
70
for window in self ._windows :
67
71
window .create (first )
72
+ if debug :
73
+ window .debug ()
68
74
first = False
69
75
70
76
if 'default_window' in self ._common :
@@ -79,13 +85,84 @@ def _parse_common(self):
79
85
if 'common' in self .__yaml_data :
80
86
self ._common = self .__yaml_data ['common' ]
81
87
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
+
82
136
def _parse_windows (self ):
83
137
if self .__yaml_data is None :
84
138
print ('parse_windows was called without yaml data loaded.' )
85
139
raise RuntimeError
86
140
87
141
if 'windows' in self .__yaml_data :
88
142
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
+
89
166
kwargs = dict ()
90
167
if 'before_commands' in self ._common :
91
168
kwargs ['before_commands' ] = self ._common ['before_commands' ]
0 commit comments