Skip to content

Commit

Permalink
setting autosave=False in PV overrides context manager
Browse files Browse the repository at this point in the history
  • Loading branch information
jsouter committed Sep 11, 2024
1 parent eac52e1 commit 6341098
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 11 deletions.
15 changes: 10 additions & 5 deletions docs/reference/api.rst
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,9 @@ and stderr streams, is sent directly to the terminal.
the context manager added to autosave tracking. The options for ``autosave``
are identical to the ones described in the builder keyword argument
:ref:`autosave`. If a PV already has :ref:`autosave` set, the two lists of fields
get combined into a single set.
get combined into a single set. If the PV's :ref:`autosave` keyword is set
explicitly to ``False``, the fields specified in the context manager's argument
are not tracked.



Expand Down Expand Up @@ -338,8 +340,8 @@ and stderr streams, is sent directly to the terminal.
~~~~~~~~~~~~~~~

Available on all record types.
Resoles to a list of string field names, when not empty it marks the record
fields for automatic periodic backing up to a file. Set to `False` by
Resolves to a list of string field names. When not empty it marks the record
fields for automatic periodic backing up to a file. Set to `None` by
default. When the IOC is restarted and a backup file exists, the saved values are
loaded from this file when :func:`~softioc.builder.LoadDatabase` is called.
The saved values takes priority over any initial field value passed to the PV
Expand All @@ -349,13 +351,16 @@ and stderr streams, is sent directly to the terminal.
The options for the argument are:

* ``True``, which is equivalent to ``["VAL"]``
* ``False``, which is equivalent to ``[]`` and disables autosave tracking for the PV
* ``False``, which is equivalent to ``[]`` and disables all autosave tracking for the PV, even inside an :class:`~softioc.autosave.Autosave` context manager
* ``None``, similar to ``False`` but does not overload any fields specified in an :class:`~softioc.autosave.Autosave` context manager
* A list of field names such as ``["VAL", "LOPR", "HOPR"]``, note that ``"VAL"`` must be explicitly provided
* A single field name such as ``"EGU"`` which is equivalent to passing ``["EGU"]``.
* A single field name such as ``"EGU"`` which is equivalent to passing ``["EGU"]``

.. seealso::
:func:`~softioc.autosave.configure` for discussion on how to configure saving.

:class:`~softioc.autosave.Autosave` for how to track PVs with autosave inside a context manager.


For all of these functions any EPICS database field can be assigned a value by
passing it as a keyword argument for the corresponding field name (in upper
Expand Down
9 changes: 6 additions & 3 deletions softioc/autosave.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ def _shutdown_autosave_thread(worker):


def _parse_autosave_fields(fields):
if fields is False:
if not fields:
return []
elif fields is True:
return ["VAL"]
Expand All @@ -107,9 +107,12 @@ def add_pv_to_autosave(pv, name, fields):
created inside an Autosave context manager, the fields passed to the
context manager are also tracked by autosave.
"""
context = _AutosaveContext()
if fields is False:
# if autosave=False explicitly set, override context manager
return
fields = set(_parse_autosave_fields(fields))
# instantiate to get thread local class variables via instance
# instantiate context to get thread local class variables via instance
context = _AutosaveContext()
if context._in_cm: # _fields should always be a list if in context manager
fields.update(context._fields)
for field in fields:
Expand Down
2 changes: 1 addition & 1 deletion softioc/device.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ class ProcessDeviceSupportCore(DeviceSupportCore, RecordLookup):

# all record types can support autosave
def __init__(self, name, **kargs):
autosave_fields = kargs.pop("autosave", False)
autosave_fields = kargs.pop("autosave", None)
autosave.add_pv_to_autosave(self, name, autosave_fields)
self.__super.__init__(name, **kargs)

Expand Down
4 changes: 2 additions & 2 deletions tests/test_autosave.py
Original file line number Diff line number Diff line change
Expand Up @@ -471,7 +471,7 @@ def test_autosave_arguments(tmp_path):
builder.aOut("AUTO-TRUE", autosave=False)
builder.aOut("FIELDS", autosave=["LOPR", "HOPR"])
assert set(autosave.Autosave._pvs) == {
"AUTO-TRUE", "FIELDS", "FIELDS.LOPR", "FIELDS.HOPR"}
"FIELDS", "FIELDS.LOPR", "FIELDS.HOPR"}
autosave.Autosave._pvs = {}
builder.ClearRecords()
with autosave.Autosave(["EGU"]):
Expand All @@ -482,7 +482,7 @@ def test_autosave_arguments(tmp_path):
autosave.Autosave._pvs = {}
builder.ClearRecords()
with autosave.Autosave(False):
builder.aOut("AUTO-FALSE")
builder.aOut("AUTO-DEFAULT")
builder.aOut("AUTO-TRUE", autosave=True)
assert set(autosave.Autosave._pvs) == {"AUTO-TRUE"}
autosave.Autosave._pvs = {}
Expand Down

0 comments on commit 6341098

Please sign in to comment.