2626 Language ,
2727 LanguageFindMode ,
2828)
29- from backend .core .translation .exceptions import LanguageSpecificFStringException , UnTranslatedFileExistException
29+ from backend .core .translation .exceptions import (
30+ IllegalImportException ,
31+ LanguageSpecificFStringException ,
32+ UnTranslatedFileExistException ,
33+ )
3034
3135logger = logging .getLogger ("root" )
3236
@@ -54,7 +58,8 @@ class NodeTranslateInit(ast.NodeVisitor):
5458 获取当前文件导入的翻译函数,并检查文件中的format字符
5559 """
5660
57- ImportPath = "django.utils.translation"
61+ TranslateImportPath = "django.utils.translation"
62+ IllegalImportPaths = ["backend.db_periodic_task.local_tasks" ]
5863
5964 def __init__ (self , ignored_string , string_regex , file_path , * args , ** kwargs ):
6065 super (NodeTranslateInit , self ).__init__ (* args , ** kwargs )
@@ -63,6 +68,8 @@ def __init__(self, ignored_string, string_regex, file_path, *args, **kwargs):
6368 self .string_regex = string_regex
6469 self .trans_func_names = []
6570 self .formatted_strings = []
71+ self .illegal_imports = []
72+ self .illegal_import_paths = self .IllegalImportPaths
6673
6774 @classmethod
6875 def get_node_module (cls , node ):
@@ -74,10 +81,39 @@ def get_node_module(cls, node):
7481 def _check_special_language (self , string ):
7582 return check_special_language (self .ignored_string , self .file_path , self .string_regex , string )
7683
84+ def visit_Import (self , node ):
85+ """检查直接import语句是否包含非法导入"""
86+ for name in node .names :
87+ if any (name .name .startswith (path ) for path in self .illegal_import_paths ):
88+ self .illegal_imports .append (
89+ {
90+ "line" : node .lineno ,
91+ "col" : node .col_offset ,
92+ "module" : name .name ,
93+ "file_path" : self .file_path ,
94+ "type" : "import" ,
95+ }
96+ )
97+ logger .warning (f"Illegal import found: { name .name } at line { node .lineno } in { self .file_path } " )
98+
7799 def visit_ImportFrom (self , node ):
78100 """游走到import,缓存翻译函数"""
79101
80- if node .module != self .ImportPath :
102+ # 检查是否导入了 from backend.db_periodic_task
103+ if node .module and any (node .module .startswith (path ) for path in self .illegal_import_paths ):
104+ self .illegal_imports .append (
105+ {
106+ "line" : node .lineno ,
107+ "col" : node .col_offset ,
108+ "module" : node .module ,
109+ "names" : [name .name for name in node .names ],
110+ "file_path" : self .file_path ,
111+ "type" : "from" ,
112+ }
113+ )
114+ logger .warning (f"Illegal import from local_tasks at line { node .lineno } in { self .file_path } " )
115+
116+ if node .module != self .TranslateImportPath :
81117 return
82118
83119 for name in node .names :
@@ -92,7 +128,7 @@ def visit_Str(self, node):
92128 # 标记Module,后续可能添加import函数
93129 module_node = self .get_node_module (node )
94130 for import_from in module_node .body :
95- if isinstance (import_from , ast .ImportFrom ) and import_from .module == self .ImportPath :
131+ if isinstance (import_from , ast .ImportFrom ) and import_from .module == self .TranslateImportPath :
96132 return
97133
98134 module_node .language_flag = True
@@ -225,7 +261,7 @@ def visit_Module(self, node):
225261 node .body .insert (
226262 0 ,
227263 ast .ImportFrom (
228- module = NodeTranslateInit .ImportPath , names = [ast .alias (name = "gettext" , asname = "_" )], level = 0
264+ module = NodeTranslateInit .TranslateImportPath , names = [ast .alias (name = "gettext" , asname = "_" )], level = 0
229265 ),
230266 )
231267 return node
@@ -248,6 +284,7 @@ class LanguageFinder:
248284 # 写入待翻译信息的文件名
249285 TRANSLATE_INFO_FILE : str = "backend/locale/translate_info.json"
250286 FORMATTED_STRINGS_FILE : str = "backend/locale/formatted_string_info.json"
287+ ILLEGAL_IMPORTS_FILE : str = "backend/locale/illegal_imports_info.json"
251288
252289 def __init__ (
253290 self ,
@@ -277,6 +314,7 @@ def __init__(
277314 "mock.py" ,
278315 "mock_data.py" ,
279316 "backend/bk_dataview/grafana" ,
317+ "local_tasks" ,
280318 }
281319 if exclude_dir_or_file_list :
282320 _exclude_dir_or_file_list .update (set (exclude_dir_or_file_list ))
@@ -296,6 +334,8 @@ def __init__(
296334 self .sentences_to_be_translated = {}
297335 # 缓存format的字符串
298336 self .formatted_strings = {}
337+ # 缓存非法导入信息
338+ self .illegal_imports = {}
299339
300340 def check_file (self , file_path ):
301341 """
@@ -316,6 +356,11 @@ def check_file(self, file_path):
316356 string_regex = LANGUAGE_REGEX_MAP [self .translate_language ],
317357 )
318358 tree .visit (nodes )
359+
360+ # 收集非法导入信息
361+ if tree .illegal_imports :
362+ self .illegal_imports [file_path ] = tree .illegal_imports
363+
319364 # 缓存当前文件的format语句
320365 if tree .formatted_strings :
321366 self .formatted_strings [file_path ] = tree .formatted_strings
@@ -393,7 +438,7 @@ def run(self):
393438
394439 # 导入翻译函数包, 默认为gettext, TODO: 做成一个参数传递?
395440 if getattr (translate_nodes , "language_flag" , False ):
396- file_lines .insert (1 , f"from { NodeTranslateInit .ImportPath } import gettext as _\n " )
441+ file_lines .insert (1 , f"from { NodeTranslateInit .TranslateImportPath } import gettext as _\n " )
397442
398443 with open (each_file , "w" ) as f :
399444 f .write ("" .join (file_lines ))
@@ -413,6 +458,18 @@ def run(self):
413458 f"There are f-strings containing the specific translation language in the project:"
414459 f"{ json .dumps (self .formatted_strings , ensure_ascii = False , indent = 4 )} "
415460 )
461+ if self .illegal_imports :
462+ # TODO: 待import问题解决后,恢复此异常抛出
463+ # raise IllegalImportException(
464+ # f"There are illegal imports in the project: "
465+ # f"{json.dumps(self.illegal_imports, ensure_ascii=False, indent=4)}"
466+ # )
467+ logger .error (
468+ IllegalImportException (
469+ f"There are illegal imports in the project: "
470+ f"{ json .dumps (self .illegal_imports , ensure_ascii = False , indent = 4 )} "
471+ )
472+ )
416473 else :
417474 # 写入未翻译信息
418475 with open (self .TRANSLATE_INFO_FILE , "w+" ) as f :
@@ -421,3 +478,6 @@ def run(self):
421478 # 写入format字符串信息
422479 with open (self .FORMATTED_STRINGS_FILE , "w+" ) as f :
423480 f .write (json .dumps (self .formatted_strings , ensure_ascii = False , indent = 4 ))
481+ # 写入非法导入信息
482+ with open (self .ILLEGAL_IMPORTS_FILE , "w+" ) as f :
483+ f .write (json .dumps (self .illegal_imports , ensure_ascii = False , indent = 4 ))
0 commit comments