1+ # Copyright 2008-2011 Nokia Networks
2+ # Copyright 2011-2016 Ryan Tomac, Ed Manlove and contributors
3+ # Copyright 2016- Robot Framework Foundation
4+ #
5+ # Licensed under the Apache License, Version 2.0 (the "License");
6+ # you may not use this file except in compliance with the License.
7+ # You may obtain a copy of the License at
8+ #
9+ # http://www.apache.org/licenses/LICENSE-2.0
10+ #
11+ # Unless required by applicable law or agreed to in writing, software
12+ # distributed under the License is distributed on an "AS IS" BASIS,
13+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+ # See the License for the specific language governing permissions and
15+ # limitations under the License.
16+
17+ import hashlib
18+ import inspect
19+ import json
20+ from pathlib import Path
21+ from typing import List , Optional
22+
23+ KEYWORD_NAME = "Keyword name"
24+ DOC_CHANGED = "Documentation update needed"
25+ NO_LIB_KEYWORD = "Keyword not found from library"
26+ MISSING_TRANSLATION = "Keyword is missing translation"
27+ MISSING_CHECKSUM = "Keyword tranlsaton is missing checksum"
28+ MAX_REASON_LEN = max (
29+ len (DOC_CHANGED ),
30+ len (NO_LIB_KEYWORD ),
31+ len (MISSING_TRANSLATION ),
32+ len (MISSING_CHECKSUM ),
33+ )
34+
35+
36+ def get_library_translaton (
37+ plugings : Optional [str ] = None
38+ ) -> dict :
39+ from SeleniumLibrary import SeleniumLibrary
40+
41+ selib = SeleniumLibrary (plugins = plugings )
42+ translation = {}
43+ for function in selib .attributes .values ():
44+ translation [function .__name__ ] = {
45+ "name" : function .__name__ ,
46+ "doc" : function .__doc__ ,
47+ "sha256" : hashlib .sha256 (function .__doc__ .encode ("utf-16" )).hexdigest (),
48+ }
49+ translation ["__init__" ] = {
50+ "name" : "__init__" ,
51+ "doc" : inspect .getdoc (selib ),
52+ "sha256" : hashlib .sha256 (inspect .getdoc (selib ).encode ("utf-16" )).hexdigest (), # type: ignore
53+ }
54+ translation ["__intro__" ] = {
55+ "name" : "__intro__" ,
56+ "doc" : selib .__doc__ ,
57+ "sha256" : hashlib .sha256 (selib .__doc__ .encode ("utf-16" )).hexdigest (), # type: ignore
58+ }
59+ return translation
60+
61+
62+ def _max_kw_name_lenght (project_tanslation : dict ) -> int :
63+ max_lenght = 0
64+ for keyword_data in project_tanslation .values ():
65+ if (current_kw_length := len (keyword_data ["name" ])) > max_lenght :
66+ max_lenght = current_kw_length
67+ return max_lenght
68+
69+
70+ def _get_heading (max_kw_lenght : int ) -> List [str ]:
71+ heading = f"| { KEYWORD_NAME } "
72+ next_line = f"| { '-' * len (KEYWORD_NAME )} "
73+ if (padding := max_kw_lenght - len (KEYWORD_NAME )) > 0 :
74+ heading = f"{ heading } { ' ' * padding } "
75+ next_line = f"{ next_line } { '-' * padding } "
76+ reason = "Reason"
77+ reason_padding = MAX_REASON_LEN - len (reason )
78+ heading = f"{ heading } | { reason } { ' ' * reason_padding } |"
79+ next_line = f"{ next_line } | { '-' * MAX_REASON_LEN } |"
80+ return [heading , next_line ]
81+
82+
83+ def _table_doc_updated (lib_kw : str , max_name_lenght : int , reason : str ) -> str :
84+ line = f"| { lib_kw } "
85+ if (padding := max_name_lenght - len (lib_kw ) - 0 ) > 0 :
86+ line = f"{ line } { ' ' * padding } | { reason } "
87+ else :
88+ line = f"{ line } | { reason } "
89+ if reason_padding := MAX_REASON_LEN - len (reason ):
90+ line = f"{ line } { ' ' * reason_padding } "
91+ return f"{ line } |"
92+
93+
94+ def compare_translatoin (filename : Path , library_translation : dict ):
95+ with filename .open ("r" ) as file :
96+ project_translation = json .load (file )
97+ max_kw_lenght = _max_kw_name_lenght (library_translation )
98+ table_body = []
99+ for lib_kw , lib_kw_data in library_translation .items ():
100+ project_kw_data = project_translation .get (lib_kw )
101+ if not project_kw_data :
102+ table_body .append (
103+ _table_doc_updated (lib_kw , max_kw_lenght , MISSING_TRANSLATION )
104+ )
105+ continue
106+ sha256_value = project_kw_data .get ("sha256" )
107+ if not sha256_value :
108+ table_body .append (
109+ _table_doc_updated (lib_kw , max_kw_lenght , MISSING_CHECKSUM )
110+ )
111+ continue
112+ if project_kw_data ["sha256" ] != lib_kw_data ["sha256" ]:
113+ table_body .append (_table_doc_updated (lib_kw , max_kw_lenght , DOC_CHANGED ))
114+ for project_kw in project_translation :
115+ if project_kw not in library_translation :
116+ table_body .append (
117+ _table_doc_updated (project_kw , max_kw_lenght , NO_LIB_KEYWORD )
118+ )
119+ if not table_body :
120+ return []
121+
122+ table = _get_heading (max_kw_lenght )
123+ table .extend (table_body )
124+ return table
0 commit comments