File: pydia-properties.c

package info (click to toggle)
dia 0.97.3%2Bgit20160930-9
  • links: PTS
  • area: main
  • in suites: bullseye
  • size: 54,372 kB
  • sloc: ansic: 155,065; xml: 16,326; python: 6,641; cpp: 4,935; makefile: 3,833; sh: 540; perl: 137; sed: 19
file content (317 lines) | stat: -rw-r--r-- 7,389 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
/* Python plug-in for dia
 * Copyright (C) 1999  James Henstridge
 * Copyright (C) 2000  Hans Breuer
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * 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., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

#include <config.h>

#include "pydia-object.h"
#include "pydia-properties.h"

#include <structmember.h> /* PyMemberDef */

/*
 * New
 */
PyObject* 
PyDiaProperties_New (DiaObject* obj)
{
  PyDiaProperties *self;
  
  self = PyObject_NEW(PyDiaProperties, &PyDiaProperties_Type);
  if (!self) return NULL;
  
  self->object = obj;  /* XXX: should be ref counted */
  self->nprops = -1;
  
  return (PyObject *)self;
}

/*
 * Dealloc
 */
static void
PyDiaProperties_Dealloc(PyDiaObject *self)
{
  self->object = NULL; /* XXX: should dec ref */
  PyObject_DEL(self);
}

/*
 * Compare
 */
static int
PyDiaProperties_Compare(PyDiaProperties *self,
                      PyDiaProperties *other)
{
  return memcmp(&(self->object), &(other->object), sizeof(DiaObject*));
}

/*
 * Hash
 */
static long
PyDiaProperties_Hash(PyObject *self)
{
  return (long)self;
}

/*
 * Repr / _Str
 */
static PyObject *
PyDiaProperties_Str(PyDiaProperties *self)
{
  PyObject* py_s;
  gchar* s = g_strdup_printf("<DiaProperties at %p of DiaObject at %p>",
                             self, self->object );
  py_s = PyString_FromString(s);
  g_free (s);
  return py_s;
}

/*
 * Implement the dictionary interface for the Properties object
 */
static PyObject *
PyDiaProperties_Get(PyDiaProperties *self, PyObject *args)
{
  PyObject *key;
  PyObject *failobj = Py_None;
  PyObject *val = NULL;

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

  if (self->object->ops->get_props != NULL) {
    Property *p;
    char* name = PyString_AsString(key);
    p = object_prop_by_name (self->object, name);  
    if (p) {
      val = PyDiaProperty_New(p); /* makes a copy */
      p->ops->free(p);
    }
  }
 
  if (val == NULL) { 
    val = failobj;
    Py_INCREF(val);
  }

  return val;
}

static PyObject *
PyDiaProperties_HasKey(PyDiaProperties *self, PyObject *args)
{
  PyObject *key;
  long ok = 0;

  if (!PyArg_ParseTuple(args, "O:has_key", &key))
    return NULL;

  if (!PyString_Check(key))
    ok = 0; /* is this too drastic? */

  if (self->object->ops->get_props != NULL) {
    Property *p;
    char     *name;

    name = PyString_AsString(key);

    p = object_prop_by_name (self->object, name);  
    ok = (NULL != p);
    if (p)
      p->ops->free(p);
  }

  return PyInt_FromLong(ok);
}

static PyObject *
PyDiaProperties_Keys(PyDiaProperties *self, PyObject *args)
{
  PyObject *list;
  const PropDescription *desc = NULL;

  if (!PyArg_NoArgs(args))
    return NULL;

  list = PyList_New(0);

  if (self->object->ops->describe_props)
    desc = self->object->ops->describe_props(self->object);
  if (desc) {
    int i;
    for (i = 0; desc[i].name; i++) {
      /* at the moment I see no use case to access widgets from Python, PROP_FLAG_LOAD_ONLY compatibility not anted here */
      if ((desc[i].flags & (PROP_FLAG_WIDGET_ONLY|PROP_FLAG_LOAD_ONLY)) == 0)
        PyList_Append(list, PyString_FromString(desc[i].name));
    }
  }

  return list;
}

