/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 pcilib/py.c

  • Committer: Vasilii Chernov
  • Date: 2016-02-11 09:37:24 UTC
  • mto: This revision was merged to the branch mainline in revision 353.
  • Revision ID: vchernov@inr.ru-20160211093724-ipw54n5wxts1jt2i
Merge script and transform view. Add get register and properties info to python wrap.

Show diffs side-by-side

added added

removed removed

Lines of Context:
16
16
    PyObject *global_dict;
17
17
};
18
18
 
 
19
struct pcilib_script_s {
 
20
    PyObject *py_script_module; /**< PyModule object, contains script enviroment */
 
21
        PyObject *dict;
 
22
        char* script_name;
 
23
};
 
24
 
19
25
int pcilib_init_py(pcilib_t *ctx) {
20
26
    ctx->py = (pcilib_py_t*)malloc(sizeof(pcilib_py_t));
21
27
    if (!ctx->py) return PCILIB_ERROR_MEMORY;
22
28
 
23
 
    Py_Initialize();
 
29
    if(!Py_IsInitialized())
 
30
        Py_Initialize();
24
31
 
25
32
    ctx->py->main_module = PyImport_AddModule("__parser__");
26
33
    if (!ctx->py->main_module)
35
42
 
36
43
void pcilib_free_py(pcilib_t *ctx) {
37
44
    if (ctx->py) {
38
 
            // Dict and module references are borrowed
 
45
        // Dict and module references are borrowed
39
46
        free(ctx->py);
40
47
        ctx->py = NULL;
41
48
    }
42
 
 
43
 
    Py_Finalize();
 
49
    //Py_Finalize();
44
50
}
45
51
 
46
52
/*
189
195
    pcilib_debug(VIEWS, "Evaluating a Python string \'%s\' to %lf=\'%s\'", codestr, PyFloat_AsDouble(obj), code);
190
196
    return pcilib_set_value_from_float(ctx, value, PyFloat_AsDouble(obj));
191
197
}
 
198
 
 
199
 
 
200
int pcilib_init_py_script(pcilib_t *ctx, char* module_name, pcilib_script_t **module, pcilib_access_mode_t *mode)
 
201
{
 
202
        int err;
 
203
        
 
204
        //Initialize python script, if it has not initialized already.
 
205
        if(!module_name)
 
206
        {
 
207
                pcilib_error("Invalid script name specified in XML property (NULL)");
 
208
                return PCILIB_ERROR_INVALID_DATA;
 
209
        }
 
210
        
 
211
        //create path string to scripts
 
212
        char* model_dir = getenv("PCILIB_MODEL_DIR");
 
213
        char* model_path = malloc(strlen(model_dir) + strlen(ctx->model) + 2);
 
214
        if (!model_path) return PCILIB_ERROR_MEMORY;
 
215
        sprintf(model_path, "%s/%s", model_dir, ctx->model);
 
216
        
 
217
        //set model path to python
 
218
        PySys_SetPath(model_path);
 
219
        free(model_path);
 
220
        model_path = NULL;
 
221
        
 
222
        //create path string to pcipywrap library
 
223
        char* app_dir = getenv("APP_PATH");
 
224
        char* pcipywrap_path;
 
225
        if(app_dir)
 
226
        {
 
227
                pcipywrap_path = malloc(strlen(app_dir) + strlen("/pcilib"));
 
228
                if (!pcipywrap_path) return PCILIB_ERROR_MEMORY;
 
229
                sprintf(pcipywrap_path, "%s/%s", "/pcilib", ctx->model);
 
230
        }
 
231
        else
 
232
        {
 
233
                pcipywrap_path = malloc(strlen("./pcilib"));
 
234
                if (!pcipywrap_path) return PCILIB_ERROR_MEMORY;
 
235
                sprintf(pcipywrap_path, "%s", "./pcilib");
 
236
 
 
237
        }
 
238
        
 
239
        //set pcipywrap library path to python
 
240
        PyObject* path = PySys_GetObject("path");
 
241
        if(PyList_Append(path, PyString_FromString(pcipywrap_path)) == -1)
 
242
        {
 
243
                pcilib_error("Cant set pcipywrap library path to python.");
 
244
                return PCILIB_ERROR_FAILED;
 
245
        }
 
246
        free(pcipywrap_path);
 
247
        pcipywrap_path = NULL;
 
248
        
 
249
        
 
250
        //extract module name from script name
 
251
        char* py_module_name = strtok(module_name, ".");
 
252
        
 
253
        if(!py_module_name)
 
254
        {
 
255
                pcilib_error("Invalid script name specified in XML property (%s)."
 
256
                                         " Seems like name doesnt contains extension", module_name);
 
257
                return PCILIB_ERROR_INVALID_DATA;
 
258
        }
 
259
        
 
260
        //import python script
 
261
        PyObject* py_script_module = PyImport_ImportModule(py_module_name);
 
262
        
 
263
        if(!py_script_module)
 
264
        {
 
265
                printf("Error in import python module: ");
 
266
                PyErr_Print();
 
267
                return PCILIB_ERROR_INVALID_DATA;
 
268
        }
 
269
 
 
270
 
 
271
        //Initializing pcipywrap module if script use it
 
272
        PyObject* dict = PyModule_GetDict(py_script_module);
 
273
        if(PyDict_Contains(dict, PyString_FromString("pcipywrap")))
 
274
        {
 
275
                PyObject* pcipywrap_module = PyDict_GetItemString(dict, "pcipywrap");
 
276
                if(!pcipywrap_module)
 
277
                {
 
278
                        pcilib_error("Cant extract pcipywrap module from script dictionary");
 
279
                        return PCILIB_ERROR_FAILED;
 
280
                }
 
281
           
 
282
           //setting pcilib_t instance                 
 
283
           PyObject_CallMethodObjArgs(pcipywrap_module,
 
284
                                                           PyUnicode_FromString("setPcilib"),
 
285
                                                           PyByteArray_FromStringAndSize((const char*)&ctx, sizeof(pcilib_t*)),
 
286
                                                           NULL);
 
287
        }
 
288
        
 
289
        //Success. Create struct and initialize values
 
290
        module[0] = (pcilib_script_t*)malloc(sizeof(pcilib_script_t));
 
291
        module[0]->py_script_module = py_script_module;
 
292
        module[0]->script_name = module_name;
 
293
        module[0]->dict = dict;
 
294
        
 
295
        //Setting correct mode
 
296
        mode[0] = 0;
 
297
        if(PyDict_Contains(dict, PyString_FromString("read_from_register")))
 
298
                mode[0] |= PCILIB_ACCESS_R;     
 
299
        if(PyDict_Contains(dict, PyString_FromString("write_to_register")))
 
300
                mode[0] |= PCILIB_ACCESS_W;
 
301
        
 
302
        return 0;
 
303
}
 
304
 
 
305
int pcilib_free_py_script(pcilib_script_t *module)
 
306
{
 
307
        if(module)
 
308
        {
 
309
                if(module->script_name)
 
310
                {
 
311
                        free(module->script_name);
 
312
                        module->script_name = NULL;
 
313
                }
 
314
                
 
315
                if(module->py_script_module)
 
316
                {
 
317
                        //PyObject_Free(module->py_script_module);
 
318
                        module->py_script_module = NULL;
 
319
                }
 
320
        }
 
321
}
 
322
 
 
323
int pcilib_script_read(pcilib_t *ctx, pcilib_script_t *module, pcilib_value_t *val)
 
324
{
 
325
        
 
326
        int err;
 
327
        
 
328
        PyObject *ret = PyObject_CallMethod(module->py_script_module, "read_from_register", "()");
 
329
        if (!ret) 
 
330
        {
 
331
           printf("Python script error: ");
 
332
           PyErr_Print();
 
333
           return PCILIB_ERROR_FAILED;
 
334
        }
 
335
        
 
336
        err = pcilib_convert_pyobject_to_val(ctx, ret, val);
 
337
        
 
338
        if(err)
 
339
        {
 
340
                pcilib_error("Failed to convert python script return value to internal type: %i", err);
 
341
                return err;
 
342
        }
 
343
    return 0;
 
344
}
 
345
 
 
346
int pcilib_script_write(pcilib_t *ctx, pcilib_script_t *module, pcilib_value_t *val)
 
347
{
 
348
        int err;
 
349
                
 
350
        PyObject *input = pcilib_convert_val_to_pyobject(ctx, val, printf);
 
351
        if(!input)
 
352
        {
 
353
           printf("Failed to convert input value to Python object");
 
354
           PyErr_Print();
 
355
           return PCILIB_ERROR_FAILED;
 
356
        }
 
357
        
 
358
        PyObject *ret = PyObject_CallMethodObjArgs(module->py_script_module,
 
359
                                                                                           PyUnicode_FromString("write_to_register"),
 
360
                                                                                           input,
 
361
                                                                                           NULL);
 
362
        if (!ret) 
 
363
        {
 
364
           printf("Python script error: ");
 
365
           PyErr_Print();
 
366
           return PCILIB_ERROR_FAILED;
 
367
        }
 
368
 
 
369
    return 0;
 
370
}