py_ext/py_ext.py
changeset 1132 28f96aa9c070
parent 1124 b1705000eba1
child 1511 91538d0c242c
equal deleted inserted replaced
1131:f794fbff8f02 1132:28f96aa9c070
     6     def GetLibraryPath(self):
     6     def GetLibraryPath(self):
     7         return os.path.join(os.path.split(__file__)[0], "pous.xml") 
     7         return os.path.join(os.path.split(__file__)[0], "pous.xml") 
     8 
     8 
     9     def Generate_C(self, buildpath, varlist, IECCFLAGS):
     9     def Generate_C(self, buildpath, varlist, IECCFLAGS):
    10         
    10         
    11         plc_python_filepath = os.path.join(os.path.split(__file__)[0], "plc_python.c")
    11         plc_python_filepath = os.path.join(
       
    12             os.path.split(__file__)[0], "plc_python.c")
    12         plc_python_file = open(plc_python_filepath, 'r')
    13         plc_python_file = open(plc_python_filepath, 'r')
    13         plc_python_code = plc_python_file.read()
    14         plc_python_code = plc_python_file.read()
    14         plc_python_file.close()
    15         plc_python_file.close()
    15         python_eval_fb_list = []
    16         python_eval_fb_list = []
    16         for v in varlist:
    17         for v in varlist:
    17             if v["vartype"] == "FB" and v["type"] in ["PYTHON_EVAL","PYTHON_POLL"]:
    18             if v["vartype"] == "FB" and v["type"] in ["PYTHON_EVAL",
       
    19                                                       "PYTHON_POLL"]:
    18                 python_eval_fb_list.append(v)
    20                 python_eval_fb_list.append(v)
    19         python_eval_fb_count = max(1, len(python_eval_fb_list))
    21         python_eval_fb_count = max(1, len(python_eval_fb_list))
    20         
    22         
    21         # prepare python code
    23         # prepare python code
    22         plc_python_code = plc_python_code % { "python_eval_fb_count": python_eval_fb_count }
    24         plc_python_code = plc_python_code % {
       
    25             "python_eval_fb_count": python_eval_fb_count }
    23         
    26         
    24         Gen_Pythonfile_path = os.path.join(buildpath, "py_ext.c")
    27         Gen_Pythonfile_path = os.path.join(buildpath, "py_ext.c")
    25         pythonfile = open(Gen_Pythonfile_path,'w')
    28         pythonfile = open(Gen_Pythonfile_path,'w')
    26         pythonfile.write(plc_python_code)
    29         pythonfile.write(plc_python_code)
    27         pythonfile.close()
    30         pythonfile.close()
    31 class PythonFile(PythonFileCTNMixin):
    34 class PythonFile(PythonFileCTNMixin):
    32     
    35     
    33     def GetIconName(self):
    36     def GetIconName(self):
    34         return "Pyfile"
    37         return "Pyfile"
    35     
    38     
    36     def CTNGenerate_C(self, buildpath, locations):
       
    37         current_location = self.GetCurrentLocation()
       
    38         # define a unique name for the generated C file
       
    39         location_str = "_".join(map(lambda x:str(x), current_location))
       
    40         
       
    41         sections_code = self.GetSectionsCode()
       
    42         
       
    43         text = "## Code generated by Beremiz python mixin confnode\n\n"
       
    44         
       
    45         # Adding variables
       
    46         text += "## User variables reference\n"
       
    47         text += sections_code["variables"]
       
    48         text += "\n"
       
    49         
       
    50         # Adding user global variables and routines
       
    51         text += "## User internal user variables and routines\n"
       
    52         text += sections_code["globals"]
       
    53         text += "\n"
       
    54         
       
    55         # Adding Beremiz python runtime functions
       
    56         text += "## Beremiz python runtime functions\n"
       
    57         for section in self.SECTIONS_NAMES:
       
    58             if section != "globals":
       
    59                 code_object = getattr(self.CodeFile, section)
       
    60                 text += "def _runtime_%s_%s():\n" % (location_str, section)
       
    61                 section_code = sections_code.get(section)
       
    62                 if section_code:
       
    63                     text += section_code
       
    64                 else:
       
    65                     text += "    pass\n\n"
       
    66         
       
    67         runtimefile_path = os.path.join(buildpath, "runtime_%s.py"%location_str)
       
    68         runtimefile = open(runtimefile_path, 'w')
       
    69         runtimefile.write(text)
       
    70         runtimefile.close()
       
    71         
       
    72         return [], "", False, ("runtime_%s.py"%location_str, file(runtimefile_path,"rb"))
       
    73 
    39