/[zanavi_public1]/navit/navit/binding/python/binding_python.c
ZANavi

Contents of /navit/navit/binding/python/binding_python.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 2 - (show annotations) (download)
Fri Oct 28 21:19:04 2011 UTC (12 years, 5 months ago) by zoff99
File MIME type: text/plain
File size: 7921 byte(s)
import files
1 /**
2 * Navit, a modular navigation system.
3 * Copyright (C) 2005-2008 Navit Team
4 *
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License
7 * version 2 as published by the Free Software Foundation.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the
16 * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17 * Boston, MA 02110-1301, USA.
18 */
19
20 #include "config.h"
21 #include <glib.h>
22 #include "common.h"
23 #include <fcntl.h>
24 #include "coord.h"
25 #include "projection.h"
26 #include "debug.h"
27 #include "item.h"
28 #include "map.h"
29 #include "mapset.h"
30 #include "plugin.h"
31 #include "debug.h"
32 #include "item.h"
33 #include "attr.h"
34 #include "xmlconfig.h"
35
36 #if defined(MS_WINDOWS) || defined(__CYGWIN__)
37 #define Obj_HEAD PyObject_HEAD_INIT(NULL);
38 #else
39 #define Obj_HEAD PyObject_HEAD_INIT(&PyType_Type)
40 #endif
41
42 /* *** coord *** */
43
44 typedef struct {
45 PyObject_HEAD
46 struct coord *c;
47 } coordObject;
48
49 static void coord_destroy_py(coordObject *self);
50
51 PyTypeObject coord_Type = {
52 Obj_HEAD
53 .tp_name="coord",
54 .tp_basicsize=sizeof(coordObject),
55 .tp_dealloc=(destructor)coord_destroy_py,
56 };
57
58
59 /* *** map *** */
60
61 typedef struct {
62 PyObject_HEAD
63 int ref;
64 struct map *m;
65 } mapObject;
66
67 static void map_destroy_py(mapObject *self);
68 static PyObject *map_getattr_py(PyObject *self, char *name);
69
70 PyTypeObject map_Type = {
71 Obj_HEAD
72 .tp_name="map",
73 .tp_basicsize=sizeof(mapObject),
74 .tp_dealloc=(destructor)map_destroy_py,
75 .tp_getattr=map_getattr_py,
76 };
77
78 /* *** IMPLEMENTATIONS *** */
79
80 /* *** coord *** */
81
82 static PyObject *
83 coord_new_py(PyObject *self, PyObject *args)
84 {
85 coordObject *ret;
86 int x,y;
87 if (!PyArg_ParseTuple(args, "ii:navit.coord",&x,&y))
88 return NULL;
89 ret=PyObject_NEW(coordObject, &coord_Type);
90 ret->c=coord_new(x,y);
91 return (PyObject *)ret;
92 }
93
94 static void
95 coord_destroy_py(coordObject *self)
96 {
97 coord_destroy(self->c);
98 }
99
100 /* *** coord_rect *** */
101
102 typedef struct {
103 PyObject_HEAD
104 struct coord_rect *r;
105 } coord_rectObject;
106
107
108 static void coord_rect_destroy_py(coord_rectObject *self);
109
110 PyTypeObject coord_rect_Type = {
111 #if defined(MS_WINDOWS) || defined(__CYGWIN__)
112 PyObject_HEAD_INIT(NULL);
113 #else
114 PyObject_HEAD_INIT(&PyType_Type)
115 #endif
116 .tp_name="coord_rect",
117 .tp_basicsize=sizeof(coord_rectObject),
118 .tp_dealloc=(destructor)coord_rect_destroy_py,
119 };
120
121 static PyObject *
122 coord_rect_new_py(PyObject *self, PyObject *args)
123 {
124 coord_rectObject *ret;
125 coordObject *lu,*rd;
126 if (!PyArg_ParseTuple(args, "O!O!:navit.coord_rect_rect",&coord_Type,&lu,&coord_Type,&rd))
127 return NULL;
128 ret=PyObject_NEW(coord_rectObject, &coord_rect_Type);
129 ret->r=coord_rect_new(lu->c,rd->c);
130 return (PyObject *)ret;
131 }
132
133 static void
134 coord_rect_destroy_py(coord_rectObject *self)
135 {
136 coord_rect_destroy(self->r);
137 }
138
139 /* *** map_rect *** */
140
141 typedef struct {
142 PyObject_HEAD
143 struct map_rect *mr;
144 } map_rectObject;
145
146
147 static void map_rect_destroy_py(map_rectObject *self);
148
149 PyTypeObject map_rect_Type = {
150 #if defined(MS_WINDOWS) || defined(__CYGWIN__)
151 PyObject_HEAD_INIT(NULL);
152 #else
153 PyObject_HEAD_INIT(&PyType_Type)
154 #endif
155 .tp_name="map_rect",
156 .tp_basicsize=sizeof(map_rectObject),
157 .tp_dealloc=(destructor)map_rect_destroy_py,
158 };
159
160 static PyObject *
161 map_rect_new_py(mapObject *self, PyObject *args)
162 {
163 map_rectObject *ret;
164 coord_rectObject *r;
165 if (!PyArg_ParseTuple(args, "O!:navit.map_rect_rect",&coord_rect_Type,&r))
166 return NULL;
167 ret=PyObject_NEW(map_rectObject, &map_rect_Type);
168 ret->mr=map_rect_new(self->m, NULL);
169 return (PyObject *)ret;
170 }
171
172 static void
173 map_rect_destroy_py(map_rectObject *self)
174 {
175 map_rect_destroy(self->mr);
176 }
177
178
179 /* *** map *** */
180
181 static PyObject *
182 map_dump_file_py(mapObject *self, PyObject *args)
183 {
184 const char *s;
185 if (!PyArg_ParseTuple(args, "s",&s))
186 return NULL;
187 map_dump_file(self->m, s);
188 Py_RETURN_NONE;
189 }
190
191
192 static PyObject *
193 map_set_attr_py(mapObject *self, PyObject *args)
194 {
195 PyObject *attr;
196 if (!PyArg_ParseTuple(args, "O!", &attr_Type, &attr))
197 return NULL;
198 map_set_attr(self->m, attr_py_get(attr));
199 Py_RETURN_NONE;
200 }
201
202
203
204 static PyMethodDef map_methods[] = {
205 {"dump_file", (PyCFunction) map_dump_file_py, METH_VARARGS },
206 {"map_rect_new", (PyCFunction) map_rect_new_py, METH_VARARGS },
207 {"set_attr", (PyCFunction) map_set_attr_py, METH_VARARGS },
208 {NULL, NULL },
209 };
210
211 static PyObject *
212 map_getattr_py(PyObject *self, char *name)
213 {
214 return Py_FindMethod(map_methods, self, name);
215 }
216
217
218 static PyObject *
219 map_new_py(PyObject *self, PyObject *args)
220 {
221 mapObject *ret;
222 char *type, *filename;
223
224 if (!PyArg_ParseTuple(args, "ss:navit.map", &type, &filename))
225 return NULL;
226 ret=PyObject_NEW(mapObject, &map_Type);
227 ret->m=map_new(NULL,NULL);
228 ret->ref=0;
229 return (PyObject *)ret;
230 }
231
232 PyObject *
233 map_py_ref(struct map *map)
234 {
235 mapObject *ret;
236 ret=PyObject_NEW(mapObject, &map_Type);
237 ret->m=map;
238 ret->ref=1;
239 return (PyObject *)ret;
240 }
241
242 static void
243 map_destroy_py(mapObject *self)
244 {
245 if (!self->ref)
246 map_destroy(self->m);
247 }
248
249 /* *** mapset *** */
250
251
252 typedef struct {
253 PyObject_HEAD
254 struct mapset *ms;
255 } mapsetObject;
256
257
258 static void mapset_destroy_py(mapsetObject *self);
259 static PyObject *mapset_getattr_py(PyObject *self, char *name);
260
261 PyTypeObject mapset_Type = {
262 #if defined(MS_WINDOWS) || defined(__CYGWIN__)
263 PyObject_HEAD_INIT(NULL);
264 #else
265 PyObject_HEAD_INIT(&PyType_Type)
266 #endif
267 .tp_name="mapset",
268 .tp_basicsize=sizeof(mapsetObject),
269 .tp_dealloc=(destructor)mapset_destroy_py,
270 .tp_getattr=mapset_getattr_py,
271 };
272
273 static PyObject *
274 mapset_add_py(mapsetObject *self, PyObject *args)
275 {
276 mapObject *map;
277 if (!PyArg_ParseTuple(args, "O:navit.mapset", &map))
278 return NULL;
279 Py_INCREF(map);
280 mapset_add_attr(self->ms, &(struct attr){attr_map,.u.map=map->m});
281 return Py_BuildValue("");
282 }
283
284 static PyMethodDef mapset_methods[] = {
285 {"add", (PyCFunction) mapset_add_py, METH_VARARGS },
286 {NULL, NULL },
287 };
288
289 static PyObject *
290 mapset_getattr_py(PyObject *self, char *name)
291 {
292 return Py_FindMethod(mapset_methods, self, name);
293 }
294
295 static PyObject *
296 mapset_new_py(PyObject *self, PyObject *args)
297 {
298 mapsetObject *ret;
299 if (!PyArg_ParseTuple(args, ":navit.mapset"))
300 return NULL;
301 ret=PyObject_NEW(mapsetObject, &mapset_Type);
302 ret->ms=mapset_new(NULL,NULL);
303 return (PyObject *)ret;
304 }
305
306 static void
307 mapset_destroy_py(mapsetObject *self)
308 {
309 mapset_destroy(self->ms);
310 }
311
312 static PyObject *
313 config_load_py(PyObject *self, PyObject *args)
314 {
315 const char *file;
316 int ret;
317 xmlerror *error;
318 if (!PyArg_ParseTuple(args, "s", &file))
319 return NULL;
320 ret=config_load(file, &error);
321 return Py_BuildValue("i",ret);
322 }
323
324 static PyMethodDef navitMethods[]={
325 {"attr", attr_new_py, METH_VARARGS},
326 {"coord", coord_new_py, METH_VARARGS, "Create a new coordinate point."},
327 {"coord_rect", coord_rect_new_py, METH_VARARGS, "Create a new coordinate rectangle."},
328 {"map", map_new_py, METH_VARARGS, "Create a new map."},
329 {"mapset", mapset_new_py, METH_VARARGS, "Create a new mapset."},
330 {"config_load", config_load_py, METH_VARARGS, "Load a config"},
331 {"config", config_py, METH_VARARGS, "Get Config Object"},
332 {"pcoord", pcoord_py, METH_VARARGS},
333 {NULL, NULL, 0, NULL}
334 };
335
336
337 PyObject *
338 python_object_from_attr(struct attr *attr)
339 {
340 switch (attr->type) {
341 case attr_navigation:
342 return navigation_py_ref(attr->u.navigation);
343 case attr_route:
344 return route_py_ref(attr->u.route);
345 default:
346 return NULL;
347 }
348 return NULL;
349 }
350
351
352 void
353 plugin_init(void)
354 {
355 int fd,size;
356 char buffer[65536];
357
358 Py_Initialize();
359 Py_InitModule("navit", navitMethods);
360 fd=open("startup.py",O_RDONLY);
361 if (fd >= 0) {
362 size=read(fd, buffer, 65535);
363 if (size > 0) {
364 buffer[size]='\0';
365 PyRun_SimpleString(buffer);
366 }
367 }
368
369 Py_Finalize();
370 }

   
Visit the ZANavi Wiki