File size: 967 Bytes
b856986
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
# discover_models.py

import importlib
import os
from typing import List

def discover_models(target_file: str,directory: str="./") -> List[str]:
    """
    Discover target file (e.g., models.py) in all directories and subdirectories.
    
    :param directory: The root directory to start searching from.
    :param target_file: The filename to look for in subdirectories (e.g., "models.py").
    :return: A list of module paths as strings.
    """
    model_modules = []
    
    # Traverse directory and subdirectories
    for root, _, files in os.walk(directory):
        if target_file in files:
            # Construct the module path, converting file path to dot notation
            relative_path = os.path.relpath(root, directory)
            module_name = f"{directory.replace('/', '.')}.{relative_path.replace('/', '.')}.{target_file[:-3]}"
            model_modules.append(module_name.replace('...',""))
    print(model_modules)
    return model_modules