POULibrary.py
author Edouard Tisserant
Thu, 10 Dec 2020 15:48:15 +0100
changeset 2699 467ff0e58327
parent 2645 d15a997859b1
child 2744 577118ebd179
child 2981 a0932a52e53b
permissions -rw-r--r--
Add a little test to see OnChange's .count .first .last in action
1511
91538d0c242c add copyright notices to python files where there were missing, that
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 772
diff changeset
     1
#!/usr/bin/env python
91538d0c242c add copyright notices to python files where there were missing, that
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 772
diff changeset
     2
# -*- coding: utf-8 -*-
91538d0c242c add copyright notices to python files where there were missing, that
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 772
diff changeset
     3
91538d0c242c add copyright notices to python files where there were missing, that
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 772
diff changeset
     4
# This file is part of Beremiz, a Integrated Development Environment for
91538d0c242c add copyright notices to python files where there were missing, that
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 772
diff changeset
     5
# programming IEC 61131-3 automates supporting plcopen standard and CanFestival.
91538d0c242c add copyright notices to python files where there were missing, that
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 772
diff changeset
     6
#
91538d0c242c add copyright notices to python files where there were missing, that
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 772
diff changeset
     7
# Copyright (C) 2007: Edouard TISSERANT and Laurent BESSARD
91538d0c242c add copyright notices to python files where there were missing, that
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 772
diff changeset
     8
#
91538d0c242c add copyright notices to python files where there were missing, that
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 772
diff changeset
     9
# See COPYING file for copyrights details.
91538d0c242c add copyright notices to python files where there were missing, that
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 772
diff changeset
    10
#
91538d0c242c add copyright notices to python files where there were missing, that
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 772
diff changeset
    11
# This program is free software; you can redistribute it and/or
91538d0c242c add copyright notices to python files where there were missing, that
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 772
diff changeset
    12
# modify it under the terms of the GNU General Public License
91538d0c242c add copyright notices to python files where there were missing, that
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 772
diff changeset
    13
# as published by the Free Software Foundation; either version 2
91538d0c242c add copyright notices to python files where there were missing, that
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 772
diff changeset
    14
# of the License, or (at your option) any later version.
91538d0c242c add copyright notices to python files where there were missing, that
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 772
diff changeset
    15
#
91538d0c242c add copyright notices to python files where there were missing, that
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 772
diff changeset
    16
# This program is distributed in the hope that it will be useful,
91538d0c242c add copyright notices to python files where there were missing, that
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 772
diff changeset
    17
# but WITHOUT ANY WARRANTY; without even the implied warranty of
91538d0c242c add copyright notices to python files where there were missing, that
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 772
diff changeset
    18
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
91538d0c242c add copyright notices to python files where there were missing, that
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 772
diff changeset
    19
# GNU General Public License for more details.
91538d0c242c add copyright notices to python files where there were missing, that
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 772
diff changeset
    20
#
91538d0c242c add copyright notices to python files where there were missing, that
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 772
diff changeset
    21
# You should have received a copy of the GNU General Public License
91538d0c242c add copyright notices to python files where there were missing, that
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 772
diff changeset
    22
# along with this program; if not, write to the Free Software
91538d0c242c add copyright notices to python files where there were missing, that
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 772
diff changeset
    23
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
91538d0c242c add copyright notices to python files where there were missing, that
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 772
diff changeset
    24
1881
091005ec69c4 fix pylint py3k conversion warning: "(no-absolute-import) import missing `from __future__ import absolute_import`"
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1831
diff changeset
    25
091005ec69c4 fix pylint py3k conversion warning: "(no-absolute-import) import missing `from __future__ import absolute_import`"
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1831
diff changeset
    26
from __future__ import absolute_import
732
c4b0f117e106 Added reference to CTR in libraries
Edouard Tisserant
parents: 731
diff changeset
    27
from weakref import ref
728
e0424e96e3fd refactoring - library support is not anymore attached to configtree nodes, but handles by project controller
Edouard Tisserant
parents:
diff changeset
    28
2645
d15a997859b1 Solve import order problem when POULibrary imports ConfigTreeNode because of UserAddressedException. Now UserAddressedException is declared in POULibrary.py, as a quick and dirty fix, but some independant module should be created for beremiz exceptions in general.
Edouard Tisserant
parents: 1917
diff changeset
    29
# Exception type for problems that user has to take action in order to fix
d15a997859b1 Solve import order problem when POULibrary imports ConfigTreeNode because of UserAddressedException. Now UserAddressedException is declared in POULibrary.py, as a quick and dirty fix, but some independant module should be created for beremiz exceptions in general.
Edouard Tisserant
parents: 1917
diff changeset
    30
