Skip to content

Commit

Permalink
v0.2.2: Sanitize new filenames immediately
Browse files Browse the repository at this point in the history
I was previously only sanitizing *after* a rename failed; but this
introduced a bug where names could include / and thus break the
path-splitting logic. Makes more sense to just always sanitize anyway,
so that the resulting filenames are portable.

Fixes #11
  • Loading branch information
acarapetis committed Aug 10, 2024
1 parent 39c2fd6 commit 5043dc5
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 11 deletions.
2 changes: 1 addition & 1 deletion shroudstone/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
"""Python utilities for working with Stormgate replays"""

__version__ = "0.2.1"
__version__ = "0.2.2"
14 changes: 4 additions & 10 deletions shroudstone/renamer.py
Original file line number Diff line number Diff line change
Expand Up @@ -300,7 +300,9 @@ def new_name_for(replay: Replay, format_1v1: str, format_generic: str) -> str:
newname = format_generic.format(**parts)

# In case we left some blanks, collapse multiple spaces to one space
return re.sub(r"\s+", " ", newname)
newname = re.sub(r"\s+", " ", newname)

return sanitize_filename(newname)


def do_rename(source: Path, target: Path, dry_run: bool):
Expand All @@ -320,15 +322,7 @@ def do_rename(source: Path, target: Path, dry_run: bool):
try:
source.rename(target)
except Exception as e:
# In case the error was due to weird characters in a player name:
new_name = sanitize_filename(target.name)
if new_name != target.name:
logger.warning(
f"Error renaming {source} => {target.name}, retrying with sanitized filename."
)
do_rename(source, target.parent / new_name, dry_run=dry_run)
else:
logger.error(f"Error renaming {source} => {target.name}: {e}")
logger.error(f"Error renaming {source} => {target.name}: {e}")


def sanitize_filename(filename: str) -> str:
Expand Down

0 comments on commit 5043dc5

Please sign in to comment.