diff options
| author | Ulf Magnusson <ulfalizer@gmail.com> | 2017-11-13 09:47:23 +0100 |
|---|---|---|
| committer | Ulf Magnusson <ulfalizer@gmail.com> | 2017-11-13 10:16:01 +0100 |
| commit | 9853e3c89cf37bba99aaa6318b6dc0f023caa2f3 (patch) | |
| tree | cc226e4f0a65f6fb0ed6cf8503fd98f2a93edc5d | |
| parent | e4518a4c41b9420e129b658c54febdf42f44e68b (diff) | |
Add a Choice._get_selection() helper
Similar to _get_assignable(). Cleaner than setting the cached value at
every 'return'.
| -rw-r--r-- | kconfiglib.py | 56 |
1 files changed, 29 insertions, 27 deletions
diff --git a/kconfiglib.py b/kconfiglib.py index 883cc33..3c9eedb 100644 --- a/kconfiglib.py +++ b/kconfiglib.py @@ -3086,33 +3086,8 @@ class Choice(object): if self._cached_selection is not _NO_CACHED_SELECTION: return self._cached_selection - # Warning: See Symbol._rec_invalidate(), and note that this is a hidden - # function call (property magic) - if self.tri_value != 2: - self._cached_selection = None - return None - - # Use the user selection if it's visible - if self.user_selection and self.user_selection.visibility == 2: - self._cached_selection = self.user_selection - return self.user_selection - - # Otherwise, check if we have a default - for sym, cond in self.defaults: - # The default symbol must be visible too - if expr_value(cond) and sym.visibility: - self._cached_selection = sym - return sym - - # Otherwise, pick the first visible symbol, if any - for sym in self.syms: - if sym.visibility: - self._cached_selection = sym - return sym - - # Couldn't find a selection - self._cached_selection = None - return None + self._cached_selection = self._get_selection() + return self._cached_selection def set_value(self, value): """ @@ -3263,6 +3238,33 @@ class Choice(object): return (0, 1) if self.is_optional else (1,) + def _get_selection(self): + """ + Worker function for the 'selection' attribute. + """ + # Warning: See Symbol._rec_invalidate(), and note that this is a hidden + # function call (property magic) + if self.tri_value != 2: + return None + + # Use the user selection if it's visible + if self.user_selection and self.user_selection.visibility == 2: + return self.user_selection + + # Otherwise, check if we have a default + for sym, cond in self.defaults: + # The default symbol must be visible too + if expr_value(cond) and sym.visibility: + return sym + + # Otherwise, pick the first visible symbol, if any + for sym in self.syms: + if sym.visibility: + return sym + + # Couldn't find a selection + return None + def _invalidate(self): self._cached_vis = self._cached_assignable = None self._cached_selection = _NO_CACHED_SELECTION |
