2001-01-26 16:38:23 +00:00
|
|
|
// This file may be redistributed and modified only under the terms of
|
2001-03-16 11:38:14 +00:00
|
|
|
// the GNU General Public License (See COPYING for details).
|
2001-01-26 16:38:23 +00:00
|
|
|
// Copyright (C) 2000 Alistair Riddoch
|
|
|
|
|
|
2000-11-17 00:18:32 +00:00
|
|
|
#include <stdio.h>
|
|
|
|
|
#include <unistd.h>
|
|
|
|
|
|
|
|
|
|
#include <Python.h>
|
|
|
|
|
|
|
|
|
|
#include "Python_API.h"
|
|
|
|
|
|
2000-12-07 12:14:35 +00:00
|
|
|
#include <modules/Location.h>
|
2000-11-17 00:18:32 +00:00
|
|
|
/*
|
|
|
|
|
* Beginning of Object methods section.
|
|
|
|
|
*/
|
|
|
|
|
|
2000-11-30 12:51:59 +00:00
|
|
|
static PyObject* Object_get_name(AtlasObject * self, PyObject * args)
|
|
|
|
|
{
|
|
|
|
|
if (self->m_obj == NULL) {
|
|
|
|
|
PyErr_SetString(PyExc_TypeError,"invalid atlas object");
|
|
|
|
|
return NULL;
|
|
|
|
|
}
|
|
|
|
|
if (!PyArg_ParseTuple(args, "")) {
|
|
|
|
|
PyErr_SetString(PyExc_TypeError,"too many args");
|
|
|
|
|
return NULL;
|
|
|
|
|
}
|
|
|
|
|
return PyString_FromString("obj");
|
|
|
|
|
}
|
|
|
|
|
|
2000-11-17 00:18:32 +00:00
|
|
|
/*
|
|
|
|
|
* Object methods structure.
|
|
|
|
|
*/
|
|
|
|
|
|
2001-02-26 14:41:10 +00:00
|
|
|
static PyMethodDef Object_methods[] = {
|
2000-11-30 12:51:59 +00:00
|
|
|
{"get_name", (PyCFunction)Object_get_name, 1},
|
2000-11-17 00:18:32 +00:00
|
|
|
{NULL, NULL} /* sentinel */
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* Beginning of Object standard methods section.
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
static void Object_dealloc(AtlasObject *self)
|
|
|
|
|
{
|
|
|
|
|
if (self->m_obj != NULL) {
|
|
|
|
|
delete self->m_obj;
|
|
|
|
|
}
|
|
|
|
|
Py_XDECREF(self->Object_attr);
|
|
|
|
|
PyMem_DEL(self);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static PyObject * Object_getattr(AtlasObject *self, char *name)
|
|
|
|
|
{
|
2000-11-29 18:02:01 +00:00
|
|
|
if (self->m_obj == NULL) {
|
|
|
|
|
PyErr_SetString(PyExc_TypeError,"invalid object");
|
|
|
|
|
return NULL;
|
|
|
|
|
}
|
|
|
|
|
if (self->m_obj->IsMap()) {
|
|
|
|
|
Object::MapType & omap = self->m_obj->AsMap();
|
2001-04-16 16:26:44 +00:00
|
|
|
Object::MapType::iterator I = omap.find(name);
|
|
|
|
|
if (I != omap.end()) {
|
|
|
|
|
return Object_asPyObject(I->second);
|
2000-11-29 18:02:01 +00:00
|
|
|
}
|
|
|
|
|
}
|
2000-11-30 12:51:59 +00:00
|
|
|
if (self->Object_attr != NULL) {
|
|
|
|
|
PyObject *v = PyDict_GetItemString(self->Object_attr, name);
|
|
|
|
|
if (v != NULL) {
|
|
|
|
|
Py_INCREF(v);
|
|
|
|
|
return v;
|
|
|
|
|
}
|
|
|
|
|
}
|
2000-11-17 00:18:32 +00:00
|
|
|
return Py_FindMethod(Object_methods, (PyObject *)self, name);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static int Object_setattr( AtlasObject *self, char *name, PyObject *v)
|
|
|
|
|
{
|
2000-11-30 12:51:59 +00:00
|
|
|
if (self->m_obj == NULL) {
|
|
|
|
|
PyErr_SetString(PyExc_TypeError,"invalid object");
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
if (self->m_obj->IsMap()) {
|
|
|
|
|
Object::MapType & omap = self->m_obj->AsMap();
|
|
|
|
|
Object v_obj = PyObject_asObject(v);
|
2000-12-07 12:14:35 +00:00
|
|
|
if ((v_obj.GetType() != Object::TYPE_NONE) &&
|
|
|
|
|
(v_obj.GetType() != Object::TYPE_MAP) &&
|
|
|
|
|
(v_obj.GetType() != Object::TYPE_LIST)) {
|
2000-11-30 12:51:59 +00:00
|
|
|
omap[name] = v_obj;
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if (self->Object_attr == NULL) {
|
|
|
|
|
self->Object_attr = PyDict_New();
|
|
|
|
|
if (self->Object_attr == NULL) {
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return PyDict_SetItemString(self->Object_attr, name, v);
|
2000-11-17 00:18:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
PyTypeObject Object_Type = {
|
|
|
|
|
PyObject_HEAD_INIT(&PyType_Type)
|
|
|
|
|
0, /*ob_size*/
|
2000-11-29 18:02:01 +00:00
|
|
|
"AtlasObject", /*tp_name*/
|
2000-11-17 00:18:32 +00:00
|
|
|
sizeof(AtlasObject), /*tp_basicsize*/
|
|
|
|
|
0, /*tp_itemsize*/
|
|
|
|
|
/* methods */
|
|
|
|
|
(destructor)Object_dealloc, /*tp_dealloc*/
|
|
|
|
|
0, /*tp_print*/
|
|
|
|
|
(getattrfunc)Object_getattr, /*tp_getattr*/
|
|
|
|
|
(setattrfunc)Object_setattr, /*tp_setattr*/
|
|
|
|
|
0, /*tp_compare*/
|
|
|
|
|
0, /*tp_repr*/
|
|
|
|
|
0, /*tp_as_number*/
|
|
|
|
|
0, /*tp_as_sequence*/
|
|
|
|
|
0, /*tp_as_mapping*/
|
|
|
|
|
0, /*tp_hash*/
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* Beginning of Object creation functions section.
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
AtlasObject * newAtlasObject(PyObject *arg)
|
|
|
|
|
{
|
|
|
|
|
AtlasObject * self;
|
|
|
|
|
self = PyObject_NEW(AtlasObject, &Object_Type);
|
|
|
|
|
if (self == NULL) {
|
|
|
|
|
return NULL;
|
|
|
|
|
}
|
|
|
|
|
self->Object_attr = NULL;
|
|
|
|
|
return self;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* Utility functions to munge between Object related types and python types
|
|
|
|
|
*/
|
|
|
|
|
|
2001-02-26 14:41:10 +00:00
|
|
|
static PyObject * MapType_asPyObject(const Object::MapType & map)
|
2000-11-17 00:18:32 +00:00
|
|
|
{
|
|
|
|
|
PyObject * args_pydict = PyDict_New();
|
2001-02-26 14:41:10 +00:00
|
|
|
Object::MapType::const_iterator I;
|
2000-11-17 00:18:32 +00:00
|
|
|
AtlasObject * item;
|
|
|
|
|
for(I=map.begin();I!=map.end();I++) {
|
|
|
|
|
const string & key = I->first;
|
|
|
|
|
item = newAtlasObject(NULL);
|
|
|
|
|
if (item == NULL) {
|
|
|
|
|
PyErr_SetString(PyExc_TypeError,"error creating map");
|
|
|
|
|
return NULL;
|
|
|
|
|
}
|
|
|
|
|
item->m_obj = new Object(I->second);
|
|
|
|
|
PyDict_SetItemString(args_pydict,(char *)key.c_str(),(PyObject *)item);
|
|
|
|
|
}
|
|
|
|
|
return(args_pydict);
|
|
|
|
|
}
|
|
|
|
|
|
2001-02-26 14:41:10 +00:00
|
|
|
static PyObject * ListType_asPyObject(const Object::ListType & list)
|
2000-11-17 00:18:32 +00:00
|
|
|
{
|
|
|
|
|
PyObject * args_pylist = PyList_New(list.size());
|
2001-02-26 14:41:10 +00:00
|
|
|
Object::ListType::const_iterator I;
|
2000-11-17 00:18:32 +00:00
|
|
|
int j=0;
|
|
|
|
|
AtlasObject * item;
|
|
|
|
|
for(I=list.begin();I!=list.end();I++,j++) {
|
|
|
|
|
item = newAtlasObject(NULL);
|
|
|
|
|
if (item == NULL) {
|
|
|
|
|
PyErr_SetString(PyExc_TypeError,"error creating list");
|
|
|
|
|
return NULL;
|
|
|
|
|
}
|
|
|
|
|
item->m_obj = new Object(*I);
|
|
|
|
|
PyList_SetItem(args_pylist, j, (PyObject *)item);
|
|
|
|
|
}
|
|
|
|
|
return(args_pylist);
|
|
|
|
|
}
|
|
|
|
|
|
2001-02-26 14:41:10 +00:00
|
|
|
PyObject * Object_asPyObject(const Object & obj)
|
2000-11-17 00:18:32 +00:00
|
|
|
{
|
|
|
|
|
PyObject * ret = NULL;
|
|
|
|
|
switch (obj.GetType()) {
|
|
|
|
|
case Object::TYPE_INT:
|
2000-12-03 16:56:10 +00:00
|
|
|
ret = PyInt_FromLong(obj.AsInt());
|
2000-11-17 00:18:32 +00:00
|
|
|
break;
|
|
|
|
|
case Object::TYPE_FLOAT:
|
|
|
|
|
ret = PyFloat_FromDouble(obj.AsFloat());
|
|
|
|
|
break;
|
|
|
|
|
case Object::TYPE_STRING:
|
|
|
|
|
ret = PyString_FromString(obj.AsString().c_str());
|
|
|
|
|
break;
|
|
|
|
|
case Object::TYPE_MAP:
|
|
|
|
|
ret = MapType_asPyObject(obj.AsMap());
|
|
|
|
|
break;
|
|
|
|
|
case Object::TYPE_LIST:
|
|
|
|
|
ret = ListType_asPyObject(obj.AsList());
|
|
|
|
|
break;
|
|
|
|
|
default:
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
return(ret);
|
|
|
|
|
}
|
|
|
|
|
|
2001-02-26 14:41:10 +00:00
|
|
|
static Object::ListType PyListObject_asListType(PyObject * list)
|
2000-11-17 00:18:32 +00:00
|
|
|
{
|
|
|
|
|
Object::ListType argslist;
|
|
|
|
|
AtlasObject * item;
|
|
|
|
|
for(int i = 0; i < PyList_Size(list); i++) {
|
|
|
|
|
item = (AtlasObject *)PyList_GetItem(list, i);
|
2000-12-30 23:16:57 +00:00
|
|
|
if (PyAtlasObject_Check(item)) {
|
|
|
|
|
argslist.push_back(*(item->m_obj));
|
|
|
|
|
} else {
|
|
|
|
|
Object o = PyObject_asObject((PyObject*)item);
|
|
|
|
|
if (o.GetType() != Object::TYPE_NONE) {
|
|
|
|
|
argslist.push_back(o);
|
|
|
|
|
}
|
2000-11-17 00:18:32 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return(argslist);
|
|
|
|
|
}
|
|
|
|
|
|
2001-02-26 14:41:10 +00:00
|
|
|
static Object::MapType PyDictObject_asMapType(PyObject * dict)
|
2000-11-17 00:18:32 +00:00
|
|
|
{
|
|
|
|
|
Object::MapType argsmap;
|
|
|
|
|
AtlasObject * item;
|
|
|
|
|
PyObject * list = PyDict_Keys(dict);
|
|
|
|
|
PyObject * key;
|
|
|
|
|
for(int i = 0; i < PyDict_Size(list); i++) {
|
|
|
|
|
key = PyList_GetItem(list, i);
|
|
|
|
|
item = (AtlasObject *)PyDict_GetItem(dict, key);
|
2000-12-16 20:28:06 +00:00
|
|
|
if (!PyAtlasObject_Check(item)) {
|
2000-11-17 00:18:32 +00:00
|
|
|
PyErr_SetString(PyExc_TypeError,"dict contains non Atlas Object");
|
2000-12-11 13:06:12 +00:00
|
|
|
Py_DECREF(list);
|
2000-11-17 00:18:32 +00:00
|
|
|
return Object::MapType();
|
|
|
|
|
}
|
|
|
|
|
argsmap[PyString_AsString(key)] = *(item->m_obj);
|
|
|
|
|
}
|
2000-12-11 13:06:12 +00:00
|
|
|
Py_DECREF(list);
|
2000-11-17 00:18:32 +00:00
|
|
|
return(argsmap);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Object PyObject_asObject(PyObject * o)
|
|
|
|
|
{
|
|
|
|
|
if (PyInt_Check(o)) {
|
2000-12-03 16:56:10 +00:00
|
|
|
return(Object((int)PyInt_AsLong(o)));
|
2000-11-17 00:18:32 +00:00
|
|
|
}
|
|
|
|
|
if (PyFloat_Check(o)) {
|
|
|
|
|
return(Object(PyFloat_AsDouble(o)));
|
|
|
|
|
}
|
|
|
|
|
if (PyString_Check(o)) {
|
|
|
|
|
return(Object(PyString_AsString(o)));
|
|
|
|
|
}
|
|
|
|
|
if (PyList_Check(o)) {
|
|
|
|
|
return(Object(PyListObject_asListType(o)));
|
|
|
|
|
}
|
|
|
|
|
if (PyDict_Check(o)) {
|
|
|
|
|
return(Object(PyDictObject_asMapType(o)));
|
|
|
|
|
}
|
2000-12-07 12:14:35 +00:00
|
|
|
if (PyTuple_Check(o)) {
|
|
|
|
|
Object::ListType list;
|
|
|
|
|
int i, size = PyTuple_Size(o);
|
|
|
|
|
for(i = 0; i < size; i++) {
|
|
|
|
|
Object item = PyObject_asObject(PyTuple_GetItem(o, i));
|
|
|
|
|
if (item.GetType() != Object::TYPE_NONE) {
|
|
|
|
|
list.push_back(item);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return Object(list);
|
|
|
|
|
}
|
2000-12-16 20:28:06 +00:00
|
|
|
if (PyAtlasObject_Check(o)) {
|
2000-11-17 00:18:32 +00:00
|
|
|
AtlasObject * obj = (AtlasObject *)o;
|
|
|
|
|
return *(obj->m_obj);
|
|
|
|
|
}
|
2000-12-16 20:28:06 +00:00
|
|
|
if (PyOperation_Check(o)) {
|
2000-12-06 18:21:35 +00:00
|
|
|
RootOperationObject * op = (RootOperationObject *)o;
|
|
|
|
|
return op->operation->AsObject();
|
|
|
|
|
}
|
2000-12-16 20:28:06 +00:00
|
|
|
if (PyOplist_Check(o)) {
|
2000-12-06 18:21:35 +00:00
|
|
|
OplistObject * opl = (OplistObject *)o;
|
|
|
|
|
Object::ListType _list;
|
|
|
|
|
Object msg(_list);
|
|
|
|
|
Object::ListType & entlist = msg.AsList();
|
2001-02-26 14:41:10 +00:00
|
|
|
const oplist & ops = *opl->ops;
|
2000-12-06 18:21:35 +00:00
|
|
|
oplist::const_iterator I;
|
|
|
|
|
for(I = ops.begin(); I != ops.end(); I++) {
|
|
|
|
|
entlist.push_back((*I)->AsObject());
|
|
|
|
|
}
|
|
|
|
|
return msg;
|
|
|
|
|
}
|
2000-12-16 20:28:06 +00:00
|
|
|
if (PyLocation_Check(o)) {
|
2000-12-07 12:14:35 +00:00
|
|
|
LocationObject * loc = (LocationObject *)o;
|
|
|
|
|
Object::MapType _map;
|
|
|
|
|
Object ent(_map);
|
2001-04-12 11:13:06 +00:00
|
|
|
loc->location->addToObject(ent);
|
2000-12-07 12:14:35 +00:00
|
|
|
return ent;
|
|
|
|
|
}
|
2000-11-17 00:18:32 +00:00
|
|
|
return Object();
|
|
|
|
|
}
|