Beispiel #1
0
static PyObject *BPy_IDGroup_Map_GetItem(BPy_IDProperty *self, PyObject *item)
{
    IDProperty *idprop;
    const char *name;

    if (self->prop->type != IDP_GROUP) {
        PyErr_SetString(PyExc_TypeError, "unsubscriptable object");
        return NULL;
    }

    name = _PyUnicode_AsString(item);

    if (name == NULL) {
        PyErr_SetString(PyExc_TypeError, "only strings are allowed as keys of ID properties");
        return NULL;
    }

    idprop = IDP_GetPropertyFromGroup(self->prop, name);

    if (idprop == NULL) {
        PyErr_SetString(PyExc_KeyError, "key not in subgroup dict");
        return NULL;
    }

    return BPy_IDGroup_WrapData(self->id, idprop, self->prop);
}
Beispiel #2
0
static PyObject *BPy_Group_Iter_Next(BPy_IDGroup_Iter *self)
{

    if (self->cur) {
        PyObject *ret;
        IDProperty *cur;

        cur = self->cur;
        self->cur = self->cur->next;

        if (self->mode == IDPROP_ITER_ITEMS) {
            ret = PyTuple_New(2);
            PyTuple_SET_ITEM(ret, 0, PyUnicode_FromString(cur->name));
            PyTuple_SET_ITEM(ret, 1, BPy_IDGroup_WrapData(self->group->id, cur, self->group->prop));
            return ret;
        }
        else {
            return PyUnicode_FromString(cur->name);
        }
    }
    else {
        PyErr_SetString(PyExc_StopIteration, "iterator at end");
        return NULL;
    }
}
Beispiel #3
0
static PyObject *idprop_py_from_idp_idparray(ID *id, IDProperty *prop)
{
	PyObject *seq = PyList_New(prop->len);
	IDProperty *array = IDP_IDPArray(prop);
	int i;

	if (!seq) {
		PyErr_Format(PyExc_RuntimeError,
		             "%s: IDP_IDPARRAY: PyList_New(%d) failed",
		             __func__, prop->len);
		return NULL;
	}

	for (i = 0; i < prop->len; i++) {
		PyObject *wrap = BPy_IDGroup_WrapData(id, array++, prop);

		/* BPy_IDGroup_MapDataToPy sets the error */
		if (UNLIKELY(wrap == NULL)) {
			Py_DECREF(seq);
			return NULL;
		}

		PyList_SET_ITEM(seq, i, wrap);
	}

	return seq;
}
Beispiel #4
0
PyObject *BPy_IDGroup_WrapData( ID *id, IDProperty *prop )
{
    switch ( prop->type ) {
    case IDP_STRING:
#ifdef USE_STRING_COERCE
        return PyC_UnicodeFromByte(prop->data.pointer);
#else
        return PyUnicode_FromString(prop->data.pointer);
#endif
    case IDP_INT:
        return PyLong_FromLong( (long)prop->data.val );
    case IDP_FLOAT:
        return PyFloat_FromDouble( (double)(*(float*)(&prop->data.val)) );
    case IDP_DOUBLE:
        return PyFloat_FromDouble( (*(double*)(&prop->data.val)) );
    case IDP_GROUP:
        /*blegh*/
    {
        BPy_IDProperty *group = PyObject_New(BPy_IDProperty, &IDGroup_Type);
        group->id = id;
        group->prop = prop;
        return (PyObject*) group;
    }
    case IDP_ARRAY:
    {
        BPy_IDProperty *array = PyObject_New(BPy_IDProperty, &IDArray_Type);
        array->id = id;
        array->prop = prop;
        return (PyObject*) array;
    }
    case IDP_IDPARRAY: /* this could be better a internal type */
    {
        PyObject *seq = PyList_New(prop->len), *wrap;
        IDProperty *array= IDP_IDPArray(prop);
        int i;

        if (!seq) {
            PyErr_Format(PyExc_RuntimeError, "BPy_IDGroup_MapDataToPy, IDP_IDPARRAY: PyList_New(%d) failed", prop->len);
            return NULL;
        }

        for (i=0; i<prop->len; i++) {
            wrap= BPy_IDGroup_WrapData(id, array++);

            if (!wrap) /* BPy_IDGroup_MapDataToPy sets the error */
                return NULL;

            PyList_SET_ITEM(seq, i, wrap);
        }

        return seq;
    }
        /* case IDP_IDPARRAY: TODO */
    }
    Py_RETURN_NONE;
}
Beispiel #5
0
/* Matches python dict.get(key, [default]) */
static PyObject *BPy_IDGroup_Get(BPy_IDProperty *self, PyObject *args)
{
    IDProperty *idprop;
    char *key;
    PyObject *def = Py_None;

    if (!PyArg_ParseTuple(args, "s|O:get", &key, &def))
        return NULL;

    idprop = IDP_GetPropertyFromGroup(self->prop, key);
    if (idprop) {
        PyObject *pyobj = BPy_IDGroup_WrapData(self->id, idprop, self->prop);
        if (pyobj)
            return pyobj;
    }

    Py_INCREF(def);
    return def;
}
Beispiel #6
0
PyObject *BPy_Wrap_GetValues(ID *id, IDProperty *prop)
{
    PyObject *list = PyList_New(prop->len);
    IDProperty *loop;
    int i;

    for (i = 0, loop = prop->data.group.first; loop; loop = loop->next, i++) {
        PyList_SET_ITEM(list, i, BPy_IDGroup_WrapData(id, loop, prop));
    }

    if (i != prop->len) {
        BPy_IDGroup_CorrectListLen(prop, list, i, __func__);
        Py_DECREF(list); /*free the list*/
        /*call self again*/
        return BPy_Wrap_GetValues(id, prop);
    }

    return list;
}
Beispiel #7
0
PyObject *BPy_Wrap_GetItems(ID *id, IDProperty *prop)
{
    PyObject *seq = PyList_New(prop->len);
    IDProperty *loop;
    int i;

    for (i = 0, loop = prop->data.group.first; loop; loop = loop->next, i++) {
        PyObject *item = PyTuple_New(2);
        PyTuple_SET_ITEM(item, 0, PyUnicode_FromString(loop->name));
        PyTuple_SET_ITEM(item, 1, BPy_IDGroup_WrapData(id, loop, prop));
        PyList_SET_ITEM(seq, i, item);
    }

    if (i != prop->len) {
        BPy_IDGroup_CorrectListLen(prop, seq, i, __func__);
        Py_DECREF(seq); /*free the list*/
        /*call self again*/
        return BPy_Wrap_GetItems(id, prop);
    }

    return seq;
}