[Sugar-devel] [PATCH sugar-toolkit-gtk3] Bind xdgmime in SugarExt

Daniel Narvaez dwnarvaez at gmail.com
Sun Dec 9 09:24:41 EST 2012


From: Daniel Narvaez <dwnarvaez at gmail.com>

And use it in the sugar3.mime modules. This allows
to get rid of the pygtk generated sugarbase module,
along with the wrapping code.

Also add more cases to test_time to make sure
everything is still working.
---
 configure.ac                      |    5 ---
 src/sugar3/Makefile.am            |   75 ++++++++++++-------------------
 src/sugar3/_sugarbaseext.defs     |   33 --------------
 src/sugar3/_sugarbaseext.override |   88 -------------------------------------
 src/sugar3/_sugarbaseextmodule.c  |   40 -----------------
 src/sugar3/mime.py                |   11 ++---
 src/sugar3/xdgmime.c              |   12 +++++
 tests/data/mime.png               |  Bin 0 -> 146 bytes
 tests/test_mime.py                |   21 ++++++---
 9 files changed, 62 insertions(+), 223 deletions(-)
 delete mode 100644 src/sugar3/_sugarbaseext.defs
 delete mode 100644 src/sugar3/_sugarbaseext.override
 delete mode 100644 src/sugar3/_sugarbaseextmodule.c
 create mode 100644 tests/data/mime.png

diff --git a/configure.ac b/configure.ac
index fd147f7..94cc70b 100644
--- a/configure.ac
+++ b/configure.ac
@@ -18,13 +18,8 @@ AC_PATH_PROG([GLIB_GENMARSHAL], [glib-genmarshal])
 AM_PATH_PYTHON
 AM_CHECK_PYTHON_HEADERS(,[AC_MSG_ERROR(could not find Python headers)])
 
-AC_PATH_PROG(PYGTK_CODEGEN, pygtk-codegen-2.0, no)
-
 PKG_CHECK_MODULES(EXT, gtk+-3.0 gdk-3.0 gdk-pixbuf-2.0 sm ice alsa librsvg-2.0)
 
-PYGTK_DEFSDIR=`$PKG_CONFIG --variable=defsdir pygtk-2.0`
-AC_SUBST(PYGTK_DEFSDIR)
-
 GLIB_MKENUMS=`$PKG_CONFIG glib-2.0 --variable=glib_mkenums`
 AC_SUBST(GLIB_MKENUMS)
 
diff --git a/src/sugar3/Makefile.am b/src/sugar3/Makefile.am
index c9a13e3..e2804d0 100644
--- a/src/sugar3/Makefile.am
+++ b/src/sugar3/Makefile.am
@@ -18,11 +18,28 @@ sugar_PYTHON =		\
 	session.py	\
 	util.py
 
+XDG_MIME_SOURCES = \
+	xdgmime.c \
+        xdgmime.h \
+        xdgmimealias.c \
+        xdgmimealias.h \
+        xdgmimecache.c \
+        xdgmimecache.h \
+        xdgmimeglob.c \
+        xdgmimeglob.h \
+        xdgmimeint.c \
+        xdgmimeint.h \
+        xdgmimemagic.c \
+        xdgmimemagic.h \
+        xdgmimeparent.c \
+        xdgmimeparent.h
+
 lib_LTLIBRARIES = libsugarext.la
 
-libsugarext_la_CFLAGS = 		\
-	-DHAVE_ALSA		\
-	$(EXT_CFLAGS)	\
+libsugarext_la_CFLAGS = \
+	-DHAVE_ALSA \
+	-DXDG_PREFIX=sugar_mime \
+	$(EXT_CFLAGS) \
 	$(WARN_CFLAGS)
 
 libsugarext_la_LDFLAGS = $(LDADD)
@@ -62,51 +79,12 @@ libsugarext_la_SOURCES =		\
 	sugar-key-grabber.c		\
 	sugar-key-grabber.h		\
 	sugar-wm.c			\
-	sugar-wm.h
-
-sugar_LTLIBRARIES = _sugarbaseext.la
-
-_sugarbaseext_la_CFLAGS = 		\
-	-DXDG_PREFIX=sugar_mime		\
-	$(WARN_CFLAGS)			\
-	$(EXT_CFLAGS)		\
-	$(PYTHON_INCLUDES)
+	$(XDG_MIME_SOURCES)
 
 BUILT_SOURCES = 			\
 	sugar-marshal.c			\
 	sugar-marshal.h
 
