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@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@1881: 
kinsamanka@3750: 
Edouard@732: from weakref import ref
Edouard@728: 
Edouard@2645: # Exception type for problems that user has to take action in order to fix
Edouard@2645: class UserAddressedException(Exception):
Edouard@2645:     pass
Edouard@2645: 
andrej@1736: 
andrej@1831: class POULibrary(object):
Edouard@732:     def __init__(self, CTR, LibName, TypeStack):
Edouard@772:         from PLCControler import PLCControler
Edouard@732:         self.CTR = ref(CTR)
Edouard@731:         self.LibName = LibName
Edouard@728:         self.LibraryControler = PLCControler()
Edouard@728:         self.LibraryControler.OpenXMLFile(self.GetLibraryPath())
Edouard@728:         self.LibraryControler.ClearConfNodeTypes()
Edouard@728:         self.LibraryControler.AddConfNodeTypesList(TypeStack)
andrej@1752:         self.program = None
Edouard@728: 
Edouard@728:     def GetSTCode(self):
Edouard@728:         if not self.program:
Edouard@3704:             self.program = self.LibraryControler.GenerateProgram(noconfig=True)[0]+"\n"
andrej@1730:         return self.program
Edouard@728: 
Edouard@731:     def GetName(self):
Edouard@731:         return self.LibName
Edouard@732: 
Edouard@732:     def GetCTR(self):
Edouard@732:         return self.CTR()
andrej@1730: 
Edouard@728:     def GetTypes(self):
andrej@1739:         return {"name": self.GetName(), "types": self.LibraryControler.Project}
Edouard@728: 
Edouard@728:     def GetLibraryPath(self):
Edouard@728:         raise Exception("Not implemented")
Edouard@728: 
Edouard@728:     def Generate_C(self, buildpath, varlist, IECCFLAGS):
Edouard@728:         # Pure python or IEC libs doesn't produce C code
Edouard@728:         return ((""), [], False), ""
Edouard@1917: 
edouard@3266:     def GlobalInstances(self):
edouard@3266:         """
Edouard@3704:         @return: [varlist_object, ...]
edouard@3266:         """
Edouard@3704:         varlists = []
Edouard@3704:         for configuration in self.LibraryControler.Project.getconfigurations():
Edouard@3704:             varlist = configuration.getglobalVars()
Edouard@3704:             if len(varlist)>0 :
Edouard@3704:                 varlists += varlist
Edouard@3704:         return varlists
edouard@3266: 
Edouard@2645:     def FatalError(self, message):
Edouard@2645:         """ Raise an exception that will trigger error message intended to 
Edouard@2645:             the user, but without backtrace since it is not a software error """
Edouard@2645: 
Edouard@2645:         raise UserAddressedException(message)
Edouard@1917: 
Edouard@1917: def SimplePOULibraryFactory(path):
Edouard@1917:     class SimplePOULibrary(POULibrary):
Edouard@1917:         def GetLibraryPath(self):
Edouard@1917:             return path
Edouard@1917:     return SimplePOULibrary