19
19
https://github.com/robotframework/PythonLibCore
20
20
"""
21
21
import inspect
22
+ import json
22
23
import os
23
24
from dataclasses import dataclass
25
+ from pathlib import Path
24
26
from typing import Any , Callable , List , Optional , Union , get_type_hints
25
27
28
+ from robot .api import logger
26
29
from robot .api .deco import keyword # noqa: F401
27
30
from robot .errors import DataError
28
31
from robot .utils import Importer
@@ -42,28 +45,47 @@ class NoKeywordFound(PythonLibCoreException):
42
45
pass
43
46
44
47
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
+
45
60
class HybridCore :
46
- def __init__ (self , library_components : List ) -> None :
61
+ def __init__ (self , library_components : List , translation : Optional [ Path ] = None ) -> None :
47
62
self .keywords = {}
48
63
self .keywords_spec = {}
49
64
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 )
52
68
self .__set_library_listeners (library_components )
53
69
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
56
73
for component in library_components :
57
74
for name , func in self .__get_members (component ):
58
75
if callable (func ) and hasattr (func , "robot_name" ):
59
76
kw = getattr (component , name )
60
- kw_name = func . robot_name or name
77
+ kw_name = self . __get_keyword_name ( func , name , translation )
61
78
self .keywords [kw_name ] = kw
62
- self .keywords_spec [kw_name ] = KeywordBuilder .build (kw )
79
+ self .keywords_spec [kw_name ] = KeywordBuilder .build (kw , translation )
63
80
# Expose keywords as attributes both using original
64
81
# method names as well as possible custom names.
65
82
self .attributes [name ] = self .attributes [kw_name ] = kw
66
83
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
+
67
89
def __set_library_listeners (self , library_components : list ):
68
90
listeners = self .__get_manually_registered_listeners ()
69
91
listeners .extend (self .__get_component_listeners ([self , * library_components ]))
@@ -198,13 +220,24 @@ def __get_keyword_path(self, method):
198
220
199
221
class KeywordBuilder :
200
222
@classmethod
201
- def build (cls , function ):
223
+ def build (cls , function , translation : Optional [dict ] = None ):
224
+ translation = translation if translation else {}
202
225
return KeywordSpecification (
203
226
argument_specification = cls ._get_arguments (function ),
204
- documentation = inspect . getdoc (function ) or "" ,
227
+ documentation = cls . get_doc (function , translation ) ,
205
228
argument_types = cls ._get_types (function ),
206
229
)
207
230
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
+
208
241
@classmethod
209
242
def unwrap (cls , function ):
210
243
return inspect .unwrap (function )
0 commit comments