From dfe8ff68065d5191a2ade265432000327b0de7ce Mon Sep 17 00:00:00 2001 From: Tomeu Vizoso Date: Thu, 16 Aug 2007 19:41:42 +0000 Subject: #2695: Recognize text files as such. --- (limited to 'sugar') 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 -- cgit v0.9.1