Skip to content

Commit 2f5b3a1

Browse files
committed
Refactor error message formatting
Signed-off-by: Tsuyoshi Hombashi <tsuyoshi.hombashi@gmail.com>
1 parent b93fce7 commit 2f5b3a1

File tree

5 files changed

+22
-22
lines changed

5 files changed

+22
-22
lines changed

pathvalidate/_const.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44

55
DEFAULT_MIN_LEN: Final = 1
6-
INVALID_CHAR_ERR_MSG_TMPL: Final = "invalids=({invalid}), value={value!r}"
6+
INVALID_CHAR_ERR_MSG_TMPL: Final = "invalids=({invalid})"
77

88

99
_NTFS_RESERVED_FILE_NAMES: Final = (

pathvalidate/_filename.py

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -229,46 +229,48 @@ def __validate_universal_filename(self, unicode_filename: str) -> None:
229229
if match:
230230
raise InvalidCharError(
231231
INVALID_CHAR_ERR_MSG_TMPL.format(
232-
invalid=findall_to_str(match), value=repr(unicode_filename)
232+
invalid=findall_to_str(match),
233233
),
234234
platform=Platform.UNIVERSAL,
235+
value=unicode_filename,
235236
)
236237

237238
def __validate_win_filename(self, unicode_filename: str) -> None:
238239
match = _RE_INVALID_WIN_FILENAME.findall(unicode_filename)
239240
if match:
240241
raise InvalidCharError(
241242
INVALID_CHAR_ERR_MSG_TMPL.format(
242-
invalid=findall_to_str(match), value=repr(unicode_filename)
243+
invalid=findall_to_str(match),
243244
),
244245
platform=Platform.WINDOWS,
246+
value=unicode_filename,
245247
)
246248

247249
if unicode_filename in (".", ".."):
248250
return
249251

250252
KB2829981_err_tmpl = "{}. Refer: https://learn.microsoft.com/en-us/troubleshoot/windows-client/shell-experience/file-folder-name-whitespace-characters" # noqa: E501
253+
err_kwargs = {
254+
ErrorAttrKey.PLATFORM: Platform.WINDOWS,
255+
ErrorAttrKey.VALUE: unicode_filename,
256+
}
251257

252258
if unicode_filename[-1] in (" ", "."):
253259
raise InvalidCharError(
254-
INVALID_CHAR_ERR_MSG_TMPL.format(
255-
invalid=re.escape(unicode_filename[-1]), value=repr(unicode_filename)
256-
),
257-
platform=Platform.WINDOWS,
260+
INVALID_CHAR_ERR_MSG_TMPL.format(invalid=re.escape(unicode_filename[-1])),
258261
description=KB2829981_err_tmpl.format(
259262
"Do not end a file or directory name with a space or a period"
260263
),
264+
**err_kwargs,
261265
)
262266

263267
if unicode_filename[0] in (" "):
264268
raise InvalidCharError(
265-
INVALID_CHAR_ERR_MSG_TMPL.format(
266-
invalid=re.escape(unicode_filename[0]), value=repr(unicode_filename)
267-
),
268-
platform=Platform.WINDOWS,
269+
INVALID_CHAR_ERR_MSG_TMPL.format(invalid=re.escape(unicode_filename[0])),
269270
description=KB2829981_err_tmpl.format(
270271
"Do not start a file or directory name with a space"
271272
),
273+
**err_kwargs,
272274
)
273275

274276

pathvalidate/_filepath.py

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -278,19 +278,17 @@ def __validate_unix_filepath(self, unicode_filepath: str) -> None:
278278
match = _RE_INVALID_PATH.findall(unicode_filepath)
279279
if match:
280280
raise InvalidCharError(
281-
INVALID_CHAR_ERR_MSG_TMPL.format(
282-
invalid=findall_to_str(match), value=repr(unicode_filepath)
283-
)
281+
INVALID_CHAR_ERR_MSG_TMPL.format(invalid=findall_to_str(match)),
282+
value=unicode_filepath,
284283
)
285284

286285
def __validate_win_filepath(self, unicode_filepath: str) -> None:
287286
match = _RE_INVALID_WIN_PATH.findall(unicode_filepath)
288287
if match:
289288
raise InvalidCharError(
290-
INVALID_CHAR_ERR_MSG_TMPL.format(
291-
invalid=findall_to_str(match), value=repr(unicode_filepath)
292-
),
289+
INVALID_CHAR_ERR_MSG_TMPL.format(invalid=findall_to_str(match)),
293290
platform=Platform.WINDOWS,
291+
value=unicode_filepath,
294292
)
295293

296294
_drive, value = self.__split_drive(unicode_filepath)

test/test_filename.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -467,10 +467,10 @@ def test_exception_escape_err_msg(self, value, platform, expected):
467467
print(platform, repr(value))
468468
validate_filename(value, platform=platform)
469469

470-
assert e.value.reason == ErrorReason.INVALID_CHARACTER
470+
assert e.value.reason == expected
471471
assert str(e.value) == (
472-
r"[PV1100] invalid characters found: invalids=('\r'), value='asdf\rsdf', "
473-
"platform=Windows"
472+
r"[PV1100] invalid characters found: invalids=('\r'), "
473+
"platform=Windows, value='asdf\\rsdf'"
474474
) # noqa
475475

476476
@pytest.mark.parametrize(

test/test_filepath.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -522,8 +522,8 @@ def test_exception_escape_err_msg(self, value, platform, expected):
522522

523523
assert e.value.reason == expected
524524
assert str(e.value) == (
525-
r"[PV1100] invalid characters found: invalids=('\r'), value='asdf\rsdf', "
526-
"platform=Windows"
525+
r"[PV1100] invalid characters found: invalids=('\r'), "
526+
"platform=Windows, value='asdf\\rsdf'"
527527
) # noqa
528528

529529
@pytest.mark.parametrize(

0 commit comments

Comments
 (0)