vtkWrapPythonClass.c 17.6 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
/*=========================================================================

  Program:   Visualization Toolkit
  Module:    vtkWrapPythonClass.c

  Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen
  All rights reserved.
  See Copyright.txt or http://www.kitware.com/Copyright.htm for details.

     This software is distributed WITHOUT ANY WARRANTY; without even
     the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
     PURPOSE.  See the above copyright notice for more information.

=========================================================================*/

#include "vtkWrapPythonClass.h"
#include "vtkWrapPythonConstant.h"
18
#include "vtkWrapPythonEnum.h"
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
#include "vtkWrapPythonMethodDef.h"
#include "vtkWrapPythonTemplate.h"
#include "vtkWrapPythonType.h"

#include "vtkWrap.h"
#include "vtkWrapText.h"
#include "vtkParseExtras.h"

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>


/* -------------------------------------------------------------------- */
/* prototypes for the methods used by the python wrappers */

/* declare the exports and imports for a VTK/Python class */
static void vtkWrapPython_ExportVTKClass(
  FILE *fp, ClassInfo *data, HierarchyInfo *hinfo);

/* generate the New method for a vtkObjectBase object */
static void vtkWrapPython_GenerateObjectNew(
  FILE *fp, const char *classname, ClassInfo *data,
  HierarchyInfo *hinfo, int class_has_new);


/* -------------------------------------------------------------------- */
/* get the true superclass */
const char *vtkWrapPython_GetSuperClass(
  ClassInfo *data, HierarchyInfo *hinfo)
{
  int i;
  const char *supername = NULL;
  const char *name;
  const char **args;
  const char *defaults[2] = { NULL, NULL };
  char *cp;

  for (i = 0; i < data->NumberOfSuperClasses; i++)
    {
    supername = data->SuperClasses[i];

    if (strncmp(supername, "vtkTypeTemplate<", 16) == 0)
      {
      vtkParse_DecomposeTemplatedType(supername, &name, 2, &args, defaults);
      cp = (char *)malloc(strlen(args[1])+1);
      strcpy(cp, args[1]);
      vtkParse_FreeTemplateDecomposition(name, 2, args);
      supername = cp;
      }

71
72
73
    /* Add QVTKInteractor as the sole exception: It is derived
     * from vtkObject but does not start with "vtk".  Given its
     * name, it would be expected to be derived from QObject. */
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
    if (vtkWrap_IsVTKObjectBaseType(hinfo, data->Name) ||
        strcmp(data->Name, "QVTKInteractor") == 0)
      {
      if (vtkWrap_IsClassWrapped(hinfo, supername) &&
          vtkWrap_IsVTKObjectBaseType(hinfo, supername))
        {
        return supername;
        }
      }
    else if (vtkWrapPython_HasWrappedSuperClass(hinfo, data->Name, NULL))
      {
      return supername;
      }
    }

  return NULL;
}

/* -------------------------------------------------------------------- */
/* check whether the superclass of the specified class is wrapped */
int vtkWrapPython_HasWrappedSuperClass(
  HierarchyInfo *hinfo, const char *classname, int *is_external)
{
  HierarchyEntry *entry;
  const char *module;
  const char *header;
  const char *name;
  const char *supername;
  int result = 0;
  int depth = 0;

  if (is_external)
    {
    *is_external = 0;
    }

  if (!hinfo)
    {
    return 0;
    }

  name = classname;
  entry = vtkParseHierarchy_FindEntry(hinfo, name);
  if (!entry)
    {
    return 0;
    }

  module = entry->Module;
  header = entry->HeaderFile;
  while (entry->NumberOfSuperClasses == 1)
    {
    supername = vtkParseHierarchy_TemplatedSuperClass(entry, name, 0);
    if (name != classname)
      {
      free((char *)name);
      }
    name = supername;
    entry = vtkParseHierarchy_FindEntry(hinfo, name);
    if (!entry)
      {
      break;
      }

    /* check if superclass is in a different module */
    if (is_external && depth == 0 && strcmp(entry->Module, module) != 0)
      {
      *is_external = 1;
      }
    depth++;

    /* the order of these conditions is important */
    if (entry->IsTypedef)
      {
      break;
      }
    else if (strncmp(entry->Name, "vtk", 3) != 0)
      {
      break;
      }
154
    else if (!vtkParseHierarchy_GetProperty(entry, "WRAP_EXCLUDE_PYTHON"))
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
      {
      result = 1;
      break;
      }
    else if (strcmp(entry->HeaderFile, header) != 0)
      {
      break;
      }
    }

  if (name != classname)
    {
    free((char *)name);
    }

  return result;
}


