Show
Ignore:
Timestamp:
01/10/08 23:01:47 (16 years ago)
Author:
jerome
Message:

Removed the dependency on python-chardet entirely. Now uses unicode
or UTF-8 strings all over the place : UTF-8 still used for datas
coming from/going to the database (TODO). Conversion to end user's
locale charset is now only done when needed. PyKota Exceptions need
a base class which, for now, will handle the charset translation,
until we get the time to replace internal loggers with Python's
logging module...

Files:
1 modified

Legend:

Unmodified
Added
Removed
  • pykota/trunk/pykota/tool.py

    r3286 r3287  
    3939from mx import DateTime 
    4040 
    41 try : 
    42     import chardet 
    43 except ImportError :     
    44     def detectCharset(text) : 
    45         """Fakes a charset detection if the chardet module is not installed.""" 
    46         return "ISO-8859-15" 
    47 else :     
    48     def detectCharset(text) : 
    49         """Uses the chardet module to workaround CUPS lying to us.""" 
    50         return chardet.detect(text)["encoding"] or "UTF-8" 
    51  
    5241from pykota import config, storage, logger 
    5342from pykota.version import __version__, __author__, __years__, __gplblurb__ 
     
    6352        Exception.__init__(self, message) 
    6453    def __repr__(self): 
    65         return self.message 
     54        return self.message.encode(sys.stdout.encoding or "UTF-8", "replace") 
    6655    __str__ = __repr__ 
    6756     
     
    142131            self.charset = self.charset or locale.getpreferredencoding() 
    143132        except locale.Error :     
    144             self.charset = sys.getfilesystemencoding() 
     133            self.charset = sys.stdout.encoding or sys.getfilesystemencoding() 
    145134         
    146135        # Dirty hack : if the charset is ASCII, we can safely use UTF-8 instead 
     
    158147            except TypeError : # Python <2.4 
    159148                trans = gettext.translation("pykota", languages=["%s.%s" % (self.language, self.charset)]) 
    160             trans.install() 
     149            trans.install(unicode=True) 
    161150        except : 
    162             gettext.NullTranslations().install() 
     151            gettext.NullTranslations().install(unicode=True) 
    163152     
    164153        # pykota specific stuff 
     
    215204        if text is None : 
    216205            return None 
    217         try : 
    218             return text.decode("UTF-8").encode(self.charset, "replace")  
    219         except (UnicodeError, AttributeError) :     
    220             try : 
    221                 # Maybe already in Unicode ? 
    222                 return text.encode(self.charset, "replace")  
    223             except (UnicodeError, AttributeError) : 
    224                 # Try to autodetect the charset 
    225                 return text.decode(detectCharset(text), "replace").encode(self.charset, "replace") 
     206        else :     
     207            return text.decode("UTF-8", "replace").encode(self.charset, "replace")  
    226208         
    227209    def userCharsetToUTF8(self, text) : 
     
    229211        if text is None : 
    230212            return None 
    231         try : 
    232             # We don't necessarily trust the default charset, because 
    233             # xprint sends us titles in UTF-8 but CUPS gives us an ISO-8859-1 charset ! 
    234             # So we first try to see if the text is already in UTF-8 or not, and 
    235             # if it is, we delete characters which can't be converted to the user's charset, 
    236             # then convert back to UTF-8. PostgreSQL 7.3.x used to reject some unicode characters, 
    237             # this is fixed by the ugly line below : 
    238             return text.decode("UTF-8").encode(self.charset, "replace").decode(self.charset).encode("UTF-8", "replace") 
    239         except (UnicodeError, AttributeError) : 
    240             try : 
    241                 return text.decode(self.charset).encode("UTF-8", "replace")  
    242             except (UnicodeError, AttributeError) :     
    243                 try : 
    244                     # Maybe already in Unicode ? 
    245                     return text.encode("UTF-8", "replace")  
    246                 except (UnicodeError, AttributeError) : 
    247                     # Try to autodetect the charset 
    248                     return text.decode(detectCharset(text), "replace").encode("UTF-8", "replace") 
    249         return newtext 
     213        else :     
     214            return text.decode(self.charset, "replace").encode("UTF-8", "replace")     
    250215         
    251216    def display(self, message) : 
    252217        """Display a message but only if stdout is a tty.""" 
    253218        if sys.stdout.isatty() : 
    254             sys.stdout.write(message) 
     219            sys.stdout.write(message.encode(sys.stdout.encoding or "UTF-8", \ 
     220                                            "replace")) 
    255221            sys.stdout.flush() 
    256222             
     
    258224        """Logs something to debug output if debug is enabled.""" 
    259225        if self.debug : 
    260             self.logger.log_message(message, "debug") 
     226            self.logger.log_message(message.encode(sys.stdout.encoding \ 
     227                                                       or "UTF-8", \ 
     228                                                   "replace"), \ 
     229                                    "debug") 
    261230             
    262231    def printInfo(self, message, level="info") :         
    263232        """Sends a message to standard error.""" 
    264         sys.stderr.write("%s: %s\n" % (level.upper(), message)) 
     233        sys.stderr.write("%s: %s\n" % (level.upper(), \ 
     234                                       message.encode(sys.stdout.encoding \ 
     235                                                          or "UTF-8", \ 
     236                                                      "replace"))) 
    265237        sys.stderr.flush() 
    266238