8
static PyObject *SpudError;
9
static PyObject *SpudTypeError;
10
static PyObject *SpudKeyError;
11
static PyObject *SpudFileError;
12
static PyObject *SpudNewKeyWarning;
13
static PyObject *SpudAttrSetFailedWarning;
14
static PyObject *SpudShapeError;
15
static PyObject *SpudRankError;
20
error_checking(int outcome, char *functionname)
22
char errormessage [MAXLENGTH];
24
if (outcome == SPUD_KEY_ERROR){
25
snprintf(errormessage, MAXLENGTH, "Error: The specified option is not present \
26
in the dictionary in %s", functionname);
27
PyErr_SetString(SpudKeyError, errormessage);
30
if (outcome == SPUD_TYPE_ERROR){
31
snprintf(errormessage, MAXLENGTH, "Error: The specified option has a different \
32
type from that of the option argument provided in %s", functionname);
33
PyErr_SetString(SpudTypeError, errormessage);
36
if (outcome == SPUD_NEW_KEY_WARNING){
37
snprintf(errormessage, MAXLENGTH, "Warning: The option being inserted is not ] \
38
already in the dictionary %s", functionname);
39
PyErr_SetString(SpudNewKeyWarning, errormessage);
42
if (outcome == SPUD_FILE_ERROR){
43
snprintf(errormessage, MAXLENGTH, "Error: The specified options file cannot be \
44
read or written to as the routine requires in %s", functionname);
45
PyErr_SetString(SpudFileError, errormessage);
48
if (outcome == SPUD_RANK_ERROR){
49
snprintf(errormessage, MAXLENGTH, "Error: The specified option has a different rank from \
50
that of the option argument provided %s", functionname);
51
PyErr_SetString(SpudRankError, errormessage);
54
if (outcome == SPUD_SHAPE_ERROR){
55
snprintf(errormessage, MAXLENGTH, "Error: The specified option has a different shape from \
56
that of the option argument provided in %s", functionname);
57
PyErr_SetString(SpudShapeError, errormessage);
60
if (outcome == SPUD_ATTR_SET_FAILED_WARNING){
61
snprintf(errormessage, MAXLENGTH, "Warning: The option being set as an attribute can not be \
62
set as an attribute in %s", functionname);
63
PyErr_SetString(SpudAttrSetFailedWarning, errormessage);
66
if (outcome == SPUD_NO_ERROR){
70
PyErr_SetString(SpudError,"Error: error checking failed.");
75
libspud_load_options(PyObject *self, PyObject *args)
79
int outcomeLoadOptions;
81
if (!PyArg_ParseTuple(args, "s", &key))
83
key_len = strlen(key);
84
outcomeLoadOptions = spud_load_options(key,key_len);
86
return error_checking(outcomeLoadOptions, "load options");
90
libspud_print_options(PyObject *self, PyObject *args)
98
libspud_clear_options(PyObject *self, PyObject *args)
100
spud_clear_options();
106
libspud_get_number_of_children(PyObject *self, PyObject *args)
111
int outcomeGetNumChildren;
113
if (!PyArg_ParseTuple(args, "s", &key))
115
key_len = strlen(key);
116
outcomeGetNumChildren = spud_get_number_of_children(key, key_len, &child_count);
117
if (error_checking(outcomeGetNumChildren, "get number of children") == NULL){
121
return Py_BuildValue("i", child_count);
125
libspud_get_child_name(PyObject *self, PyObject *args)
130
char child_name [MAXLENGTH];
132
int outcomeGetChildName;
134
for (i = 0; i < MAXLENGTH; i++){
135
child_name[i] = '\0';
137
if (!PyArg_ParseTuple(args, "si", &key, &index)){
140
key_len = strlen(key);
141
outcomeGetChildName = spud_get_child_name(key, key_len, index, child_name, MAXLENGTH);
142
if (error_checking(outcomeGetChildName, "get child name") == NULL){
146
return Py_BuildValue("s", child_name);
150
libspud_option_count(PyObject *self, PyObject *args)
156
if (!PyArg_ParseTuple(args, "s", &key)){
159
key_len = strlen(key);
160
numoptions = spud_option_count(key, key_len);
162
return Py_BuildValue("i", numoptions);
166
libspud_have_option(PyObject *self, PyObject *args)
172
if (!PyArg_ParseTuple(args, "s", &key)){
175
key_len = strlen(key);
176
haveoption = spud_have_option(key, key_len);
178
if (haveoption == 0){
187
libspud_add_option(PyObject *self, PyObject *args)
191
int outcomeAddOption;
193
if (!PyArg_ParseTuple(args, "s", &key)){
196
key_len = strlen(key);
197
outcomeAddOption = spud_add_option(key, key_len);
198
return error_checking(outcomeAddOption, "add option");
203
libspud_get_option_type(PyObject *self, PyObject *args)
208
int outcomeGetOptionType;
210
if (!PyArg_ParseTuple(args, "s", &key)){
213
key_len = strlen(key);
214
outcomeGetOptionType = spud_get_option_type(key, key_len, &type);
215
if (error_checking(outcomeGetOptionType, "get option type") == NULL){
218
if (type == SPUD_DOUBLE){
219
Py_INCREF(&PyFloat_Type);
220
return (PyObject*) &PyFloat_Type;
222
else if (type == SPUD_INT){
223
Py_INCREF(&PyInt_Type);
224
return (PyObject*) &PyInt_Type;
226
else if (type == SPUD_NONE){
229
else if (type == SPUD_STRING){
230
Py_INCREF(&PyString_Type);
231
return (PyObject*) &PyString_Type;
234
PyErr_SetString(SpudError,"Error: Get option type function failed");
239
libspud_get_option_rank(PyObject *self, PyObject *args)
244
int outcomeGetOptionRank;
246
if (!PyArg_ParseTuple(args, "s", &key)){
249
key_len = strlen(key);
250
outcomeGetOptionRank = spud_get_option_rank(key, key_len, &rank);
251
if (error_checking(outcomeGetOptionRank, "get option rank") == NULL){
255
return Py_BuildValue("i", rank);
259
libspud_get_option_shape(PyObject *self, PyObject *args)
264
int outcomeGetOptionShape;
266
if (!PyArg_ParseTuple(args, "s", &key)){
269
key_len = strlen(key);
270
outcomeGetOptionShape = spud_get_option_shape(key, key_len, shape);
271
if (error_checking(outcomeGetOptionShape, "get option shape") == NULL){
275
return Py_BuildValue("(i,i)", shape[0],shape[1]);
279
spud_get_option_aux_list_ints(const char *key, int key_len, int type, int rank, int *shape)
280
{ // this function is for getting option when the option is of type a list of ints
281
int outcomeGetOption;
286
outcomeGetOption = spud_get_option(key, key_len, val);
287
if (error_checking(outcomeGetOption, "get option aux list") == NULL){
290
PyObject* pylist = PyList_New(size);
292
printf("New list error.");
295
for (j = 0; j < size; j++){
296
PyObject* element = Py_BuildValue("i", val[j]);
297
PyList_SetItem(pylist, j, element);
304
spud_get_option_aux_list_doubles(const char *key, int key_len, int type, int rank, int *shape)
305
{ // this function is for getting option when the option is of type a list of doubles
306
int outcomeGetOption;
311
outcomeGetOption = spud_get_option(key, key_len, val);
312
if (error_checking(outcomeGetOption, "get option aux list") == NULL){
315
PyObject* pylist = PyList_New(size);
317
printf("New list error.");
320
for (j = 0; j < size; j++){
321
PyObject* element = Py_BuildValue("f", val[j]);
322
PyList_SetItem(pylist, j, element);
329
spud_get_option_aux_scalar_or_string(const char *key, int key_len, int type, int rank, int *shape)
330
{ // this function is for getting option when the option is of type a scalar or string
331
int outcomeGetOption;
332
if (type == SPUD_DOUBLE){
334
outcomeGetOption = spud_get_option(key, key_len, &val);
335
if (error_checking(outcomeGetOption, "get option aux scalar or string") == NULL){
338
return Py_BuildValue("d", val);
340
else if (type == SPUD_INT){
342
outcomeGetOption = spud_get_option(key, key_len, &val);
343
if (error_checking(outcomeGetOption, "get option aux scalar or string") == NULL){
346
return Py_BuildValue("i", val);
348
else if (type == SPUD_STRING) {
352
for (i = 0; i < size+1; i++)
355
outcomeGetOption = spud_get_option(key, key_len, val);
356
if (error_checking(outcomeGetOption, "get option aux scalar or string") == NULL){
359
return Py_BuildValue("s", val);
362
PyErr_SetString(SpudError,"Error: Get option aux scalar failed");
367
spud_get_option_aux_tensor_doubles(const char *key, int key_len, int type, int rank, int *shape)
368
{ // this function is for getting option when the option is of type a tensor o doubles
369
int outcomeGetOption;
370
int rowsize = shape[0];
371
int colsize = shape[1];
372
int size = rowsize*colsize;
378
outcomeGetOption = spud_get_option(key, key_len, val);
379
if (error_checking(outcomeGetOption, "get option aux tensor") == NULL){
382
PyObject* pylist = PyList_New(rowsize);
384
printf("New list error");
388
for (m = 0; m < rowsize; m++){
389
PyObject* pysublist = PyList_New(colsize);
390
if (pysublist == NULL){
391
printf("New sublist error");
394
for (n = 0; n < colsize; n++){
395
PyObject* element = Py_BuildValue("d", val[counter]);
396
PyList_SetItem(pysublist, n, element);
399
PyList_SetItem(pylist, m, pysublist);
406
spud_get_option_aux_tensor_ints(const char *key, int key_len, int type, int rank, int *shape)
407
{ // this function is for getting option when the option is of type a tensor of ints
408
int outcomeGetOption;
409
int rowsize = shape[0];
410
int colsize = shape[1];
411
int size = rowsize*colsize;
417
outcomeGetOption = spud_get_option(key, key_len, val);
418
if (error_checking(outcomeGetOption, "get option aux tensor") == NULL){
421
PyObject* pylist = PyList_New(rowsize);
423
printf("New list error");
427
for (m = 0; m < rowsize; m++){
428
PyObject* pysublist = PyList_New(colsize);
429
if (pysublist == NULL){
430
printf("New sublist error");
433
for (n = 0; n < colsize; n++){
434
PyObject* element = Py_BuildValue("i", val[counter]);
435
PyList_SetItem(pysublist, n, element);
438
PyList_SetItem(pylist, m, pysublist);
445
libspud_get_option(PyObject *self, PyObject *args)
452
int outcomeGetOptionType;
453
int outcomeGetOptionRank;
454
int outcomeGetOptionShape;
456
if(!PyArg_ParseTuple(args, "s", &key)){
459
key_len = strlen(key);
460
outcomeGetOptionRank = spud_get_option_rank(key, key_len, &rank);
461
if (error_checking(outcomeGetOptionRank, "get option") == NULL){
464
outcomeGetOptionType = spud_get_option_type(key, key_len, &type);
465
if (error_checking(outcomeGetOptionType, "get option") == NULL){
468
outcomeGetOptionShape = spud_get_option_shape(key, key_len, shape);
469
if (error_checking(outcomeGetOptionShape, "get option") == NULL){
473
if (rank == -1){ // type error
474
char errormessage [MAXLENGTH];
475
snprintf(errormessage, MAXLENGTH, "Error: The specified option has a different \
476
type from that of the option argument provided in %s", "get option");
477
PyErr_SetString(SpudTypeError, errormessage);
480
else if (rank == 0){ // scalar
481
return spud_get_option_aux_scalar_or_string(key, key_len, type, rank, shape);
483
else if (rank == 1){ // list or string
484
if (type == SPUD_INT){ //a list of ints
485
return spud_get_option_aux_list_ints(key, key_len, type, rank, shape);
487
else if (type == SPUD_DOUBLE){ //a list of doubles
488
return spud_get_option_aux_list_doubles(key, key_len, type, rank, shape);
490
else if (type == SPUD_STRING){ //string
491
return spud_get_option_aux_scalar_or_string(key, key_len, type, rank, shape);
494
else if (rank == 2){ // tensor
495
if (type == SPUD_DOUBLE){ //a tensor of doubles
496
return spud_get_option_aux_tensor_doubles(key, key_len, type, rank, shape);
498
else if (type == SPUD_INT){ //a tensor of ints
499
return spud_get_option_aux_tensor_ints(key, key_len, type, rank, shape);
503
PyErr_SetString(SpudError,"Error: Get option failed.");
507
set_option_aux_list_ints(PyObject *pylist, const char *key, int key_len, int type, int rank, int *shape)
508
{ // this function is for setting option when the second argument is of type a list of ints
510
int psize = PyList_Size(pylist);
513
int outcomeSetOption;
516
for (j = 0; j < psize; j++){
518
PyObject* pelement = PyList_GetItem(pylist, j);
519
PyArg_Parse(pelement, "i", &element);
522
outcomeSetOption = spud_set_option(key, key_len, val, type, rank, shape);
523
if (error_checking(outcomeSetOption, "set option aux list ints") == NULL){
530
set_option_aux_list_doubles(PyObject *pylist, const char *key, int key_len, int type, int rank, int *shape)
531
{ // this function is for setting option when the second argument is of type a list of doubles
533
int psize = PyList_Size(pylist);
536
int outcomeSetOption;
539
for (j = 0; j < psize; j++){
541
PyObject* pelement = PyList_GetItem(pylist, j);
542
element = PyFloat_AS_DOUBLE(pelement);
545
outcomeSetOption = spud_set_option(key, key_len, val, type, rank, shape);
546
if (error_checking(outcomeSetOption, "set option aux list ints") == NULL){
554
set_option_aux_string(PyObject *pystring, const char *key, int key_len, int type, int rank, int *shape)
555
{ // this function is for setting option when the second argument is of type string
556
char *val = PyString_AsString(pystring);
557
int outcomeSetOption = spud_set_option(key, key_len, val, type, rank, shape);
558
return error_checking(outcomeSetOption, "set option aux string");
562
libspud_set_option_attribute(PyObject *self, PyObject *args)
570
int outcomeSetOption;
572
firstArg = PyTuple_GetItem(args, 0);
573
secondArg = PyTuple_GetItem(args, 1);
574
PyArg_Parse(firstArg, "s", &key);
575
key_len = strlen(key);
576
PyArg_Parse(secondArg, "s", &val);
577
val_len = strlen(val);
578
outcomeSetOption = spud_set_option_attribute(key, key_len, val, val_len);
579
return error_checking(outcomeSetOption, "set option attribute");
583
libspud_delete_option(PyObject *self, PyObject *args)
588
int outcomeDeleteOption;
590
firstArg = PyTuple_GetItem(args, 0);
591
PyArg_Parse(firstArg, "s", &key);
592
key_len = strlen(key);
593
outcomeDeleteOption = spud_delete_option(key, key_len);
594
return error_checking(outcomeDeleteOption, "delete option");
598
set_option_aux_tensor_doubles(PyObject *pylist, const char *key, int key_len, int type, int rank, int *shape)
599
{ // this function is for setting option when the second argument is of type a tensor of doubles
604
int outcomeSetOption;
606
int size = shape[0]*shape[1];
611
for (i = 0; i < shape[0]; i++){
612
PyObject* pysublist = PyList_GetItem(pylist, i);
613
for (j = 0; j < shape[1]; j++){
614
PyObject* pysublistElement = PyList_GetItem(pysublist, j);
615
element = PyFloat_AS_DOUBLE(pysublistElement);
616
val[counter] = (double) element;
621
outcomeSetOption = spud_set_option(key, key_len, val, type, rank, shape);
622
return error_checking(outcomeSetOption, "set option aux tensor doubles");
626
set_option_aux_tensor_ints(PyObject *pylist, const char *key, int key_len, int type, int rank, int *shape)
627
{ // this function is for setting option when the second argument is of type a tensor of ints
631
int size = shape[0]*shape[1];
633
int outcomeSetOption;
637
for (i = 0; i < shape[0]; i++){
638
PyObject* pysublist = PyList_GetItem(pylist, i);
639
for (j = 0; j < shape[1]; j++){
641
PyObject* pysublistElement = PyList_GetItem(pysublist, j);
642
PyArg_Parse(pysublistElement, "i", &element);
643
val[counter] = element;
648
outcomeSetOption = spud_set_option(key, key_len, val, type, rank, shape);
649
return error_checking(outcomeSetOption, "set option aux tensor ints");
653
set_option_aux_scalar(PyObject *pyscalar, const char *key, int key_len, int type, int rank, int *shape)
654
{ // this function is for setting option when the second argument is of type scalar
655
int outcomeSetOption = SPUD_NO_ERROR;
657
if (type == SPUD_DOUBLE){ //scalar is double
659
PyArg_Parse(pyscalar, "f", &val);
660
outcomeSetOption = spud_set_option(key, key_len, &val, type, rank, shape);
662
else if (type == SPUD_INT){
664
PyArg_Parse(pyscalar, "i", &val);
665
outcomeSetOption = spud_set_option(key, key_len, &val, type, rank, shape);
668
return error_checking(outcomeSetOption, "set option aux scalar");
673
libspud_set_option(PyObject *self, PyObject *args)
683
if(PyTuple_GET_SIZE(args)!=2){
684
PyErr_SetString(SpudError,"Error: set_option takes exactly 2 arguments.");
688
firstArg = PyTuple_GetItem(args, 0);
689
secondArg = PyTuple_GetItem(args, 1);
690
PyArg_Parse(firstArg, "s", &key);
691
key_len = strlen(key);
693
if (!spud_have_option(key, key_len)){ //option does not exist yet
694
int outcomeAddOption = spud_add_option(key, key_len);
695
error_checking(outcomeAddOption, "set option");
698
if (PyInt_Check(secondArg)){ //just an int
705
else if (PyString_Check(secondArg)){// a string
708
shape[0] = PyString_GET_SIZE(secondArg);
711
else if (PyFloat_Check(secondArg)){// a double
717
else if (PyList_Check(secondArg)){
718
PyObject* listElement = PyList_GetItem(secondArg, 0);
719
if (PyInt_Check(listElement)){ //list of ints
725
else if (PyFloat_Check(listElement)){
726
type = SPUD_DOUBLE; //list of doubles
731
else if (PyList_Check(listElement)){ //list of lists
732
int pylistSize = PyList_GET_SIZE(secondArg);
733
int pysublistSize = PyList_GET_SIZE(listElement);
734
PyObject* sublistElement = PyList_GetItem(listElement, 0);
735
if (PyInt_Check(sublistElement)){ //list of lists of ints
738
else if (PyFloat_Check(sublistElement)){//list of lists of doubles
742
shape[0] = pylistSize;
743
shape[1] = pysublistSize;
747
if (rank == 0){ // scalar
748
set_option_aux_scalar(secondArg, key, key_len, type, rank, shape);
750
else if (rank == 1){ // list or string
751
if (PyString_Check(secondArg)){ // pystring
752
set_option_aux_string(secondArg, key, key_len, type, rank, shape);
754
else if (type == SPUD_INT) { // list of ints
755
set_option_aux_list_ints(secondArg, key, key_len, type, rank, shape);
757
else if (type == SPUD_DOUBLE){ // list of doubles
758
set_option_aux_list_doubles(secondArg, key, key_len, type, rank, shape);
761
else if (rank == 2){ // tensor
762
if (type == SPUD_DOUBLE) { // tensor of doubles
763
set_option_aux_tensor_doubles(secondArg, key, key_len, type, rank, shape);
765
else if (type == SPUD_INT) { // tensor of ints
766
set_option_aux_tensor_ints(secondArg, key, key_len, type, rank, shape);
774
libspud_write_options(PyObject *self, PyObject *args)
779
int outcomeWriteOptions;
781
firstArg = PyTuple_GetItem(args, 0);
782
PyArg_Parse(firstArg, "s", &filename);
783
filename_len = strlen(filename);
784
outcomeWriteOptions = spud_write_options (filename, filename_len);
785
return error_checking(outcomeWriteOptions, "write options");
788
static PyMethodDef libspudMethods[] = {
789
{"load_options", libspud_load_options, METH_VARARGS,
790
PyDoc_STR("Reads the xml file into the options tree.")},
791
{"print_options", libspud_print_options, METH_VARARGS,
792
PyDoc_STR("Print the entire options tree to standard output.")},
793
{"clear_options", libspud_clear_options, METH_VARARGS,
794
PyDoc_STR("Clears the entire options tree.")},
795
{"get_number_of_children", libspud_get_number_of_children, METH_VARARGS,
796
PyDoc_STR("get number of children under key.")},
797
{"get_child_name", libspud_get_child_name, METH_VARARGS,
798
PyDoc_STR("Get name of the indexth child of key.")},
799
{"option_count", libspud_option_count, METH_VARARGS,
800
PyDoc_STR("Return the number of options matching key.")},
801
{"have_option", libspud_have_option, METH_VARARGS,
802
PyDoc_STR("Checks whether key is present in options dictionary.")},
803
{"get_option_type", libspud_get_option_type, METH_VARARGS,
804
PyDoc_STR("Returns the type of option specified by key.")},
805
{"get_option_rank", libspud_get_option_rank, METH_VARARGS,
806
PyDoc_STR("Return the rank of option specified by key.")},
807
{"get_option_shape", libspud_get_option_shape, METH_VARARGS,
808
PyDoc_STR("Return the shape of option specified by key.")},
809
{"get_option", libspud_get_option, METH_VARARGS,
810
PyDoc_STR("Retrives option values from the options dictionary.")},
811
{"set_option", libspud_set_option, METH_VARARGS,
812
PyDoc_STR("Sets options in the options tree.")},
813
{"write_options", libspud_write_options, METH_VARARGS,
814
PyDoc_STR("Write options tree out to the xml file specified by name.")},
815
{"delete_option", libspud_delete_option, METH_VARARGS,
816
PyDoc_STR("Delete options at the specified key.")},
817
{"set_option_attribute", libspud_set_option_attribute, METH_VARARGS,
818
PyDoc_STR("As set_option, but additionally attempts to mark the option at the \
819
specified key as an attribute. Set_option_attribute accepts only string data for val.")},
820
{"add_option", libspud_add_option, METH_VARARGS,
821
PyDoc_STR("Creates a new option at the supplied key.")},
822
{NULL, NULL, 0, NULL},
831
m = Py_InitModule("libspud", libspudMethods);
835
SpudError = PyErr_NewException("Spud.error", NULL, NULL);
836
SpudNewKeyWarning = PyErr_NewException("SpudNewKey.warning", NULL, NULL);
837
SpudKeyError = PyErr_NewException("SpudKey.error", NULL, NULL);
838
SpudTypeError = PyErr_NewException("SpudType.error", NULL, NULL);
839
SpudFileError = PyErr_NewException("SpudFile.warning", NULL, NULL);
840
SpudAttrSetFailedWarning = PyErr_NewException("SpudAttrSetFailed.warning", NULL, NULL);
841
SpudShapeError = PyErr_NewException("SpudShape.error", NULL, NULL);
842
SpudRankError = PyErr_NewException("SpudRank.error", NULL, NULL);
844
Py_INCREF(SpudError);
845
Py_INCREF(SpudNewKeyWarning);
846
Py_INCREF(SpudKeyError);
847
Py_INCREF(SpudTypeError);
848
Py_INCREF(SpudFileError);
849
Py_INCREF(SpudRankError);
850
Py_INCREF(SpudShapeError);
851
Py_INCREF(SpudAttrSetFailedWarning);
853
PyModule_AddObject(m, "SpudError", SpudError);
854
PyModule_AddObject(m, "SpudNewKeyWarning", SpudNewKeyWarning);
855
PyModule_AddObject(m, "SpudKeyError", SpudKeyError);
856
PyModule_AddObject(m, "SpudTypeError", SpudTypeError);
857
PyModule_AddObject(m, "SpudFileError", SpudFileError);
858
PyModule_AddObject(m, "SpudAttrSetFailedWarning", SpudAttrSetFailedWarning);
859
PyModule_AddObject(m, "SpudShapeError", SpudShapeError);
860
PyModule_AddObject(m, "SpudRankError", SpudRankError);
863
#if PY_MINOR_VERSION > 6
864
manager = PyCapsule_Import("spud_manager._spud_manager", 0);
865
if (manager != NULL) spud_set_manager(manager);