File: pydia-properties.c

package info (click to toggle)
dia 0.98%2Bgit20250126-2
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 52,072 kB
  • sloc: ansic: 155,381; xml: 14,056; python: 6,250; cpp: 3,598; sh: 439; perl: 137; makefile: 25
file content (357 lines) | stat: -rw-r--r-- 8,067 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
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
/* 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 <glib/gi18n-lib.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 (PyObject *self)
{
  ((PyDiaObject *) self)->object = NULL; /* XXX: should dec ref */
  PyObject_DEL (self);
}


/*
 * Compare
 */
static PyObject *
PyDiaProperties_RichCompare (PyObject *self,
                             PyObject *other,
                             int       op)
{
  Py_RETURN_RICHCOMPARE (((PyDiaProperties *) self)->object,
                         ((PyDiaProperties *) other)->object,
                         op);
}


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


/*
 * Repr / _Str
 */
static PyObject *
PyDiaProperties_Str (PyObject *self)
{
  PyObject* py_s;
  char* s = g_strdup_printf ("<DiaProperties at %p of DiaObject at %p>",
                             self, ((PyDiaObject *) self)->object);

  py_s = PyUnicode_FromString (s);
  g_clear_pointer (&s, g_free);

  return py_s;
}


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

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

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

    p = object_prop_by_name (((PyDiaObject *) 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 (!PyUnicode_Check (key)) {
    ok = 0; /* is this too drastic? */
  }

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

    name = PyUnicode_AsUTF8 (key);

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

  return PyLong_FromLong (ok);
}


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

  list = PyList_New (0);

  // TODO: Why the check?
  if (self->object->ops->describe_props) {
    desc = dia_object_describe_properties (self->object);
  }

  if (desc) {
    for (int 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, PyUnicode_FromString (desc[i].name));
      }
    }
  }

  return list;
}


static Py_ssize_t
PyDiaProperties_Length (PyObject *obj)
{
  PyDiaProperties *self = (PyDiaProperties *) obj;

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

    if (self->object->ops->describe_props) {
      desc = dia_object_describe_properties (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 (PyObject *obj, PyObject *key)
{
  PyDiaProperties *self = (PyDiaProperties *) obj;
  PyObject *v = NULL;

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

    name = PyUnicode_AsUTF8 (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 (PyObject *obj, PyObject *key, PyObject *val)
{
  PyDiaProperties *self = (PyDiaProperties *) obj;
  int ret = -1;

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

    name = PyUnicode_AsUTF8 (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 PyObject *
PyDiaProperties_Item (PyObject *o, gssize i)
{
  PyDiaProperties *self = (PyDiaProperties *) o;
  PyObject *v = NULL;
  Property *p;
  GPtrArray *props;

  if (i > self->nprops || i < 0) {
    PyErr_SetString (PyExc_IndexError, "PyDiaProperties index out of range");
    return NULL;
  }

  props = g_ptr_array_new ();

  object_get_props (self->object, props);

  p = g_ptr_array_index (props, i);

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

  g_ptr_array_unref (props);

  return v;
}


static PyMappingMethods PyDiaProperties_AsMapping = {
  .mp_length = PyDiaProperties_Length,
  .mp_subscript = PyDiaProperties_Subscript,
  .mp_ass_subscript = PyDiaProperties_AssSub,
};


static PySequenceMethods PyDiaProperties_AsSequence = {
  .sq_length = PyDiaProperties_Length,
  .sq_item = PyDiaProperties_Item,
};


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


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


/*
 * Python object
 */
PyTypeObject PyDiaProperties_Type = {
  PyVarObject_HEAD_INIT (NULL, 0)
  .tp_name = "dia.Properties",
  .tp_basicsize = sizeof (PyDiaProperties),
  .tp_dealloc = PyDiaProperties_Dealloc,
  .tp_getattro = PyObject_GenericGetAttr,
  .tp_richcompare = PyDiaProperties_RichCompare,
  .tp_as_mapping = &PyDiaProperties_AsMapping,
  .tp_as_sequence = &PyDiaProperties_AsSequence,
  .tp_hash = PyDiaProperties_Hash,
  .tp_str = PyDiaProperties_Str,
  .tp_doc = "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.",
  .tp_methods = PyDiaProperties_Methods,
  .tp_members = PyDiaProperties_Members,
};