/alps/pcitool

To get this branch, use:
bzr branch http://darksoft.org/webbzr/alps/pcitool

« back to all changes in this revision

Viewing changes to pcilib/py.c

  • Committer: Vasilii Chernov
  • Date: 2016-02-12 13:43:20 UTC
  • mto: This revision was merged to the branch mainline in revision 353.
  • Revision ID: vchernov@inr.ru-20160212134320-qj1oj1v4g6ixn9c8
1. Cmakelists - move copy xml folder command to root file
2. - Move set python paths code to python module init funtction
     - pci.c move python module init block code after checking model
       to get paths before it runs.
   - Fix set python path code to work with PYTHONPATH
   - Update pci run script to work with PYTHONPATH
   - Fix python finalize code
3. Change pcilib_script_s interacting method. Now it stores in hash.
4. Change names of some fucntions to more unified ones
5. Remove old unused function pcilib_xml_create_script_or_transform_view
6. cli - disable reading register after set if write_verification flag is off
7. Remove uninformative error messages fro Python wrap.
8. - Server.py - add read/write property/register command handling
   - Add help message
   - Correcting paths

Show diffs side-by-side

added added

removed removed

Lines of Context:
11
11
#include "py.h"
12
12
#include "error.h"
13
13
 
 
14
 
 
15
 
14
16
struct pcilib_py_s {
15
17
    PyObject *main_module;
16
18
    PyObject *global_dict;
 
19
    int py_initialized_inside; ///< Flag, shows that Py_Initialize has been called inside class
17
20
};
18
21
 
19
 
struct pcilib_script_s {
 
22
typedef struct pcilib_script_s {
 
23
        char* script_name;
20
24
    PyObject *py_script_module; /**< PyModule object, contains script enviroment */
21
25
        PyObject *dict;
22
 
        char* script_name;
23
 
};
 
26
        pcilib_access_mode_t mode;
 
27
        
 
28
        UT_hash_handle hh;
 
29
} pcilib_script_s;
 
30
 
 
31
struct pcilib_script_s *scripts = NULL;
24
32
 
25
33
int pcilib_init_py(pcilib_t *ctx) {
26
34
    ctx->py = (pcilib_py_t*)malloc(sizeof(pcilib_py_t));
27
35
    if (!ctx->py) return PCILIB_ERROR_MEMORY;
28
36
 
29
37
    if(!Py_IsInitialized())
 
38
    {
30
39
        Py_Initialize();
31
 
 
 
40
        ctx->py->py_initialized_inside = 1;
 
41
        }
 
42
        else
 
43
                ctx->py->py_initialized_inside = 0;
 
44
                
32
45
    ctx->py->main_module = PyImport_AddModule("__parser__");
33
46
    if (!ctx->py->main_module)
34
47
        return PCILIB_ERROR_FAILED;
36
49
    ctx->py->global_dict = PyModule_GetDict(ctx->py->main_module);
37
50
    if (!ctx->py->global_dict)
38
51
        return PCILIB_ERROR_FAILED;
39
 
 
 
52
        
 
53
        
 
54
        //create path string, where the model scripts should be
 
55
        static int model_dir_added = 0;
 
56
        if(!model_dir_added)
 
57
        {
 
58
                char* model_dir = getenv("PCILIB_MODEL_DIR");
 
59
                char* model_path = malloc(strlen(model_dir) + strlen(ctx->model) + 2);
 
60
                if (!model_path) return PCILIB_ERROR_MEMORY;
 
61
                sprintf(model_path, "%s/%s", model_dir, ctx->model);
 
62
                //add path to python
 
63
                PyObject* path = PySys_GetObject("path");
 
64
                if(PyList_Append(path, PyString_FromString(model_path)) == -1)
 
65
                {
 
66
                        pcilib_error("Cant set PCILIB_MODEL_DIR library path to python.");
 
67
                        free(model_path);
 
68
                        return PCILIB_ERROR_FAILED;
 
69
                }
 
70
                free(model_path);
 
71
                model_dir_added = 1;
 
72
        }
40
73
    return 0;
41
74
}
42
75
 
