Add:binding_python:Added support for attributes
authormartin-s <martin-s@ffa7fe5e-494d-0410-b361-a75ebd5db220>
Wed, 17 Dec 2008 13:36:28 +0000 (13:36 +0000)
committermartin-s <martin-s@ffa7fe5e-494d-0410-b361-a75ebd5db220>
Wed, 17 Dec 2008 13:36:28 +0000 (13:36 +0000)
git-svn-id: https://navit.svn.sourceforge.net/svnroot/navit/trunk/navit@1817 ffa7fe5e-494d-0410-b361-a75ebd5db220

navit/binding/python/Makefile.am
navit/binding/python/attr.c [new file with mode: 0644]
navit/binding/python/binding_python.c
navit/binding/python/common.h
navit/binding/python/navit.c
navit/binding/python/template.c

index 0d64c87..1900693 100644 (file)
@@ -1,5 +1,5 @@
 include $(top_srcdir)/Makefile.inc
 AM_CPPFLAGS = @NAVIT_CFLAGS@ @PYTHON_CFLAGS@ -I$(top_srcdir)/navit -DMODULE=binding_python
 modulebinding_LTLIBRARIES = libbinding_python.la
-libbinding_python_la_SOURCES = binding_python.c main.c navit.c pcoord.c route.c navigation.c common.h
+libbinding_python_la_SOURCES = binding_python.c main.c navit.c pcoord.c route.c navigation.c attr.c common.h
 libbinding_python_la_LIBADD = @PYTHON_LIBS@
diff --git a/navit/binding/python/attr.c b/navit/binding/python/attr.c
new file mode 100644 (file)
index 0000000..2d5ed92
--- /dev/null
@@ -0,0 +1,99 @@
+/**
+ * Navit, a modular navigation system.
+ * Copyright (C) 2005-2008 Navit Team
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License
+ * version 2 as published by the Free Software Foundation.
+ *
+ * This program 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 General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the
+ * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
+ * Boston, MA  02110-1301, USA.
+ */
+
+#include "common.h"
+#include "item.h"
+#include "attr.h"
+
+typedef struct {
+       PyObject_HEAD
+       int ref;
+       struct attr *attr;
+} attrObject;
+
+static PyObject *
+attr_func(attrObject *self, PyObject *args)
+{
+       const char *file;
+       int ret;
+       if (!PyArg_ParseTuple(args, "s", &file))
+               return NULL;
+       ret=0;
+       return Py_BuildValue("i",ret);
+}
+
+
+
+static PyMethodDef attr_methods[] = {
+       {"func",        (PyCFunction) attr_func, METH_VARARGS },
+       {NULL, NULL },
+};
+
+
+static PyObject *
+attr_getattr_py(PyObject *self, char *name)
+{
+       return Py_FindMethod(attr_methods, self, name);
+}
+
+static void
+attr_destroy_py(attrObject *self)
+{
+       if (! self->ref)
+               attr_free(self->attr);
+}
+
+PyTypeObject attr_Type = {
+       Obj_HEAD
+       .tp_name="attr",
+       .tp_basicsize=sizeof(attrObject),
+       .tp_dealloc=(destructor)attr_destroy_py,
+       .tp_getattr=attr_getattr_py,
+};
+
+struct attr *
+attr_py_get(PyObject *self)
+{
+       return ((attrObject *)self)->attr;
+}
+
+PyObject *
+attr_new_py(PyObject *self, PyObject *args)
+{
+       attrObject *ret;
+       const char *name,*value;
+        if (!PyArg_ParseTuple(args, "ss", &name, &value))
+                return NULL;
+       ret=PyObject_NEW(attrObject, &attr_Type);
+       ret->attr=attr_new_from_text(name, value);
+       ret->ref=0;
+       return (PyObject *)ret;
+}
+
+PyObject *
+attr_new_py_ref(struct attr *attr)
+{
+       attrObject *ret;
+
+       ret=PyObject_NEW(attrObject, &attr_Type);
+       ret->ref=1;
+       ret->attr=attr;
+       return (PyObject *)ret;
+}
+
index 3cebeb8..3ec4d4a 100644 (file)
@@ -188,10 +188,22 @@ map_dump_file_py(mapObject *self, PyObject *args)
 }
 
 
