Skip to content

Commit

Permalink
Added conditional_before_commands
Browse files Browse the repository at this point in the history
Signed-off-by: Niklas Spielbauer <[email protected]>
  • Loading branch information
nspielbau committed Dec 18, 2024
1 parent 4a12eec commit de4d9b3
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 2 deletions.
21 changes: 19 additions & 2 deletions catmux/session.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,8 @@ def init_from_yaml(self, yaml_data):
"""Initialize config directly by an already loaded yaml structure."""

self.__yaml_data = yaml_data
self._parse_common()
self._parse_parameters()
self._parse_common()
self._parse_windows()

def run(self, parent_server: libtmux.Server, debug=False):
Expand Down Expand Up @@ -101,11 +101,28 @@ def _parse_common(self):
if "common" in self.__yaml_data and self.__yaml_data["common"]:
common = self.__yaml_data["common"]
if "before_commands" in common and common["before_commands"]:
self._before_commands = common["before_commands"]
# self._before_commands = common["before_commands"]
self._parse_before_commands(common["before_commands"])

if "default_window" in common and common["default_window"]:
self._default_window = common["default_window"]


def _parse_before_commands(self, commands):
command_list = []
for command in commands:
if isinstance(command, str):
command_list.append(command)
elif isinstance(command, dict):
conditional_param = command.get('if')
print(conditional_param)
if conditional_param not in self._parameters:
raise RuntimeError
elif self._parameters[conditional_param]:
command_list.append(command)
self._before_commands = command_list


def _parse_overwrites(self, data_string):
"""Separates a comma-separated list of foo=val1,bar=val2 into a dictionary."""
if data_string is None:
Expand Down
23 changes: 23 additions & 0 deletions test/test_parsing.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,29 @@ def test_empty_before_commands():
assert session._default_window == None


def test_conditional_before_commands():
CONFIG = """common:
before_commands:
- if: param_true
command: echo "true"
- if: param_false_
command: echo "false"
windows:
- name: left-right
splits:
- commands:
- echo "left"
- name: foo
splits:
- commands:
- echo "left"
"""
session = Session("name")
session.init_from_yaml(yaml.safe_load(CONFIG))

assert len(session._before_commands) == 1


def test_missing_parameter_for_if_unless():
CONFIG = """common:
before_commands:
Expand Down

0 comments on commit de4d9b3

Please sign in to comment.