static int
PyDiaProperties_Length (PyDiaProperties* self)
{
  if (self->nprops < 0) {
    const PropDescription *desc = NULL;

    if (self->object->ops->describe_props)
      desc = self->object->ops->describe_props(self->object);

    self->nprops = 0;
    if (desc) {
      int i;
      for (i = 0; desc[i].name; i++)
        self->nprops++;
    }
  }

  return self->nprops;
}

static PyObject *
PyDiaProperties_Subscript (PyDiaProperties *self, register PyObject *key)
{
  PyObject *v = NULL;

  if (!self->object->ops->describe_props) {
    PyErr_SetObject(PyExc_KeyError, key);
    return NULL;
  }
  else {
    Property *p;
    char     *name;  

    name = PyString_AsString(key);
    p = object_prop_by_name (self->object, name);  

    if (p) {
      v = PyDiaProperty_New(p); /* makes a copy */
      p->ops->free (p);
    }
  }

  if (v == NULL)
    PyErr_SetObject(PyExc_KeyError, key);

  return v;
}

static int
PyDiaProperties_AssSub (PyDiaProperties* self, PyObject *key, PyObject *val)
{
  int ret = -1;

  if (val == NULL) {
    PyErr_SetString(PyExc_TypeError, "can not delete properties.");
  }
  else {
    Property *p;
    char     *name;  

    name = PyString_AsString(key);
    p = object_prop_by_name (self->object, name);  

    /* g_print ("AssSub(key: '%s', type <%s>)\n", name, (p ? p->type : "none")); */
    if (p) {
      if (0 == PyDiaProperty_ApplyToObject(self->object, name, p, val)) {
        /* if applied the property is deleted */
        ret = 0;
      }
      else {
        p->ops->free (p);
        PyErr_SetString(PyExc_TypeError, "prop type mis-match.");
      }
    }
    else {
      PyErr_SetObject(PyExc_KeyError, key);
    }
  }

  return ret;
}

static PyMappingMethods PyDiaProperties_AsMapping = {
	(inquiry)PyDiaProperties_Length, /*mp_length*/
	(binaryfunc)PyDiaProperties_Subscript, /*mp_subscript*/
	(objobjargproc)PyDiaProperties_AssSub, /*mp_ass_subscript*/
};

static PyMethodDef PyDiaProperties_Methods[] = {
	/* duck-typing dictionary */
	{"get",     (PyCFunction)PyDiaProperties_Get,     METH_VARARGS},
	{"has_key", (PyCFunction)PyDiaProperties_HasKey,  METH_VARARGS},
	{"keys",    (PyCFunction)PyDiaProperties_Keys},
	{NULL,		NULL}		/* sentinel */
};

#define T_INVALID -1 /* can't allow direct access due to pyobject->handle indirection */
static PyMemberDef PyDiaProperties_Members[] = {
	{ NULL }
};

/*
 * GetAttr
 */
static PyObject*
PyDiaProperties_GetAttr(PyDiaProperties *self, gchar *attr)
{
  return Py_FindMethod(PyDiaProperties_Methods, (PyObject *)self, attr);
}

/*
 * Python object
 */
PyTypeObject PyDiaProperties_Type = {
    PyObject_HEAD_INIT(&PyType_Type)
    0,
    "dia.Properties",
    sizeof(PyDiaProperties),
    0,
    (destructor)PyDiaProperties_Dealloc,
    (printfunc)0,
    (getattrfunc)PyDiaProperties_GetAttr,
    (setattrfunc)0,
    (cmpfunc)PyDiaProperties_Compare,
    (reprfunc)0,
    0,
    0,
    &PyDiaProperties_AsMapping, /* PyMappingMethods */
    (hashfunc)PyDiaProperties_Hash,
    (ternaryfunc)0,
    (reprfunc)PyDiaProperties_Str,
    (getattrofunc)0,
    (setattrofunc)0,
    (PyBufferProcs *)0,
    0L, /* Flags */
    "A dictionary interface to dia.Object's standard properties. Many properties"
    "can be get and set through this. If there is a specific method to change an"
    "objects property like o.move() or o.move_handle() use that instead.",
    (traverseproc)0,
    (inquiry)0,
    (richcmpfunc)0,
    0, /* tp_weakliszoffset */
    (getiterfunc)0,
    (iternextfunc)0,
    PyDiaProperties_Methods, /* tp_methods */
    PyDiaProperties_Members, /* tp_members */
    0
};