File size: 2,406 Bytes
9b5ca29
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
import os
from tqdm import tqdm


def call_parse_prompt():
    """
    Find the prompts_raw directory and generate an __init__.py file containing prompt texts.

    Searches for prompts_raw directory in current and parent directories. Once found,
    calls create_python_file_with_texts() to generate the __init__.py file.
    """
    current_file_path = os.path.abspath(__file__)
    current_folder_path = os.path.dirname(current_file_path)
    folder_path = os.path.join(current_folder_path, "prompts_raw")
    
    # If prompts_raw not found in current directory, search parent directories
    if not os.path.exists(folder_path):
        parent_dir = current_folder_path
        while parent_dir != os.path.dirname(parent_dir):  # Stop at root directory
            parent_dir = os.path.dirname(parent_dir)
            test_path = os.path.join(parent_dir, "prompts_raw")
            if os.path.exists(test_path):
                folder_path = test_path
                break
    
    output_file = os.path.join(folder_path, "__init__.py")
    create_python_file_with_texts(folder_path, output_file)


def create_python_file_with_texts(folder_path: str, output_file: str) -> None:
    """
    Generate a Python file containing prompt texts from .txt files.

    Args:
        folder_path (str): Path to directory containing prompt .txt files
        output_file (str): Path where the generated Python file will be saved

    The function reads all .txt files in the given folder, converts their contents
    into Python variables, and writes them to the output file. Variable names are
    derived from file paths with special characters replaced.
    """
    with open(output_file, 'w', encoding='utf-8') as out_file:
        out_file.write("# This file is generated automatically through parse_prompt.py\n\n")
        txt_files = [file for root, dirs, files in os.walk(folder_path) for file in files if file.endswith(".txt")]
        for file in tqdm(txt_files, desc="Processing files"):
            file_path = os.path.join(folder_path, file)
            var_name = "_" + file_path.replace(folder_path, "").replace(os.sep, "_").replace(".txt", "").strip("_")
            with open(file_path, 'r', encoding='utf-8') as f:
                content = f.read().replace('"""', '\"\"\"')
                out_file.write(f'{var_name} = """{content}"""\n\n')


if __name__ == "__main__":
    call_parse_prompt()