From d28567753b91a16965745da9d16797532592ba96 Mon Sep 17 00:00:00 2001 From: calvin Date: Sun, 18 Dec 2005 08:16:36 +0000 Subject: [PATCH] add is_encoding() method git-svn-id: https://linkchecker.svn.sourceforge.net/svnroot/linkchecker/trunk/linkchecker@2980 e7d03fd6-7b0d-0410-9947-9c21f3af8025 --- linkcheck/strformat.py | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/linkcheck/strformat.py b/linkcheck/strformat.py index 449cfbdb..29602ce8 100644 --- a/linkcheck/strformat.py +++ b/linkcheck/strformat.py @@ -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.