5
5
import os
6
6
import sys
7
7
import tempfile
8
+ from dataclasses import dataclass
9
+ from enum import Enum , auto
8
10
from importlib .metadata import PackageNotFoundError , version
9
11
from pathlib import Path
10
12
from types import ModuleType
@@ -32,6 +34,17 @@ class PluginError(Exception):
32
34
pass
33
35
34
36
37
+ class ParamStrategy (Enum ):
38
+ POSITIONAL = auto ()
39
+ NAMED = auto ()
40
+
41
+
42
+ @dataclass
43
+ class Params :
44
+ params : list | dict
45
+ type : ParamStrategy
46
+
47
+
35
48
hook_registry : set [Callable [..., Any ]] = set ()
36
49
imported_modules : dict [str , ModuleType ] = {}
37
50
@@ -103,24 +116,40 @@ def toggle(plugin: str):
103
116
@plugin .command ()
104
117
@click .argument ("plugin_name" , type = str , default = "" )
105
118
@click .argument ("function_name" , type = str , default = "" )
106
- @click .option ("--params" , type = str , default = "" )
107
- @click .option ("--json-input" , type = str , default = "" )
108
- def run (plugin_name : str , function_name : str , params : str , json_input : str ):
109
- """Explore and run plugins"""
119
+ @click .option (
120
+ "--params" , type = str , default = "" , help = "Paramter data to be fed to the plugin function"
121
+ )
122
+ def run (plugin_name : str , function_name : str , params : str ):
123
+ """Explore and run plugins
124
+
125
+ Use `--params` to pass a JSON list for positional arguments or a JSON object for named arguments.
126
+
127
+ Like this:
128
+
129
+ Positional - '["first element", 2, 3.0]'
130
+
131
+ Named - '{"first": "first_element", "second": 2, "third": 3.0}'
132
+ """
110
133
show_explainer = False
111
134
112
135
plugin_dir = _get_plugin_directory ()
113
136
if plugin_dir is None :
114
137
direct_user_to_plugin_directory_and_exit ()
115
138
116
139
plugins = get_plugins_with_status (plugin_dir )
140
+ plugin_was_found = False
117
141
for plugin_path , status in plugins :
142
+ if plugin_path .stem == plugin_name :
143
+ plugin_was_found = True
118
144
if plugin_path .stem == plugin_name and not status :
119
145
click .secho (f"The plugin '{ plugin_path .stem } ' is not enabled" , fg = "yellow" )
120
146
click .secho ("Please toggle it on to use it." )
121
147
sys .exit (0 )
148
+ if plugin_name and not plugin_was_found :
149
+ click .secho (f"The plugin '{ plugin_name } ' was not found." , fg = "yellow" )
150
+ sys .exit (0 )
122
151
123
- if plugin_name == "" and sys . stdin . isatty () :
152
+ if plugin_name == "" :
124
153
show_explainer = True
125
154
plugin_names = [
126
155
plugin_name .stem for plugin_name , status in get_plugins_with_status () if status
@@ -132,7 +161,7 @@ def run(plugin_name: str, function_name: str, params: str, json_input: str):
132
161
sys .exit (0 )
133
162
plugin_name = plugin_answer .get ("plugin" )
134
163
135
- if function_name == "" and sys . stdin . isatty () :
164
+ if function_name == "" :
136
165
show_explainer = True
137
166
module = imported_modules .get (f"plugins.{ plugin_name } " )
138
167
funcs = [
@@ -152,20 +181,7 @@ def run(plugin_name: str, function_name: str, params: str, json_input: str):
152
181
if not func :
153
182
sys .exit (0 )
154
183
155
- if params :
156
- print (params )
157
- params = json .loads (params )
158
- try :
159
- return_value = func (* params )
160
- if return_value is not None :
161
- jsonified = json .dumps (return_value )
162
- print (f"'{ jsonified } '" )
163
- sys .exit (0 )
164
- except Exception as e :
165
- click .secho (f"Exception: { e } " , fg = "yellow" )
166
- sys .exit (1 )
167
-
168
- if not json_input and not params :
184
+ if not params :
169
185
params = {}
170
186
sig = inspect .signature (func )
171
187
for name , param in sig .parameters .items ():
@@ -207,18 +223,35 @@ def run(plugin_name: str, function_name: str, params: str, json_input: str):
207
223
f"\n warnet plugin run { plugin_name } { function_name } --json-input '{ json .dumps (params )} '" ,
208
224
fg = "green" ,
209
225
)
210
-
211
226
else :
212
- params = json .loads (json_input )
227
+ params = json .loads (params )
213
228
214
- try :
215
- return_value = func (** params )
216
- if return_value is not None :
217
- jsonified = json .dumps (return_value )
218
- print (f"'{ jsonified } '" )
219
- except Exception as e :
220
- click .secho (f"Exception: { e } " , fg = "yellow" )
221
- sys .exit (1 )
229
+ execute_function_with_params (func , params )
230
+
231
+
232
+ def execute_function_with_params (func : Callable [..., Any ], params : dict | list ):
233
+ match params :
234
+ case dict ():
235
+ try :
236
+ return_value = func (** params )
237
+ if return_value is not None :
238
+ jsonified = json .dumps (return_value )
239
+ print (f"'{ jsonified } '" )
240
+ except Exception as e :
241
+ click .secho (f"Exception: { e } " , fg = "yellow" )
242
+ sys .exit (1 )
243
+ case list ():
244
+ try :
245
+ return_value = func (* params )
246
+ if return_value is not None :
247
+ jsonified = json .dumps (return_value )
248
+ print (f"'{ jsonified } '" )
249
+ except Exception as e :
250
+ click .secho (f"Exception: { e } " , fg = "yellow" )
251
+ sys .exit (1 )
252
+ case _:
253
+ click .secho (f"Did not anticipate this type: { params } --> { type (params )} " )
254
+ sys .exit (1 )
222
255
223
256
224
257
def process_obj (some_obj , func ) -> dict :
0 commit comments