uranusjr commented on code in PR #30495:
URL: https://github.com/apache/airflow/pull/30495#discussion_r1162502632


##########
airflow/utils/file.py:
##########
@@ -371,3 +372,22 @@ def might_contain_dag_via_default_heuristic(file_path: 
str, zip_file: zipfile.Zi
             content = dag_file.read()
     content = content.lower()
     return all(s in content for s in (b"dag", b"airflow"))
+
+
+def get_airflow_modules_in(file_path: str) -> list[str]:
+    """Returns a list of the airflow modules that are imported in the given 
file"""
+    with open(file_path, "rb") as dag_file:
+        content = dag_file.read()
+    try:
+        parsed = ast.parse(content)
+    except SyntaxError:
+        return []
+
+    modules = []
+    for st in parsed.body:
+        if isinstance(st, ast.Import):
+            modules.extend([n.name for n in st.names])
+        elif isinstance(st, ast.ImportFrom) and st.module:
+            modules.append(st.module)
+
+    return [m for m in modules if m.startswith("airflow.")]

Review Comment:
   ```suggestion
   def _find_imported_modules(module: ast.Module) -> Generator[str, None, None]:
       for st in module.body:
           if isinstance(st, ast.Import):
               for n in st.names:
                   yield n.name
           elif isinstance(st, ast.ImportFrom) and st.module is not None:
               yield st.module
   
   
   def iter_airflow_imports(file_path: str) -> Generator[str, None, None]:
       """Find Airflow modules imported in the given file."""
       try:
           parsed = ast.parse(Path(file_path).read_bytes())
       except (OSError, SyntaxError, UnicodeDecodeError):
           return
       for m in _find_imported_modules(parsed):
           if m.startswith("airflow."):
               yield m
   ```



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to