Fix path sanitation for absolute Windows paths

When using an absolute Windows path (e.g. `C:\Users\USER\Documents\`)
for the `newbot` command the translation table replaced the valid `:`
character in the drive causing it to create the directory at the wrong
place.

Fixes #10096
This commit is contained in:
Eric Schneider 2025-02-09 03:08:45 +01:00 committed by GitHub
parent 42e4a87374
commit 52967ec103
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -28,7 +28,7 @@ from typing import Optional, Tuple, Dict
import argparse import argparse
import sys import sys
from pathlib import Path from pathlib import Path, PurePath, PureWindowsPath
import discord import discord
import importlib.metadata import importlib.metadata
@ -225,7 +225,13 @@ def to_path(parser: argparse.ArgumentParser, name: str, *, replace_spaces: bool
) )
if len(name) <= 4 and name.upper() in forbidden: if len(name) <= 4 and name.upper() in forbidden:
parser.error('invalid directory name given, use a different one') parser.error('invalid directory name given, use a different one')
path = PurePath(name)
if isinstance(path, PureWindowsPath) and path.drive:
drive, rest = path.parts[0], path.parts[1:]
transformed = tuple(map(lambda p: p.translate(_translation_table), rest))
name = drive + '\\'.join(transformed)
else:
name = name.translate(_translation_table) name = name.translate(_translation_table)
if replace_spaces: if replace_spaces:
name = name.replace(' ', '-') name = name.replace(' ', '-')