source: devel/C++/pbs.i @ 319

Last change on this file since 319 was 309, checked in by bas, 10 years ago

Checkin the pbs swig development files

File size: 13.0 KB
Line 
1// PBS python interface
2// Author: Bas van der Vlies <bas.vandervlies@surfsara.nl>
3// Date  : 04 Sep 2001
4// Vers. : 1.0
5// Desc. : This is a simple python wrapper for PBS.
6//
7// SVN info
8//  $Id: pbs.i 522 2010-03-26 08:45:32Z bas $
9//
10%module pbs
11%include "typemaps.i"
12
13// %include pointer.i
14// %include PBS.i
15
16// header declarations
17//
18%{
19
20#include "pbs_ifl.h"
21#include "pbs_error.h"
22#include "rm.h"
23#include "log.h"
24
25#define SARA_DEBUG 0
26
27//extern int pbs_errno;
28
29%}
30
31
32#if defined(SWIGPYTHON)
33
34// *****************************************************************
35// Some IN typemaps from Python ---> C
36//
37/*
38 * Convert Python batch status list to a valid C-linked list
39*/
40%typemap(in) struct batch_status *BATCH_STATUS {
41  PyObject              *py_obj;
42  struct batch_status   *ptr, *prev;
43  char                  s[255];
44  int                   i=0, size=0;
45
46  // printf("Python --> C\n");
47
48  if (SARA_DEBUG) printf("Converteren python -> c (struct batch_status *):\n");
49
50  size = Get_List_Size($input);
51  if (SARA_DEBUG) printf("\tSize of batch_status list: %d\n", size);
52 
53  if ( size == -1 ) {
54    PyErr_SetString(PyExc_TypeError, "not a list");
55    return NULL;
56  }
57  // printf("Size = %d\n", size);
58
59  if (SARA_DEBUG) printf("\t<Contents>\n");
60
61  $1 = prev = NULL;
62  for ( i=0; i < size; i++ ) {
63    py_obj = PyList_GetItem($input, i);
64    if (SWIG_ConvertPtr(py_obj, (void **) &ptr, SWIGTYPE_p_batch_status, 1)) {
65
66       sprintf(s,"list item %d has wrong type", i);
67       PyErr_SetString(PyExc_TypeError, s);
68       return NULL;
69
70       // This will skipp the wrong entry
71       // continue;
72    }
73
74    /*
75     * Make first entry head of C linked list
76    */
77    if ( i == 0) {
78      $1 = ptr;
79      ptr->next = prev;
80    }
81    else {
82      prev->next = ptr;
83      ptr->next = NULL;
84    }
85
86    if (SARA_DEBUG) printf("\t\t- %s\n", ptr->name);
87    prev = ptr;
88
89  } // end for
90
91  if (SARA_DEBUG) printf("\t</Contents>\n");
92} // end struct batch_status *IN typemap
93
94
95/*
96 * Convert Python attrl list to a valid C-linked list
97*/
98%typemap(in) struct attrl *ATTRL {
99  PyObject      *py_obj;
100  struct attrl  *ptr, *prev;
101  char          s[255];
102  int           i=0, size=0;
103
104  // printf("Python --> C\n");
105
106  if (SARA_DEBUG) printf("Converteren python -> c (struct attrl *):\n");
107
108  size = Get_List_Size($input);
109  if (SARA_DEBUG) printf("\tSize of attrl List: %d\n", size);
110 
111  if ( size == -1 ) {
112      PyErr_SetString(PyExc_TypeError, "not a list");
113      return NULL;
114  }
115
116  if (SARA_DEBUG) printf("\t<Contents>\n");
117
118  $1 = prev = NULL;
119  for ( i=0; i < size; i++ ) {
120    py_obj = PyList_GetItem($input, i);
121    if (SWIG_ConvertPtr(py_obj, (void **) &ptr, SWIGTYPE_p_attrl, 1)) {
122
123      sprintf(s,"list item %d has wrong type", i);
124      PyErr_SetString(PyExc_TypeError, s);
125      return NULL;
126
127      // This will skipp the wrong entry
128      // continue;
129    }
130
131    /*
132     * Make first entry head of C linked list
133    */
134    if ( i == 0) {
135      $1 = ptr;
136      ptr->next = prev;
137    }
138    else {
139      prev->next = ptr;
140      ptr->next = NULL;
141    }
142    if (SARA_DEBUG) printf("\t\t- %s\n", ptr->name);
143   
144    prev = ptr;
145
146  } // end for
147  if (SARA_DEBUG) printf("\t</Contents>\n");
148} // end struct attrl *IN typemap
149
150/*
151 * Convert Python attropl list to a valid C-linked list
152*/
153%typemap(in) struct attropl *ATTROPL {
154  PyObject          *py_obj;
155  struct attropl    *ptr, *prev;
156  char              s[255];
157  int               i=0, size=0;
158
159  // printf("Python --> C\n");
160
161  if (SARA_DEBUG) printf("Converteren python -> c (struct attropl *):\n");
162
163  size = Get_List_Size($input);
164
165  if (SARA_DEBUG) printf("\tSize attropl List: %d\n", size);
166
167  if ( size == -1 ) {
168    PyErr_SetString(PyExc_TypeError, "not a list");
169    return NULL;
170  }
171  //printf("Size = %d\n", size);
172
173  if (SARA_DEBUG) printf("\t<Contents>\n");
174
175  $1 = prev = NULL;
176  for ( i=0; i < size; i++ ) {
177    py_obj = PyList_GetItem($input, i);
178    if (SWIG_ConvertPtr(py_obj, (void **) &ptr, SWIGTYPE_p_attropl, 1)) {
179
180       sprintf(s,"list item %d has wrong type", i);
181       PyErr_SetString(PyExc_TypeError, s);
182       return NULL;
183
184       // This will skipp the wrong entry
185       // continue;
186    }
187
188    /*
189     * Make first entry head of C linked list
190    */
191    if ( i == 0) {
192      $1 = ptr;
193      ptr->next = prev;
194    }
195    else {
196      prev->next = ptr;
197      ptr->next = NULL;
198    }
199    prev = ptr;
200
201  } // end for
202  if (SARA_DEBUG) printf("\t</Contents>\n");
203} // end struct attrl *IN typemap
204
205%typemap(in) char **CHARCAST {
206  int       i=0, size=0;
207  PyObject  *py_obj;
208
209  if (SARA_DEBUG) printf("Convert python -> c (char **):\n");
210
211  size = Get_List_Size($input);
212
213  if (SARA_DEBUG) printf("\tSize of List: %d\n", size);
214
215  if ( size == -1 ) {
216    PyErr_SetString(PyExc_TypeError, "not a list");
217    return NULL;
218  }
219  // printf("Size = %d\n", size);
220
221  if (SARA_DEBUG) printf("\t<Contents>\n");
222
223  $1 = (char **) malloc( (size+1) * sizeof(char *));
224  for (i=0; i < size; i++) {
225    py_obj = PyList_GetItem($input, i);
226    if (PyString_Check(py_obj)) {
227      $1[i] = PyString_AsString(py_obj);
228      if (SARA_DEBUG) printf("%s", $1[i]);
229    }
230    else {
231      PyErr_SetString(PyExc_TypeError, "not a list of strings");
232      free($1);
233      return NULL;
234    }
235  } // end for
236  $1[i] = 0;
237  if (SARA_DEBUG) printf("\t</Contents>\n");
238} // end typemap char **IN
239
240// *****************************************************************
241// Some OUT typemaps from C ---> Python
242//
243%typemap(out) struct batch_status * {
244
245  PyObject              *obj_batch;
246  struct batch_status   *ptr;
247  int                   i=0, len=0;
248
249  // printf("Ja we are in bussniss\n");
250  if (SARA_DEBUG) printf("Converteren c (struct batch_status *) -> python:\n");
251 
252  // Deterime length of list
253  //
254  ptr = $1;
255  while (ptr != NULL) {
256    len++;
257    ptr = ptr->next;
258  }
259  $result = PyList_New(len);
260
261  if (SARA_DEBUG) printf("\tSize of List: %d\n", len);
262
263  // Make a list of batch_status pointers
264  //
265  if (SARA_DEBUG) printf("\t<Contents>\n");
266  ptr = $1;
267  for (i=0; i < len; i++) {
268    obj_batch = SWIG_NewPointerObj((void *)ptr, SWIGTYPE_p_batch_status,0);
269    PyList_SetItem($result, i, obj_batch);
270    if (SARA_DEBUG)  {
271        printf("\t\t- %s\n", ptr->name);
272    }
273    ptr = ptr->next;
274  }
275
276  if (SARA_DEBUG) printf("\t</Contents>\n");
277} // end typemap struct batch_status *
278
279%typemap(out) struct attrl * {
280  PyObject      *obj_batch;
281  struct attrl  *ptr;
282  int           i=0, len=0;
283
284  if (SARA_DEBUG) printf("Converteren c (struct attrl *) -> python:\n");
285
286  ptr = $1;
287  while (ptr != NULL) {
288    len++;
289    ptr = ptr->next;
290  }
291  $result = PyList_New(len);
292
293  if (SARA_DEBUG) printf("\tSize of List: %d\n", len);
294
295  if (SARA_DEBUG) printf("\t<Contents>\n");
296  ptr = $1;
297  for (i=0; i < len; i++) {
298    obj_batch = SWIG_NewPointerObj((void *)ptr, SWIGTYPE_p_attrl,1);
299    PyList_SetItem($result, i, obj_batch);
300    if (SARA_DEBUG) printf("\t\t- %s\n", ptr->name);
301    ptr = ptr->next;
302  }
303  if (SARA_DEBUG) printf("\t</Contents>\n");
304} // end typemap struct attrl *
305
306%typemap(out) struct attropl * {
307  PyObject          *obj_batch;
308  struct attropl    *ptr;
309  int               i=0, len=0;
310
311  if (SARA_DEBUG) printf("Converteren c (struct attropl *) -> python:\n");
312 
313  ptr = $1;
314  while (ptr != NULL) {
315    len++;
316    ptr = ptr->next;
317  }
318  $result = PyList_New(len);
319  if (SARA_DEBUG) printf("\tSize of List: %d\n", len);
320
321  if (SARA_DEBUG) printf("\t<Contents>\n");
322  ptr = $1;
323  for (i=0; i < len; i++) {
324    obj_batch = SWIG_NewPointerObj((void *)ptr, SWIGTYPE_p_attropl,1);
325    PyList_SetItem($result, i, obj_batch);
326    ptr = ptr->next;
327  }
328  if (SARA_DEBUG) printf("\t</Contents>\n");
329} // end typemap struct attropl *
330
331// Convert C (char **) to Python List
332//
333%typemap(out) char ** {
334   int len=0, i;
335
336   if (SARA_DEBUG)
337     printf("Converteren char ** -> python list\n");
338
339
340   if ($1 == NULL)
341     $result = PyList_New(0);
342   else {
343     while ($1[len])
344       len++;
345   }
346
347   if (SARA_DEBUG)
348     printf("\tSize of List: %d\n", len);
349
350   $result = PyList_New(len);
351   if (SARA_DEBUG)
352     printf("\t<Contents>\n");
353   for (i=0; i < len; i++ ) {
354      PyList_SetItem($result, i , PyString_FromString($1[i]));
355      if (SARA_DEBUG)
356        printf("\t\t- %s\n", $1[i]);
357   }
358   if (SARA_DEBUG)
359     printf("\t</Contents>\n");
360} // end typemap char **pbs_selectjob
361
362// *****************************************************************
363// Some freearg typemaps
364//
365%typemap(freearg) char ** {
366  free( (char *) $1);
367}
368
369#endif
370
371// *****************************************************************
372// Some Functions used by all C-structs typemaps
373//
374
375%{
376int Get_List_Size(PyObject *src)
377{
378  if (PyList_Check(src))
379    return(PyList_Size(src));
380
381  /* check if valid NULL pointer */
382  if ( PyString_Check(src) ) {
383    if ( ! strcmp(PyString_AsString(src), "NULL") )
384      return(0);
385  }
386  return(-1);
387
388} // end Get_List_Size()
389
390%}
391
392
393// *****************************************************************
394// These C-functions are the constructurs for the different C-structs
395//
396/*
397 * Make some default constructors for the various structs
398*/
399%inline %{
400
401// The default constructor for struct attrl
402//
403struct attrl *new_attrl(int number)
404{
405  struct attrl *ptr;
406  struct attrl *prev, *current;
407  int i;
408
409  /*
410    allocate memory as a one block is handy for Python scripts
411    and fill in the next fields so it also works for the C-library
412  */
413  printf("basje \n");
414  ptr = (struct attrl *) malloc(number * sizeof(struct attrl));
415
416  prev = NULL;
417  current = ptr + (number - 1);
418  for (i=0; i < number; i++)
419  {
420    printf("constructor called\n");
421    current->name     = (char *) malloc(MAXNAMLEN * sizeof(char));
422    current->resource = (char *) malloc(MAXNAMLEN * sizeof(char));
423    current->value    = (char *) malloc(MAXNAMLEN * sizeof(char));
424
425    bzero( (void*) current->name, sizeof(current->name));
426    bzero( (void*) current->resource, sizeof(current->resource));
427    bzero( (void*) current->value, sizeof(current->value));
428
429    current->next     = prev;
430    prev = current;
431    current--;
432  }
433  return (struct attrl *)ptr;
434
435} // end new_attrl()
436
437
438
439// The default constructor for struct attropl
440//
441struct attropl *new_attropl(int number)
442{
443  struct attropl *ptr;
444  struct attropl *prev, *current;
445  int i;
446
447  /*
448    allocate memory as a one block is handy for Python scripts
449    and fill in the next fields so it also works for the C-library
450  */
451  ptr = (struct attropl *) malloc(number * sizeof(struct attropl));
452
453  prev = NULL;
454  current = ptr + (number - 1);
455  for (i=0; i < number; i++)
456  {
457    printf("constructor called\n");
458    current->name     = (char *) malloc(MAXNAMLEN * sizeof(char));
459    current->resource = (char *) malloc(MAXNAMLEN * sizeof(char));
460    current->value    = (char *) malloc(MAXNAMLEN * sizeof(char));
461
462    bzero( (void*) current->name, sizeof(current->name));
463    bzero( (void*) current->resource, sizeof(current->resource));
464    bzero( (void*) current->value, sizeof(current->value));
465    // current->op = 0;
466
467    current->next     = prev;
468    prev = current;
469    current--;
470  }
471  return (struct attropl *)ptr;
472
473} // end new_attropl()
474
475/* Not used only returned */
476struct batch_status *new_batch_status()
477{
478   struct batch_status *ptr;
479
480   ptr = (struct batch_status *) malloc(sizeof(struct batch_status));
481   return (struct batch_status *)ptr;
482} // end new struct batch_status
483
484int get_error()
485{
486   char *errmsg;
487
488   errmsg = pbse_to_txt(pbs_errno);
489   if (SARA_DEBUG)
490   {
491      printf("Bas = %d\n", pbs_errno);
492      printf("Bas = %d, text = %s\n", pbs_errno, errmsg);
493   }
494   return (pbs_errno);
495}
496
497
498%}  // end %inline functions
499
500%include <carrays.i>
501%array_class(struct attrl, attrlArray);
502
503
504// *****************************************************************
505// Here the Python shadow class is generated. We have added some
506// stuff here to extend the python classes.
507//
508%nodefault;
509// %include "pbs_python.h"
510%include "pbs_ifl.h"
511%include "rm.h"
512%include "log.h"
513
514
515// Iets van uhhh..... obsolete
516// %ignore pbs_errno;
517//%include <pbs_error.h>
518
519
520/*
521%feature("shadow") attrl::__str__ {
522  def __str__(self): print self.name + self.value;
523}
524%extend attrl {
525  void __str__();
526}
527*/
528
529/* not used
530%extend batch_status {
531  ~batch_status() {
532        if (SARA_DEBUG)
533      printf("Bas free batch_status\n");
534        free(self);
535  }
536}
537*/
538
539%extend attrl {
540  char *__str__() {
541    static char temp[4 * MAXNAMLEN] ;
542    snprintf(temp, sizeof(temp), "(%s,%s,%s)",
543      self->name, self->resource, self->value);
544   
545    return &temp[0];
546  }
547
548/*
549 * For swig 1.3.29-2.1 we must create a destructor, but
550 * do not free anything pbs_statfree must do it
551*/
552  ~attrl() {
553    if (SARA_DEBUG)
554       printf("Bas free attrl\n");
555
556    /*
557        free(self);
558    */
559  }
560}
561
562%extend attropl {
563  char *__str__() {
564    static char temp[4 * MAXNAMLEN] ;
565    snprintf(temp, sizeof(temp), "(%s,%s,%s)",
566      self->name, self->resource, self->value);
567   
568    return &temp[0];
569  }
570
571/*
572 * For swig 1.3.29-2.1 we must create a destructor, but
573 * do not free anything pbs_statfree must do it
574*/
575  ~attropl() {
576    if (SARA_DEBUG)
577       printf("Bas free attropl\n");
578
579    /*
580        free(self);
581    */
582  }
583}
584
585
586%shadow "resmom.py"
587%shadow "version.py"
588%shadow "errors.py"
Note: See TracBrowser for help on using the repository browser.