in scripts/export_tasks.py [0:0]
def traverse_global_assignments(library_name, file_content, handler):
"""Traverse all global assignments and apply handler on each of them.
Args:
library_name: The name of the library (e.g. paddlenlp)
file_content: The content of app/main.py file in string.
handler: A callback that processes the AST.
"""
for element in ast.parse(file_content).body:
# Typical case, e.g. TARGET_ID: Type = VALUE
if isinstance(element, ast.AnnAssign):
handler(library_name, element.target.id, element.value)
# Just in case user omitted the type annotation
# Unpacking and multi-variable assignment is rare so not handled
# e.g. TARGET_ID = VALUE
elif isinstance(element, ast.Assign):
target = element.targets[0]
if isinstance(target, ast.Name):
handler(library_name, target.id, element.value)