def format_files_as_string()

in devai-cli/src/devai/util/file_processor.py [0:0]


def format_files_as_string(input):
    def process_file(file_path):
        if not is_ascii_text(file_path):
            return f"file: {file_path}\nsource: [Binary File - Not ASCII Text]\n"
            # pass

        with open(file_path, 'r') as file:
            content = file.read()
            return f"\nfile: {file_path}\ncontent:\n{content}\n"
            # return f"{content}\n\n"

    formatted_string = ""

    exclude_directories = set(['venv', '__pycache__', '.gitignore']) 

    if isinstance(input, str):
        if os.path.isdir(input):
            
            for root, dirs, files in os.walk(input):
                dirs[:] = [d for d in dirs if d not in exclude_directories]
                files[:] = [f for f in files if f not in exclude_directories]
                for file in files:
                    file_path = os.path.join(root, file)
                    if os.path.exists(file_path):
                        formatted_string += process_file(file_path)
        else:
            if os.path.exists(input):
                formatted_string += process_file(input)
    elif isinstance(input, list):
        for file_path in input:
            if os.path.exists(file_path):
                formatted_string += process_file(file_path)
    else:
        raise ValueError("Input must be a directory path, a single file path, or a list of file paths")

    return formatted_string