Skip to content

Commit 0916646

Browse files
committed
types(workspace-imports) Example typings via NotRequried
1 parent a339934 commit 0916646

File tree

2 files changed

+91
-24
lines changed

2 files changed

+91
-24
lines changed

src/tmuxp/_internal/types.py

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,3 +35,63 @@ class PluginConfigSchema(TypedDict):
3535
tmuxp_min_version: NotRequired[str]
3636
tmuxp_max_version: NotRequired[str]
3737
tmuxp_version_incompatible: NotRequired[list[str]]
38+
39+
40+
class ShellCommandConfig(TypedDict):
41+
"""Shell command configuration."""
42+
43+
cmd: str
44+
enter: NotRequired[bool]
45+
suppress_history: NotRequired[bool]
46+
47+
48+
ShellCommandValue = t.Union[
49+
str, ShellCommandConfig, list[t.Union[str, ShellCommandConfig]]
50+
]
51+
52+
53+
class PaneConfig(TypedDict, total=False):
54+
"""Pane configuration."""
55+
56+
shell_command: NotRequired[ShellCommandValue]
57+
shell_command_before: NotRequired[ShellCommandValue]
58+
start_directory: NotRequired[str]
59+
environment: NotRequired[dict[str, str]]
60+
focus: NotRequired[str | bool]
61+
suppress_history: NotRequired[bool]
62+
63+
64+
PaneValue = t.Union[str, PaneConfig]
65+
66+
67+
class WindowConfig(TypedDict, total=False):
68+
"""Window configuration."""
69+
70+
window_name: str
71+
start_directory: NotRequired[str]
72+
shell_command_before: NotRequired[ShellCommandValue]
73+
layout: NotRequired[str]
74+
options: NotRequired[dict[str, t.Any]]
75+
options_after: NotRequired[dict[str, t.Any]]
76+
environment: NotRequired[dict[str, str]]
77+
focus: NotRequired[str | bool]
78+
suppress_history: NotRequired[bool]
79+
panes: NotRequired[list[PaneValue]]
80+
81+
82+
class WorkspaceConfig(TypedDict, total=False):
83+
"""Complete tmuxp workspace configuration."""
84+
85+
session_name: str | None # Can be None during import
86+
start_directory: NotRequired[str]
87+
before_script: NotRequired[str]
88+
shell_command_before: NotRequired[ShellCommandValue]
89+
shell_command: NotRequired[ShellCommandValue] # Used in import
90+
environment: NotRequired[dict[str, str]]
91+
global_options: NotRequired[dict[str, t.Any]]
92+
options: NotRequired[dict[str, t.Any]]
93+
config: NotRequired[str] # tmux config file path
94+
socket_name: NotRequired[str] # tmux socket name
95+
plugins: NotRequired[list[str | PluginConfigSchema]]
96+
suppress_history: NotRequired[bool]
97+
windows: list[WindowConfig]

src/tmuxp/workspace/importers.py

Lines changed: 31 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,11 @@
44

55
import typing as t
66

7+
if t.TYPE_CHECKING:
8+
from tmuxp._internal.types import WindowConfig, WorkspaceConfig
79

8-
def import_tmuxinator(workspace_dict: dict[str, t.Any]) -> dict[str, t.Any]:
10+
11+
def import_tmuxinator(workspace_dict: dict[str, t.Any]) -> WorkspaceConfig:
912
"""Return tmuxp workspace from a `tmuxinator`_ yaml workspace.
1013
1114
.. _tmuxinator: https://github.com/aziz/tmuxinator
@@ -18,8 +21,9 @@ def import_tmuxinator(workspace_dict: dict[str, t.Any]) -> dict[str, t.Any]:
1821
Returns
1922
-------
2023
dict
24+
A dictionary conforming to WorkspaceConfig structure.
2125
"""
22-
tmuxp_workspace: dict[str, t.Any] = {}
26+
tmuxp_workspace: WorkspaceConfig = {"windows": []} # type: ignore[typeddict-item]
2327

2428
if "project_name" in workspace_dict:
2529
tmuxp_workspace["session_name"] = workspace_dict.pop("project_name")
@@ -51,7 +55,6 @@ def import_tmuxinator(workspace_dict: dict[str, t.Any]) -> dict[str, t.Any]:
5155
if "socket_name" in workspace_dict:
5256
tmuxp_workspace["socket_name"] = workspace_dict["socket_name"]
5357

