Utilities/__init__.py

changeset 3394
d1d4d79b4f11
parent 3206
f516fc427551
child 3432
7cfe7471175d
equal deleted inserted replaced
3393:080ace4829b4 3394:d1d4d79b4f11
248 except (UnicodeError, LookupError): 248 except (UnicodeError, LookupError):
249 pass 249 pass
250 250
251 # Assume UTF-8 loosing information 251 # Assume UTF-8 loosing information
252 return str(text, "utf-8", "ignore"), 'utf-8-ignore' 252 return str(text, "utf-8", "ignore"), 'utf-8-ignore'
253
254
255 def readEncodedFileWithEncoding(filename, encoding):
256 """
257 Function to read a file and decode its contents into proper text.
258
259 @param filename name of the file to read (string)
260 @keyparam encoding encoding to be used to read the file (string)
261 @return tuple of decoded text and encoding (string, string)
262 """
263 f = open(filename, "rb")
264 text = f.read()
265 f.close()
266 if encoding:
267 try:
268 return str(text, encoding), '{0}-selected'.format(encoding)
269 except (UnicodeError, LookupError):
270 pass
271 # Try default encoding
272 try:
273 codec = Preferences.getEditor("DefaultEncoding")
274 return str(text, codec), '{0}-default'.format(codec)
275 except (UnicodeError, LookupError):
276 pass
277 # Assume UTF-8 loosing information
278 return str(text, "utf-8", "ignore"), 'utf-8-ignore'
279 else:
280 return decode(text)
253 281
254 282
255 def writeEncodedFile(filename, text, orig_coding): 283 def writeEncodedFile(filename, text, orig_coding):
256 """ 284 """
257 Function to write a file with properly encoded text. 285 Function to write a file with properly encoded text.

eric ide

mercurial