Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorTomeu Vizoso <tomeu@tomeuvizoso.net>2007-08-20 08:50:20 (GMT)
committer Tomeu Vizoso <tomeu@tomeuvizoso.net>2007-08-20 08:50:20 (GMT)
commitfff0daf8c4aef388f12f801162a050f13acd6cb8 (patch)
tree8ef155e280bf4a56f2b98b114a26144e5a539bb2
parent7a641198433762c83a84e9464f103e33e38f8fa9 (diff)
Improvements in mime handling and added some tests.
-rw-r--r--services/shell/clipboardobject.py7
-rw-r--r--sugar/objects/mime.py10
-rw-r--r--tests/sugar/runall.py2
-rw-r--r--tests/sugar/test_mime.py72
4 files changed, 84 insertions, 7 deletions
diff --git a/services/shell/clipboardobject.py b/services/shell/clipboardobject.py
index 65f3bc5..c3cdab3 100644
--- a/services/shell/clipboardobject.py
+++ b/services/shell/clipboardobject.py
@@ -101,8 +101,11 @@ class ClipboardObject:
if len(uris) == 1 or not uris[1]:
uri = urlparse.urlparse(uris[0], 'file')
if uri.scheme == 'file':
- logging.debug('Choosed %r!' % mime.get_for_file(uri.path))
- format = mime.get_for_file(uri.path)
+ if os.path.exists(uri.path):
+ format = mime.get_for_file(uri.path)
+ else:
+ format = mime.get_from_file_name(uri.path)
+ logging.debug('Choosed %r!' % format)
return format
diff --git a/sugar/objects/mime.py b/sugar/objects/mime.py
index 74e4e16..b933143 100644
--- a/sugar/objects/mime.py
+++ b/sugar/objects/mime.py
@@ -58,13 +58,9 @@ def choose_most_significant(mime_types):
if 'text/uri-list' in mime_types:
return 'text/uri-list'
- for mime_category in ['image/', 'text/', 'application/']:
+ for mime_category in ['image/', 'application/']:
for mime_type in mime_types:
- # skip text/plain and text/html, these have lower priority.
- if mime_type in ['text/plain', 'text/html']:
- continue
-
if mime_type.startswith(mime_category):
# skip mozilla private types (second component starts with '_'
# or ends with '-priv')
@@ -77,6 +73,10 @@ def choose_most_significant(mime_types):
logging.debug('Choosed %r!' % mime_type)
return mime_type
+ if 'text/x-moz-url' in mime_types:
+ logging.debug('Choosed text/x-moz-url!')
+ return 'text/x-moz-url'
+
if 'text/html' in mime_types:
logging.debug('Choosed text/html!')
return 'text/html'
diff --git a/tests/sugar/runall.py b/tests/sugar/runall.py
index ff672be..6ee9442 100644
--- a/tests/sugar/runall.py
+++ b/tests/sugar/runall.py
@@ -18,11 +18,13 @@
import unittest
import test_date
+import test_mime
runner = unittest.TextTestRunner()
loader = unittest.TestLoader()
suite = unittest.TestSuite()
suite.addTest(loader.loadTestsFromModule(test_date))
+suite.addTest(loader.loadTestsFromModule(test_mime))
runner.run(suite)
diff --git a/tests/sugar/test_mime.py b/tests/sugar/test_mime.py
new file mode 100644
index 0000000..88598fe
--- /dev/null
+++ b/tests/sugar/test_mime.py
@@ -0,0 +1,72 @@
+#!/usr/bin/env python
+
+# Copyright (C) 2006, Red Hat, Inc.
+# Copyright (C) 2007, One Laptop Per Child
+#
+# This program is free software; you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation; either version 2 of the License, or
+# (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program; if not, write to the Free Software
+# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
+
+import sys
+import unittest
+
+from sugar import objects
+
+class TestMime(unittest.TestCase):
+ def test_from_file_name(self):
+ self.assertEqual(objects.mime.get_from_file_name('test.pdf'),
+ 'application/pdf')
+
+ def test_choose_most_significant(self):
+ # Mozilla's text in dnd
+ mime_type = objects.mime.choose_most_significant(
+ ['text/plain', 'text/_moz_htmlcontext', 'text/unicode',
+ 'text/html', 'text/_moz_htmlinfo'])
+ self.assertEqual(mime_type, 'text/html')
+
+ # Mozilla's text in c&v
+ mime_type = objects.mime.choose_most_significant(
+ ['text/_moz_htmlcontext', 'STRING', 'text/html', 'text/_moz_htmlinfo',
+ 'text/x-moz-url-priv', 'UTF8_STRING', 'COMPOUND_TEXT'])
+ self.assertEqual(mime_type, 'text/html')
+
+ # Mozilla gif in dnd
+ mime_type = objects.mime.choose_most_significant(
+ ['application/x-moz-file-promise-url',
+ 'application/x-moz-file-promise-dest-filename', 'text/_moz_htmlinfo',
+ 'text/x-moz-url-desc', 'text/_moz_htmlcontext', 'text/x-moz-url-data',
+ 'text/uri-list'])
+ self.assertEqual(mime_type, 'text/uri-list')
+
+ # Mozilla url in dnd
+ mime_type = objects.mime.choose_most_significant(
+ ['text/_moz_htmlcontext', 'text/html', 'text/_moz_htmlinfo',
+ '_NETSCAPE_URL', 'text/x-moz-url', 'text/x-moz-url-desc',
+ 'text/x-moz-url-data', 'text/plain', 'text/unicode'])
+ self.assertEqual(mime_type, 'text/x-moz-url')
+
+ # Abiword text in dnd
+ mime_type = objects.mime.choose_most_significant(
+ ['text/rtf', 'text/uri-list'])
+ self.assertEqual(mime_type, 'text/uri-list')
+
+ # Abiword text in c&v
+ mime_type = objects.mime.choose_most_significant(
+ ['UTF8_STRING', 'STRING', 'text/html', 'TEXT', 'text/rtf',
+ 'COMPOUND_TEXT', 'application/rtf', 'text/plain',
+ 'application/xhtml+xml'])
+ self.assertEqual(mime_type, 'application/rtf')
+
+if __name__ == "__main__":
+ unittest.main()
+