+static PyObject *
+map_set_attr_py(mapObject *self, PyObject *args)
+{
+       PyObject *attr;
+       if (!PyArg_ParseTuple(args, "O!", &attr_Type, &attr))
+               return NULL;
+       map_set_attr(self->m, attr_py_get(attr));
+       Py_RETURN_NONE;
+}
+
+
 
 static PyMethodDef map_methods[] = {
        {"dump_file",           (PyCFunction) map_dump_file_py, METH_VARARGS },
        {"map_rect_new",        (PyCFunction) map_rect_new_py, METH_VARARGS },
+       {"set_attr",            (PyCFunction) map_set_attr_py, METH_VARARGS },
        {NULL, NULL },
 };
 
@@ -308,6 +320,7 @@ config_load_py(PyObject *self, PyObject *args)
 }
 
 static PyMethodDef navitMethods[]={
+       {"attr", attr_new_py, METH_VARARGS},
        {"coord", coord_new_py, METH_VARARGS, "Create a new coordinate point."},
        {"coord_rect", coord_rect_new_py, METH_VARARGS, "Create a new coordinate rectangle."},
        {"map", map_new_py, METH_VARARGS, "Create a new map."},
index 9f18fc9..5c1a270 100644 (file)
@@ -48,3 +48,8 @@ struct pcoord *pcoord_py_get(PyObject *self);
 struct route;
 PyObject * route_py(PyObject *self, PyObject *args);
 PyObject * route_py_ref(struct route *route);
+
+extern PyTypeObject attr_Type;
+PyObject * attr_new_py(PyObject *self, PyObject *args);
+PyObject * attr_new_py_ref(struct attr *attr);
+struct attr * attr_py_get(PyObject *self);
index 4de5b2f..34b1823 100644 (file)
@@ -73,6 +73,17 @@ navit_set_position_py(navitObject *self, PyObject *args)
 }
 
 
+static PyObject *
+navit_zoom_to_route_py(navitObject *self, PyObject *args)
+{
+       PyObject *pcoord;
+       if (!PyArg_ParseTuple(args, ""))
+               return NULL;
+       navit_zoom_to_route(self->navit);
+       Py_RETURN_NONE;
+}
+
+
 
 
 static PyMethodDef navit_methods[] = {
@@ -80,6 +91,7 @@ static PyMethodDef navit_methods[] = {
        {"set_center",          (PyCFunction) navit_set_center_py, METH_VARARGS },
        {"set_destination",     (PyCFunction) navit_set_destination_py, METH_VARARGS },
        {"set_position",        (PyCFunction) navit_set_position_py, METH_VARARGS },
+       {"zoom_to_route",       (PyCFunction) navit_zoom_to_route_py, METH_VARARGS },
        {NULL, NULL },
 };
 
index 00613ea..088cb5e 100644 (file)
@@ -22,6 +22,8 @@
 
 typedef struct {
        PyObject_HEAD
+       int ref;
+       struct template *template;
 } templateObject;
 
 static PyObject *
@@ -52,6 +54,8 @@ template_getattr_py(PyObject *self, char *name)
 static void
 template_destroy_py(templateObject *self)
 {
+       if (! self->ref)
+               template_destroy(self->template);
 }
 
 PyTypeObject template_Type = {
@@ -62,12 +66,31 @@ PyTypeObject template_Type = {
        .tp_getattr=template_getattr_py,
 };
 
+struct template *
+template_py_get(PyObject *self)
+{
+       return ((templateObject *)self)->template;
+}
+
+PyObject *
+template_new_py(PyObject *self, PyObject *args)
+{
+       templateObject *ret;
+
+       ret=PyObject_NEW(templateObject, &template_Type);
+       ret->template=template_new();
+       ret->ref=0;
+       return (PyObject *)ret;
+}
+
 PyObject *
-template_py(PyObject *self, PyObject *args)
+template_new_py_ref(struct template *template)
 {
        templateObject *ret;
 
        ret=PyObject_NEW(templateObject, &template_Type);
+       ret->ref=1;
+       ret->template=template;
        return (PyObject *)ret;
 }