added shutter opened/closed contexts

This commit is contained in:
2022-09-21 11:33:11 +02:00
parent 3ca9fea8ab
commit 6d76722afe
2 changed files with 45 additions and 1 deletions
+3 -1
View File
@@ -1,6 +1,8 @@
from slic.core.adjustable import PVAdjustable
from slic.utils import typename
from .shutterctx import ShutterContextMixin
#REQUEST
# 0: close
@@ -15,7 +17,7 @@ from slic.utils import typename
# 1: TRUE
class Shutter:
class Shutter(ShutterContextMixin):
def __init__(self, ID):
self.ID = ID
+42
View File
@@ -0,0 +1,42 @@
class ShutterContextMixin:
def opened(self):
"""
Returns a context manager that opens the shutter on enter and closes on exit
"""
return ShutterCloser(self.open, self.close)
def closed(self):
"""
Returns a context manager that closes the shutter on enter and opens on exit
"""
return ShutterOpener(self.close, self.open)
class Context:
def __init__(self, on_enter, on_exit):
self.on_enter = on_enter
self.on_exit = on_exit
def __enter__(self):
self.on_enter()
return self
def __exit__(self, exc_type, exc_val, exc_tb):
self.on_exit()
# subclasses such that returned objects have meaningful names
# if opened/closed are called directly, i.e., outside with
class ShutterCloser(Context):
pass
class ShutterOpener(Context):
pass