Skip to content

Commit bf65166

Browse files
authored
Merge pull request #2544 from astrofrog/override-defaults
Allow initial values to be customized for state classes
2 parents 0c61267 + 84d583f commit bf65166

File tree

2 files changed

+47
-1
lines changed

2 files changed

+47
-1
lines changed

glue/core/state_objects.py

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
from textwrap import indent
22

33
from collections import defaultdict
4+
from contextlib import contextmanager
45

56
import numpy as np
67

@@ -13,6 +14,7 @@
1314
from glue.core.exceptions import IncompatibleAttribute
1415
from glue.core.units import UnitConverter
1516

17+
1618
__all__ = ['State', 'StateAttributeCacheHelper',
1719
'StateAttributeLimitsHelper', 'StateAttributeSingleValueHelper', 'StateAttributeHistogramHelper']
1820

@@ -27,6 +29,36 @@ def _load_callback_list(rec, context):
2729
return [context.object(obj) for obj in rec['values']]
2830

2931

32+
_state_default_picker = None
33+
34+
35+
def set_state_default_picker(func):
36+
"""
37+
This function can be used to set a function that will be used
38+
to override default values in state classes.
39+
40+
The function will be called whenever a state class is being
41+
initialized, and will be given the instance of the class being
42+
created. The function is then free to modify any of the callback
43+
properties, before any of the callbacks are set up.
44+
"""
45+
46+
global _state_default_picker
47+
_state_default_picker = func
48+
49+
50+
@contextmanager
51+
def with_state_default_picker(func):
52+
"""
53+
Context manager version of `set_state_default_picker`.
54+
"""
55+
56+
global _state_default_picker
57+
_state_default_picker = func
58+
yield
59+
_state_default_picker = None
60+
61+
3062
class State(HasCallbackProperties):
3163
"""
3264
A class to represent the state of a UI element. Initially this doesn't add
@@ -37,6 +69,8 @@ class State(HasCallbackProperties):
3769
def __init__(self, **kwargs):
3870
super(State, self).__init__()
3971
self.update_from_dict(kwargs)
72+
if _state_default_picker is not None:
73+
_state_default_picker(self)
4074

4175
def update_from_state(self, state):
4276
"""

glue/core/tests/test_state_objects.py

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@
88
from .test_state import clone
99
from ..state_objects import (State, StateAttributeLimitsHelper,
1010
StateAttributeSingleValueHelper,
11-
StateAttributeHistogramHelper)
11+
StateAttributeHistogramHelper,
12+
with_state_default_picker)
1213

1314

1415
class SimpleTestState(State):
@@ -532,3 +533,14 @@ class SimpleState(State):
532533
percentile='scale')
533534

534535
state.scale = 90
536+
537+
538+
def default_picker(state):
539+
if isinstance(state, SimpleTestState):
540+
state.a = 5
541+
542+
543+
def test_set_state_default_picker():
544+
with with_state_default_picker(default_picker):
545+
state = SimpleTestState()
546+
assert state.a == 5

0 commit comments

Comments
 (0)