/* -------------------------------------------------------------------- */
/* Create the docstring for a class, and print it to fp */
void vtkWrapPython_ClassDoc(
  FILE *fp, FileInfo *file_info, ClassInfo *data, HierarchyInfo *hinfo,
  int is_vtkobject)
{
  char pythonname[1024];
  const char *supername;
  char *cp;
183
  const char *ccp = NULL;
184
  size_t i, n;
185
  size_t briefmax = 255;
186
187
188
189
  int j;
  char temp[500];
  char *comment;

190
  if (data == file_info->MainClass && file_info->NameComment)
191
    {
192
    /* use the old VTK-style class description */
193
194
195
196
197
    fprintf(fp,
            "    \"%s\\n\",\n",
            vtkWrapText_QuoteString(
              vtkWrapText_FormatComment(file_info->NameComment, 70), 500));
    }
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
  else if (data->Comment)
    {
    strncpy(temp, data->Name, briefmax);
    temp[briefmax] = '\0';
    i = strlen(temp);
    temp[i++] = ' ';
    temp[i++] = '-';
    if (data->Comment[0] != ' ')
      {
      temp[i++] = ' ';
      }

    /* extract the brief comment, if present */
    ccp = data->Comment;
    while (i < briefmax && *ccp != '\0')
      {
      /* a blank line ends the brief comment */
      if (ccp[0] == '\n' && ccp[1] == '\n')
        {
        break;
        }
      /* fuzzy: capital letter or a new command on next line ends brief */
      if (ccp[0] == '\n' && ccp[1] == ' ' &&
          ((ccp[2] >= 'A' && ccp[2] <= 'Z') ||
           ccp[2] == '@' || ccp[2] == '\\'))
        {
        break;
        }
      temp[i] = *ccp;
      /* a sentence-ending period ends the brief comment */
      if (ccp[0] == '.' && (ccp[1] == ' ' || ccp[1] == '\n'))
        {
        i++;
        ccp++;
        while (*ccp == ' ')
          {
          ccp++;
          }
        break;
        }
      ccp++;
      i++;
      }
    /* skip all blank lines */
    while (*ccp == '\n')
      {
      ccp++;
      }
    if (*ccp == '\0')
      {
      ccp = NULL;
      }

    temp[i] = '\0';
    fprintf(fp,
            "    \"%s\\n\",\n",
            vtkWrapText_QuoteString(
              vtkWrapText_FormatComment(temp, 70), 500));
    }
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
  else
    {
    fprintf(fp,
            "    \"%s - no description provided.\\n\\n\",\n",
            vtkWrapText_QuoteString(data->Name, 500));
    }

  /* only consider superclasses that are wrapped */
  supername = vtkWrapPython_GetSuperClass(data, hinfo);
  if (supername)
    {
    vtkWrapPython_PyTemplateName(supername, pythonname);
    fprintf(fp,
            "    \"Superclass: %s\\n\\n\",\n",
            vtkWrapText_QuoteString(pythonname, 500));
    }

274
275
276
277
  if (data == file_info->MainClass &&
      (file_info->Description ||
       file_info->Caveats ||
       file_info->SeeAlso))
278
    {
279
280
281
282
283
    n = 100;
    if (file_info->Description)
      {
      n += strlen(file_info->Description);
      }
284

285
286
287
288
    if (file_info->Caveats)
      {
      n += strlen(file_info->Caveats);
      }
289

290
291
292
293
    if (file_info->SeeAlso)
      {
      n += strlen(file_info->SeeAlso);
      }
294

295
296
297
    comment = (char *)malloc(n);
    cp = comment;
    *cp = '\0';
298

299
300
301
302
303
304
    if (file_info->Description)
      {
      strcpy(cp, file_info->Description);
      cp += strlen(cp);
      *cp++ = '\n'; *cp++ = '\n'; *cp = '\0';
      }
305

306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
    if (file_info->Caveats)
      {
      sprintf(cp, ".SECTION Caveats\n\n");
      cp += strlen(cp);
      strcpy(cp, file_info->Caveats);
      cp += strlen(cp);
      *cp++ = '\n'; *cp++ = '\n'; *cp = '\0';
      }

    if (file_info->SeeAlso)
      {
      sprintf(cp, ".SECTION See Also\n\n");
      cp += strlen(cp);
      strcpy(cp, file_info->SeeAlso);
      cp += strlen(cp);
      *cp = '\0';
      }
323

324
325
326
327
    ccp = vtkWrapText_FormatComment(comment, 70);
    free(comment);
    }
  else if (ccp)
328
    {
329
    ccp = vtkWrapText_FormatComment(ccp, 70);
330
331
    }

332
  if (ccp)
333
    {
334
335
    n = (strlen(ccp) + 400-1)/400;
    for (i = 0; i < n; i++)
336
      {
337
338
339
340
341
342
343
344
345
346
347
348
349
350
      strncpy(temp, &ccp[400*i], 400);
      temp[400] = '\0';
      if (i < n-1)
        {
        fprintf(fp,
                "    \"%s\",\n",
                vtkWrapText_QuoteString(temp, 500));
        }
      else
        { /* just for the last time */
        fprintf(fp,
                "    \"%s\\n\",\n",
                vtkWrapText_QuoteString(temp, 500));
        }
351
352
353
354
      }
    }

  /* for special objects, add constructor signatures to the doc */
355
  if (!is_vtkobject && !data->Template && !data->IsAbstract)
356
357
358
    {
    for (j = 0; j < data->NumberOfFunctions; j++)
      {
359
      if (vtkWrapPython_MethodCheck(data, data->Functions[j], hinfo) &&
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
          vtkWrap_IsConstructor(data, data->Functions[j]))
        {
        fprintf(fp,"    \"%s\\n\",\n",
                vtkWrapText_FormatSignature(
                  data->Functions[j]->Signature, 70, 2000));
        }
      }
    }
}

