[pygobject] Wrap g_uri_list_extract_uris. Fixes bug #584431



commit 7fe831081cdd2e26f5d948326b9f89ea0694e752
Author: Tomeu Vizoso <tomeu sugarlabs org>
Date:   Sat Jul 18 19:35:08 2009 +0200

    Wrap g_uri_list_extract_uris. Fixes bug #584431

 glib/glibmodule.c   |   38 ++++++++++++++++++++++++++++++++++++++
 gobject/__init__.py |    2 +-
 tests/Makefile.am   |    3 ++-
 tests/test_uris.py  |   15 +++++++++++++++
 4 files changed, 56 insertions(+), 2 deletions(-)
---
diff --git a/glib/glibmodule.c b/glib/glibmodule.c
index 94335a1..4724e8d 100644
--- a/glib/glibmodule.c
+++ b/glib/glibmodule.c
@@ -613,6 +613,38 @@ pyglib_find_program_in_path(PyObject *unused, PyObject *args, PyObject *kwargs)
     return retval;
 }
 
+static PyObject *
+pyglib_uri_list_extract_uris(PyObject *self, PyObject *args, PyObject *kwargs)
+{
+    static char *kwlist[] = { "uri_list", NULL };
+    char *uri_list;
+    char **uris, **tmp;
+    int i = 0, j;
+    PyObject *ret;
+
+    if (!PyArg_ParseTupleAndKeywords(args, kwargs,"s:uri_list_extract_uris", kwlist, &uri_list))
+        return NULL;
+
+    uris = (char **)g_uri_list_extract_uris(uri_list);
+    if (!uris) {
+        Py_INCREF(Py_None);
+        return Py_None;
+    }
+
+    tmp = uris;
+    while (*tmp)
+        tmp++, i++;
+
+    ret = PyTuple_New(i);
+    for (j = 0; j < i; j++)
+        PyTuple_SetItem(ret, j, PyString_FromString(uris[j]));
+
+    g_strfreev(uris);
+
+    return ret;
+}
+
+
 static PyMethodDef _glib_functions[] = {
     { "threads_init",
       (PyCFunction) pyglib_threads_init, METH_NOARGS,
@@ -708,6 +740,12 @@ static PyMethodDef _glib_functions[] = {
       (PyCFunction)pyglib_markup_escape_text, METH_VARARGS|METH_KEYWORDS },
     { "find_program_in_path",
       (PyCFunction)pyglib_find_program_in_path, METH_VARARGS|METH_KEYWORDS },
+    { "uri_list_extract_uris",
+      (PyCFunction)pyglib_uri_list_extract_uris, METH_VARARGS|METH_KEYWORDS,
+      "uri_list_extract_uris(uri_list) -> tuple of strings holding URIs\n"
+      "Splits an string containing an URI list conforming to the \n"
+      "text/uri-list mime type defined in RFC 2483 into individual URIs, \n"
+      "discarding any comments. The URIs are not validated." },
     { NULL, NULL, 0 }
 };
 
diff --git a/gobject/__init__.py b/gobject/__init__.py
index d5aec45..31e6bb0 100644
--- a/gobject/__init__.py
+++ b/gobject/__init__.py
@@ -29,7 +29,7 @@ from glib import spawn_async, idle_add, timeout_add, timeout_add_seconds, \
      filename_from_utf8, get_application_name, set_application_name, \
      get_prgname, set_prgname, main_depth, Pid, GError, glib_version, \
      MainLoop, MainContext, main_context_default, IOChannel, Source, Idle, \
-     Timeout, PollFD, OptionGroup, OptionContext, option
+     Timeout, PollFD, OptionGroup, OptionContext, option, uri_list_extract_uris
 from glib import SPAWN_LEAVE_DESCRIPTORS_OPEN, SPAWN_DO_NOT_REAP_CHILD, \
      SPAWN_SEARCH_PATH, SPAWN_STDOUT_TO_DEV_NULL, SPAWN_STDERR_TO_DEV_NULL, \
      SPAWN_CHILD_INHERITS_STDIN, SPAWN_FILE_AND_ARGV_ZERO, PRIORITY_HIGH, \
diff --git a/tests/Makefile.am b/tests/Makefile.am
index 42a2642..cf3c4f1 100644
--- a/tests/Makefile.am
+++ b/tests/Makefile.am
@@ -68,7 +68,8 @@ TEST_FILES = \
 	test_signal.py \
 	test_source.py \
 	test_subprocess.py \
-	test_thread.py
+	test_thread.py \
+	test_uris.py
 
 if BUILD_GIO
 TEST_FILES += \
diff --git a/tests/test_uris.py b/tests/test_uris.py
new file mode 100644
index 0000000..ee24215
--- /dev/null
+++ b/tests/test_uris.py
@@ -0,0 +1,15 @@
+import unittest
+
+import glib
+
+class TestUris(unittest.TestCase):
+    def testExtractUris(self):
+        uri_list_text = "# urn:isbn:0-201-08372-8\n" + \
+                        "http://www.huh.org/books/foo.html\n"; + \
+                        "http://www.huh.org/books/foo.pdf\n"; + \
+                        "ftp://ftp.foo.org/books/foo.txt\n";
+        uri_list = glib.uri_list_extract_uris(uri_list_text)
+        assert uri_list[0] == "http://www.huh.org/books/foo.html";
+        assert uri_list[1] == "http://www.huh.org/books/foo.pdf";
+        assert uri_list[2] == "ftp://ftp.foo.org/books/foo.txt";
+



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]