GNOME Bugzilla – Bug 525113
Add g_content_type_guess()
Last modified: 2008-04-08 15:14:04 UTC
Please describe the problem: g_content_type_guess() is not available from the Python bindings. Steps to reproduce: Actual results: Expected results: Does this happen every time? Other information:
Created attachment 108266 [details] [review] Patch to add content_type_guess function
Comment on attachment 108266 [details] [review] Patch to add content_type_guess function >commit 965deeda0c0947cb0ff29729f3ff4ba99b5b8ab4 >Author: Thomas Leonard <talex5@gmail.com> >Date: Sun Mar 30 13:09:47 2008 +0100 > > Added g_content_type_guess. > >diff --git a/gio/gio.defs b/gio/gio.defs >index 68b794e..a3f3a8c 100644 >--- a/gio/gio.defs >+++ b/gio/gio.defs >@@ -545,13 +545,6 @@ > > (define-function content_type_guess > (c-name "g_content_type_guess") >- (return-type "char*") >- (parameters >- '("const-char*" "filename") >- '("const-guchar*" "data") >- '("gsize" "data_size") >- '("gboolean*" "result_uncertain") >- ) > ) > > (define-function content_types_get_registered >diff --git a/gio/gio.override b/gio/gio.override >index 633f4bd..95263f6 100644 >--- a/gio/gio.override >+++ b/gio/gio.override >@@ -190,3 +190,24 @@ _wrap_g_themed_icon_get_names (PyGObject *self) > return ret; > } > %% >+override g_content_type_guess args >+static PyObject * >+_wrap_g_content_type_guess (PyGObject *self, PyObject *args) >+{ >+ PyObject *result_uncertain_object; >+ gboolean result_uncertain; >+ char *filename; >+ char *data; >+ char *type; >+ int data_size; >+ >+ if (!PyArg_ParseTuple (args, "zz#:g_content_type_guess", &filename, &data, &data_size)) >+ return NULL; >+ >+ type = g_content_type_guess(filename, (guchar *) data, data_size, &result_uncertain); >+ >+ result_uncertain_object = PyBool_FromLong(result_uncertain); >+ >+ return Py_BuildValue("zN", type, result_uncertain_object); >+} >+%% >diff --git a/tests/test_gio.py b/tests/test_gio.py >index ce1095e..4080840 100644 >--- a/tests/test_gio.py >+++ b/tests/test_gio.py >@@ -32,6 +32,17 @@ class TestFile(unittest.TestCase): > loop = gobject.MainLoop() > loop.run() > >+class TestType(unittest.TestCase): >+ def testGuessFromName(self): >+ mime_type, result_uncertain = gio.content_type_guess('diagram.svg', None) >+ self.assertEquals('image/svg+xml', mime_type) >+ self.assertEquals(bool, type(result_uncertain)) >+ >+ def testGuessFromContents(self): >+ mime_type, result_uncertain = gio.content_type_guess(None, '<html></html>') >+ self.assertEquals('text/html', mime_type) >+ self.assertEquals(bool, type(result_uncertain)) >+ > class TestGFileEnumerator(unittest.TestCase): > def setUp(self): > self.file = gio.file_new_for_path(".")
Created attachment 108267 [details] [review] Patch (with fixed reference counting)
Comment on attachment 108267 [details] [review] Patch (with fixed reference counting) >index 68b794e..a3f3a8c 100644 >--- a/gio/gio.defs >+++ b/gio/gio.defs >@@ -545,13 +545,6 @@ > > (define-function content_type_guess > (c-name "g_content_type_guess") >- (return-type "char*") >- (parameters >- '("const-char*" "filename") >- '("const-guchar*" "data") >- '("gsize" "data_size") >- '("gboolean*" "result_uncertain") >- ) > ) You can safely leave this as it was. >diff --git a/gio/gio.override b/gio/gio.override >+override g_content_type_guess args >+ char *data; guchar *data? >+ char *type; >+ int data_size; >+ >+ if (!PyArg_ParseTuple (args, "zz#:g_content_type_guess", &filename, &data, &data_size)) >+ return NULL; data should probably be made optional >diff --git a/tests/test_gio.py b/tests/test_gio.py >+ def testGuessFromName(self): >+ mime_type, result_uncertain = gio.content_type_guess('diagram.svg', None) Does that depend on a file called diagram.svg in the current directory?
> You can safely leave this as it was. OK. I thought the unknown boolean* was stopping it from being added, but I was mistaken. > guchar *data? ParseTuple wants char*, guess wants guchar*. I don't think it matters one way or the other which one has the cast. > data should probably be made optional As in "z|z#"? Fine by me. > Does that depend on a file called diagram.svg in the current directory? No. Thanks,
I just committed a version of this to SVN: 2008-04-08 Johan Dahlin <jdahlin@async.com.br> * gio/gio.defs: * gio/gio.override: * tests/test_gio.py: Add bindings for content_type_guess. Based on patch by Thomas Leonard (#525113) I changed so you can do: content_type = gio.content_type_guess(filename) content_type = gio.content_type_guess(data=foo) If you really want to see the result_uncertain, you have to say so: content_type, uncertain = gio.content_type_guess(filename, want_uncertain=True)