summary refs log tree commit diff stats
path: root/ranger
diff options
context:
space:
mode:
authorhut <hut@lavabit.com>2011-04-26 21:01:46 +0200
committerhut <hut@lavabit.com>2011-04-26 21:01:46 +0200
commit755e7df1a75b48e068113bd9811c68a84418dc68 (patch)
treeb708d052dbaccae3881b80c85343bba539c70163 /ranger
parent7c33d7792efa6c4a1e56310c18ff04a721a2ea50 (diff)
downloadranger-755e7df1a75b48e068113bd9811c68a84418dc68.tar.gz
Use chardet in case of encoding detection problems
Diffstat (limited to 'ranger')
-rw-r--r--ranger/core/loader.py24
1 files changed, 19 insertions, 5 deletions
diff --git a/ranger/core/loader.py b/ranger/core/loader.py
index 4341595c..7fd7dbab 100644
--- a/ranger/core/loader.py
+++ b/ranger/core/loader.py
@@ -23,7 +23,11 @@ import math
 import os
 import sys
 import select
-
+try:
+	import chardet
+	HAVE_CHARDET = True
+except:
+	HAVE_CHARDET = False
 
 class Loadable(object):
 	paused = False
@@ -89,13 +93,13 @@ class CommandLoader(Loadable, SignalDispatcher, FileManagerAware):
 						if rd == process.stderr:
 							read = rd.readline()
 							if py3:
-								read = read.decode('utf-8')
+								read = safeDecode(read)
 							if read:
 								self.fm.notify(read, bad=True)
 						elif rd == process.stdout:
 							read = rd.read(512)
 							if py3:
-								read = read.decode('utf-8')
+								read = safeDecode(read)
 							if read:
 								self.stdout_buffer += read
 				except select.error:
@@ -103,12 +107,12 @@ class CommandLoader(Loadable, SignalDispatcher, FileManagerAware):
 			if not self.silent:
 				for l in process.stderr.readlines():
 					if py3:
-						l = l.decode('utf-8')
+						l = safeDecode(l)
 					self.fm.notify(l, bad=True)
 			if self.read:
 				read = process.stdout.read()
 				if py3:
-					read = read.decode('utf-8')
+					read = safeDecode(read)
 				self.stdout_buffer += read
 		self.finished = True
 		self.signal_emit('after', process=process, loader=self)
@@ -137,6 +141,16 @@ class CommandLoader(Loadable, SignalDispatcher, FileManagerAware):
 			self.process.kill()
 
 
+def safeDecode(string):
+	try:
+		return string.decode("utf-8")
+	except (UnicodeDecodeError):
+		if HAVE_CHARDET:
+			return string.decode(chardet.detect(str)["encoding"])
+		else:
+			return ""
+
+
 class Loader(FileManagerAware):
 	seconds_of_work_time = 0.03
 	throbber_chars = r'/-\|'
'n222' href='#n222'>222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323