43
76
void pcilib_free_py(pcilib_t *ctx) {
44
 
    if (ctx->py) {
 
77
        
 
78
        int py_initialized_inside = 0;
 
79
        
 
80
    if (ctx->py) {              
 
81
                if(ctx->py->py_initialized_inside)
 
82
                        py_initialized_inside = 1;
 
83
                
45
84
        // Dict and module references are borrowed
46
85
        free(ctx->py);
47
86
        ctx->py = NULL;
48
87
    }
49
 
    //Py_Finalize();
 
88
    
 
89
    if(py_initialized_inside)
 
90
                Py_Finalize();
50
91
}
51
92
 
52
93
/*
196
237
    return pcilib_set_value_from_float(ctx, value, PyFloat_AsDouble(obj));
197
238
}
198
239
 
199
 
void* pcilib_convert_val_to_pyobject(pcilib_t* ctx, pcilib_value_t *val)
 
240
void* pcilib_get_value_as_pyobject(pcilib_t* ctx, pcilib_value_t *val)
200
241
{
201
242
        int err;
202
243
        
242
283
        }
243
284
}
244
285
 
245
 
int pcilib_convert_pyobject_to_val(pcilib_t* ctx, void* pyObjVal, pcilib_value_t *val)
 
286
int pcilib_set_value_from_pyobject(pcilib_t* ctx, void* pyObjVal, pcilib_value_t *val)
246
287
{
247
288
        PyObject* pyVal = pyObjVal;
248
289
        int err;
268
309
    return 0;
269
310
}
270
311
 
271
 
int pcilib_init_py_script(pcilib_t *ctx, char* module_name, pcilib_script_t **module, pcilib_access_mode_t *mode)
 
312
int pcilib_py_init_script(pcilib_t *ctx, char* module_name, pcilib_access_mode_t *mode)
272
313
{       
273
 
        //Initialize python script, if it has not initialized already.
274
 
        if(!module_name)
275
 
        {
276
 
                pcilib_error("Invalid script name specified in XML property (NULL)");
277
 
                return PCILIB_ERROR_INVALID_DATA;
278
 
        }
279
 
        
280
 
        //create path string to scripts
281
 
        char* model_dir = getenv("PCILIB_MODEL_DIR");
282
 
        char* model_path = malloc(strlen(model_dir) + strlen(ctx->model) + 2);
283
 
        if (!model_path) return PCILIB_ERROR_MEMORY;
284
 
        sprintf(model_path, "%s/%s", model_dir, ctx->model);
285
 
        
286
 
        //set model path to python
287
 
        PySys_SetPath(model_path);
288
 
        free(model_path);
289
 
        model_path = NULL;
290
 
        
291
 
        //create path string to pcipywrap library
292
 
        char* app_dir = getenv("APP_PATH");
293
 
        char* pcipywrap_path;
294
 
        if(app_dir)
295
 
        {
296
 
                pcipywrap_path = malloc(strlen(app_dir) + strlen("/pywrap"));
297
 
                if (!pcipywrap_path) return PCILIB_ERROR_MEMORY;
298
 
                sprintf(pcipywrap_path, "%s/%s", "/pywrap", ctx->model);
299
 
        }
300
 
        else
301
 
        {
302
 
                pcipywrap_path = malloc(strlen("./pywrap"));
303
 
                if (!pcipywrap_path) return PCILIB_ERROR_MEMORY;
304
 
                sprintf(pcipywrap_path, "%s", "./pywrap");
305
 
 
306
 
        }
307
 
        
308
 
        //set pcipywrap library path to python
309
 
        PyObject* path = PySys_GetObject("path");
310
 
        if(PyList_Append(path, PyString_FromString(pcipywrap_path)) == -1)
311
 
        {
312
 
                pcilib_error("Cant set pcipywrap library path to python.");
313
 
                return PCILIB_ERROR_FAILED;
314
 
        }
315
 
        free(pcipywrap_path);
316
 
        pcipywrap_path = NULL;
317
 
        
318
 
        
319
314
        //extract module name from script name
320
315
        char* py_module_name = strtok(module_name, ".");
321
 
        
322
316
        if(!py_module_name)
323
317
        {
324
318
                pcilib_error("Invalid script name specified in XML property (%s)."
326
320
                return PCILIB_ERROR_INVALID_DATA;
327
321
        }
328
322
        
 
323
        pcilib_script_s* module = NULL;
 
324
        HASH_FIND_STR( scripts, module_name, module);
 
325
        if(module)
 
326
        {
 
327
                pcilib_warning("Python module %s is already in hash. Skip init step", module_name);
 
328
                mode[0] = module->mode;
 
329
                return 0;
 
330
        }
 
331
        
 
332
        //Initialize python module
 
333
        if(!module_name)
 
334
        {
 
335
                pcilib_error("Invalid script name specified in XML property (NULL)");
 
336
                return PCILIB_ERROR_INVALID_DATA;
 
337
        }
 
338
        
329
339
        //import python script
330
340
        PyObject* py_script_module = PyImport_ImportModule(py_module_name);
331
341
        
336
346
                return PCILIB_ERROR_INVALID_DATA;
337
347
        }
338
348
 
339
 
 
340
349
        //Initializing pcipywrap module if script use it
341
350
        PyObject* dict = PyModule_GetDict(py_script_module);
342
351
        if(PyDict_Contains(dict, PyString_FromString("pcipywrap")))
350
359
           
351
360
           //setting pcilib_t instance                 
352
361
           PyObject_CallMethodObjArgs(pcipywrap_module,
353
 
                                                           PyUnicode_FromString("setPcilib"),
 
362
                               PyUnicode_FromString("set_pcilib"),
354
363
                                                           PyByteArray_FromStringAndSize((const char*)&ctx, sizeof(pcilib_t*)),
355
364
                                                           NULL);
356
365
        }
357
366
        
358
367
        //Success. Create struct and initialize values
359
 
        module[0] = (pcilib_script_t*)malloc(sizeof(pcilib_script_t));
360
 
        module[0]->py_script_module = py_script_module;
361
 
        module[0]->script_name = module_name;
362
 
        module[0]->dict = dict;
 
368
        module = malloc(sizeof(pcilib_script_s));
 
369
        module->py_script_module = py_script_module;
 
370
        module->script_name = malloc(strlen(module_name));
 
371
        sprintf(module->script_name, "%s", module_name);
 
372
        module->dict = dict;
 
373
 
363
374
        
364
375
        //Setting correct mode
365
376
        mode[0] = 0;
367
378
                mode[0] |= PCILIB_ACCESS_R;     
368
379
        if(PyDict_Contains(dict, PyString_FromString("write_to_register")))
369
380
                mode[0] |= PCILIB_ACCESS_W;
370
 
        
371
 
        return 0;
372
 
}
373
 
 
374
 
int pcilib_free_py_script(pcilib_script_t *module)
375
 
{
376
 
        if(module)
377
 
        {
378
 
                if(module->script_name)
379
 
                {
380
 
                        free(module->script_name);
381
 
                        module->script_name = NULL;
382
 
                }
383
 
                
384
 
                if(module->py_script_module)
385
 
                {
386
 
                        //PyObject_Free(module->py_script_module);
387
 
                        module->py_script_module = NULL;
388
 
                }
389
 
        }
390
 
        
391
 
        return 0;
392
 
}
393
 
 
394
 
int pcilib_script_read(pcilib_t *ctx, pcilib_script_t *module, pcilib_value_t *val)
395
 
{
 
381
                
 
382
        module->mode = mode[0];
 
383
        HASH_ADD_STR( scripts, script_name, module);
 
384
        
 
385
        return 0;
 
386
}
 
387
 
 
388
int pcilib_py_free_script(char* module_name)
 
389
{
 
390
        pcilib_script_s *module;
 
391
        HASH_FIND_STR(scripts, module_name, module);
 
392
        
 
393
        if(!module)
 
394
        {
 
395
                //For some reason it will crash if uncomment. printf same warning is ok
 
396
                //pcilib_warning("Cant find Python module %s in hash. Seems it has already deleted.", module_name);
 
397
                return 0;
 
398
        }
 
399
        
 
400
        if(module->script_name)
 
401
        {
 
402
                free(module->script_name);
 
403
                module->script_name = NULL;
 
404
        }
 
405
                
 
406
        if(module->py_script_module)
 
407
        {
 
408
                //PyObject_Free(module->py_script_module);
 
409
                module->py_script_module = NULL;
 
410
        }
 
411
        
 
412
        HASH_DEL(scripts, module);
 
413
        free(module);
 
414
        return 0;
 
415
}
 
416
 
 
417
int pcilib_script_read(pcilib_t *ctx, char* module_name, pcilib_value_t *val)
 
418
{
 
419
        pcilib_script_s *module;
 
420
        HASH_FIND_STR(scripts, module_name, module);
 
421
        
 
422
        if(!module)
 
423
        {
 
424
                pcilib_error("Failed to find script module (%s) in hash", module_name);
 
425
                return PCILIB_ERROR_NOTFOUND;
 
426
        }
396
427
        
397
428
        int err;
398
429
        
404
435
           return PCILIB_ERROR_FAILED;
405
436
        }
406
437
        
407
 
        err = pcilib_convert_pyobject_to_val(ctx, ret, val);
 
438
    err = pcilib_set_value_from_pyobject(ctx, ret, val);
408
439
        
409
440
        if(err)
410
441
        {
414
445
    return 0;
415
446
}
416
447
 
417
 
int pcilib_script_write(pcilib_t *ctx, pcilib_script_t *module, pcilib_value_t *val)
 
448
int pcilib_script_write(pcilib_t *ctx, char* module_name, pcilib_value_t *val)
418
449
{       
419
 
    PyObject *input = pcilib_convert_val_to_pyobject(ctx, val);
 
450
        pcilib_script_s *module;
 
451
        HASH_FIND_STR(scripts, module_name, module);
 
452
        
 
453
        if(!module)
 
454
        {
 
455
                pcilib_error("Failed to find script module (%s) in hash", module_name);
 
456
                return PCILIB_ERROR_NOTFOUND;
 
457
        }
 
458
        
 
459
    PyObject *input = pcilib_get_value_as_pyobject(ctx, val);
420
460
        if(!input)
421
461
        {
422
462
           printf("Failed to convert input value to Python object");