Skip to content

Commit

Permalink
refactor: lint
Browse files Browse the repository at this point in the history
  • Loading branch information
vzhd1701 committed Dec 31, 2021
1 parent 8bea1e6 commit e0d4039
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 21 deletions.
34 changes: 18 additions & 16 deletions evernote_backup/note_exporter_util.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import os
from typing import Dict


MAX_FILE_NAME_LEN = 255


Expand Down Expand Up @@ -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}")
16 changes: 11 additions & 5 deletions tests/test_note_exporter_util.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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))
Expand All @@ -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))
Expand All @@ -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):
Expand Down

0 comments on commit e0d4039

Please sign in to comment.