/* -------------------------------------------------------------------- */
/* Declare the exports and imports for a VTK/Python class */
static void vtkWrapPython_ExportVTKClass(
  FILE *fp, ClassInfo *data, HierarchyInfo *hinfo)
{
  char classname[1024];
  const char *supername;

  /* mangle the classname if necessary */
379
  vtkWrapText_PythonName(data->Name, classname);
380
381
382

  /* for vtkObjectBase objects: export New method for use by subclasses */
  fprintf(fp,
383
          "extern \"C\" { %s PyObject *Py%s_ClassNew(); }\n"
384
          "\n",
385
          "VTK_ABI_EXPORT", classname);
386
387
388
389
390

  /* declare the New methods for all the superclasses */
  supername = vtkWrapPython_GetSuperClass(data, hinfo);
  if (supername)
    {
391
    vtkWrapText_PythonName(supername, classname);
392
    fprintf(fp,
393
      "#ifndef DECLARED_Py%s_ClassNew\n"
394
      "extern \"C\" { PyObject *Py%s_ClassNew(); }\n"
395
      "#define DECLARED_Py%s_ClassNew\n"
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
      "#endif\n",
      classname, classname, classname);
    }
}

/* -------------------------------------------------------------------- */
/* generate the New method for a vtkObjectBase object */
static void vtkWrapPython_GenerateObjectNew(
  FILE *fp, const char *classname, ClassInfo *data,
  HierarchyInfo *hinfo, int class_has_new)
{
  char superclassname[1024];
  const char *name;
  int has_constants = 0;
  int i;

  if (class_has_new)
    {
    fprintf(fp,
            "static vtkObjectBase *Py%s_StaticNew()\n"
            "{\n"
            "  return %s::New();\n"
            "}\n"
            "\n",
            classname, data->Name);
    }

  fprintf(fp,
424
          "PyObject *Py%s_ClassNew()\n"
425
          "{\n"
426
          "  PyVTKClass_Add(\n"
427
428
          "    &Py%s_Type, Py%s_Methods,\n",
          classname, classname, classname);
429

430
  if (strcmp(data->Name, classname) == 0)
431
432
    {
    fprintf(fp,
433
            "    \"%s\",\n"
434
435
            "    Py%s_Doc(),",
            classname, classname);
436
437
438
    }
  else
    {
439
    /* use of typeid() matches vtkTypeTemplate */
440
    fprintf(fp,
441
            "    typeid(%s).name(),\n"
442
            "    Py%s_Doc(),",
443
            data->Name, classname);
444
445
    }

446
  if (class_has_new)
447
448
    {
    fprintf(fp,
449
450
            " &Py%s_StaticNew);\n\n",
            classname);
451
452
453
454
    }
  else
    {
    fprintf(fp,
455
            " NULL);\n\n");
456
457
    }

458
459
460
461
  fprintf(fp,
          "  PyTypeObject *pytype = &Py%s_Type;\n\n",
          classname);

462
  /* if type is already ready, then return */
463
  fprintf(fp,
464
          "  if ((pytype->tp_flags & Py_TPFLAGS_READY) != 0)\n"
465
          "    {\n"
466
467
          "    return (PyObject *)pytype;\n"
          "    }\n\n");
468

469
470
471
472
473
474
  /* add any flags specific to this type */
  fprintf(fp,
          "#if !defined(VTK_PY3K) && PY_VERSION_HEX >= 0x02060000\n"
          "  pytype->tp_flags |= Py_TPFLAGS_HAVE_NEWBUFFER;\n"
          "#endif\n\n");

475
  /* find the first superclass that is a VTK class, create it first */
476
477
478
  name = vtkWrapPython_GetSuperClass(data, hinfo);
  if (name)
    {
479
    vtkWrapText_PythonName(name, superclassname);
480
    fprintf(fp,
481
            "  pytype->tp_base = (PyTypeObject *)Py%s_ClassNew();\n\n",
482
483
484
            superclassname);
    }

485
  /* check if any constants need to be added to the class dict */
486
487
488
489
490
491
492
493
494
495
496
497
  for (i = 0; i < data->NumberOfConstants; i++)
    {
    if (data->Constants[i]->Access == VTK_ACCESS_PUBLIC)
      {
      has_constants = 1;
      break;
      }
    }

  if (has_constants)
    {
    fprintf(fp,
498
            "  PyObject *d = pytype->tp_dict;\n"
499
            "  PyObject *o;\n"
500
            "\n");
501

502
    /* add any enum types defined in the class to its dict */
503
    vtkWrapPython_AddPublicEnumTypes(fp, "  ", "d", "o", data);
504

505
    /* add any constants defined in the class to its dict */
506
507
508
509
    vtkWrapPython_AddPublicConstants(fp, "  ", "d", "o", data);
    }

  fprintf(fp,
510
511
512
          "  PyType_Ready(pytype);\n"
          "  return (PyObject *)pytype;\n"
          "}\n\n");
513
514
515
516
}

