py_ext/py_ext.py
author Laurent Bessard
Mon, 13 May 2013 14:31:23 +0200
changeset 1124 b1705000eba1
parent 781 cdc6393705ce
child 1132 28f96aa9c070
permissions -rw-r--r--
Fixed support for defining python runtime code using sections like in c_ext
366
cd90e4c10261 Move python evaluator to create a python plugin containing any related python module
laurent
parents:
diff changeset
     1
import os
728
e0424e96e3fd refactoring - library support is not anymore attached to configtree nodes, but handles by project controller
Edouard Tisserant
parents: 725
diff changeset
     2
from POULibrary import POULibrary
e0424e96e3fd refactoring - library support is not anymore attached to configtree nodes, but handles by project controller
Edouard Tisserant
parents: 725
diff changeset
     3
from PythonFileCTNMixin import PythonFileCTNMixin
366
cd90e4c10261 Move python evaluator to create a python plugin containing any related python module
laurent
parents:
diff changeset
     4
728
e0424e96e3fd refactoring - library support is not anymore attached to configtree nodes, but handles by project controller
Edouard Tisserant
parents: 725
diff changeset
     5
class PythonLibrary(POULibrary):
e0424e96e3fd refactoring - library support is not anymore attached to configtree nodes, but handles by project controller
Edouard Tisserant
parents: 725
diff changeset
     6
    def GetLibraryPath(self):
e0424e96e3fd refactoring - library support is not anymore attached to configtree nodes, but handles by project controller
Edouard Tisserant
parents: 725
diff changeset
     7
        return os.path.join(os.path.split(__file__)[0], "pous.xml") 
366
cd90e4c10261 Move python evaluator to create a python plugin containing any related python module
laurent
parents:
diff changeset
     8
728
e0424e96e3fd refactoring - library support is not anymore attached to configtree nodes, but handles by project controller
Edouard Tisserant
parents: 725
diff changeset
     9
    def Generate_C(self, buildpath, varlist, IECCFLAGS):
366
cd90e4c10261 Move python evaluator to create a python plugin containing any related python module
laurent
parents:
diff changeset
    10
        
cd90e4c10261 Move python evaluator to create a python plugin containing any related python module
laurent
parents:
diff changeset
    11
        plc_python_filepath = os.path.join(os.path.split(__file__)[0], "plc_python.c")
cd90e4c10261 Move python evaluator to create a python plugin containing any related python module
laurent
parents:
diff changeset
    12
        plc_python_file = open(plc_python_filepath, 'r')
cd90e4c10261 Move python evaluator to create a python plugin containing any related python module
laurent
parents:
diff changeset
    13
        plc_python_code = plc_python_file.read()
cd90e4c10261 Move python evaluator to create a python plugin containing any related python module
laurent
parents:
diff changeset
    14
        plc_python_file.close()
cd90e4c10261 Move python evaluator to create a python plugin containing any related python module
laurent
parents:
diff changeset
    15
        python_eval_fb_list = []
728
e0424e96e3fd refactoring - library support is not anymore attached to configtree nodes, but handles by project controller
Edouard Tisserant
parents: 725
diff changeset
    16
        for v in varlist:
366
cd90e4c10261 Move python evaluator to create a python plugin containing any related python module
laurent
parents:
diff changeset
    17
            if v["vartype"] == "FB" and v["type"] in ["PYTHON_EVAL","PYTHON_POLL"]:
cd90e4c10261 Move python evaluator to create a python plugin containing any related python module
laurent
parents:
diff changeset
    18
                python_eval_fb_list.append(v)
cd90e4c10261 Move python evaluator to create a python plugin containing any related python module
laurent
parents:
diff changeset
    19
        python_eval_fb_count = max(1, len(python_eval_fb_list))
cd90e4c10261 Move python evaluator to create a python plugin containing any related python module
laurent
parents:
diff changeset
    20
        
cd90e4c10261 Move python evaluator to create a python plugin containing any related python module
laurent
parents:
diff changeset
    21
        # prepare python code
728
e0424e96e3fd refactoring - library support is not anymore attached to configtree nodes, but handles by project controller
Edouard Tisserant
parents: 725
diff changeset
    22
        plc_python_code = plc_python_code % { "python_eval_fb_count": python_eval_fb_count }
366
cd90e4c10261 Move python evaluator to create a python plugin containing any related python module
laurent
parents:
diff changeset
    23
        
