Title-related changes, better sanitization (bug 1351)

This commit is contained in:
Thomas Perl 2011-08-08 13:06:20 +02:00
parent 714251a7a9
commit c077f38b8c
2 changed files with 32 additions and 12 deletions

View File

@ -849,9 +849,18 @@ class PodcastChannel(PodcastModelObject):
"""
return self.EpisodeClass.create_from_dict(d, self)
def _consume_updated_title(self, new_title):
# Replace multi-space and newlines with single space (Maemo bug 11173)
new_title = re.sub('\s+', ' ', new_title).strip()
# Only update the podcast-supplied title when we
# don't yet have a title, or if the title is the
# feed URL (e.g. we didn't find a title before).
if not self.title or self.title == self.url:
self.title = new_title
def _consume_custom_feed(self, custom_feed, max_episodes=0):
if not self.title:
self.title = custom_feed.get_title()
self._consume_updated_title(custom_feed.get_title())
self.link = custom_feed.get_link()
self.description = custom_feed.get_description()
self.cover_url = custom_feed.get_image()
@ -874,9 +883,7 @@ class PodcastChannel(PodcastModelObject):
def _consume_updated_feed(self, feed, max_episodes=0, mimetype_prefs=''):
#self.parse_error = feed.get('bozo_exception', None)
# Replace multi-space and newlines with single space (Maemo bug 11173)
if not self.title:
self.title = re.sub('\s+', ' ', feed.feed.get('title', self.url))
self._consume_updated_title(feed.feed.get('title', self.url))
self.link = feed.feed.get('link', self.link)
self.description = feed.feed.get('subtitle', self.description)

View File

@ -42,6 +42,7 @@ import stat
import shlex
import socket
import sys
import string
import re
import subprocess
@ -94,6 +95,20 @@ if encoding is None:
logger.info('Assuming encoding: ISO-8859-15 ($LANG not set).')
# Filename / folder name sanitization
def _sanitize_char(c):
if c in string.whitespace:
return ' '
elif c in ',-.()':
return c
elif c in string.punctuation or ord(c) <= 31:
return '_'
return c
SANITIZATION_TABLE = ''.join(map(_sanitize_char, map(chr, range(256))))
del _sanitize_char
# Used by file_type_by_extension()
_BUILTIN_FILE_TYPES = None
@ -1131,12 +1146,6 @@ def sanitize_filename(filename, max_length=0, use_ascii=False):
If use_ascii is True, don't encode in the native language,
but use only characters from the ASCII character set.
"""
global encoding
if use_ascii:
e = 'ascii'
else:
e = encoding
if not isinstance(filename, unicode):
filename = filename.decode(encoding, 'ignore')
@ -1145,7 +1154,11 @@ def sanitize_filename(filename, max_length=0, use_ascii=False):
filename, max_length)
filename = filename[:max_length]
return re.sub('[/|?*<>:+\[\]\"\\\]', '_', filename.strip().encode(e, 'ignore'))
filename = filename.encode('ascii' if use_ascii else encoding, 'ignore')
filename = filename.translate(SANITIZATION_TABLE)
filename = filename.strip('.' + string.whitespace)
return filename
def find_mount_point(directory):