After an evaluation, GNOME has moved from Bugzilla to GitLab. Learn more about GitLab.
No new issues can be reported in GNOME Bugzilla anymore.
To report an issue in a GNOME project, go to GNOME GitLab.
Do not go to GNOME Gitlab for: Bluefish, Doxygen, GnuCash, GStreamer, java-gnome, LDTP, NetworkManager, Tomboy.
Bug 525113 - Add g_content_type_guess()
Add g_content_type_guess()
Status: RESOLVED FIXED
Product: pygobject
Classification: Bindings
Component: gobject
Git master
Other All
: Normal normal
: ---
Assigned To: Nobody's working on this now (help wanted and appreciated)
Python bindings maintainers
Depends on:
Blocks:
 
 
Reported: 2008-03-30 12:20 UTC by Thomas Leonard
Modified: 2008-04-08 15:14 UTC
See Also:
GNOME target: ---
GNOME version: Unversioned Enhancement


Attachments
Patch to add content_type_guess function (2.23 KB, patch)
2008-03-30 12:21 UTC, Thomas Leonard
none Details | Review
Patch (with fixed reference counting) (2.23 KB, patch)
2008-03-30 12:39 UTC, Thomas Leonard
reviewed Details | Review

Description Thomas Leonard 2008-03-30 12:20:42 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:
Comment 1 Thomas Leonard 2008-03-30 12:21:38 UTC
Created attachment 108266 [details] [review]
Patch to add content_type_guess function
Comment 2 Thomas Leonard 2008-03-30 12:36:33 UTC
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(".")
Comment 3 Thomas Leonard 2008-03-30 12:39:05 UTC
Created attachment 108267 [details] [review]
Patch (with fixed reference counting)
Comment 4 Johan (not receiving bugmail) Dahlin 2008-03-31 03:40:25 UTC
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?
Comment 5 Thomas Leonard 2008-03-31 17:25:31 UTC
> 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,
Comment 6 Johan (not receiving bugmail) Dahlin 2008-04-08 15:14:04 UTC
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)