Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Safe names #33

Merged
merged 7 commits into from
Jun 11, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
improved safe_name docstring
  • Loading branch information
sezanzeb committed Jun 8, 2020
commit 9d10bfbd8917a41f24df24fc5abdab980298cf73
6 changes: 4 additions & 2 deletions soundconverter/namegenerator.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,11 @@ def _unicode_to_ascii(unicode_string):

@staticmethod
def safe_name(filename):
"""Replace all characters that are not ascii, digits or '.' '-' '_' '/' with '_'.
"""Make a filename safe to use if it contains special characters than can make things break sometimes

Will not be applied on the part of the path that already exists, as that part apparently is already safe
Replace all characters that are not ascii, digits or '.' '-' '_' '/' with '_'. Umlaute will be changed
to their closest non-umlaut counterpart. Will not be applied on the part of the path that already exists,
as that part apparently is already safe.

Parameters
----------
Expand Down
4 changes: 2 additions & 2 deletions tests/unittests.py
Original file line number Diff line number Diff line change
Expand Up @@ -257,15 +257,15 @@ def test_beautify_uri(self):

def test_safe_name(self):
# 1. path doesn't exist at all
self.assertEqual(self.g.safe_name('/b az/quズx/foo.mp3'), '/b_az/qux/foo.mp3')
self.assertEqual(self.g.safe_name('/b äz/quズx/foo.mp3'), '/b_az/qux/foo.mp3')
self.assertEqual(self.g.safe_name('/baズz/qux'), '/baz/qux')
self.assertEqual(self.g.safe_name('./ qux/foズo.mp3'), './_qux/foo.mp3')
self.assertEqual(self.g.safe_name('./qズux/'), './qux/')
self.assertEqual(self.g.safe_name('/ズfoo.mp3'), '/foo.mp3')
self.assertEqual(self.g.safe_name('fooズ.mp3'), 'foo.mp3')
self.assertEqual(self.g.safe_name('bla /foズo.mp3'), 'bla_/foo.mp3')
self.assertEqual(self.g.safe_name('blズa/'), 'bla/')
self.assertEqual(self.g.safe_name('ズbla'), 'bla')
self.assertEqual(self.g.safe_name('ズblä'), 'bla')

# 2. the outer dir exists
self.assertEqual(self.g.safe_name('/home/qфux/foo.mp3'), '/home/qux/foo.mp3')
Expand Down