@@ -74,8 +74,13 @@ def check_file_is_txt(path: Path, verbose: bool = True) -> bool:
7474 return check_file_mime_type (path , mime_extensions_for_txt_files , verbose )
7575
7676
77- def check_file_contains_handler (path : Path ) -> Tuple [bool , Optional [List [str ]]]:
78- """Return true if the file contain PluginHandler Class."""
77+ def check_file_contains_handler (path : Path ) -> Tuple [bool , Optional [List [str ]], bool ]:
78+ """Test if the file is a python file with the right content.
79+
80+ Return true if the file contain PluginHandler Class.
81+ Return the list of handlers or None if they are present in the file.
82+ Return true if the file contains on_event method.
83+ """
7984 with path .open (encoding = "utf-8" ) as file :
8085 module = ast .parse (file .read ())
8186 for node in module .body :
@@ -85,8 +90,12 @@ def check_file_contains_handler(path: Path) -> Tuple[bool, Optional[List[str]]]:
8590 for child in node .body
8691 if isinstance (child , ast .FunctionDef ) and child .name in POSSIBLE_HANDLERS
8792 ]
88- return True , handlers
89- return False , None
93+ has_on_event = any (
94+ isinstance (child , ast .FunctionDef ) and child .name == "on_event"
95+ for child in node .body
96+ )
97+ return (True , handlers , has_on_event ) if handlers else (True , None , has_on_event )
98+ return False , None , False
9099
91100
92101class WebhookUploader :
@@ -101,17 +110,20 @@ def __init__(
101110 header : Optional [str ],
102111 verbose : bool ,
103112 handler_types : Optional [List [str ]],
113+ event_matcher : Optional [List [str ]],
104114 ) -> None :
105115 self .kili = kili
106116 self .webhook_url = webhook_url
107117 self .plugin_name = plugin_name or self .webhook_url
108118 self .header = header
109119 self .verbose = verbose
110120 self .handler_types = handler_types
121+ self .event_matcher = event_matcher
111122
112123 def create_webhook (self ) -> str :
113124 """Create a webhook receiving Kili events."""
114125 variables = {
126+ "eventMatcher" : self .event_matcher ,
115127 "handlerTypes" : self .handler_types ,
116128 "pluginName" : self .plugin_name ,
117129 "webhookUrl" : self .webhook_url ,
@@ -125,6 +137,7 @@ def create_webhook(self) -> str:
125137 def update_webhook (self ) -> str :
126138 """Update a webhook receiving Kili events."""
127139 variables = {
140+ "eventMatcher" : self .event_matcher ,
128141 "handlerTypes" : self .handler_types ,
129142 "pluginName" : self .plugin_name ,
130143 "webhookUrl" : self .webhook_url ,
@@ -147,10 +160,12 @@ def __init__(
147160 plugin_name : Optional [str ],
148161 verbose : bool ,
149162 http_client : HttpClient ,
163+ event_matcher : Optional [List [str ]],
150164 ) -> None :
151165 self .kili = kili
152166 self .plugin_path = Path (plugin_path )
153167 self .http_client = http_client
168+ self .event_matcher = event_matcher
154169
155170 if (not self .plugin_path .is_dir ()) and (not self .plugin_path .is_file ()):
156171 raise FileNotFoundError (
@@ -173,10 +188,16 @@ def _retrieve_plugin_src(self) -> List[Path]:
173188 raise FileNotFoundError (
174189 f"No main.py file in the provided folder: { self .plugin_path .absolute ()} "
175190 )
176- contains_handler , handler_types = check_file_contains_handler (file_path )
191+ contains_handler , handler_types , has_on_event = check_file_contains_handler (file_path )
177192 if not contains_handler :
178193 raise ValueError ("PluginHandler class is not present in your main.py file." )
179194
195+ if has_on_event and not self .event_matcher :
196+ raise ValueError ("Event matcher is required for plugins with on_event method." )
197+
198+ if handler_types and self .event_matcher :
199+ raise ValueError ("Cannot have both handler types and event matcher." )
200+
180201 self .handler_types = handler_types
181202
182203 return list (self .plugin_path .glob ("**/*.py" ))
@@ -186,10 +207,16 @@ def _retrieve_plugin_src(self) -> List[Path]:
186207 if not check_file_is_py (file_path , self .verbose ):
187208 raise ValueError ("Wrong file format." )
188209
189- contains_handler , handler_types = check_file_contains_handler (file_path )
210+ contains_handler , handler_types , has_on_event = check_file_contains_handler (file_path )
190211 if not contains_handler :
191212 raise ValueError ("PluginHandler class is not present in your plugin file." )
192213
214+ if has_on_event and not self .event_matcher :
215+ raise ValueError ("Event matcher is required for plugins with on_event method." )
216+
217+ if handler_types and self .event_matcher :
218+ raise ValueError ("Cannot have both handler types and event matcher." )
219+
193220 self .handler_types = handler_types
194221
195222 return [file_path ]
@@ -276,7 +303,11 @@ def _upload_script(self, is_updating_plugin: bool) -> None:
276303
277304 def _create_plugin_runner (self ) -> Any :
278305 """Create plugin's runner."""
279- variables = {"pluginName" : self .plugin_name , "handlerTypes" : self .handler_types }
306+ variables = {
307+ "pluginName" : self .plugin_name ,
308+ "handlerTypes" : self .handler_types ,
309+ "eventMatcher" : self .event_matcher ,
310+ }
280311
281312 result = self .kili .graphql_client .execute (GQL_CREATE_PLUGIN_RUNNER , variables )
282313 return self .kili .format_result ("data" , result )
@@ -345,7 +376,11 @@ def create_plugin(self):
345376
346377 def _update_plugin_runner (self ) -> Any :
347378 """Update plugin's runner."""
348- variables = {"pluginName" : self .plugin_name , "handlerTypes" : self .handler_types }
379+ variables = {
380+ "pluginName" : self .plugin_name ,
381+ "handlerTypes" : self .handler_types ,
382+ "eventMatcher" : self .event_matcher ,
383+ }
349384
350385 result = self .kili .graphql_client .execute (GQL_UPDATE_PLUGIN_RUNNER , variables )
351386 return self .kili .format_result ("data" , result )
0 commit comments