class UserAddressedException(Exception):
d15a997859b1 Solve import order problem when POULibrary imports ConfigTreeNode because of UserAddressedException. Now UserAddressedException is declared in POULibrary.py, as a quick and dirty fix, but some independant module should be created for beremiz exceptions in general.
Edouard Tisserant
parents: 1917
diff changeset
    31
    pass
d15a997859b1 Solve import order problem when POULibrary imports ConfigTreeNode because of UserAddressedException. Now UserAddressedException is declared in POULibrary.py, as a quick and dirty fix, but some independant module should be created for beremiz exceptions in general.
Edouard Tisserant
parents: 1917
diff changeset
    32
1736
7e61baa047f0 clean-up: fix PEP8 E302 expected 2 blank lines, found 1
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1730
diff changeset
    33
1831
56b48961cc68 fix (old-style-class) Old-style class defined error for most parts of
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1752
diff changeset
    34
class POULibrary(object):
732
c4b0f117e106 Added reference to CTR in libraries
Edouard Tisserant
parents: 731
diff changeset
    35
    def __init__(self, CTR, LibName, TypeStack):
772
98786137232d Fixed import dependency order for POUlibrary and PLCControler
Edouard Tisserant
parents: 732
diff changeset
    36
        from PLCControler import PLCControler
732
c4b0f117e106 Added reference to CTR in libraries
Edouard Tisserant
parents: 731
diff changeset
    37
        self.CTR = ref(CTR)
731
4fc681ed0c61 refecored library extension machanism
Edouard Tisserant
parents: 728
diff changeset
    38
        self.LibName = LibName
728
e0424e96e3fd refactoring - library support is not anymore attached to configtree nodes, but handles by project controller
Edouard Tisserant
parents:
diff changeset
    39
        self.LibraryControler = PLCControler()
e0424e96e3fd refactoring - library support is not anymore attached to configtree nodes, but handles by project controller
Edouard Tisserant
parents:
diff changeset
    40
        self.LibraryControler.OpenXMLFile(self.GetLibraryPath())
e0424e96e3fd refactoring - library support is not anymore attached to configtree nodes, but handles by project controller
Edouard Tisserant
parents:
diff changeset
    41
        self.LibraryControler.ClearConfNodeTypes()
e0424e96e3fd refactoring - library support is not anymore attached to configtree nodes, but handles by project controller
Edouard Tisserant
parents:
diff changeset
    42
        self.LibraryControler.AddConfNodeTypesList(TypeStack)
1752
d14ff9d7eb76 clean-up: fix PEP8 E703 statement ends with a semicolon
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1739
diff changeset
    43
        self.program = None
728
e0424e96e3fd refactoring - library support is not anymore attached to configtree nodes, but handles by project controller
Edouard Tisserant
parents:
diff changeset
    44
e0424e96e3fd refactoring - library support is not anymore attached to configtree nodes, but handles by project controller
Edouard Tisserant
parents:
diff changeset
    45
    def GetSTCode(self):
e0424e96e3fd refactoring - library support is not anymore attached to configtree nodes, but handles by project controller
Edouard Tisserant
parents:
diff changeset
    46
        if not self.program:
e0424e96e3fd refactoring - library support is not anymore attached to configtree nodes, but handles by project controller
Edouard Tisserant
parents:
diff changeset
    47
            self.program = self.LibraryControler.GenerateProgram()[0]+"\n"
1730
64d8f52bc8c8 clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1511
diff changeset
    48
        return self.program
728
e0424e96e3fd refactoring - library support is not anymore attached to configtree nodes, but handles by project controller
Edouard Tisserant
parents:
diff changeset
    49
731
4fc681ed0c61 refecored library extension machanism
Edouard Tisserant
parents: 728
diff changeset
    50
    def GetName(self):
4fc681ed0c61 refecored library extension machanism
Edouard Tisserant
parents: 728
diff changeset
    51
        return self.LibName
732
c4b0f117e106 Added reference to CTR in libraries
Edouard Tisserant
parents: 731
diff changeset
    52
c4b0f117e106 Added reference to CTR in libraries
Edouard Tisserant
parents: 731
diff changeset
    53
    def GetCTR(self):
c4b0f117e106 Added reference to CTR in libraries
Edouard Tisserant
parents: 731
diff changeset
    54
        return self.CTR()
1730
64d8f52bc8c8 clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1511
diff changeset
    55
728
e0424e96e3fd refactoring - library support is not anymore attached to configtree nodes, but handles by project controller
Edouard Tisserant
parents:
diff changeset
    56
    def GetTypes(self):
1739
ec153828ded2 clean-up: fix PEP8 E203 whitespace before ':' and whitespace before ','
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1736
diff changeset
    57
        return {"name": self.GetName(), "types": self.LibraryControler.Project}
