13
13
# limitations under the License.
14
14
15
15
import copy
16
- import importlib
17
16
import uuid
18
17
from threading import Lock
19
18
from typing import Any , Dict , List , Optional , Tuple , Type
23
22
import numpy .typing as npt
24
23
from langchain_core .tools import BaseTool
25
24
from pydantic import BaseModel , ValidationError , computed_field
25
+ from rai .communication .ros2 .api .conversion import import_message_from_str
26
26
from rai .communication .ros2 .connectors import ROS2Connector
27
27
from rai .communication .ros2 .messages import ROS2Message
28
28
from rai .messages import MultimodalArtifact , preprocess_image
@@ -270,7 +270,7 @@ def _run(self, msg_type: str) -> str:
270
270
271
271
class ServiceValidator :
272
272
"""
273
- Validator that is responsible for checking if gicen service type exists
273
+ Validator that is responsible for checking if given service type exists
274
274
and if it is used correctly.
275
275
Validator uses ROS 2 native types when available,
276
276
falls back to Pydantic models of custom interfaces when not.
@@ -280,47 +280,6 @@ def __init__(self, custom_models: Dict[str, Type[BaseModel]]):
280
280
self .custom_models = custom_models
281
281
self .ros2_services_cache : Dict [str , Any ] = {}
282
282
283
- def get_ros2_service_class (self , service_type : str ):
284
- """
285
- Dynamically import ROS 2 service class.
286
-
287
- Parameters
288
- ----------
289
- service_type : str
290
- The ROS 2 service type in format "package_name/srv/ServiceName".
291
-
292
- Returns
293
- -------
294
- class
295
- The dynamically imported ROS 2 service class.
296
-
297
- Raises
298
- ------
299
- ValueError
300
- If the service_type format is invalid (not in format
301
- "package_name/srv/ServiceName").
302
-
303
- Notes
304
- -----
305
- Results are cached in ros2_services_cache to avoid repeated imports
306
- of the same service type.
307
- """
308
- if service_type in self .ros2_services_cache :
309
- return self .ros2_services_cache [service_type ]
310
-
311
- # Parse service type: "package_name/srv/ServiceName"
312
- parts = service_type .split ("/" )
313
- if len (parts ) != 3 or parts [1 ] != "srv" :
314
- raise ValueError (f"Service type: { service_type } is invalid" )
315
-
316
- package_name , _ , service_name = parts
317
-
318
- module = importlib .import_module (f"{ package_name } .srv" )
319
- service_class = getattr (module , service_name )
320
-
321
- self .ros2_services_cache [service_type ] = service_class
322
- return service_class
323
-
324
283
def validate_with_ros2 (self , service_type : str , args : Dict [str , Any ]):
325
284
"""Validate using installed ROS2 packages services definition
326
285
@@ -335,7 +294,7 @@ def validate_with_ros2(self, service_type: str, args: Dict[str, Any]):
335
294
TypeError
336
295
When service type does not exist in ROS2 installed packages
337
296
"""
338
- service_class = self . get_ros2_service_class (service_type )
297
+ service_class = import_message_from_str (service_type )
339
298
if not service_class :
340
299
raise TypeError (f"Service type: { service_type } does not exist." )
341
300
0 commit comments