11from textwrap import indent
22
33from collections import defaultdict
4+ from contextlib import contextmanager
45
56import numpy as np
67
1314from glue .core .exceptions import IncompatibleAttribute
1415from 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+
3062class 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 """
0 commit comments