Commit 185e8710 authored by Michael Büsch's avatar Michael Büsch

Move dynamic attributes support to its own base class

Signed-off-by: Michael Büsch's avatarMichael Buesch <[email protected]>
parent a9091d86
# -*- coding: utf-8 -*-
#
# AWL simulator - Dynamic attributes base class
#
# Copyright 2014 Michael Buesch <[email protected]>
#
# 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.core.compat import *
class DynAttrs(object):
"""Dynamic attributes base class.
The specified attributes are automatically initialized
to their initial values on the first read access."""
# Dict of dynamic attributes.
# Key is the attribute name.
# Value is the initial attribute value.
dynAttrs = {}
def __getattr__(self, name):
# Create the attribute, if it is in the dynAttrs dict.
if name in self.dynAttrs:
value = self.dynAttrs[name]
setattr(self, name, value)
return value
# Fail for all other attributes
raise AttributeError
......@@ -22,11 +22,12 @@
from __future__ import division, absolute_import, print_function, unicode_literals
from awlsim.core.compat import *
from awlsim.core.dynattrs import *
from awlsim.core.util import *
from awlsim.core.datatypehelpers import *
class S7StatusWord(object):
class S7StatusWord(DynAttrs):
"""STEP 7 status word
The instance of this class holds the following nine
attributes. One for each STW bit:
......@@ -48,6 +49,19 @@ class S7StatusWord(object):
NR_BITS = 9
# Dynamic attributes
dynAttrs = {
"NER" : 0,
"VKE" : 0,
"STA" : 0,
"OR" : 0,
"OS" : 0,
"OV" : 0,
"A0" : 0,
"A1" : 0,
"BIE" : 0,
}
@classmethod
def getBitnrByName(cls, name):
try:
......@@ -56,16 +70,6 @@ class S7StatusWord(object):
raise AwlSimError("Invalid status word bit "
"name: " + str(name))
def __getattr__(self, name):
# Return 0 as default value for all STW bits,
# if they were not set, yet.
if name in ("NER", "VKE", "STA", "OR", "OS",
"OV", "A0", "A1", "BIE"):
setattr(self, name, 0)
return 0
# Fail for all other attributes
raise AttributeError
def __getNER(self):
return self.NER
......
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