def get_directory_for_filetype()

in tools/url-checker/create_test_files.py [0:0]


def get_directory_for_filetype(ext, test_root, randomize=False):
    """Get an appropriate directory for a file type, with optional randomization."""
    # Get all directories recursively
    if randomize and random.random() < 0.3:
        # Get all directories
        all_dirs = []
        for root, dirs, files in os.walk(test_root):
            all_dirs.extend([os.path.join(root, d) for d in dirs])
        
        if all_dirs:
            return random.choice(all_dirs)
    
    # Default directories based on file type
    if ext in ['md', 'txt']:
        base = os.path.join(test_root, "docs")
        options = [
            base,
            os.path.join(base, "user_guide"),
            os.path.join(base, "api_reference"),
            os.path.join(base, "examples")
        ]
    elif ext in ['js', 'ts']:
        base = os.path.join(test_root, "src")
        options = [
            os.path.join(base, "utils"),
            os.path.join(base, "components"),
            os.path.join(base, "services")
        ]
    elif ext == 'css':
        base = os.path.join(test_root, "src")
        options = [
            os.path.join(base, "styles"),
            os.path.join(test_root, "assets")
        ]
    elif ext in ['json', 'yaml', 'yml']:
        base = os.path.join(test_root, "config")
        options = [
            base,
            os.path.join(base, "app")
        ]
    elif ext == 'html':
        options = [test_root, os.path.join(test_root, "docs")]
    elif ext == 'xml':
        options = [os.path.join(test_root, "config")]
    elif ext == 'py':
        base = os.path.join(test_root, "src")
        options = [
            os.path.join(base, "utils"),
            os.path.join(test_root, "scripts")
        ]
    elif ext in ['sh', 'bash', 'zsh']:
        options = [os.path.join(test_root, "scripts", "shell")]
    elif ext in ['ps1', 'psm1', 'psd1']:
        options = [os.path.join(test_root, "scripts", "powershell")]
    elif ext in ['bat', 'cmd']:
        options = [os.path.join(test_root, "scripts", "batch")]
    elif ext in ['rb', 'pl', 'php', 'r', 'R']:
        options = [os.path.join(test_root, "scripts", "other")]
    elif ext in ['conf', 'cfg', 'ini']:
        options = [os.path.join(test_root, "config", "app")]
    else:
        options = [test_root]
    
    # Pick a random option from the list
    selected_dir = random.choice(options)
    ensure_directory(selected_dir)  # Make sure it exists
    return selected_dir