From: Oleg Broytman Date: Sat, 29 Jan 2005 19:30:38 +0000 (+0000) Subject: If sys.getdefaultencoding() returns "ascii" - use X-Git-Tag: v4.5.3~325 X-Git-Url: https://git.phdru.name/?a=commitdiff_plain;h=62977582003da9f434cd4fd377837935b6f0520c;p=bookmarks_db.git If sys.getdefaultencoding() returns "ascii" - use locale.getpreferredencoding() to get user's charset. Convert charset names to the canonical form: replace "windows-" by "cp", and make the name lowercase. git-svn-id: file:///home/phd/archive/SVN/bookmarks_db/trunk@51 fdd5c36f-1aea-0310-aeeb-c58d7e2b6c23 --- diff --git a/Robots/parse_html.py b/Robots/parse_html.py index ffacbee..350ad78 100755 --- a/Robots/parse_html.py +++ b/Robots/parse_html.py @@ -2,13 +2,21 @@ """ HTML Parser - Written by BroytMann, Jun 2002 - May 2003. Copyright (C) 1997-2003 PhiloSoft Design + Written by BroytMann. Copyright (C) 1997-2005 PhiloSoft Design """ import sys current_charset = sys.getdefaultencoding() -DEFAULT_CHARSET = "windows-1251" # Stupid default for Russian Cyrillic +if current_charset == "ascii": + try: + import locale + except ImportError: + pass + else: + current_charset = locale.getpreferredencoding() +current_charset = current_charset.replace("windows-", "cp").lower() +DEFAULT_CHARSET = "cp1251" # Stupid default for Russian Cyrillic from HTMLParser import HTMLParseError @@ -96,14 +104,14 @@ def parse_html(filename, charset=None, log=None): title = parser.title if not parser.charset: - ascii = 1 - for c in title: - if not (32 <= ord(c) <= 127): # non-ASCII character - ascii = 0 - break - if not ascii: + try: + unicode(title, "ascii") + except UnicodeDecodeError: parser.charset = DEFAULT_CHARSET + if parser.charset: + parser.charset = parser.charset.replace("windows-", "cp").lower() + if parser.charset and (parser.charset <> current_charset): try: if parser.meta_charset: