move _sanitize_for_path to module namespace

This commit is contained in:
Adrian Sampson 2011-01-23 19:59:41 -08:00
parent 6ade5635e1
commit 75e0924832
2 changed files with 26 additions and 17 deletions

View file

@ -239,6 +239,22 @@ def _sanitize_path(path, pathmod=None):
comps[i] = comp
return pathmod.join(*comps)
def _sanitize_for_path(value, pathmod, key=None):
"""Sanitize the value for inclusion in a path: replace separators
with _, etc. Doesn't guarantee that the whole path will be valid;
you should still call _sanitize_path on the complete path.
"""
if isinstance(value, basestring):
for sep in (pathmod.sep, pathmod.altsep):
if sep:
value = value.replace(sep, '_')
elif key in ('track', 'tracktotal', 'disc', 'disctotal'):
# pad with zeros
value = '%02i' % value
else:
value = str(value)
return value
# Library items (songs).
@ -846,21 +862,6 @@ class Library(BaseLibrary):
self.conn.executescript(setup_sql)
self.conn.commit()
def _sanitize_for_path(self, value, pathmod, key=None):
"""Sanitize the value for inclusion in a path: replace separators
with _, etc.
"""
if isinstance(value, basestring):
for sep in (pathmod.sep, pathmod.altsep):
if sep:
value = value.replace(sep, '_')
elif key in ('track', 'tracktotal', 'disc', 'disctotal'):
# pad with zeros
value = '%02i' % value
else:
value = str(value)
return value
def destination(self, item, pathmod=None):
"""Returns the path in the library directory designated for item
item (i.e., where the file ought to be).
@ -888,11 +889,11 @@ class Library(BaseLibrary):
else:
# From Item.
value = getattr(item, key)
mapping[key] = self._sanitize_for_path(value, pathmod, key)
mapping[key] = _sanitize_for_path(value, pathmod, key)
# Use the track's artist if it differs
if item.albumartist and item.albumartist != item.artist:
mapping['artist'] = self._sanitize_for_path(item.artist, pathmod)
mapping['artist'] = _sanitize_for_path(item.artist, pathmod)
# Perform substitution.
subpath = subpath_tmpl.substitute(mapping)

View file

@ -296,6 +296,14 @@ class DestinationTest(unittest.TestCase):
p = beets.library._sanitize_path('one/two /three', ntpath)
self.assertFalse(' ' in p)
def test_component_sanitize_replaces_separators(self):
name = posixpath.join('a', 'b')
newname = beets.library._sanitize_for_path(name, posixpath)
self.assertNotEqual(name, newname)
def test_component_sanitize_pads_with_zero(self):
name = beets.library._sanitize_for_path(1, posixpath, 'track')
self.assertTrue(name.startswith('0'))
class MigrationTest(unittest.TestCase):
"""Tests the ability to change the database schema between