728
e0424e96e3fd refactoring - library support is not anymore attached to configtree nodes, but handles by project controller
Edouard Tisserant
parents: 725
diff changeset
    24
        Gen_Pythonfile_path = os.path.join(buildpath, "py_ext.c")
366
cd90e4c10261 Move python evaluator to create a python plugin containing any related python module
laurent
parents:
diff changeset
    25
        pythonfile = open(Gen_Pythonfile_path,'w')
cd90e4c10261 Move python evaluator to create a python plugin containing any related python module
laurent
parents:
diff changeset
    26
        pythonfile.write(plc_python_code)
cd90e4c10261 Move python evaluator to create a python plugin containing any related python module
laurent
parents:
diff changeset
    27
        pythonfile.close()
cd90e4c10261 Move python evaluator to create a python plugin containing any related python module
laurent
parents:
diff changeset
    28
        
728
e0424e96e3fd refactoring - library support is not anymore attached to configtree nodes, but handles by project controller
Edouard Tisserant
parents: 725
diff changeset
    29
        return (["py_ext"], [(Gen_Pythonfile_path, IECCFLAGS)], True), ""
e0424e96e3fd refactoring - library support is not anymore attached to configtree nodes, but handles by project controller
Edouard Tisserant
parents: 725
diff changeset
    30
e0424e96e3fd refactoring - library support is not anymore attached to configtree nodes, but handles by project controller
Edouard Tisserant
parents: 725
diff changeset
    31
class PythonFile(PythonFileCTNMixin):
738
413946c04c87 refactoring
laurent
parents: 731
diff changeset
    32
    
781
cdc6393705ce Adding support using plcopeneditor bitmap library for icon request
laurent
parents: 742
diff changeset
    33
    def GetIconName(self):
cdc6393705ce Adding support using plcopeneditor bitmap library for icon request
laurent
parents: 742
diff changeset
    34
        return "Pyfile"
738
413946c04c87 refactoring
laurent
parents: 731
diff changeset
    35
    
728
e0424e96e3fd refactoring - library support is not anymore attached to configtree nodes, but handles by project controller
Edouard Tisserant
parents: 725
diff changeset
    36
    def CTNGenerate_C(self, buildpath, locations):
e0424e96e3fd refactoring - library support is not anymore attached to configtree nodes, but handles by project controller
Edouard Tisserant
parents: 725
diff changeset
    37
        current_location = self.GetCurrentLocation()
e0424e96e3fd refactoring - library support is not anymore attached to configtree nodes, but handles by project controller
Edouard Tisserant
parents: 725
diff changeset
    38
        # define a unique name for the generated C file
e0424e96e3fd refactoring - library support is not anymore attached to configtree nodes, but handles by project controller
Edouard Tisserant
parents: 725
diff changeset
    39
        location_str = "_".join(map(lambda x:str(x), current_location))
e0424e96e3fd refactoring - library support is not anymore attached to configtree nodes, but handles by project controller
Edouard Tisserant
parents: 725
diff changeset
    40
        
1124
b1705000eba1 Fixed support for defining python runtime code using sections like in c_ext
Laurent Bessard
parents: 781
diff changeset
    41
        sections_code = self.GetSectionsCode()
b1705000eba1 Fixed support for defining python runtime code using sections like in c_ext
Laurent Bessard
parents: 781
diff changeset
    42
        
b1705000eba1 Fixed support for defining python runtime code using sections like in c_ext
Laurent Bessard
parents: 781
diff changeset
    43
        text = "## Code generated by Beremiz python mixin confnode\n\n"
b1705000eba1 Fixed support for defining python runtime code using sections like in c_ext
Laurent Bessard
parents: 781
diff changeset
    44
        
b1705000eba1 Fixed support for defining python runtime code using sections like in c_ext
Laurent Bessard
parents: 781
diff changeset
    45
        # Adding variables
b1705000eba1 Fixed support for defining python runtime code using sections like in c_ext
Laurent Bessard
parents: 781
diff changeset
    46
        text += "## User variables reference\n"
b1705000eba1 Fixed support for defining python runtime code using sections like in c_ext
Laurent Bessard
parents: 781
diff changeset
    47
        text += sections_code["variables"]
b1705000eba1 Fixed support for defining python runtime code using sections like in c_ext
Laurent Bessard
parents: 781
diff changeset
    48
        text += "\n"
