From 75e092483259c9830fc461b10356385b16e80af6 Mon Sep 17 00:00:00 2001 From: Adrian Sampson Date: Sun, 23 Jan 2011 19:59:41 -0800 Subject: [PATCH] move _sanitize_for_path to module namespace --- beets/library.py | 35 ++++++++++++++++++----------------- test/test_db.py | 8 ++++++++ 2 files changed, 26 insertions(+), 17 deletions(-) diff --git a/beets/library.py b/beets/library.py index 184ef2766..4cc1f7204 100644 --- a/beets/library.py +++ b/beets/library.py @@ -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) diff --git a/test/test_db.py b/test/test_db.py index 9299e3575..4463cf680 100644 --- a/test/test_db.py +++ b/test/test_db.py @@ -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