/* -------------------------------------------------------------------- */
/* write out the type object */
517
518
void vtkWrapPython_GenerateObjectType(
  FILE *fp, const char *module, const char *classname)
519
520
{
  /* Generate the TypeObject */
521
  fprintf(fp,
522
    "static PyTypeObject Py%s_Type = {\n"
523
    "  PyVarObject_HEAD_INIT(&PyType_Type, 0)\n"
524
    "  \"%sPython.%s\", // tp_name\n"
525
526
527
528
529
530
531
532
    "  sizeof(PyVTKObject), // tp_basicsize\n"
    "  0, // tp_itemsize\n"
    "  PyVTKObject_Delete, // tp_dealloc\n"
    "  0, // tp_print\n"
    "  0, // tp_getattr\n"
    "  0, // tp_setattr\n"
    "  0, // tp_compare\n"
    "  PyVTKObject_Repr, // tp_repr\n",
533
    classname, module, classname);
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552

  fprintf(fp,
    "  0, // tp_as_number\n"
    "  0, // tp_as_sequence\n"
    "  0, // tp_as_mapping\n"
    "  0, // tp_hash\n"
    "  0, // tp_call\n"
    "  PyVTKObject_String, // tp_str\n");

  fprintf(fp,
    "  PyObject_GenericGetAttr, // tp_getattro\n"
    "  PyObject_GenericSetAttr, // tp_setattro\n"
    "  &PyVTKObject_AsBuffer, // tp_as_buffer\n"
    "  Py_TPFLAGS_DEFAULT|Py_TPFLAGS_HAVE_GC|Py_TPFLAGS_BASETYPE,"
      " // tp_flags\n"
    "  0, // tp_doc\n"
    "  PyVTKObject_Traverse, // tp_traverse\n"
    "  0, // tp_clear\n"
    "  0, // tp_richcompare\n"
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
    "  offsetof(PyVTKObject, vtk_weakreflist), // tp_weaklistoffset\n");
  if (strcmp(classname, "vtkCollection") == 0)
    {
    fprintf(fp,
      "  PyvtkCollection_iter, // tp_iter\n"
      "  0, // tp_iternext\n");
    }
  else
    {
    if(strcmp(classname, "vtkCollectionIterator") == 0)
      {
      fprintf(fp,
        "  PyvtkCollectionIterator_iter, // tp_iter\n"
        "  PyvtkCollectionIterator_next, // tp_iternext\n");
      }
    else
      {
      fprintf(fp,
        "  0, // tp_iter\n"
        "  0, // tp_iternext\n");
      }
    }
  fprintf(fp,
576
577
    "  0, // tp_methods\n"
    "  0, // tp_members\n"
578
579
    "  PyVTKObject_GetSet, // tp_getset\n"
    "  0, // tp_base\n"
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
    "  0, // tp_dict\n"
    "  0, // tp_descr_get\n"
    "  0, // tp_descr_set\n"
    "  offsetof(PyVTKObject, vtk_dict), // tp_dictoffset\n"
    "  0, // tp_init\n"
    "  0, // tp_alloc\n"
    "  PyVTKObject_New, // tp_new\n"
    "  PyObject_GC_Del, // tp_free\n"
    "  0, // tp_is_gc\n");

  /* fields set by python itself */
  fprintf(fp,
    "  0, // tp_bases\n"
    "  0, // tp_mro\n"
    "  0, // tp_cache\n"
    "  0, // tp_subclasses\n"
    "  0, // tp_weaklist\n");

  /* internal struct members */
  fprintf(fp,
600
    "  VTK_WRAP_PYTHON_SUPPRESS_UNINITIALIZED\n"
601
602
    "};\n"
    "\n");
603
604
605
606
607
}

