From faa0d42084979400104032299936c84606afcd7b Mon Sep 17 00:00:00 2001 From: Gonzalo Odiard Date: Thu, 30 Jan 2014 16:02:38 -0300 Subject: [PATCH] Use Gio.content_type_guess to identify mime type - Fixes #4715 Replace the use of a custom implentation in SugarExt Signed-off-by: Gonzalo Odiard --- src/sugar3/mime.py | 38 +++++++++++--------------------------- 1 file changed, 11 insertions(+), 27 deletions(-) diff --git a/src/sugar3/mime.py b/src/sugar3/mime.py index e08336ff..6a01e5a8 100644 --- a/src/sugar3/mime.py +++ b/src/sugar3/mime.py @@ -124,18 +124,22 @@ def get_for_file(file_name): file_name = os.path.realpath(file_name) - mime_type = SugarExt.mime_get_mime_type_for_file(file_name, None) - if mime_type == 'application/octet-stream': - if _file_looks_like_text(file_name): - return 'text/plain' - else: - return 'application/octet-stream' + f = Gio.File.new_for_path(file_name) + try: + info = f.query_info(Gio.FILE_ATTRIBUTE_STANDARD_CONTENT_TYPE, 0, None) + mime_type = info.get_content_type() + except GLib.GError: + mime_type = Gio.content_type_guess(file_name, None)[0] return mime_type def get_from_file_name(file_name): - return SugarExt.mime_get_mime_type_from_file_name(file_name) + """ + DEPRECATED: 0.102 (removed in 4 releases) + Use Gio.content_type_guess(file_name, None)[0] instead. + """ + return Gio.content_type_guess(file_name, None)[0] def get_mime_icon(mime_type): @@ -258,26 +262,6 @@ def split_uri_list(uri_list): return GLib.uri_list_extract_uris(uri_list) -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: - unicode(sample, encoding) - return True - except Exception: - pass - - return False - - def _get_generic_type_for_mime(mime_type): for generic_type in _generic_types: if mime_type in generic_type['types']: