Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
path: root/sugar
diff options
context:
space:
mode:
authorTomeu Vizoso <tomeu@tomeuvizoso.net>2007-08-16 19:41:42 (GMT)
committer Tomeu Vizoso <tomeu@tomeuvizoso.net>2007-08-16 19:41:42 (GMT)
commitdfe8ff68065d5191a2ade265432000327b0de7ce (patch)
tree39d59c774a5654fd303c301e36044af01db7ebe1 /sugar
parentcace8c0a478065c4d7b815291974bc7510f5942b (diff)
#2695: Recognize text files as such.
Diffstat (limited to 'sugar')
-rw-r--r--sugar/objects/mime.py28
1 files changed, 27 insertions, 1 deletions
diff --git a/sugar/objects/mime.py b/sugar/objects/mime.py
index 80eac9b..74e4e16 100644
--- a/sugar/objects/mime.py
+++ b/sugar/objects/mime.py
@@ -1,4 +1,5 @@
# Copyright (C) 2006-2007, Red Hat, Inc.
+# Copyright (C) 2007, One Laptop Per Child
#
# This library is free software; you can redistribute it and/or
# modify it under the terms of the GNU Lesser General Public
@@ -20,7 +21,13 @@ import logging
from sugar import _sugarext
def get_for_file(file_name):
- return _sugarext.get_mime_type_for_file(file_name)
+ mime_type = _sugarext.get_mime_type_for_file(file_name)
+ if mime_type == 'application/octet-stream':
+ if _file_looks_like_text(file_name):
+ return 'text/plain'
+ else:
+ return 'application/octet-stream'
+ return mime_type
def get_from_file_name(file_name):
return _sugarext.get_mime_type_from_file_name(file_name)
@@ -80,3 +87,22 @@ def choose_most_significant(mime_types):
logging.debug('Returning first: %r.' % mime_types[0])
return mime_types[0]
+
+def _file_looks_like_text(file_name):
+ f = open(file_name, 'r')
+ try:
+ sample = f.read(256)
+ finally:
+ f.close()
+
+ if '\000' in sample:
+ return False
+
+ for encoding in ('ascii', 'latin_1', 'utf_8', 'utf_16'):
+ try:
+ string = unicode(sample, encoding)
+ return True
+ except Exception, e:
+ pass
+
+ return False