0

I am using TreeSitter to parse python code.

I need to understand check_files_in_directory is invoked from GPT4Readability.utils. I already captured all the function calls. I have to do this programatically.

But now I have to find out from which file check_files_in_directory is called. I am struggling to understand what would the logic to do it. Can anyone please suggest?

import os
from getpass import getpass
from GPT4Readability.utils import *
import importlib.resources as pkg_resources  


def generate_readme(root_dir, output_name, model):
    """Generates a README.md file based on the python files in the provided directory

    Args:
        root_dir (str): The root directory of the python package to parse and generate a readme for
    """

    # prompt_folder_name = os.path.join(os.path.dirname(__file__), "prompts")
    # prompt_path = os.path.join(prompt_folder_name, "readme_prompt.txt")

    with pkg_resources.open_text('GPT4Readability.prompts','readme_prompt.txt') as f:         
        inb_msg = f.read()

    # with open(prompt_path) as f:
    #     lines = f.readlines()
    # inb_msg = "".join(lines)

    file_check_result = check_files_in_directory(root_dir)
Exploring
  • 2,493
  • 11
  • 56
  • 97

1 Answers1

0

Treesitter parses an individual file, not a code-tree or "project". Also, it does not attempt to identify call graphs (who calls who and where).

What I think you're looking for is a language-server, which does work at the scope of an entire project and does work out who calls who. These would be the definitions and references functionality of a language server (commonly called an LSP, although that's not strictly correct). pyright is one to look at for Python.

If you were satisifed with references within a single source file, i.e. the function is both defined and called in the same module, then you could write a script to do that by identifying call nodes and function_definition nodes and matching them up by name.

Such a solution would be imperfect because you can assign a function to a variable of a different name, and those wouldn't match:

def fn():
    return 42

fn_alias = fn

print(fn_alias())
# 42

These are the sort of shortcomings that a language-server overcomes.

scanny
  • 26,423
  • 5
  • 54
  • 80