Skip to content

Commit ad9a395

Browse files
committed
Add new argument on_update_name as alternative to on_update
This addresses the problem of returning a context to the caller, as raised in Jira CGP-181.
1 parent 944e072 commit ad9a395

File tree

5 files changed

+27
-3
lines changed

5 files changed

+27
-3
lines changed

docs/records.rst

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,12 @@ specified:
101101
one callback is dispatched at a time, so if a callback blocks it will delay
102102
`on_update` callbacks for other records.
103103

104+
`on_update_name`
105+
This is an alternative form of `on_update` with the same behaviour: note
106+
that at most one of `on_update` and `on_update_name` may be passed. The
107+
difference is that `on_update_name` is called with the record name as its
108+
second argument after the value as the first argument.
109+
104110
`validate`
105111
If used this should be set to a callable taking two arguments. The first
106112
argument will be the record object, and the second will be the new value

docs/softioc.rst

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -222,6 +222,10 @@ functions:
222222
:func:`WaveformOut`). This specifies a function that will be called after
223223
record processing has completed.
224224

225+
`on_update_name`
226+
This is an alternative callback function to use instead of `on_update`.
227+
This function will be passed the record name as well as updated value.
228+
225229
`validate`
226230
Also only available on OUT records, specifies a function called during
227231
record processing. Note that this function is not cothread safe, that is to

example/testing.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111
def on_update(value):
1212
print('on_update', repr(value))
1313

14+
def on_update_name(value, name):
15+
print('on_update', name, ':', repr(value))
1416

1517
t_ai = aIn('AI')
1618
t_boolin = boolIn('BOOLIN', 'True', 'False')
@@ -19,7 +21,7 @@ def on_update(value):
1921
t_mbbi = mbbIn('MBBI', 'One', 'Two', 'Three')
2022

2123
t_ao = aOut ('AO',
22-
initial_value = 12.45, on_update = on_update)
24+
initial_value = 12.45, on_update_name = on_update_name)
2325
t_boolout = boolOut ('BOOLOUT', 'Zero', 'One',
2426
initial_value = True, on_update = on_update)
2527
t_longout = longOut ('LONGOUT',

python/softioc/device.py

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,18 @@ class ProcessDeviceSupportOut(ProcessDeviceSupportCore):
8686
__Callback = cothread.cothread._Callback()
8787

8888
def __init__(self, name, **kargs):
89-
self.__on_update = kargs.pop('on_update', None)
89+
on_update = kargs.pop('on_update', None)
90+
on_update_name = kargs.pop('on_update_name', None)
91+
# At most one of on_update and on_update_name can be specified
92+
assert on_update is None or on_update_name is None, \
93+
'Cannot specify on_update and on_update_name together'
94+
if on_update:
95+
self.__on_update = on_update
96+
elif on_update_name:
97+
self.__on_update = lambda value: on_update_name(value, name)
98+
else:
99+
self.__on_update = None
100+
90101
self.__validate = kargs.pop('validate', None)
91102
self.__always_update = kargs.pop('always_update', False)
92103
self._value = kargs.pop('initial_value', None)

python/softioc/pythonSoftIoc.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,8 @@ def __init__(self, builder, device, name, **fields):
2323
# remaining arguments are passed to the builder. It's a shame we
2424
# have to maintain this separately from the corresponding device list.
2525
DeviceKeywords = [
26-
'on_update', 'validate', 'initial_value', 'always_update']
26+
'on_update', 'on_update_name', 'validate',
27+
'initial_value', 'always_update']
2728
device_kargs = {}
2829
for keyword in DeviceKeywords:
2930
if keyword in fields:

0 commit comments

Comments
 (0)