b1705000eba1 Fixed support for defining python runtime code using sections like in c_ext
Laurent Bessard
parents: 781
diff changeset
    49
        
b1705000eba1 Fixed support for defining python runtime code using sections like in c_ext
Laurent Bessard
parents: 781
diff changeset
    50
        # Adding user global variables and routines
b1705000eba1 Fixed support for defining python runtime code using sections like in c_ext
Laurent Bessard
parents: 781
diff changeset
    51
        text += "## User internal user variables and routines\n"
b1705000eba1 Fixed support for defining python runtime code using sections like in c_ext
Laurent Bessard
parents: 781
diff changeset
    52
        text += sections_code["globals"]
b1705000eba1 Fixed support for defining python runtime code using sections like in c_ext
Laurent Bessard
parents: 781
diff changeset
    53
        text += "\n"
b1705000eba1 Fixed support for defining python runtime code using sections like in c_ext
Laurent Bessard
parents: 781
diff changeset
    54
        
b1705000eba1 Fixed support for defining python runtime code using sections like in c_ext
Laurent Bessard
parents: 781
diff changeset
    55
        # Adding Beremiz python runtime functions
b1705000eba1 Fixed support for defining python runtime code using sections like in c_ext
Laurent Bessard
parents: 781
diff changeset
    56
        text += "## Beremiz python runtime functions\n"
b1705000eba1 Fixed support for defining python runtime code using sections like in c_ext
Laurent Bessard
parents: 781
diff changeset
    57
        for section in self.SECTIONS_NAMES:
b1705000eba1 Fixed support for defining python runtime code using sections like in c_ext
Laurent Bessard
parents: 781
diff changeset
    58
            if section != "globals":
b1705000eba1 Fixed support for defining python runtime code using sections like in c_ext
Laurent Bessard
parents: 781
diff changeset
    59
                code_object = getattr(self.CodeFile, section)
b1705000eba1 Fixed support for defining python runtime code using sections like in c_ext
Laurent Bessard
parents: 781
diff changeset
    60
                text += "def _runtime_%s_%s():\n" % (location_str, section)
b1705000eba1 Fixed support for defining python runtime code using sections like in c_ext
Laurent Bessard
parents: 781
diff changeset
    61
                section_code = sections_code.get(section)
b1705000eba1 Fixed support for defining python runtime code using sections like in c_ext
Laurent Bessard
parents: 781
diff changeset
    62
                if section_code:
b1705000eba1 Fixed support for defining python runtime code using sections like in c_ext
Laurent Bessard
parents: 781
diff changeset
    63
                    text += section_code
b1705000eba1 Fixed support for defining python runtime code using sections like in c_ext
Laurent Bessard
parents: 781
diff changeset
    64
                else:
b1705000eba1 Fixed support for defining python runtime code using sections like in c_ext
Laurent Bessard
parents: 781
diff changeset
    65
                    text += "    pass\n\n"
b1705000eba1 Fixed support for defining python runtime code using sections like in c_ext
Laurent Bessard
parents: 781
diff changeset
    66
        
366
cd90e4c10261 Move python evaluator to create a python plugin containing any related python module
laurent
parents:
diff changeset
    67
        runtimefile_path = os.path.join(buildpath, "runtime_%s.py"%location_str)
cd90e4c10261 Move python evaluator to create a python plugin containing any related python module
laurent
parents:
diff changeset
    68
        runtimefile = open(runtimefile_path, 'w')
1124
b1705000eba1 Fixed support for defining python runtime code using sections like in c_ext
Laurent Bessard
parents: 781
diff changeset
    69
        runtimefile.write(text)
366
cd90e4c10261 Move python evaluator to create a python plugin containing any related python module
laurent
parents:
diff changeset
    70
        runtimefile.close()
cd90e4c10261 Move python evaluator to create a python plugin containing any related python module
laurent
parents:
diff changeset
    71
        
728
e0424e96e3fd refactoring - library support is not anymore attached to configtree nodes, but handles by project controller
Edouard Tisserant
parents: 725
diff changeset
    72
        return [], "", False, ("runtime_%s.py"%location_str, file(runtimefile_path,"rb"))
e0424e96e3fd refactoring - library support is not anymore attached to configtree nodes, but handles by project controller
Edouard Tisserant
parents: 725
diff changeset
    73