8
8
9
9
10
10
class OptionInfo :
11
- def __init__ (self , default = None , label = "" , component = None , component_args = None , onchange = None , section = None , refresh = None , comment_before = '' , comment_after = '' , infotext = None ):
11
+ def __init__ (self , default = None , label = "" , component = None , component_args = None , onchange = None , section = None , refresh = None , comment_before = '' , comment_after = '' , infotext = None , restrict_api = False ):
12
12
self .default = default
13
13
self .label = label
14
14
self .component = component
@@ -26,6 +26,9 @@ def __init__(self, default=None, label="", component=None, component_args=None,
26
26
27
27
self .infotext = infotext
28
28
29
+ self .restrict_api = restrict_api
30
+ """If True, the setting will not be accessible via API"""
31
+
29
32
def link (self , label , url ):
30
33
self .comment_before += f"[<a href='{ url } ' target='_blank'>{ label } </a>]"
31
34
return self
@@ -71,7 +74,7 @@ def options_section(section_identifier, options_dict):
71
74
class Options :
72
75
typemap = {int : float }
73
76
74
- def __init__ (self , data_labels , restricted_opts ):
77
+ def __init__ (self , data_labels : dict [ str , OptionInfo ] , restricted_opts ):
75
78
self .data_labels = data_labels
76
79
self .data = {k : v .default for k , v in self .data_labels .items ()}
77
80
self .restricted_opts = restricted_opts
@@ -113,24 +116,28 @@ def __getattr__(self, item):
113
116
114
117
return super (Options , self ).__getattribute__ (item )
115
118
116
- def set (self , key , value ):
119
+ def set (self , key , value , is_api = False ):
117
120
"""sets an option and calls its onchange callback, returning True if the option changed and False otherwise"""
118
121
119
122
oldval = self .data .get (key , None )
120
123
if oldval == value :
121
124
return False
122
125
123
- if self .data_labels [key ].do_not_save :
126
+ option = self .data_labels [key ]
127
+ if option .do_not_save :
128
+ return False
129
+
130
+ if is_api and option .restrict_api :
124
131
return False
125
132
126
133
try :
127
134
setattr (self , key , value )
128
135
except RuntimeError :
129
136
return False
130
137
131
- if self . data_labels [ key ] .onchange is not None :
138
+ if option .onchange is not None :
132
139
try :
133
- self . data_labels [ key ] .onchange ()
140
+ option .onchange ()
134
141
except Exception as e :
135
142
errors .display (e , f"changing setting { key } to { value } " )
136
143
setattr (self , key , oldval )
0 commit comments