Commit dc6efb37 authored by Michael Büsch's avatar Michael Büsch

library: Add library selections

Signed-off-by: Michael Büsch's avatarMichael Buesch <m@bues.ch>
parent f4602f0e
......@@ -2,3 +2,4 @@ from __future__ import division, absolute_import, print_function, unicode_litera
from awlsim.common.compat import *
from awlsim.library.libentry import *
from awlsim.library.libselection import *
......@@ -27,6 +27,8 @@ from awlsim.common.dynamic_import import *
from awlsim.core.blocks import *
from awlsim.library.libselection import *
class AwlLib(object):
"""AWL library."""
......@@ -37,6 +39,7 @@ class AwlLib(object):
def register(cls, libName, description):
"""Register an AWL library."""
libName = libName.lower()
if libName in cls.__awlLibs:
raise AwlSimError("Trying to register library '%s', "
"but it does already exist.")
......@@ -46,25 +49,38 @@ class AwlLib(object):
def registerEntry(cls, entryClass):
"""Register an entry-class for an already registered library."""
if entryClass.libraryName not in cls.__awlLibs:
libName = entryClass.libraryName.lower()
if libName not in cls.__awlLibs:
raise AwlSimError("Trying to register element '%s' "
"for unknown library '%s'." %\
(str(entryClass), entryClass.libraryName))
cls.__awlLibs[entryClass.libraryName].entryClasses.add(entryClass)
cls.__awlLibs[libName].entryClasses.add(entryClass)
@classmethod
def getByName(cls, libName):
"""Get a library, by name."""
importModule("awlsim.library.%s" % libName.lower())
libName = libName.lower()
for c in libName:
if not c.isalnum() and c != "_":
raise AwlSimError("Library name '%s' "
"is invalid." % libName)
try:
importModule("awlsim.library.%s" % libName)
return cls.__awlLibs[libName]
except KeyError:
except (ImportError, KeyError) as e:
raise AwlSimError("Library '%s' was not found "
"in the standard library catalog." %\
libName)
@classmethod
def getEntryBySelection(cls, selection):
"""Get a library entry class by AwlLibEntrySelection().
selection -> An AwlLibEntrySelection instance."""
return cls.getByName(selection.getLibName()).getEntry(selection)
def __init__(self, name, description):
self.name = name
self.description = description
......@@ -81,6 +97,23 @@ class AwlLib(object):
for cls in sorted(self.entryClasses, key=sortKey):
yield cls
def getEntry(self, selection):
"""Get a library entry class by AwlLibEntrySelection().
selection -> An AwlLibEntrySelection instance."""
for cls in self.entryClasses:
if selection.getEntryType() == selection.TYPE_FC:
if not cls.isFC:
continue
else:
if not cls.isFB:
continue
if cls.staticIndex != selection.getEntryIndex():
continue
return cls
raise AwlSimError("The selected library entry '%s' was "
"not found." % str(selection))
class AwlLibEntry(StaticCodeBlock):
"""AWL library entry base class."""
......@@ -136,10 +169,18 @@ class AwlLibEntry(StaticCodeBlock):
code.append("")
return "\n".join(code), retValType
# Get the AWL code
def getCode(self):
"""Get the AWL code."""
raise NotImplementedError
def makeSelection(self):
"""Get an AwlLibEntrySelection for this entry."""
return AwlLibEntrySelection(libName = self.libraryName,
entryIndex = self.staticIndex,
effectiveEntryIndex = self.index)
def __repr__(self):
return "AwlLibEntry %d" % self.index
......@@ -151,8 +192,9 @@ class AwlLibFC(AwlLibEntry):
def __init__(self, index=None):
AwlLibEntry.__init__(self, index, AwlLibFCInterface())
# Get the AWL code
def getCode(self, symbolic=False):
"""Get the AWL code."""
interfCode, retValType = self._generateInterfaceCode(True)
code = []
if symbolic:
......@@ -167,6 +209,13 @@ class AwlLibFC(AwlLibEntry):
code.append("\nEND_FUNCTION\n")
return "".join(code)
def makeSelection(self):
"""Get an AwlLibEntrySelection for this entry."""
sel = AwlLibEntry.makeSelection(self)
sel.setEntryType(sel.TYPE_FC)
return sel
class AwlLibFCInterface(FCInterface):
pass
......@@ -178,8 +227,9 @@ class AwlLibFB(AwlLibEntry):
def __init__(self, index=None):
AwlLibEntry.__init__(self, index, AwlLibFBInterface())
# Get the AWL code
def getCode(self, symbolic=False):
"""Get the AWL code."""
interfCode, retValType = self._generateInterfaceCode(False)
code = []
if symbolic:
......@@ -192,5 +242,12 @@ class AwlLibFB(AwlLibEntry):
code.append("\nEND_FUNCTION_BLOCK\n")
return "".join(code)
def makeSelection(self):
"""Get an AwlLibEntrySelection for this entry."""
sel = AwlLibEntry.makeSelection(self)
sel.setEntryType(sel.TYPE_FB)
return sel
class AwlLibFBInterface(FBInterface):
pass
# -*- coding: utf-8 -*-
#
# AWL simulator - Library entry selection
#
# Copyright 2014 Michael Buesch <m@bues.ch>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License along
# with this program; if not, write to the Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
#
from __future__ import division, absolute_import, print_function, unicode_literals
from awlsim.common.compat import *
from awlsim.common.enumeration import *
class AwlLibEntrySelection(object):
"""AWL library entry selection."""
EnumGen.start
TYPE_UNKNOWN = EnumGen.item
TYPE_FC = EnumGen.item
TYPE_FB = EnumGen.item
EnumGen.end
def __init__(self, libName="",
entryType=TYPE_UNKNOWN,
entryIndex=-1, effectiveEntryIndex=-1):
self.setLibName(libName)
self.setEntryType(entryType)
self.setEntryIndex(entryIndex)
self.setEffectiveEntryIndex(effectiveEntryIndex)
def setLibName(self, libName):
self.__libName = libName.strip()
def getLibName(self):
return self.__libName
def setEntryType(self, entryType):
self.__entryType = entryType
def getEntryType(self):
return self.__entryType
def getEntryTypeStr(self):
return {
self.TYPE_UNKNOWN : None,
self.TYPE_FC : "FC",
self.TYPE_FB : "FB",
}[self.getEntryType()]
def setEntryIndex(self, entryIndex):
self.__entryIndex = entryIndex
def getEntryIndex(self):
return self.__entryIndex
def setEffectiveEntryIndex(self, effectiveEntryIndex):
self.__effectiveEntryIndex = effectiveEntryIndex
def getEffectiveEntryIndex(self):
return self.__effectiveEntryIndex
def isValid(self):
return self.__libName and\
self.__entryType in (self.TYPE_FC, self.TYPE_FB) and\
self.__entryIndex >= 0 and\
self.__entryIndex <= 0xFFFF and\
self.__effectiveEntryIndex >= 0 and\
self.__effectiveEntryIndex <= 0xFFFF
def __repr__(self):
return "%s - %s %d (=> %s %d)" % (
self.getLibName(),
self.getEntryTypeStr(),
self.getEntryIndex(),
self.getEntryTypeStr(),
self.getEffectiveEntryIndex(),
)
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment