/alps/pcitool

To get this branch, use:
bzr branch http://suren.me/webbzr/alps/pcitool

« back to all changes in this revision

Viewing changes to views/script.c

  • Committer: Vasilii Chernov
  • Date: 2016-02-05 11:33:48 UTC
  • mto: This revision was merged to the branch mainline in revision 353.
  • Revision ID: vchernov@inr.ru-20160205113348-l4gmpvm9df3n22sx
Add support for python script properties. Correct pcilib python wrapping. Update examples. Update cmakelists for work in shadow build mode.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#define _PCILIB_VIEW_TRANSFORM_C
 
2
 
 
3
#include <stdio.h>
 
4
#include <stdlib.h>
 
5
 
 
6
#include "pci.h"
 
7
#include "error.h"
 
8
 
 
9
#include "model.h"
 
10
#include "script.h"
 
11
 
 
12
static int pcilib_script_view_read(pcilib_t *ctx, pcilib_view_context_t *view_ctx, pcilib_register_value_t regval, pcilib_value_t *val) {
 
13
        
 
14
        int err;
 
15
 
 
16
    const pcilib_model_description_t *model_info = pcilib_get_model_description(ctx);
 
17
    pcilib_script_view_description_t *v = (pcilib_script_view_description_t*)(model_info->views[view_ctx->view]);
 
18
        
 
19
        //Initialize python script, if it has not initialized already.
 
20
        if(!v->py_script_module)
 
21
        {
 
22
                if(!v->script_name)
 
23
                {
 
24
                        pcilib_error("Invalid script name specified in XML property (NULL)");
 
25
                        return PCILIB_ERROR_INVALID_DATA;
 
26
                }
 
27
                
 
28
                //create path string to scripts
 
29
                char* model_dir = getenv("PCILIB_MODEL_DIR");
 
30
                char* model_path = malloc(strlen(model_dir) + strlen(ctx->model) + 2);
 
31
                if (!model_path) return PCILIB_ERROR_MEMORY;
 
32
                sprintf(model_path, "%s/%s", model_dir, ctx->model);
 
33
                
 
34
                //set model path to python
 
35
                PySys_SetPath(model_path);
 
36
                free(model_path);
 
37
                model_path = NULL;
 
38
                
 
39
                //create path string to pcipywrap library
 
40
                char* app_dir = getenv("APP_PATH");
 
41
                char* pcipywrap_path;
 
42
                if(app_dir)
 
43
                {
 
44
                        pcipywrap_path = malloc(strlen(app_dir) + strlen("/pcilib"));
 
45
                        if (!pcipywrap_path) return PCILIB_ERROR_MEMORY;
 
46
                        sprintf(pcipywrap_path, "%s/%s", "/pcilib", ctx->model);
 
47
                }
 
48
                else
 
49
                {
 
50
                        pcipywrap_path = malloc(strlen("./pcilib"));
 
51
                        if (!pcipywrap_path) return PCILIB_ERROR_MEMORY;
 
52
                        sprintf(pcipywrap_path, "%s", "./pcilib");
 
53
        
 
54
                }
 
55
                
 
56
                //set pcipywrap library path to python
 
57
                PyObject* path = PySys_GetObject("path");
 
58
                if(PyList_Append(path, PyString_FromString(pcipywrap_path)) == -1)
 
59
                {
 
60
                        pcilib_error("Cant set pcipywrap library path to python.");
 
61
                        return PCILIB_ERROR_FAILED;
 
62
                }
 
63
                free(pcipywrap_path);
 
64
                pcipywrap_path = NULL;
 
65
                
 
66
                
 
67
                //extract module name from script name
 
68
                char* py_module_name = strtok(v->script_name, ".");
 
69
                
 
70
                if(!py_module_name)
 
71
                {
 
72
                        pcilib_error("Invalid script name specified in XML property (%s)."
 
73
                                                 " Seems like name doesnt contains extension", v->script_name);
 
74
                        return PCILIB_ERROR_INVALID_DATA;
 
75
                }
 
76
                
 
77
                //import python script
 
78
                v->py_script_module = PyImport_ImportModule(py_module_name);
 
79
                
 
80
                if(!v->py_script_module)
 
81
                {
 
82
                        printf("Error in import python module: ");
 
83
                        PyErr_Print();
 
84
                        return PCILIB_ERROR_INVALID_DATA;
 
85
                }
 
86
        }
 
87
        
 
88
        //Initializing pcipywrap module if script use it
 
89
        PyObject* dict = PyModule_GetDict(v->py_script_module);
 
90
        if(PyDict_Contains(dict, PyString_FromString("pcipywrap")))
 
91
        {
 
92
                PyObject* pcipywrap_module = PyDict_GetItemString(dict, "pcipywrap");
 
93
                if(!pcipywrap_module)
 
94
                {
 
95
                        pcilib_error("Cant extract pcipywrap module from script dictionary");
 
96
                        return PCILIB_ERROR_FAILED;
 
97
                }
 
98
       
 
99
       //setting pcilib_t instance                 
 
100
           PyObject_CallMethodObjArgs(pcipywrap_module,
 
101
                               PyUnicode_FromString("__setPcilib"),
 
102
                               PyByteArray_FromStringAndSize((const char*)&ctx, sizeof(pcilib_t*)),
 
103
                               NULL);
 
104
        }
 
105
        
 
106
        
 
107
        PyObject *ret = PyObject_CallMethod(v->py_script_module, "read_from_register", "()");
 
108
        if (!ret) 
 
109
        {
 
110
           printf("Python script error: ");
 
111
           PyErr_Print();
 
112
           return PCILIB_ERROR_FAILED;
 
113
        }
 
114
        
 
115
        if(PyInt_Check(ret))
 
116
        {
 
117
                err = pcilib_set_value_from_int(ctx, val, PyInt_AsLong(ret));
 
118
        }
 
119
        else
 
120
                if(PyFloat_Check(ret))
 
121
                        err = pcilib_set_value_from_float(ctx, val, PyFloat_AsDouble(ret));
 
122
                else
 
123
                        if(PyString_Check(ret))
 
124
                                err = pcilib_set_value_from_static_string(ctx, val, PyString_AsString(ret));
 
125
                                else
 
126
                                {
 
127
                                        pcilib_error("Invalid return type in read_from_register() method. Return type should be int, float or string.");
 
128
                                        return PCILIB_ERROR_NOTSUPPORTED;
 
129
                                }
 
130
        if(err)
 
131
        {
 
132
                pcilib_error("Failed to convert python script return value to internal type: %i", err);
 
133
                return err;
 
134
        }
 
135
    return 0;
 
136
}
 
137
 
 
138
 
 
139
void pcilib_script_view_free_description (pcilib_t *ctx, pcilib_view_description_t *view)
 
140
{
 
141
        pcilib_script_view_description_t *v = (pcilib_script_view_description_t*)(view);
 
142
        
 
143
        if(v->script_name)
 
144
        {
 
145
                free(v->script_name);
 
146
                v->script_name = NULL;
 
147
        }
 
148
        
 
149
        if(v->py_script_module)
 
150
        {
 
151
                PyObject_Free(v->py_script_module);
 
152
                v->py_script_module = NULL;
 
153
        }
 
154
}
 
155
 
 
156
const pcilib_view_api_description_t pcilib_script_view_api =
 
157
  { PCILIB_VERSION, sizeof(pcilib_script_view_description_t), NULL, NULL, pcilib_script_view_free_description, pcilib_script_view_read, NULL};