Skip to content

Commit

Permalink
Fix for enthought#302 part 2
Browse files Browse the repository at this point in the history
Make sure ignore, first, last are propagated to nested set() calls
  • Loading branch information
johannesloibl committed Jun 29, 2022
1 parent c688806 commit c752239
Showing 1 changed file with 34 additions and 7 deletions.
41 changes: 34 additions & 7 deletions apptools/persistence/state_pickler.py
Original file line number Diff line number Diff line change
Expand Up @@ -694,7 +694,31 @@ class StateSetter:
that when it sets the state.
"""

def __init__(self):
def __init__(self, ignore=None, first=None, last=None):
"""Sets the state of the object.
Parameters
----------
- ignore : `list(str)`
The list of attributes specified in this list are ignored
and the state of these attributes are not set (this excludes
the ones specified in `first` and `last`). If one specifies
a '*' then all attributes are ignored except the ones
specified in `first` and `last`.
- first : `list(str)`
The list of attributes specified in this list are set first (in
order), before any other attributes are set.
- last : `list(str)`
The list of attributes specified in this list are set last (in
order), after all other attributes are set.
"""
# Stores the ids of instances already done.
self._instance_ids = []
self.type_map = {
Expand All @@ -703,6 +727,9 @@ def __init__(self):
StateList: self._do_list,
StateDict: self._do_dict,
}
self.ignore = ignore
self.first = first
self.last = last

def set(self, obj, state, ignore=None, first=None, last=None):
"""Sets the state of the object.
Expand Down Expand Up @@ -757,15 +784,15 @@ def set(self, obj, state, ignore=None, first=None, last=None):

self._register(obj)

# This wierdness is needed since the state's own `keys` might
# This weirdness is needed since the state's own `keys` might
# be set to something else.
state_keys = list(dict.keys(state))
state_keys.remove("__metadata__")

if first is None:
first = []
if last is None:
last = []
# fallback to instance default
first = first or self.first or []
last = last or self.last or []
ignore = ignore or self.ignore or []

# Remove all the ignored keys.
if ignore:
Expand Down Expand Up @@ -998,7 +1025,7 @@ def get_state(obj):


def set_state(obj, state, ignore=None, first=None, last=None):
StateSetter().set(obj, state, ignore, first, last)
StateSetter(ignore, first, last).set(obj, state, ignore, first, last)


set_state.__doc__ = StateSetter.set.__doc__
Expand Down

0 comments on commit c752239

Please sign in to comment.