Update to v0.1.4 Add `is_function_in_file` and if the key is "<ORIGINAL>", the `TranslationManager.translate()` will return it directly. (For some strings that don't need to translate in specific language) Launcher Update to 0.1.0a0 Add plugin system support and add some useful apis for plugin. Add "Refresh avatar cache" to account page.
23 lines
685 B
Python
23 lines
685 B
Python
import ast
|
|
|
|
|
|
def is_function_in_file(file_path, function_name) -> tuple[bool, bool]:
|
|
"""
|
|
Parse a Python file via ast module to check if a function exists.
|
|
:param file_path:
|
|
:param function_name:
|
|
:return:
|
|
is_check_success: bool
|
|
is_function_exist: bool
|
|
"""
|
|
try:
|
|
with open(file_path, "r", encoding="utf-8") as f:
|
|
root = ast.parse(f.read(), filename=file_path)
|
|
|
|
for node in ast.iter_child_nodes(root):
|
|
if isinstance(node, ast.FunctionDef) and node.name == function_name:
|
|
return True, True
|
|
return True, False
|
|
except (FileNotFoundError, SyntaxError):
|
|
return False, False
|