You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

56 lines
1.6 KiB

14 years ago
from couchpotato.core.logger import CPLog
from string import ascii_letters, digits
import re
import unicodedata
log = CPLog(__name__)
14 years ago
14 years ago
def toSafeString(original):
valid_chars = "-_.() %s%s" % (ascii_letters, digits)
cleanedFilename = unicodedata.normalize('NFKD', toUnicode(original)).encode('ASCII', 'ignore')
return ''.join(c for c in cleanedFilename if c in valid_chars)
def simplifyString(original):
string = stripAccents(original.lower())
string = toSafeString(' '.join(re.split('\W+', string)))
14 years ago
split = re.split('\W+', string.lower())
return toUnicode(' '.join(split))
def toUnicode(original, *args):
try:
if type(original) is unicode:
14 years ago
return original
else:
13 years ago
try:
return unicode(original, *args)
except:
try:
return ek(original, *args)
except:
raise
14 years ago
except UnicodeDecodeError:
log.error('Unable to decode value: %s... ' % repr(original)[:20])
14 years ago
ascii_text = str(original).encode('string_escape')
13 years ago
return toUnicode(ascii_text)
def ek(original, *args):
if type(original) in [str, unicode]:
try:
from couchpotato.environment import Env
return original.decode(Env.get('encoding'))
except UnicodeDecodeError:
13 years ago
raise
return original
def isInt(value):
try:
int(value)
return True
except ValueError:
return False
def stripAccents(s):
return ''.join((c for c in unicodedata.normalize('NFD', toUnicode(s)) if unicodedata.category(c) != 'Mn'))