27
27
28
28
29
29
if t .TYPE_CHECKING :
30
- from typing_extensions import TypedDict
30
+ from typing_extensions import NotRequired , TypedDict , Unpack
31
31
32
32
class VersionConstraints (TypedDict ):
33
33
version : t .Union [Version , str ]
@@ -40,21 +40,65 @@ class TmuxpPluginVersionConstraints(TypedDict):
40
40
tmuxp : VersionConstraints
41
41
libtmux : VersionConstraints
42
42
43
+ class ConfigSchema (TypedDict ):
44
+ plugin_name : NotRequired [str ]
45
+ tmux_min_version : NotRequired [str ]
46
+ tmux_max_version : NotRequired [str ]
47
+ tmux_version_incompatible : NotRequired [t .List [str ]]
48
+ libtmux_min_version : NotRequired [str ]
49
+ libtmux_max_version : NotRequired [str ]
50
+ libtmux_version_incompatible : NotRequired [t .List [str ]]
51
+ tmuxp_min_version : NotRequired [str ]
52
+ tmuxp_max_version : NotRequired [str ]
53
+ tmuxp_version_incompatible : NotRequired [t .List [str ]]
54
+
55
+
56
+ class Config (t .TypedDict ):
57
+ plugin_name : str
58
+ tmux_min_version : str
59
+ tmux_max_version : t .Optional [str ]
60
+ tmux_version_incompatible : t .Optional [t .List [str ]]
61
+ libtmux_min_version : str
62
+ libtmux_max_version : t .Optional [str ]
63
+ libtmux_version_incompatible : t .Optional [t .List [str ]]
64
+ tmuxp_min_version : str
65
+ tmuxp_max_version : t .Optional [str ]
66
+ tmuxp_version_incompatible : t .Optional [t .List [str ]]
67
+
68
+
69
+ DEFAULT_CONFIG : "Config" = {
70
+ "plugin_name" : "tmuxp-plugin" ,
71
+ "tmux_min_version" : TMUX_MIN_VERSION ,
72
+ "tmux_max_version" : TMUX_MAX_VERSION ,
73
+ "tmux_version_incompatible" : None ,
74
+ "libtmux_min_version" : LIBTMUX_MIN_VERSION ,
75
+ "libtmux_max_version" : LIBTMUX_MAX_VERSION ,
76
+ "libtmux_version_incompatible" : None ,
77
+ "tmuxp_min_version" : TMUXP_MIN_VERSION ,
78
+ "tmuxp_max_version" : TMUXP_MAX_VERSION ,
79
+ "tmuxp_version_incompatible" : None ,
80
+ }
81
+
82
+
83
+ def validate_plugin_config (config : "ConfigSchema" ) -> t .TypeGuard ["Config" ]:
84
+ return isinstance (config , dict )
85
+
86
+
87
+ def setup_plugin_config (
88
+ config : "ConfigSchema" , default_config : "Config" = DEFAULT_CONFIG
89
+ ) -> "Config" :
90
+ new_config = config .copy ()
91
+ for default_key , default_value in default_config .items ():
92
+ if default_key not in new_config :
93
+ new_config [default_key ] = default_value
94
+
95
+ assert validate_plugin_config (new_config )
96
+
97
+ return new_config
98
+
43
99
44
100
class TmuxpPlugin :
45
- def __init__ (
46
- self ,
47
- plugin_name : str = "tmuxp-plugin" ,
48
- tmux_min_version : str = TMUX_MIN_VERSION ,
49
- tmux_max_version : t .Optional [str ] = TMUX_MAX_VERSION ,
50
- tmux_version_incompatible : t .Optional [t .List [str ]] = None ,
51
- libtmux_min_version : str = LIBTMUX_MIN_VERSION ,
52
- libtmux_max_version : t .Optional [str ] = LIBTMUX_MAX_VERSION ,
53
- libtmux_version_incompatible : t .Optional [t .List [str ]] = None ,
54
- tmuxp_min_version : str = TMUXP_MIN_VERSION ,
55
- tmuxp_max_version : t .Optional [str ] = TMUXP_MAX_VERSION ,
56
- tmuxp_version_incompatible : t .Optional [t .List [str ]] = None ,
57
- ) -> None :
101
+ def __init__ (self , ** kwargs : "Unpack[ConfigSchema]" ) -> None :
58
102
"""
59
103
Initialize plugin.
60
104
@@ -95,7 +139,8 @@ def __init__(
95
139
Versions of tmuxp that are incompatible with the plugin
96
140
97
141
"""
98
- self .plugin_name = plugin_name
142
+ config = setup_plugin_config (config = kwargs )
143
+ self .plugin_name = config ["plugin_name" ]
99
144
100
145
# Dependency versions
101
146
self .tmux_version = get_version ()
@@ -105,26 +150,26 @@ def __init__(
105
150
self .version_constraints : "TmuxpPluginVersionConstraints" = {
106
151
"tmux" : {
107
152
"version" : self .tmux_version ,
108
- "vmin" : tmux_min_version ,
109
- "vmax" : tmux_max_version ,
110
- "incompatible" : tmux_version_incompatible
111
- if tmux_version_incompatible
153
+ "vmin" : config [ " tmux_min_version" ] ,
154
+ "vmax" : config [ " tmux_max_version" ] ,
155
+ "incompatible" : config [ " tmux_version_incompatible" ]
156
+ if config [ " tmux_version_incompatible" ]
112
157
else [],
113
158
},
114
159
"libtmux" : {
115
160
"version" : self .libtmux_version ,
116
- "vmin" : libtmux_min_version ,
117
- "vmax" : libtmux_max_version ,
118
- "incompatible" : libtmux_version_incompatible
119
- if libtmux_version_incompatible
161
+ "vmin" : config [ " libtmux_min_version" ] ,
162
+ "vmax" : config [ " libtmux_max_version" ] ,
163
+ "incompatible" : config [ " libtmux_version_incompatible" ]
164
+ if config [ " libtmux_version_incompatible" ]
120
165
else [],
121
166
},
122
167
"tmuxp" : {
123
168
"version" : self .tmuxp_version ,
124
- "vmin" : tmuxp_min_version ,
125
- "vmax" : tmuxp_max_version ,
126
- "incompatible" : tmuxp_version_incompatible
127
- if tmuxp_version_incompatible
169
+ "vmin" : config [ " tmuxp_min_version" ] ,
170
+ "vmax" : config [ " tmuxp_max_version" ] ,
171
+ "incompatible" : config [ " tmuxp_version_incompatible" ]
172
+ if config [ " tmuxp_version_incompatible" ]
128
173
else [],
129
174
},
130
175
}
0 commit comments