diff options
-rw-r--r-- | ChangeLog | 8 | ||||
-rw-r--r-- | gio/gio.defs | 7 | ||||
-rw-r--r-- | gio/gio.override | 30 | ||||
-rw-r--r-- | tests/test_gio.py | 15 |
4 files changed, 60 insertions, 0 deletions
@@ -1,5 +1,13 @@ 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) + +2008-04-08 Johan Dahlin <jdahlin@async.com.br> + * gio/gio-types.defs: * gio/gio.defs: * gio/gio.override: diff --git a/gio/gio.defs b/gio/gio.defs index 4f293ac..dfe70ec 100644 --- a/gio/gio.defs +++ b/gio/gio.defs @@ -544,6 +544,13 @@ ) (define-function content_type_guess + (docstring +"content_type_guess(filename=None, data=None, want_uncertain=False) -> mime-type\n" +"\n" +"Guesses the content-type based on the parameters passed.\n" +"Returns a string containing the mime type.\n" +"If want_uncertain is set to True, return a tuple with the mime type and \n" +"True/False if the type guess was uncertain or not.") (c-name "g_content_type_guess") (return-type "char*") (parameters diff --git a/gio/gio.override b/gio/gio.override index 33b8611..e34e5f9 100644 --- a/gio/gio.override +++ b/gio/gio.override @@ -180,3 +180,33 @@ _wrap_g_themed_icon_get_names(PyGObject *self) return ret; } +%% +override g_content_type_guess kwargs +static PyObject * +_wrap_g_content_type_guess(PyGObject *self, PyObject *args, PyObject *kwargs) +{ + char *kwlist[] = {"filename", "data", "want_uncertain", NULL}; + char *filename = NULL, *data = NULL, *type; + int data_size = 0; + gboolean result_uncertain, want_uncertain = FALSE; + + if (!PyArg_ParseTupleAndKeywords (args, kwargs, + "|zz#i:g_content_type_guess", + kwlist, + &filename, &data, &data_size, + &want_uncertain)) + return NULL; + + if (!filename && !data) { + PyErr_SetString(PyExc_TypeError, "need at least one argument"); + return NULL; + } + + type = g_content_type_guess(filename, (guchar *) data, + data_size, &result_uncertain); + + if (want_uncertain) + return Py_BuildValue("zN", type, + PyBool_FromLong(result_uncertain)); + return PyString_FromString(type); +} diff --git a/tests/test_gio.py b/tests/test_gio.py index 2b8e9f1..b70f9c2 100644 --- a/tests/test_gio.py +++ b/tests/test_gio.py @@ -212,3 +212,18 @@ class TestThemedIcon(unittest.TestCase): self.assertEquals(self.icon.get_names(), ['open']) self.icon.append_name('close') self.assertEquals(self.icon.get_names(), ['open', 'close']) + +class TestType(unittest.TestCase): + def testGuessFromName(self): + mime_type = gio.content_type_guess('diagram.svg') + self.assertEquals('image/svg+xml', mime_type) + + def testGuessFromContents(self): + mime_type = gio.content_type_guess(data='<html></html>') + self.assertEquals('text/html', mime_type) + + def testGuessFromContentsUncertain(self): + mime_type, result_uncertain = gio.content_type_guess( + data='<html></html>', want_uncertain=True) + self.assertEquals('text/html', mime_type) + self.assertEquals(bool, type(result_uncertain)) |