From e71c5541457f3d5c78c9359e84f771765fc5d2b8 Mon Sep 17 00:00:00 2001 From: Niklas Spielbauer Date: Mon, 13 Jan 2025 13:10:12 +0100 Subject: [PATCH 1/5] Added unified parsing Signed-off-by: Niklas Spielbauer --- catmux/session.py | 3 ++- catmux/split.py | 12 +++++++----- catmux/utils.py | 30 ++++++++++++++++++++++++++++++ catmux/window.py | 6 ++---- 4 files changed, 41 insertions(+), 10 deletions(-) create mode 100644 catmux/utils.py diff --git a/catmux/session.py b/catmux/session.py index 5cea1aa..c0cce20 100644 --- a/catmux/session.py +++ b/catmux/session.py @@ -28,6 +28,7 @@ import libtmux import yaml +from catmux.utils import parse_commands from catmux.window import Window import catmux.exceptions as cme @@ -101,7 +102,7 @@ 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 = parse_commands(common["before_commands"]) if "default_window" in common and common["default_window"]: self._default_window = common["default_window"] diff --git a/catmux/split.py b/catmux/split.py index 2b2191e..91af596 100644 --- a/catmux/split.py +++ b/catmux/split.py @@ -27,17 +27,19 @@ import libtmux +from catmux.utils import parse_commands + class Split(object): """A split is a pane where commands can be executed""" - def __init__(self, **kwargs): + def __init__(self, *args): """TODO: to be defined1.""" + self.commands = list() - if kwargs is not None: - for (key, value) in kwargs.items(): - setattr(self, key, value) + if args is not None: + self.commands = parse_commands(args) def debug(self, name="", prefix=""): """Prints all information about this window""" @@ -48,5 +50,5 @@ def debug(self, name="", prefix=""): def run(self, parent_pane: libtmux.Pane): "Executes all configured commands" "" - for command in getattr(self, "commands"): + for command in self.commands: parent_pane.send_keys(command) diff --git a/catmux/utils.py b/catmux/utils.py new file mode 100644 index 0000000..aa6f768 --- /dev/null +++ b/catmux/utils.py @@ -0,0 +1,30 @@ +# -- BEGIN LICENSE BLOCK ---------------------------------------------- + +# catmux +# Copyright (C) 2018 Felix Mauch +# MIT License +# +# Permission is hereby granted, free of charge, to any person obtaining a copy +# of this software and associated documentation files (the "Software"), to deal +# in the Software without restriction, including without limitation the rights +# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +# copies of the Software, and to permit persons to whom the Software is +# furnished to do so, subject to the following conditions: +# +# The above copyright notice and this permission notice shall be included in all +# copies or substantial portions of the Software. +# +# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +# SOFTWARE. +# -- END LICENSE BLOCK ------------------------------------------------ + +"""General utility functions for catmux""" + + +def parse_commands(command_list): + return command_list diff --git a/catmux/window.py b/catmux/window.py index 5bdffc4..9124000 100644 --- a/catmux/window.py +++ b/catmux/window.py @@ -42,9 +42,7 @@ def __init__(self, **kwargs): split_list = kwargs.pop("splits", None) if not split_list: if "commands" in kwargs: - split_dict = dict() - split_dict["commands"] = kwargs.pop("commands") - split_list = [split_dict] + split_list = [kwargs.pop("commands")] else: raise InvalidConfig( f"No splits and no commands given for window '{kwargs['name']}'." @@ -54,7 +52,7 @@ def __init__(self, **kwargs): self.splits = list() for split_data in split_list: - self.splits.append(Split(**split_data)) + self.splits.append(Split(*split_data["commands"])) if kwargs is not None: for (key, value) in kwargs.items(): From 0b7dc320d6647334d959f4bedb27cc0cc8c91150 Mon Sep 17 00:00:00 2001 From: Niklas Spielbauer Date: Mon, 13 Jan 2025 13:56:36 +0100 Subject: [PATCH 2/5] Made unified parsing work Signed-off-by: Niklas Spielbauer --- catmux/session.py | 2 +- catmux/split.py | 2 +- catmux/utils.py | 4 ++-- catmux/window.py | 24 +++++++++++++----------- 4 files changed, 17 insertions(+), 15 deletions(-) diff --git a/catmux/session.py b/catmux/session.py index c0cce20..9d5468b 100644 --- a/catmux/session.py +++ b/catmux/session.py @@ -102,7 +102,7 @@ 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 = parse_commands(common["before_commands"]) + self._before_commands = parse_commands(common, "before_commands") if "default_window" in common and common["default_window"]: self._default_window = common["default_window"] diff --git a/catmux/split.py b/catmux/split.py index 91af596..1c5fcb6 100644 --- a/catmux/split.py +++ b/catmux/split.py @@ -39,7 +39,7 @@ def __init__(self, *args): self.commands = list() if args is not None: - self.commands = parse_commands(args) + self.commands = args def debug(self, name="", prefix=""): """Prints all information about this window""" diff --git a/catmux/utils.py b/catmux/utils.py index aa6f768..2095670 100644 --- a/catmux/utils.py +++ b/catmux/utils.py @@ -26,5 +26,5 @@ """General utility functions for catmux""" -def parse_commands(command_list): - return command_list +def parse_commands(command_dict, key): + return command_dict[key] diff --git a/catmux/window.py b/catmux/window.py index 9124000..7977dc5 100644 --- a/catmux/window.py +++ b/catmux/window.py @@ -30,6 +30,7 @@ from catmux.split import Split from catmux.exceptions import InvalidConfig +from catmux.utils import parse_commands class Window(object): @@ -40,19 +41,20 @@ def __init__(self, **kwargs): """TODO: to be defined1.""" split_list = kwargs.pop("splits", None) - if not split_list: - if "commands" in kwargs: - split_list = [kwargs.pop("commands")] - else: - raise InvalidConfig( - f"No splits and no commands given for window '{kwargs['name']}'." - ) - - print(split_list) + split_command_list = list() + if split_list: + for split_data in split_list: + split_command_list.append(parse_commands(split_data, "commands")) + elif not split_list and "commands" in kwargs: + split_command_list.append(parse_commands(kwargs, "commands")) + else: + raise InvalidConfig( + f"No splits and no commands given for window '{kwargs['name']}'." + ) self.splits = list() - for split_data in split_list: - self.splits.append(Split(*split_data["commands"])) + for split_data in split_command_list: + self.splits.append(Split(*split_data)) if kwargs is not None: for (key, value) in kwargs.items(): From 6cfb73f5fd13e9874fd0d2c859a6f6fcaf9d8665 Mon Sep 17 00:00:00 2001 From: Niklas Spielbauer Date: Tue, 4 Feb 2025 08:44:09 +0100 Subject: [PATCH 3/5] Fixed Copyright Signed-off-by: Niklas Spielbauer --- catmux/utils.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/catmux/utils.py b/catmux/utils.py index 2095670..e97807c 100644 --- a/catmux/utils.py +++ b/catmux/utils.py @@ -1,7 +1,7 @@ # -- BEGIN LICENSE BLOCK ---------------------------------------------- # catmux -# Copyright (C) 2018 Felix Mauch +# Copyright (C) 2025 Felix Exner # MIT License # # Permission is hereby granted, free of charge, to any person obtaining a copy From f0ae6418a56630deb21632415539e82f8f451827 Mon Sep 17 00:00:00 2001 From: Niklas Spielbauer Date: Tue, 4 Feb 2025 08:45:36 +0100 Subject: [PATCH 4/5] Switched to command arg instead of *args Signed-off-by: Niklas Spielbauer --- catmux/split.py | 6 +++--- catmux/window.py | 2 +- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/catmux/split.py b/catmux/split.py index 1c5fcb6..fc10adf 100644 --- a/catmux/split.py +++ b/catmux/split.py @@ -34,12 +34,12 @@ class Split(object): """A split is a pane where commands can be executed""" - def __init__(self, *args): + def __init__(self, commands): """TODO: to be defined1.""" self.commands = list() - if args is not None: - self.commands = args + if commands is not None: + self.commands = commands def debug(self, name="", prefix=""): """Prints all information about this window""" diff --git a/catmux/window.py b/catmux/window.py index 7977dc5..8440362 100644 --- a/catmux/window.py +++ b/catmux/window.py @@ -54,7 +54,7 @@ def __init__(self, **kwargs): self.splits = list() for split_data in split_command_list: - self.splits.append(Split(*split_data)) + self.splits.append(Split(split_data)) if kwargs is not None: for (key, value) in kwargs.items(): From 50340c16300c0288510b78c98f79f3dde1850e97 Mon Sep 17 00:00:00 2001 From: Niklas Spielbauer Date: Tue, 4 Feb 2025 08:46:15 +0100 Subject: [PATCH 5/5] Added empty split check to skip instead of crash Signed-off-by: Niklas Spielbauer --- catmux/window.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/catmux/window.py b/catmux/window.py index 8440362..e00adba 100644 --- a/catmux/window.py +++ b/catmux/window.py @@ -54,7 +54,10 @@ def __init__(self, **kwargs): self.splits = list() for split_data in split_command_list: - self.splits.append(Split(split_data)) + if hasattr(split_data, "__iter__"): + self.splits.append(Split(split_data)) + else: + print("Split is empty, dropping") if kwargs is not None: for (key, value) in kwargs.items():