utils: move elide_filename to own function
Also increase the elide limit in TempDownloadManager to 50, since that's probably still enough for all systems.
This commit is contained in:
parent
2f50d100e9
commit
716ce701f5
@ -1334,8 +1334,7 @@ class TempDownloadManager(QObject):
|
|||||||
encoding = sys.getfilesystemencoding()
|
encoding = sys.getfilesystemencoding()
|
||||||
suggested_name = utils.force_encoding(suggested_name, encoding)
|
suggested_name = utils.force_encoding(suggested_name, encoding)
|
||||||
# Make sure that the filename is not too long
|
# Make sure that the filename is not too long
|
||||||
if len(suggested_name) > 50:
|
suggested_name = utils.elide_filename(suggested_name, 50)
|
||||||
suggested_name = suggested_name[:25] + '...' + suggested_name[-25:]
|
|
||||||
fobj = tempfile.NamedTemporaryFile(dir=tmpdir.name, delete=False,
|
fobj = tempfile.NamedTemporaryFile(dir=tmpdir.name, delete=False,
|
||||||
suffix=suggested_name)
|
suffix=suggested_name)
|
||||||
self.files.append(fobj)
|
self.files.append(fobj)
|
||||||
|
@ -58,6 +58,38 @@ def elide(text, length):
|
|||||||
return text[:length - 1] + '\u2026'
|
return text[:length - 1] + '\u2026'
|
||||||
|
|
||||||
|
|
||||||
|
def elide_filename(filename, length):
|
||||||
|
"""Elide a filename to the given length.
|
||||||
|
|
||||||
|
The difference to the elide() is that the text is removed from
|
||||||
|
the middle instead of from the end. This preserves file name extensions.
|
||||||
|
Additionally, standard ASCII dots are used ("...") instead of the unicode
|
||||||
|
"…" (U+2026) so it works regardless of the filesystem encoding.
|
||||||
|
|
||||||
|
This function does not handle path separators.
|
||||||
|
|
||||||
|
Args:
|
||||||
|
filename: The filename to elide.
|
||||||
|
length: The maximum length of the filename, must be at least 3.
|
||||||
|
|
||||||
|
Return:
|
||||||
|
The elided filename.
|
||||||
|
"""
|
||||||
|
elidestr = '...'
|
||||||
|
if length < len(elidestr):
|
||||||
|
raise ValueError('length must be greater or equal to 3')
|
||||||
|
if len(filename) <= length:
|
||||||
|
return filename
|
||||||
|
# Account for '...'
|
||||||
|
length -= len(elidestr)
|
||||||
|
left = length // 2
|
||||||
|
right = length - left
|
||||||
|
if right == 0:
|
||||||
|
return filename[:left] + elidestr
|
||||||
|
else:
|
||||||
|
return filename[:left] + elidestr + filename[-right:]
|
||||||
|
|
||||||
|
|
||||||
def compact_text(text, elidelength=None):
|
def compact_text(text, elidelength=None):
|
||||||
"""Remove leading whitespace and newlines from a text and maybe elide it.
|
"""Remove leading whitespace and newlines from a text and maybe elide it.
|
||||||
|
|
||||||
|
@ -94,6 +94,24 @@ class TestEliding:
|
|||||||
assert utils.elide(text, length) == expected
|
assert utils.elide(text, length) == expected
|
||||||
|
|
||||||
|
|
||||||
|
class TestElidingFilenames:
|
||||||
|
|
||||||
|
"""Test elide_filename."""
|
||||||
|
|
||||||
|
def test_too_small(self):
|
||||||
|
"""Test eliding to less than 3 characters which should fail."""
|
||||||
|
with pytest.raises(ValueError):
|
||||||
|
utils.elide_filename('foo', 1)
|
||||||
|
|
||||||
|
@pytest.mark.parametrize('filename, length, expected', [
|
||||||
|
('foobar', 3, '...'),
|
||||||
|
('foobar.txt', 50, 'foobar.txt'),
|
||||||
|
('foobarbazqux.py', 10, 'foo...x.py'),
|
||||||
|
])
|
||||||
|
def test_elided(self, filename, length, expected):
|
||||||
|
assert utils.elide_filename(filename, length) == expected
|
||||||
|
|
||||||
|
|
||||||
@pytest.fixture(params=[True, False])
|
@pytest.fixture(params=[True, False])
|
||||||
def freezer(request, monkeypatch):
|
def freezer(request, monkeypatch):
|
||||||
if request.param and not getattr(sys, 'frozen', False):
|
if request.param and not getattr(sys, 'frozen', False):
|
||||||
|
Loading…
Reference in New Issue
Block a user