andrej@1511: #!/usr/bin/env python
andrej@1511: # -*- coding: utf-8 -*-
andrej@1511: 
andrej@1511: # This file is part of Beremiz, a Integrated Development Environment for
andrej@1511: # programming IEC 61131-3 automates supporting plcopen standard and CanFestival.
andrej@1511: #
andrej@1511: # Copyright (C) 2007: Edouard TISSERANT and Laurent BESSARD
andrej@1680: # Copyright (C) 2017: Andrey Skvortsov
andrej@1511: #
andrej@1511: # See COPYING file for copyrights details.
andrej@1511: #
andrej@1511: # This program is free software; you can redistribute it and/or
andrej@1511: # modify it under the terms of the GNU General Public License
andrej@1511: # as published by the Free Software Foundation; either version 2
andrej@1511: # of the License, or (at your option) any later version.
andrej@1511: #
andrej@1511: # This program is distributed in the hope that it will be useful,
andrej@1511: # but WITHOUT ANY WARRANTY; without even the implied warranty of
andrej@1511: # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
andrej@1511: # GNU General Public License for more details.
andrej@1511: #
andrej@1511: # You should have received a copy of the GNU General Public License
andrej@1511: # along with this program; if not, write to the Free Software
andrej@1511: # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
andrej@1511: 
andrej@1853: 
andrej@1853: from __future__ import absolute_import
laurent@366: import os
Edouard@728: from POULibrary import POULibrary
andrej@1853: from py_ext.PythonFileCTNMixin import PythonFileCTNMixin
andrej@1680: import util.paths as paths
laurent@366: 
andrej@1736: 
Edouard@728: class PythonLibrary(POULibrary):
Edouard@728:     def GetLibraryPath(self):
andrej@1730:         return paths.AbsNeighbourFile(__file__, "pous.xml")
laurent@366: 
Edouard@728:     def Generate_C(self, buildpath, varlist, IECCFLAGS):
andrej@1730: 
andrej@1680:         plc_python_filepath = paths.AbsNeighbourFile(__file__, "plc_python.c")
laurent@366:         plc_python_file = open(plc_python_filepath, 'r')
laurent@366:         plc_python_code = plc_python_file.read()
laurent@366:         plc_python_file.close()
laurent@366:         python_eval_fb_list = []
Edouard@728:         for v in varlist:
Edouard@1132:             if v["vartype"] == "FB" and v["type"] in ["PYTHON_EVAL",
Edouard@1132:                                                       "PYTHON_POLL"]:
laurent@366:                 python_eval_fb_list.append(v)
laurent@366:         python_eval_fb_count = max(1, len(python_eval_fb_list))
andrej@1730: 
laurent@366:         # prepare python code
Edouard@1132:         plc_python_code = plc_python_code % {
andrej@1746:             "python_eval_fb_count": python_eval_fb_count}
andrej@1730: 
Edouard@728:         Gen_Pythonfile_path = os.path.join(buildpath, "py_ext.c")
andrej@1740:         pythonfile = open(Gen_Pythonfile_path, 'w')
laurent@366:         pythonfile.write(plc_python_code)
laurent@366:         pythonfile.close()
andrej@1730: 
Edouard@728:         return (["py_ext"], [(Gen_Pythonfile_path, IECCFLAGS)], True), ""
Edouard@728: 
andrej@1736: 
Edouard@728: class PythonFile(PythonFileCTNMixin):
andrej@1730: 
laurent@781:     def GetIconName(self):
laurent@781:         return "Pyfile"