more filtering

This commit is contained in:
Thomas Vander Stichele
2013-07-13 22:46:20 +02:00
parent 3ee98930c2
commit 34d58e2b5c
4 changed files with 33 additions and 7 deletions

View File

@@ -28,17 +28,31 @@ class PathFilter(object):
I filter path components for safe storage on file systems.
"""
def __init__(self, slashes=True, fat=True, special=True):
def __init__(self, slashes=True, quotes=True, fat=True, special=False):
"""
@param slashes: whether to convert slashes to dashes
@parm fat: whether to strip characters illegal on FAT filesystems
@param quotes: whether to normalize quotes
@param fat: whether to strip characters illegal on FAT filesystems
@param special: whether to strip special characters
"""
self._slashes = slashes
self._quotes = quotes
self._fat = fat
self._special = special
def filter(self, path):
if self._slashes:
path = re.sub(r'[/]', '-', path, re.UNICODE)
path = re.sub(r'[/\\]', '-', path, re.UNICODE)
if self._quotes:
path = re.sub(ur'[\u2019]', "'", path, re.UNICODE)
if self._special:
# replace separators with a hyphen
path = re.sub(r'[:\|]', '-', path, re.UNICODE)
path = re.sub(r'[\*\?&!\'\"\$\(\)`{}\[\]<>]', '_', path, re.UNICODE)
if self._fat:
path = re.sub(r'[:\*\?"<>|"]', '_', path, re.UNICODE)
return path

View File

@@ -67,11 +67,14 @@ class Program(log.Loggable):
d = {}
for key in ['fat', 'special']:
for key, default in {
'fat': True,
'special': False
}.items():
value = None
value = self._config.getboolean('main', 'path_filter_'+ key)
if value is None:
value = True
value = default
d[key] = value

View File

@@ -9,8 +9,17 @@ from morituri.test import common
class FilterTestCase(common.TestCase):
def setUp(self):
self._filter = path.PathFilter()
self._filter = path.PathFilter(special=True)
def testSlash(self):
part = u'A Charm/A Blade'
self.assertEquals(self._filter.filter(part), u'A Charm-A Blade')
def testFat(self):
part = u'A Word: F**k you?'
self.assertEquals(self._filter.filter(part), u'A Word- F__k you_')
def testSpecial(self):
part = u'<<< $&*!\' "()`{}[]spaceship>>>'
self.assertEquals(self._filter.filter(part),
u'___ _____ ________spaceship___')

View File

@@ -69,7 +69,7 @@ class VersionTestCase(common.TestCase):
self.failUnless(v)
# of the form III 10.2
# make sure it ends with a digit
self.failUnless(int(v[-1]))
self.failUnless(int(v[-1]), v)
class AnalyzeFileTask(cdparanoia.AnalyzeTask):