Changeset 3287 for pykota/trunk/pykota/tool.py
- Timestamp:
- 01/10/08 23:01:47 (16 years ago)
- Files:
-
- 1 modified
Legend:
- Unmodified
- Added
- Removed
-
pykota/trunk/pykota/tool.py
r3286 r3287 39 39 from mx import DateTime 40 40 41 try :42 import chardet43 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 52 41 from pykota import config, storage, logger 53 42 from pykota.version import __version__, __author__, __years__, __gplblurb__ … … 63 52 Exception.__init__(self, message) 64 53 def __repr__(self): 65 return self.message 54 return self.message.encode(sys.stdout.encoding or "UTF-8", "replace") 66 55 __str__ = __repr__ 67 56 … … 142 131 self.charset = self.charset or locale.getpreferredencoding() 143 132 except locale.Error : 144 self.charset = sys. getfilesystemencoding()133 self.charset = sys.stdout.encoding or sys.getfilesystemencoding() 145 134 146 135 # Dirty hack : if the charset is ASCII, we can safely use UTF-8 instead … … 158 147 except TypeError : # Python <2.4 159 148 trans = gettext.translation("pykota", languages=["%s.%s" % (self.language, self.charset)]) 160 trans.install( )149 trans.install(unicode=True) 161 150 except : 162 gettext.NullTranslations().install( )151 gettext.NullTranslations().install(unicode=True) 163 152 164 153 # pykota specific stuff … … 215 204 if text is None : 216 205 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") 226 208 227 209 def userCharsetToUTF8(self, text) : … … 229 211 if text is None : 230 212 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") 250 215 251 216 def display(self, message) : 252 217 """Display a message but only if stdout is a tty.""" 253 218 if sys.stdout.isatty() : 254 sys.stdout.write(message) 219 sys.stdout.write(message.encode(sys.stdout.encoding or "UTF-8", \ 220 "replace")) 255 221 sys.stdout.flush() 256 222 … … 258 224 """Logs something to debug output if debug is enabled.""" 259 225 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") 261 230 262 231 def printInfo(self, message, level="info") : 263 232 """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"))) 265 237 sys.stderr.flush() 266 238