feat: Add context manager for ios

Signed-off-by: Sven Sager <akira@narux.de>
This commit is contained in:
2023-10-27 14:55:58 +02:00
parent 9a90efeaf2
commit ab2703a4d3
2 changed files with 20 additions and 3 deletions

View File

@@ -35,10 +35,11 @@ class IOEvent(object):
class IOList(object): class IOList(object):
"""Basisklasse fuer direkten Zugriff auf IO Objekte.""" """Basisklasse fuer direkten Zugriff auf IO Objekte."""
def __init__(self): def __init__(self, modio):
"""Init IOList class.""" """Init IOList class."""
self.__dict_iobyte = {k: [] for k in range(PROCESS_IMAGE_SIZE)} self.__dict_iobyte = {k: [] for k in range(PROCESS_IMAGE_SIZE)}
self.__dict_iorefname = {} self.__dict_iorefname = {}
self.__modio = modio
def __contains__(self, key): def __contains__(self, key):
""" """
@@ -85,6 +86,18 @@ class IOList(object):
object.__delattr__(self, key) object.__delattr__(self, key)
io_del._parentdevice._update_my_io_list() io_del._parentdevice._update_my_io_list()
def __enter__(self):
if self.__modio._looprunning:
raise RuntimeError("can not start multiple mainloop/cycleloop/with sessions")
self.__modio._looprunning = True
self.__modio.readprocimg()
return self
def __exit__(self, exc_type, exc_val, exc_tb):
self.__modio.writeprocimg()
self.__modio._looprunning = False
def __getattr__(self, key): def __getattr__(self, key):
""" """
Verwaltet geloeschte IOs (Attribute, die nicht existieren). Verwaltet geloeschte IOs (Attribute, die nicht existieren).
@@ -148,7 +161,11 @@ class IOList(object):
def __setattr__(self, key, value): def __setattr__(self, key, value):
"""Verbietet aus Leistungsguenden das direkte Setzen von Attributen.""" """Verbietet aus Leistungsguenden das direkte Setzen von Attributen."""
if key in ("_IOList__dict_iobyte", "_IOList__dict_iorefname"): if key in (
"_IOList__dict_iobyte",
"_IOList__dict_iorefname",
"_IOList__modio",
):
object.__setattr__(self, key, value) object.__setattr__(self, key, value)
else: else:
raise AttributeError("direct assignment is not supported - use .value Attribute") raise AttributeError("direct assignment is not supported - use .value Attribute")

View File

@@ -306,7 +306,7 @@ class RevPiModIO(object):
# Device und IO Klassen anlegen # Device und IO Klassen anlegen
self.device = devicemodule.DeviceList() self.device = devicemodule.DeviceList()
self.io = IOList() self.io = IOList(self)
# Devices initialisieren # Devices initialisieren
err_names_check = {} err_names_check = {}