728
e0424e96e3fd refactoring - library support is not anymore attached to configtree nodes, but handles by project controller
Edouard Tisserant
parents:
diff changeset
    58
e0424e96e3fd refactoring - library support is not anymore attached to configtree nodes, but handles by project controller
Edouard Tisserant
parents:
diff changeset
    59
    def GetLibraryPath(self):
e0424e96e3fd refactoring - library support is not anymore attached to configtree nodes, but handles by project controller
Edouard Tisserant
parents:
diff changeset
    60
        raise Exception("Not implemented")
e0424e96e3fd refactoring - library support is not anymore attached to configtree nodes, but handles by project controller
Edouard Tisserant
parents:
diff changeset
    61
e0424e96e3fd refactoring - library support is not anymore attached to configtree nodes, but handles by project controller
Edouard Tisserant
parents:
diff changeset
    62
    def Generate_C(self, buildpath, varlist, IECCFLAGS):
e0424e96e3fd refactoring - library support is not anymore attached to configtree nodes, but handles by project controller
Edouard Tisserant
parents:
diff changeset
    63
        # Pure python or IEC libs doesn't produce C code
e0424e96e3fd refactoring - library support is not anymore attached to configtree nodes, but handles by project controller
Edouard Tisserant
parents:
diff changeset
    64
        return ((""), [], False), ""
1917
d51d14719392 Add some class factory function to ease declaration of simple POU libraries in customizations (i.e. TC6 XML file only, no special C code generation)
Edouard Tisserant
parents: 1881
diff changeset
    65
2645
d15a997859b1 Solve import order problem when POULibrary imports ConfigTreeNode because of UserAddressedException. Now UserAddressedException is declared in POULibrary.py, as a quick and dirty fix, but some independant module should be created for beremiz exceptions in general.
Edouard Tisserant
parents: 1917
diff changeset
    66
    def FatalError(self, message):
d15a997859b1 Solve import order problem when POULibrary imports ConfigTreeNode because of UserAddressedException. Now UserAddressedException is declared in POULibrary.py, as a quick and dirty fix, but some independant module should be created for beremiz exceptions in general.
Edouard Tisserant
parents: 1917
diff changeset
    67
        """ Raise an exception that will trigger error message intended to 
d15a997859b1 Solve import order problem when POULibrary imports ConfigTreeNode because of UserAddressedException. Now UserAddressedException is declared in POULibrary.py, as a quick and dirty fix, but some independant module should be created for beremiz exceptions in general.
Edouard Tisserant
parents: 1917
diff changeset
    68
            the user, but without backtrace since it is not a software error """
d15a997859b1 Solve import order problem when POULibrary imports ConfigTreeNode because of UserAddressedException. Now UserAddressedException is declared in POULibrary.py, as a quick and dirty fix, but some independant module should be created for beremiz exceptions in general.
Edouard Tisserant
parents: 1917
diff changeset
    69
d15a997859b1 Solve import order problem when POULibrary imports ConfigTreeNode because of UserAddressedException. Now UserAddressedException is declared in POULibrary.py, as a quick and dirty fix, but some independant module should be created for beremiz exceptions in general.
Edouard Tisserant
parents: 1917
diff changeset
    70
        raise UserAddressedException(message)
1917
d51d14719392 Add some class factory function to ease declaration of simple POU libraries in customizations (i.e. TC6 XML file only, no special C code generation)
Edouard Tisserant
parents: 1881
diff changeset
    71
d51d14719392 Add some class factory function to ease declaration of simple POU libraries in customizations (i.e. TC6 XML file only, no special C code generation)
Edouard Tisserant
parents: 1881
diff changeset
    72
def SimplePOULibraryFactory(path):
d51d14719392 Add some class factory function to ease declaration of simple POU libraries in customizations (i.e. TC6 XML file only, no special C code generation)
Edouard Tisserant
parents: 1881
diff changeset
    73
    class SimplePOULibrary(POULibrary):
d51d14719392 Add some class factory function to ease declaration of simple POU libraries in customizations (i.e. TC6 XML file only, no special C code generation)
Edouard Tisserant
parents: 1881
diff changeset
    74
        def GetLibraryPath(self):
d51d14719392 Add some class factory function to ease declaration of simple POU libraries in customizations (i.e. TC6 XML file only, no special C code generation)
Edouard Tisserant
parents: 1881
diff changeset
    75
            return path
d51d14719392 Add some class factory function to ease declaration of simple POU libraries in customizations (i.e. TC6 XML file only, no special C code generation)
Edouard Tisserant
parents: 1881
diff changeset
    76
    return SimplePOULibrary