-_sugarbaseext_la_LDFLAGS = -module -avoid-version
-_sugarbaseext_la_LIBADD = $(EXT_LIBS)
-_sugarbaseext_la_SOURCES = 	\
-	_sugarbaseextmodule.c	\
-	xdgmime.c 		\
-	xdgmime.h		\
-	xdgmimealias.c		\
-	xdgmimealias.h		\
-	xdgmimecache.c		\
-	xdgmimecache.h		\
-	xdgmimeglob.c 		\
-	xdgmimeglob.h 		\
-	xdgmimeint.c 		\
-	xdgmimeint.h 		\
-	xdgmimemagic.c  	\
-	xdgmimemagic.h		\
-	xdgmimeparent.c		\
-	xdgmimeparent.h
-
-nodist__sugarbaseext_la_SOURCES = _sugarbaseext.c
-
-_sugarbaseext.c: _sugarbaseext.defs _sugarbaseext.override
-
-.defs.c:
-	(cd $(srcdir)\
-	 && $(PYGTK_CODEGEN) \
-	    --override $*.override \
-	    --prefix py$* $*.defs) > gen-$*.c \
-	&& cp gen-$*.c $*.c \
-	&& rm -f gen-$*.c
-
 sugar-marshal.c: sugar-marshal.list
 	$(GLIB_GENMARSHAL) --prefix=sugar_marshal \
 		$(srcdir)/sugar-marshal.list --header --body > sugar-marshal.c
@@ -115,12 +93,14 @@ sugar-marshal.h: sugar-marshal.list
 	$(GLIB_GENMARSHAL) --prefix=sugar_marshal \
 		$(srcdir)/sugar-marshal.list --header > sugar-marshal.h
 
-CLEANFILES = $(BUILT_SOURCES) _sugarbaseext.c
-EXTRA_DIST = sugar-marshal.list _sugarbaseext.override _sugarbaseext.defs
+CLEANFILES = $(BUILT_SOURCES)
+EXTRA_DIST = sugar-marshal.list
 
 -include $(INTROSPECTION_MAKEFILE)
 INTROSPECTION_GIRS = SugarExt-1.0.gir
-INTROSPECTION_SCANNER_ARGS = --identifier-prefix=Sugar --symbol-prefix=sugar \
+INTROSPECTION_SCANNER_ARGS = \
+	-D XDG_PREFIX=sugar_mime \	
+        --identifier-prefix=Sugar --symbol-prefix=sugar \
 	--identifier-prefix=EggSM --symbol-prefix=egg_sm \
 	--identifier-prefix=Gsm --symbol-prefix=gsm \
 	--identifier-prefix=Acme --symbol-prefix=acme \
@@ -149,7 +129,8 @@ SugarExt_1_0_gir_FILES = \
 	sugar-grid.c \
 	sugar-grid.h \
         sugar-wm.c    \
-        sugar-wm.h
+        sugar-wm.h \
+	$(XDG_MIME_SOURCES)
 
 SugarExt_1_0_gir_INCLUDES = Gtk-3.0 Gdk-3.0
 SugarExt_1_0_gir_PACKAGES = gtk+-3.0 gdk-3.0
