1919https://github.com/robotframework/PythonLibCore
2020"""
2121import inspect
22+ import json
2223import os
2324from dataclasses import dataclass
25+ from pathlib import Path
2426from typing import Any , Callable , List , Optional , Union , get_type_hints
2527
28+ from robot .api import logger
2629from robot .api .deco import keyword # noqa: F401
2730from robot .errors import DataError
2831from robot .utils import Importer
@@ -42,28 +45,47 @@ class NoKeywordFound(PythonLibCoreException):
4245 pass
4346
4447
48+ def _translation (translation : Optional [Path ] = None ):
49+ if translation and isinstance (translation , Path ) and translation .is_file ():
50+ with translation .open ("r" ) as file :
51+ try :
52+ return json .load (file )
53+ except json .decoder .JSONDecodeError :
54+ logger .warn (f"Could not find file: { translation } " )
55+ return {}
56+ else :
57+ return {}
58+
59+
4560class HybridCore :
46- def __init__ (self , library_components : List ) -> None :
61+ def __init__ (self , library_components : List , translation : Optional [ Path ] = None ) -> None :
4762 self .keywords = {}
4863 self .keywords_spec = {}
4964 self .attributes = {}
50- self .add_library_components (library_components )
51- self .add_library_components ([self ])
65+ translation_data = _translation (translation )
66+ self .add_library_components (library_components , translation_data )
67+ self .add_library_components ([self ], translation_data )
5268 self .__set_library_listeners (library_components )
5369
54- def add_library_components (self , library_components : List ):
55- self .keywords_spec ["__init__" ] = KeywordBuilder .build (self .__init__ ) # type: ignore
70+ def add_library_components (self , library_components : List , translation : Optional [dict ] = None ):
71+ translation = translation if translation else {}
72+ self .keywords_spec ["__init__" ] = KeywordBuilder .build (self .__init__ , translation ) # type: ignore
5673 for component in library_components :
5774 for name , func in self .__get_members (component ):
5875 if callable (func ) and hasattr (func , "robot_name" ):
5976 kw = getattr (component , name )
60- kw_name = func . robot_name or name
77+ kw_name = self . __get_keyword_name ( func , name , translation )
6178 self .keywords [kw_name ] = kw
62- self .keywords_spec [kw_name ] = KeywordBuilder .build (kw )
79+ self .keywords_spec [kw_name ] = KeywordBuilder .build (kw , translation )
6380 # Expose keywords as attributes both using original
6481 # method names as well as possible custom names.
6582 self .attributes [name ] = self .attributes [kw_name ] = kw
6683
84+ def __get_keyword_name (self , func : Callable , name : str , translation : dict ):
85+ if name in translation :
86+ return translation [name ]["name" ]
87+ return func .robot_name or name
88+
6789 def __set_library_listeners (self , library_components : list ):
6890 listeners = self .__get_manually_registered_listeners ()
6991 listeners .extend (self .__get_component_listeners ([self , * library_components ]))
@@ -198,13 +220,24 @@ def __get_keyword_path(self, method):
198220
199221class KeywordBuilder :
200222 @classmethod
201- def build (cls , function ):
223+ def build (cls , function , translation : Optional [dict ] = None ):
224+ translation = translation if translation else {}
202225 return KeywordSpecification (
203226 argument_specification = cls ._get_arguments (function ),
204- documentation = inspect . getdoc (function ) or "" ,
227+ documentation = cls . get_doc (function , translation ) ,
205228 argument_types = cls ._get_types (function ),
206229 )
207230
231+ @classmethod
232+ def get_doc (cls , function , translation : dict ):
233+ if kw := cls ._get_kw_transtation (function , translation ):
234+ return kw ["doc" ]
235+ return inspect .getdoc (function ) or ""
236+
237+ @classmethod
238+ def _get_kw_transtation (cls , function , translation : dict ):
239+ return translation .get (function .__name__ , {})
240+
208241 @classmethod
209242 def unwrap (cls , function ):
210243 return inspect .unwrap (function )
0 commit comments