add is_encoding() method

git-svn-id: https://linkchecker.svn.sourceforge.net/svnroot/linkchecker/trunk/linkchecker@2980 e7d03fd6-7b0d-0410-9947-9c21f3af8025
This commit is contained in:
calvin 2005-12-18 08:16:36 +00:00
parent cff9b1341b
commit d28567753b

View file

@ -21,6 +21,7 @@ necessarily optimised for large strings, so use with care.
import re
import textwrap
import codecs
import os
import time
import urlparse
@ -52,10 +53,10 @@ def ascii_safe (s):
@param s: the Unicode string to be encoded
@type s: unicode or None
@return: encoded ASCII version of s, or s itself if s evaluated to False
@return: encoded ASCII version of s, or None if s was None
@rtype: string
"""
if s:
if isinstance(s, unicode):
s = s.encode('ascii', 'ignore')
return s
@ -71,6 +72,16 @@ def is_ascii (s):
return False
def is_encoding (text):
"""
Check if string is a valid encoding.
"""
try:
return codecs.lookup(text)
except LookupError:
return False
def url_unicode_split (url):
"""
Like urlparse.urlsplit(), but always returning unicode parts.