diff --git a/src/sugar3/_sugarbaseext.defs b/src/sugar3/_sugarbaseext.defs
deleted file mode 100644
index 893219a..0000000
--- a/src/sugar3/_sugarbaseext.defs
+++ /dev/null
@@ -1,33 +0,0 @@
-; functions
-
-(define-function get_mime_type_from_file_name 
-  (c-name "sugar_mime_get_mime_type_from_file_name")
-  (return-type "const-char*")
-  (parameters
-    '("const-char*" "filename")
-  )
-)
-
-(define-function get_mime_type_for_file
-  (c-name "sugar_mime_get_mime_type_for_file")
-  (return-type "const-char*")
-  (parameters
-    '("const-char*" "filename")
-  )
-)
-
-(define-function list_mime_parents
-  (c-name "sugar_mime_list_mime_parents")
-  (return-type "char**")
-  (parameters
-    '("const-char*" "mime")
-  )
-)
-
-(define-function uri_list_extract_uris
-  (c-name "g_uri_list_extract_uris")
-  (return-type "gchar**")
-  (parameters
-    '("const-char*" "uri_list")
-  )
-)
diff --git a/src/sugar3/_sugarbaseext.override b/src/sugar3/_sugarbaseext.override
deleted file mode 100644
index 18356a9..0000000
--- a/src/sugar3/_sugarbaseext.override
+++ /dev/null
@@ -1,88 +0,0 @@
-/* -*- Mode: C; c-basic-offset: 4 -*- */
-%%
-headers
-#include <Python.h>
-#include <glib.h>
-#include "xdgmime.h"
-%%
-modulename _sugarext
-%%
-ignore-glob
-  *_get_type
-  _*
-%%
-override sugar_mime_get_mime_type_for_file kwargs
-static PyObject *
-_wrap_sugar_mime_get_mime_type_for_file(PyObject *self, PyObject *args, PyObject *kwargs)
-{
-    static char *kwlist[] = { "filename", NULL };
-    char *filename;
-    const char *ret;
-
-    if (!PyArg_ParseTupleAndKeywords(args, kwargs,"s:get_mime_type_for_file", kwlist, &filename))
-        return NULL;
-    
-    ret = sugar_mime_get_mime_type_for_file(filename, NULL);
-    
-    if (ret)
-        return PyString_FromString(ret);
-    Py_INCREF(Py_None);
-    return Py_None;
-}
-%%
-override sugar_mime_list_mime_parents kwargs
-static PyObject *
-_wrap_sugar_mime_list_mime_parents(PyObject *self, PyObject *args, PyObject *kwargs)
-{
-    static char *kwlist[] = { "mime_type", NULL };
-    char *mime_type;
-    char **parents, **tmp;
-    int i = 0, j;
-    PyObject *ret;
-
-    if (!PyArg_ParseTupleAndKeywords(args, kwargs,"s:list_mime_parents", kwlist, &mime_type))
-        return NULL;
-
-    parents = (char **)sugar_mime_list_mime_parents(mime_type);
-    if (!parents)
-        return PyTuple_New(0);
-
-    tmp = parents;
-    while (*tmp)
-        tmp++, i++;
-
-    ret = PyTuple_New(i);
-    for (j = 0; j < i; j++)
-        PyTuple_SetItem(ret, j, PyString_FromString(parents[j]));
-
-    return ret;
-}
-%%
-override g_uri_list_extract_uris kwargs
-static PyObject *
-_wrap_g_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)
-        return PyTuple_New(0);
-
-    tmp = uris;
-    while (*tmp)
-        tmp++, i++;
-
-    ret = PyTuple_New(i);
-    for (j = 0; j < i; j++)
-        PyTuple_SetItem(ret, j, PyString_FromString(uris[j]));
-
-    return ret;
-}
-%%
diff --git a/src/sugar3/_sugarbaseextmodule.c b/src/sugar3/_sugarbaseextmodule.c
deleted file mode 100644
index 57fb139..0000000
--- a/src/sugar3/_sugarbaseextmodule.c
+++ /dev/null
@@ -1,40 +0,0 @@
-/*
- * Copyright (C) 2006-2007, Red Hat, Inc.
- *
- * This library is free software; you can redistribute it and/or
- * modify it under the terms of the GNU Lesser General Public
- * License as published by the Free Software Foundation; either
- * version 2 of the License, or (at your option) any later version.
- *
- * This library 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
- * Lesser General Public License for more details.
- *
- * You should have received a copy of the GNU Lesser General Public
- * License along with this library; if not, write to the
- * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
- * Boston, MA 02111-1307, USA.
- */
-
-#ifdef HAVE_CONFIG_H
-#include "config.h"
-#endif
-
-/* include this first, before NO_IMPORT_PYGOBJECT is defined */
-#include <Python.h>
-
-extern PyMethodDef py_sugarbaseext_functions[];
-
-DL_EXPORT(void)
-init_sugarbaseext(void)
-{
-    PyObject *m, *d;
-
-    m = Py_InitModule ("_sugarbaseext", py_sugarbaseext_functions);
-    d = PyModule_GetDict (m);
-
-    if (PyErr_Occurred ()) {
-        Py_FatalError ("can't initialise module _sugarext");
-    }
-}
diff --git a/src/sugar3/mime.py b/src/sugar3/mime.py
index 70b4d2b..c7c8904 100644
--- a/src/sugar3/mime.py
+++ b/src/sugar3/mime.py
@@ -25,9 +25,10 @@ import os
 import logging
 import gettext
 
+from gi.repository import GLib
 from gi.repository import GdkPixbuf
 
-from sugar3 import _sugarbaseext
+from gi.repository import SugarExt
 
 _ = lambda msg: gettext.dgettext('sugar-base', msg)
 
@@ -122,7 +123,7 @@ def get_for_file(file_name):
 
     file_name = os.path.realpath(file_name)
 
