Bind xdgmime in SugarExt
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.
This commit is contained in:
parent
e6397cf2db
commit
6202d4d5cc
@ -18,14 +18,9 @@ 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 xfixes xi x11)
|
||||
|
||||
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)
|
||||
|
||||
|
@ -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
|
||||
|
@ -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")
|
||||
)
|
||||
)
|
@ -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;
|
||||
}
|
||||
%%
|
@ -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");
|
||||
}
|
||||
}
|
@ -25,10 +25,11 @@ import os
|
||||
import logging
|
||||
import gettext
|
||||
|
||||
from gi.repository import GLib
|
||||
from gi.repository import GdkPixbuf
|
||||
from gi.repository import Gio
|
||||
|
||||
from sugar3 import _sugarbaseext
|
||||
from gi.repository import SugarExt
|
||||
|
||||
_ = lambda msg: gettext.dgettext('sugar-base', msg)
|
||||
|
||||
@ -123,7 +124,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'
|
||||
@ -134,7 +135,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):
|
||||
|
@ -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)
|
||||
{
|
||||
|
3
tests/data/mime.svg
Normal file
3
tests/data/mime.svg
Normal file
@ -0,0 +1,3 @@
|
||||
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
|
||||
<svg xmlns="http://www.w3.org/2000/svg" version="2.0">
|
||||
</svg>
|
After Width: | Height: | Size: 117 B |
@ -17,12 +17,27 @@
|
||||
# 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 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.svg")),
|
||||
'image/svg+xml')
|
||||
|
||||
def test_from_file_name(self):
|
||||
self.assertEqual(mime.get_from_file_name('test.pdf'),
|
||||
'application/pdf')
|
||||
@ -77,7 +92,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()
|
||||
|
Loading…
Reference in New Issue
Block a user