Example #1
0
/* _ped_Constraint -> PedConstraint functions */
PedConstraint *_ped_Constraint2PedConstraint(PyObject *s) {
    PedConstraint *ret = NULL;
    PedAlignment *start_align = NULL, *end_align = NULL;
    PedGeometry *start_range = NULL, *end_range = NULL;
    _ped_Constraint *constraint = (_ped_Constraint *) s;

    if (constraint == NULL) {
        PyErr_SetString(PyExc_TypeError, "Empty _ped.Constraint()");
        return NULL;
    }

    start_align = _ped_Alignment2PedAlignment(constraint->start_align);
    if (start_align == NULL) {
        return NULL;
    }

    end_align = _ped_Alignment2PedAlignment(constraint->end_align);
    if (end_align == NULL) {
        ped_alignment_destroy(start_align);
        return NULL;
    }

    start_range = _ped_Geometry2PedGeometry(constraint->start_range);
    if (start_range == NULL) {
        ped_alignment_destroy(start_align);
        ped_alignment_destroy(end_align);
        return NULL;
    }

    end_range = _ped_Geometry2PedGeometry(constraint->end_range);
    if (end_range == NULL) {
        ped_alignment_destroy(start_align);
        ped_alignment_destroy(end_align);
        return NULL;
    }

    ret = ped_constraint_new(start_align, end_align, start_range, end_range,
                             constraint->min_size, constraint->max_size);
    if (ret == NULL) {
        /* Fall through to clean up memory, but set the error condition now. */
        PyErr_NoMemory();
    }

    ped_alignment_destroy(start_align);
    ped_alignment_destroy(end_align);

    return ret;
}
Example #2
0
PyObject *py_ped_alignment_is_aligned(PyObject *s, PyObject *args) {
    int ret = -1;
    PyObject *in_geom = NULL;
    PedAlignment *align = NULL;
    PedGeometry *out_geom = NULL;
    PedSector sector;

    if (!PyArg_ParseTuple(args, "O!L", &_ped_Geometry_Type_obj, &in_geom, &sector)) {
        return NULL;
    }

    align = _ped_Alignment2PedAlignment(s);
    if (align == NULL) {
        return NULL;
    }

    out_geom = _ped_Geometry2PedGeometry(in_geom);
    if (out_geom == NULL) {
        return NULL;
    }

    ret = ped_alignment_is_aligned(align, out_geom, sector);
    ped_alignment_destroy(align);

    if (ret) {
        Py_RETURN_TRUE;
    } else {
        Py_RETURN_FALSE;
    }
}
Example #3
0
PyObject *py_ped_alignment_align_nearest(PyObject *s, PyObject *args) {
    PyObject *in_geom = NULL;
    PedAlignment *align = NULL;
    PedGeometry *out_geom = NULL;
    PedSector sector, ret;

    if (!PyArg_ParseTuple(args, "O!L", &_ped_Geometry_Type_obj, &in_geom, &sector)) {
        return NULL;
    }

    align = _ped_Alignment2PedAlignment(s);
    if (align == NULL) {
        return NULL;
    }

    out_geom = _ped_Geometry2PedGeometry(in_geom);
    if (out_geom == NULL) {
        return NULL;
    }

    ret = ped_alignment_align_nearest(align, out_geom, sector);

    ped_alignment_destroy(align);

    if (ret == -1) {
        PyErr_SetString(PyExc_ArithmeticError, "Could not align to closest sector");
        return NULL;
    }

    return PyLong_FromLong(ret);
}
Example #4
0
/* 1:1 function mappings for natmath.h in libparted */
PyObject *py_ped_alignment_duplicate(PyObject *s, PyObject *args) {
    PedAlignment *alignment = NULL, *align = NULL;
    _ped_Alignment *ret = NULL;

    alignment = _ped_Alignment2PedAlignment(s);
    if (alignment == NULL) {
        return NULL;
    }

    align = ped_alignment_duplicate(alignment);

    ped_alignment_destroy(alignment);

    if (align) {
        ret = PedAlignment2_ped_Alignment(align);
    }
    else {
        PyErr_SetString(CreateException, "Could not duplicate alignment");
        return NULL;
    }

    ped_alignment_destroy(align);

    return (PyObject *) ret;
}
Example #5
0
PyObject *py_ped_alignment_intersect(PyObject *s, PyObject *args) {
    PyObject *in_b = NULL;
    PedAlignment *out_a = NULL, *out_b = NULL, *align = NULL;
    _ped_Alignment *ret = NULL;

    if (!PyArg_ParseTuple(args, "O!", &_ped_Alignment_Type_obj, &in_b)) {
        return NULL;
    }

    out_a = _ped_Alignment2PedAlignment(s);
    if (out_a == NULL) {
        return NULL;
    }

    out_b = _ped_Alignment2PedAlignment(in_b);
    if (out_b == NULL) {
        return NULL;
    }

    align = ped_alignment_intersect(out_a, out_b);

    ped_alignment_destroy(out_a);
    ped_alignment_destroy(out_b);

    if (align) {
        ret = PedAlignment2_ped_Alignment(align);
    }
    else {
        PyErr_SetString(PyExc_ArithmeticError, "Could not find alignment intersection");
        return NULL;
    }

    ped_alignment_destroy(align);

    return (PyObject *) ret;
}
Example #6
0
int _ped_Constraint_init(_ped_Constraint *self, PyObject *args,
                         PyObject *kwds) {
    static char *kwlist[] = {"start_align", "end_align", "start_range",
                             "end_range", "min_size", "max_size", NULL};
    PedConstraint *constraint = NULL;
    PedAlignment *start_align = NULL, *end_align = NULL;
    PedGeometry *start_range = NULL, *end_range = NULL;

    if (kwds == NULL) {
        if (!PyArg_ParseTuple(args, "O!O!O!O!LL",
                              &_ped_Alignment_Type_obj, &self->start_align,
                              &_ped_Alignment_Type_obj, &self->end_align,
                              &_ped_Geometry_Type_obj, &self->start_range,
                              &_ped_Geometry_Type_obj, &self->end_range,
                              &self->min_size, &self->max_size)) {
            self->start_align = self->end_align = NULL;
            self->start_range = self->end_range = NULL;
            return -1;
        }
    } else {
        if (!PyArg_ParseTupleAndKeywords(args, kwds, "O!O!O!O!LL", kwlist,
                                         &_ped_Alignment_Type_obj,
                                         &self->start_align,
                                         &_ped_Alignment_Type_obj,
                                         &self->end_align,
                                         &_ped_Geometry_Type_obj,
                                         &self->start_range,
                                         &_ped_Geometry_Type_obj,
                                         &self->end_range,
                                         &self->min_size,
                                         &self->max_size)) {
            self->start_align = self->end_align = NULL;
            self->start_range = self->end_range = NULL;
            return -2;
        }
    }

    /*
     * try to call libparted with provided information,
     * on failure, raise an exception
     */
    start_align = _ped_Alignment2PedAlignment(self->start_align);
    end_align = _ped_Alignment2PedAlignment(self->end_align);
    start_range = _ped_Geometry2PedGeometry(self->start_range);
    end_range = _ped_Geometry2PedGeometry(self->end_range);

    constraint = ped_constraint_new(start_align, end_align,
                                    start_range, end_range,
                                    self->min_size, self->max_size);
    if (constraint == NULL) {
        PyErr_SetString(CreateException, "Could not create new constraint");

        ped_alignment_destroy(start_align);
        ped_alignment_destroy(end_align);
        self->start_align = NULL;
        self->end_align = NULL;
        self->start_range = NULL;
        self->end_range = NULL;
        return -3;
    }

    /* increment reference count for PyObjects read by PyArg_ParseTuple */
    Py_INCREF(self->start_align);
    Py_INCREF(self->end_align);
    Py_INCREF(self->start_range);
    Py_INCREF(self->end_range);

    /* clean up libparted objects we created */
    ped_alignment_destroy(start_align);
    ped_alignment_destroy(end_align);
    ped_constraint_destroy(constraint);
    return 0;
}