diff options
Diffstat (limited to 'sugar')
-rw-r--r-- | sugar/Makefile.am | 2 | ||||
-rw-r--r-- | sugar/_sugarext.c | 22 | ||||
-rw-r--r-- | sugar/_sugarext.defs | 12 | ||||
-rw-r--r-- | sugar/objects/Makefile.am | 7 | ||||
-rw-r--r-- | sugar/objects/__init__.py | 2 | ||||
-rw-r--r-- | sugar/objects/mime.py | 4 | ||||
-rw-r--r-- | sugar/objects/typeinfo.py | 58 | ||||
-rw-r--r-- | sugar/objects/typeregistry.py | 99 |
8 files changed, 42 insertions, 164 deletions
diff --git a/sugar/Makefile.am b/sugar/Makefile.am index 93e548d..77aacdd 100644 --- a/sugar/Makefile.am +++ b/sugar/Makefile.am @@ -1,4 +1,4 @@ -SUBDIRS = activity clipboard graphics p2p presence datastore +SUBDIRS = activity clipboard graphics objects p2p presence datastore sugardir = $(pythondir)/sugar sugar_PYTHON = \ diff --git a/sugar/_sugarext.c b/sugar/_sugarext.c index 7c7c35a..c5bf22d 100644 --- a/sugar/_sugarext.c +++ b/sugar/_sugarext.c @@ -79,7 +79,27 @@ PyTypeObject G_GNUC_INTERNAL PySugarAddressEntry_Type = { /* ----------- functions ----------- */ +static PyObject * +_wrap_sugar_mime_get_mime_type_from_file_name(PyObject *self, PyObject *args, PyObject *kwargs) +{ + static char *kwlist[] = { "filename", NULL }; + char *filename; + const gchar *ret; + + if (!PyArg_ParseTupleAndKeywords(args, kwargs,"s:get_mime_type_from_file_name", kwlist, &filename)) + return NULL; + + ret = sugar_mime_get_mime_type_from_file_name(filename); + + if (ret) + return PyString_FromString(ret); + Py_INCREF(Py_None); + return Py_None; +} + const PyMethodDef py_sugarext_functions[] = { + { "get_mime_type_from_file_name", (PyCFunction)_wrap_sugar_mime_get_mime_type_from_file_name, METH_VARARGS|METH_KEYWORDS, + NULL }, { NULL, NULL, 0, NULL } }; @@ -103,6 +123,6 @@ py_sugarext_register_classes(PyObject *d) } -#line 107 "_sugarext.c" +#line 127 "_sugarext.c" pygobject_register_class(d, "SugarAddressEntry", SUGAR_TYPE_ADDRESS_ENTRY, &PySugarAddressEntry_Type, Py_BuildValue("(O)", &PyGtkEntry_Type)); } diff --git a/sugar/_sugarext.defs b/sugar/_sugarext.defs index e35a6c9..666fb36 100644 --- a/sugar/_sugarext.defs +++ b/sugar/_sugarext.defs @@ -1,5 +1,5 @@ ;; -*- scheme -*- -; object definitions ... +; object definitions (define-object AddressEntry (in-module "Sugar") @@ -7,3 +7,13 @@ (c-name "SugarAddressEntry") (gtype-id "SUGAR_TYPE_ADDRESS_ENTRY") ) + +; 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") + ) +) diff --git a/sugar/objects/Makefile.am b/sugar/objects/Makefile.am index c93713b..35d4ddc 100644 --- a/sugar/objects/Makefile.am +++ b/sugar/objects/Makefile.am @@ -1,5 +1,4 @@ sugardir = $(pythondir)/sugar/objects -sugar_PYTHON = \ - __init__.py \ - typeregistry.py \ - typeinfo.py +sugar_PYTHON = \ + __init__.py \ + mime.py diff --git a/sugar/objects/__init__.py b/sugar/objects/__init__.py new file mode 100644 index 0000000..139597f --- /dev/null +++ b/sugar/objects/__init__.py @@ -0,0 +1,2 @@ + + diff --git a/sugar/objects/mime.py b/sugar/objects/mime.py new file mode 100644 index 0000000..9e40561 --- /dev/null +++ b/sugar/objects/mime.py @@ -0,0 +1,4 @@ +from sugar import _sugarext + +def get_from_filename(filename): + return _sugarext.get_mime_type_from_filename(filename) diff --git a/sugar/objects/typeinfo.py b/sugar/objects/typeinfo.py deleted file mode 100644 index 8ac8471..0000000 --- a/sugar/objects/typeinfo.py +++ /dev/null @@ -1,58 +0,0 @@ -# Copyright (C) 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. - -class TypeInfo(object): - def __init__(self, info_dict=None): - self.type_id = None - self.name = None - self.icon = 'theme:stock-missing' - self.parent = None - self.formats = [] - - if info_dict: - self._read_from_dict(info_dict) - - def get_default_activity(self): - return None - - def get_activities(self): - return [] - - def _read_from_config(self, section, items, l_items): - self.type_id = section - - for item in items: - if item[0] == 'name': - self.name = item[1] - elif item[0] == 'icon': - self.icon = item[1] - elif item[0] == 'parent': - self.parent = item[1] - elif item[0] == 'formats': - self.formats = item[1].split(';') - - for item in litems: - if item[0] == 'name': - self.name = item[1] - - return (self.name and self.parent and self.formats) - - def _read_from_dict(self, info_dict): - self.type_id = info_dict['type_id'] - self.name = info_dict['name'] - self.icon = info_dict['icon'] - self.formats = info_dict['formats'] diff --git a/sugar/objects/typeregistry.py b/sugar/objects/typeregistry.py deleted file mode 100644 index 8d24b39..0000000 --- a/sugar/objects/typeregistry.py +++ /dev/null @@ -1,99 +0,0 @@ -# Copyright (C) 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. - -from gettext import gettext as _ -from ConfigParser import ConfigParser - -from sugar.objects.typeinfo import TypeInfo -from sugar.activity import bundleregistry - -_text_type = { - 'type_id' : 'Text', - 'name' : _('Text'), - 'icon' : 'theme:object-text', - 'formats' : [ 'text/plain', 'application/pdf' ] -} - -_image_type = { - 'type_id' : 'Image', - 'name' : _('Image'), - 'icon' : 'theme:object-image', - 'formats' : [ 'image/jpeg', 'image/gif', 'image/png' ] -} - -class _RootNode(_TypeNode): - def __init__(self): - _TypeNode.__init__('') - - def append_primitive(self, info_dict): - self.append(TypeInfo(info_dict)) - -class _TypeNode(list): - def __init__(self, type_info): - self.type_info = type_info - - def get_node_from_type(self, type_id): - for node in self: - if node.type_info.type_id == type_id: - return node - - for node in self: - child = node.get_node_from_type() - if child: - return child - - return None - -class TypeRegistry(object): - def __init__(self): - self._tree = _RootNode() - self._tree.append_primitive(_image_type) - self._tree.append_primitive(_text_type) - - self._bundle_registry = bundleregistry.get_registry() - for bundle in self._bundle_registry: - self._read_from_bundle(bundle) - self._bundle_registry.connect('bundle-added', self._bundle_added_cb) - - def _bundle_added_cb (self, registry, bundle): - self._read_from_bundle(bundle) - - def _read_from_bundle(self, bundle): - cp = ConfigParser() - path = bundle.get_path() - cp.read([os.path.join(path, 'activity', 'object_types.info')]) - items = cp.items() - - cp = ConfigParser() - path = bundle.get_locale_path() - cp.read([os.path.join(path, 'object_types.linfo')]) - l_items = cp.items() - - for section in cp.sections(): - type_info = TypeInfo() - if type_info.read_from_config(section, items, l_items): - parent_node = self._tree.get_node_from_type(type_info.parent) - if parent_node: - parent_node.append(_TypeNode(type_info)) - return True - - return False - -def get_registry(): - return _type_registry - -_type_registry = TypeRegistry() |