From e0d40398106577997696dfe5ae8073ab9ab9b38d Mon Sep 17 00:00:00 2001 From: vzhd1701 Date: Fri, 31 Dec 2021 12:33:41 +0500 Subject: [PATCH] refactor: lint --- evernote_backup/note_exporter_util.py | 34 ++++++++++++++------------- tests/test_note_exporter_util.py | 16 +++++++++---- 2 files changed, 29 insertions(+), 21 deletions(-) diff --git a/evernote_backup/note_exporter_util.py b/evernote_backup/note_exporter_util.py index 46d378c..9e102e2 100644 --- a/evernote_backup/note_exporter_util.py +++ b/evernote_backup/note_exporter_util.py @@ -1,7 +1,6 @@ import os from typing import Dict - MAX_FILE_NAME_LEN = 255 @@ -82,25 +81,28 @@ def _get_non_existant_name(safe_name: str, target_dir: str) -> str: return safe_name -def _trim_name(safe_name: str, max_len=MAX_FILE_NAME_LEN) -> str: - """ Trim file name to 255 characters while maintaining extension - 255 characters is max file name length on linux and macOS - Windows has a path limit of 260 characters which includes - the entire path (drive letter, path, and file name) - This does not trim the path length, just the file name +def _trim_name(safe_name: str, max_len: int = MAX_FILE_NAME_LEN) -> str: + """Trim file name to 255 characters while maintaining extension + 255 characters is max file name length on linux and macOS + Windows has a path limit of 260 characters which includes + the entire path (drive letter, path, and file name) + This does not trim the path length, just the file name - max_len: if provided, trims to this length otherwise MAX_FILE_NAME_LEN + max_len: if provided, trims to this length otherwise MAX_FILE_NAME_LEN - Raises: ValueError if the file name is too long and cannot be trimmed + Raises: ValueError if the file name is too long and cannot be trimmed """ if len(safe_name) <= max_len: return safe_name + name, ext = os.path.splitext(safe_name) + drop_chars = len(safe_name) - max_len - file_parts = safe_name.rsplit(".", 1) - if len(file_parts) != 2: - return f"{file_parts[0][:-drop_chars]}" - if len(file_parts[0]) > drop_chars: - return f"{file_parts[0][:-drop_chars]}.{file_parts[1]}" - else: - raise ValueError("File name is too long but cannot be safely trimmed: {safe_name}") # noqa: E501 + trimmed_name = name[:-drop_chars] + + if not ext: + return trimmed_name + if len(name) > drop_chars: + return f"{trimmed_name}{ext}" + + raise ValueError(f"File name is too long but cannot be safely trimmed: {safe_name}") diff --git a/tests/test_note_exporter_util.py b/tests/test_note_exporter_util.py index 2c01868..176dc81 100644 --- a/tests/test_note_exporter_util.py +++ b/tests/test_note_exporter_util.py @@ -4,6 +4,7 @@ from evernote_backup import note_exporter_util from evernote_backup.note_exporter_util import ( + MAX_FILE_NAME_LEN, SafePath, _get_non_existant_name, _get_safe_path, @@ -63,8 +64,10 @@ def test_get_safe_path(): def test_safe_path_long_file_name(tmp_path): """Test that SafePath trims a long file name with extension""" test_dir = tmp_path / "test" - long_file_name = "X" * 255 + ".ext" - expected_file_name = "X" * 251 + ".ext" + + test_ext = ".ext" + long_file_name = "X" * MAX_FILE_NAME_LEN + test_ext + expected_file_name = "X" * (MAX_FILE_NAME_LEN - len(test_ext)) + test_ext expected_file = tmp_path / "test" / "test1" / expected_file_name safe_path = SafePath(str(test_dir)) @@ -79,8 +82,11 @@ def test_safe_path_long_file_name(tmp_path): def test_safe_path_long_file_name_no_ext(tmp_path): """Test that SafePath trims a long file name with no extension""" test_dir = tmp_path / "test" - long_file_name = "X" * 260 - expected_file_name = "X" * 255 + + slightly_longer_than_supported = MAX_FILE_NAME_LEN + 10 + + long_file_name = "X" * slightly_longer_than_supported + expected_file_name = "X" * MAX_FILE_NAME_LEN expected_file = tmp_path / "test" / "test1" / expected_file_name safe_path = SafePath(str(test_dir)) @@ -95,7 +101,7 @@ def test_safe_path_long_file_name_no_ext(tmp_path): def test_safe_path_long_file_name_invalid(tmp_path): """Test that SafePath raises ValueError if path is too long but cannot be trimmed""" test_dir = tmp_path / "test" - bad_file_name = "X" + "." + "x" * 255 + bad_file_name = "X" + "." + "x" * MAX_FILE_NAME_LEN safe_path = SafePath(str(test_dir)) with pytest.raises(ValueError):