54-
tmuxp_workspace["windows"] = []
5558

5659
if "tabs" in workspace_dict:
5760
workspace_dict["windows"] = workspace_dict.pop("tabs")
@@ -76,33 +79,33 @@ def import_tmuxinator(workspace_dict: dict[str, t.Any]) -> dict[str, t.Any]:
7679
"rbenv shell {}".format(workspace_dict["rbenv"]),
7780
)
7881

79-
for window_dict in workspace_dict["windows"]:
80-
for k, v in window_dict.items():
81-
window_dict = {"window_name": k}
82+
for window_item in workspace_dict["windows"]:
83+
for k, v in window_item.items():
84+
new_window: WindowConfig = {"window_name": k}
8285

8386
if isinstance(v, str) or v is None:
84-
window_dict["panes"] = [v]
85-
tmuxp_workspace["windows"].append(window_dict)
87+
new_window["panes"] = [v]
88+
tmuxp_workspace["windows"].append(new_window)
8689
continue
8790
if isinstance(v, list):
88-
window_dict["panes"] = v
89-
tmuxp_workspace["windows"].append(window_dict)
91+
new_window["panes"] = v
92+
tmuxp_workspace["windows"].append(new_window)
9093
continue
9194

9295
if "pre" in v:
93-
window_dict["shell_command_before"] = v["pre"]
96+
new_window["shell_command_before"] = v["pre"]
9497
if "panes" in v:
95-
window_dict["panes"] = v["panes"]
98+
new_window["panes"] = v["panes"]
9699
if "root" in v:
97-
window_dict["start_directory"] = v["root"]
100+
new_window["start_directory"] = v["root"]
98101

99102
if "layout" in v:
100-
window_dict["layout"] = v["layout"]
101-
tmuxp_workspace["windows"].append(window_dict)
103+
new_window["layout"] = v["layout"]
104+
tmuxp_workspace["windows"].append(new_window)
102105
return tmuxp_workspace
103106

104107

105-
def import_teamocil(workspace_dict: dict[str, t.Any]) -> dict[str, t.Any]:
108+
def import_teamocil(workspace_dict: dict[str, t.Any]) -> WorkspaceConfig:
106109
"""Return tmuxp workspace from a `teamocil`_ yaml workspace.
107110
108111
.. _teamocil: https://github.com/remiprev/teamocil
@@ -112,6 +115,11 @@ def import_teamocil(workspace_dict: dict[str, t.Any]) -> dict[str, t.Any]:
112115
workspace_dict : dict
113116
python dict for tmuxp workspace
114117
118+
Returns
119+
-------
120+
dict
121+
A dictionary conforming to WorkspaceConfig structure.
122+
115123
Notes
116124
-----
117125
Todos:
@@ -122,7 +130,7 @@ def import_teamocil(workspace_dict: dict[str, t.Any]) -> dict[str, t.Any]:
122130
- clear
123131
- cmd_separator
124132
"""
125-
tmuxp_workspace: dict[str, t.Any] = {}
133+
tmuxp_workspace: WorkspaceConfig = {"windows": []} # type: ignore[typeddict-item]
126134

127135
if "session" in workspace_dict:
128136
workspace_dict = workspace_dict["session"]
@@ -132,21 +140,20 @@ def import_teamocil(workspace_dict: dict[str, t.Any]) -> dict[str, t.Any]:
132140
if "root" in workspace_dict:
133141
tmuxp_workspace["start_directory"] = workspace_dict.pop("root")
134142

135-
tmuxp_workspace["windows"] = []
136143

137144
for w in workspace_dict["windows"]:
138-
window_dict = {"window_name": w["name"]}
145+
window_dict: WindowConfig = {"window_name": w["name"]}
139146

140147
if "clear" in w:
141-
window_dict["clear"] = w["clear"]
148+
# TODO: handle clear attribute
149+
pass
142150

143151
if "filters" in w:
144152
if "before" in w["filters"]:
145-
for _b in w["filters"]["before"]:
146-
window_dict["shell_command_before"] = w["filters"]["before"]
153+
window_dict["shell_command_before"] = w["filters"]["before"]
147154
if "after" in w["filters"]:
148-
for _b in w["filters"]["after"]:
149-
window_dict["shell_command_after"] = w["filters"]["after"]
155+
# TODO: handle shell_command_after
156+
pass
150157

151158
if "root" in w:
152159
window_dict["start_directory"] = w.pop("root")

0 commit comments

Comments
 (0)