-    mime_type = _sugarbaseext.get_mime_type_for_file(file_name)
+    mime_type = SugarExt.mime_get_mime_type_for_file(file_name, None)
     if mime_type == 'application/octet-stream':
         if _file_looks_like_text(file_name):
             return 'text/plain'
@@ -133,7 +134,7 @@ def get_for_file(file_name):
 
 
 def get_from_file_name(file_name):
-    return _sugarbaseext.get_mime_type_from_file_name(file_name)
+    return SugarExt.mime_get_mime_type_from_file_name(file_name)
 
 
 def get_mime_icon(mime_type):
@@ -154,7 +155,7 @@ def get_mime_description(mime_type):
 
 
 def get_mime_parents(mime_type):
-    return _sugarbaseext.list_mime_parents(mime_type)
+    return SugarExt.mime_list_mime_parents(mime_type)
 
 
 def get_primary_extension(mime_type):
@@ -254,7 +255,7 @@ def choose_most_significant(mime_types):
 
 
 def split_uri_list(uri_list):
-    return _sugarbaseext.uri_list_extract_uris(uri_list)
+    return GLib.uri_list_extract_uris(uri_list)
 
 
 def _file_looks_like_text(file_name):
diff --git a/src/sugar3/xdgmime.c b/src/sugar3/xdgmime.c
index 1d2007c..ec8a6a3 100644
--- a/src/sugar3/xdgmime.c
+++ b/src/sugar3/xdgmime.c
@@ -457,6 +457,13 @@ xdg_mime_get_mime_type_for_data (const void *data,
   return XDG_MIME_TYPE_UNKNOWN;
 }
 
+/**
+ * sugar_mime_get_mime_type_for_file:
+ * @file_name: the file path
+ * @statbuf: (allow-none)
+ *
+ * Returns: (transfer none): the mime type.
+ */
 const char *
 xdg_mime_get_mime_type_for_file (const char  *file_name,
                                  struct stat *statbuf)
@@ -752,6 +759,11 @@ xdg_mime_mime_type_subclass (const char *mime,
   return _xdg_mime_mime_type_subclass (mime, base);
 }
 
+/**
+ * sugar_mime_list_mime_parents:
+ *
+ * Return value: (array zero-terminated=1) (transfer full):
+ **/
 char **
 xdg_mime_list_mime_parents (const char *mime)
 {
diff --git a/tests/data/mime.png b/tests/data/mime.png
new file mode 100644
index 0000000000000000000000000000000000000000..cd63e66dd2a68f6d31a0ed57e58270e2e1db5004
GIT binary patch
literal 146
zcmeAS at N?(olHy`uVBq!ia0vp^j3CUx1SBVv2j2ryoCO|{#S9GG!XV7ZFl&wkP>{XE
z)7O>#4i6_UGuv&qjkkb8k|nMYCC>S|xv6<249-QVi6yBi3gww484B*6z5(HleBwYw
lJf1F&Asp9}fBgS%&%pYR>EE3*k<WqR44$rjF6*2UngF%gCK~_%

literal 0
HcmV?d00001

diff --git a/tests/test_mime.py b/tests/test_mime.py
index b59b58e..89e5ae1 100644
--- a/tests/test_mime.py
+++ b/tests/test_mime.py
@@ -17,13 +17,28 @@
 # along with this program; if not, write to the Free Software
 # Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 
+import os
 import sys
 import unittest
 
-from sugar import mime
+from sugar3 import mime
 
+tests_dir = os.path.dirname(__file__)
+data_dir = os.path.join(tests_dir, "data")
 
 class TestMime(unittest.TestCase):
+    def test_split_uri_list(self):
+        self.assertSequenceEqual(mime.split_uri_list("http://one\nhttp://two"),
+                                 ("http://one", "http://two"))
+
+    def test_get_mime_parents(self):
+        self.assertListEqual(mime.get_mime_parents("image/svg+xml"),
+                             ["application/xml"])
+
+    def test_get_for_file(self):
+        self.assertEqual(mime.get_for_file(os.path.join(data_dir, "mime.png")),
+                         'image/png')
+
     def test_from_file_name(self):
         self.assertEqual(mime.get_from_file_name('test.pdf'),
                          'application/pdf')
@@ -78,7 +93,3 @@ class TestMime(unittest.TestCase):
                  'text/plain;charset=utf-8', 'text/plain;charset=UTF-8',
                  'text/plain'])
         self.assertEqual(mime_type, 'text/plain')
-
-
-if __name__ == '__main__':
-    unittest.main()
-- 
1.7.10.4



More information about the Sugar-devel mailing list