/* -------------------------------------------------------------------- */
/* Wrap one class */
int vtkWrapPython_WrapOneClass(
608
609
610
  FILE *fp, const char *module, const char *classname,
  ClassInfo *data, FileInfo *finfo, HierarchyInfo *hinfo,
  int is_vtkobject)
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
{
  int class_has_new = 0;
  int i;

  /* recursive handling of templated classes */
  if (data->Template)
    {
    return vtkWrapPython_WrapTemplatedClass(fp, data, finfo, hinfo);
    }

  /* verify wrappability */
  if (!is_vtkobject && !vtkWrapPython_IsSpecialTypeWrappable(data))
    {
    return 0;
    }

  /* declare items to be exported or imported */
  if (is_vtkobject)
    {
    vtkWrapPython_ExportVTKClass(fp, data, hinfo);
    }

  /* prototype for the docstring function */
  fprintf(fp,
          "\n"
          "static const char **Py%s_Doc();\n"
          "\n",
          classname);

  /* check for New() function */
  for (i = 0; i < data->NumberOfFunctions; i++)
    {
    if (data->Functions[i]->Name &&
        data->Functions[i]->Access == VTK_ACCESS_PUBLIC &&
        strcmp("New",data->Functions[i]->Name) == 0 &&
        data->Functions[i]->NumberOfParameters == 0)
      {
      class_has_new = 1;
      }
    }

652
653
654
655
656
657
  /* create any enum types defined in the class */
  for (i = 0; i < data->NumberOfEnums; i++)
    {
    if (data->Enums[i]->Access == VTK_ACCESS_PUBLIC)
      {
      vtkWrapPython_GenerateEnumType(
658
        fp, module, classname, data->Enums[i]);
659
660
661
      }
    }

662
  /* now output all the methods are wrappable */
663
664
  vtkWrapPython_GenerateMethods(
    fp, classname, data, finfo, hinfo, is_vtkobject, 0);
665
666
667
668

  /* output the class initilization function for VTK objects */
  if (is_vtkobject)
    {
669
    vtkWrapPython_GenerateObjectType(
670
      fp, module, classname);
671
672
673
674
675
    vtkWrapPython_GenerateObjectNew(
      fp, classname, data, hinfo, class_has_new);
    }

  /* output the class initilization function for special objects */
676
  else
677
678
    {
    vtkWrapPython_GenerateSpecialType(
679
      fp, module, classname, data, finfo, hinfo);
680
681
682
    }

  /* the docstring for the class, as a static var ending in "Doc" */
683
684
685
686
687
  fprintf(fp,
          "const char **Py%s_Doc()\n"
          "{\n"
          "  static const char *docstring[] = {\n",
          classname);
688

689
  vtkWrapPython_ClassDoc(fp, finfo, data, hinfo, is_vtkobject);
690

691
692
693
694
695
696
697
  fprintf(fp,
          "    NULL\n"
          "  };\n"
          "\n"
          "  return docstring;\n"
          "}\n"
          "\n");
698
699
700

  return 1;
}