135
135
136
136
def check_name (name ):
137
137
assert all (i in _html_value_chars for i in name ), "pin `name` can only contain letters, digits and underscore"
138
+ assert name != 'use_strict' , "'use_strict' is a reserve name, can't use as pin widget name"
138
139
assert name [0 ] in string .ascii_letters , "pin `name` can only starts with letters"
139
140
140
141
@@ -224,20 +225,30 @@ def put_actions(name, *, label='', buttons=None, help_text=None,
224
225
225
226
226
227
@chose_impl
227
- def get_client_val ():
228
+ def get_client_val (strict = False ):
228
229
res = yield next_client_event ()
229
230
assert res ['event' ] == 'js_yield' , "Internal Error, please report this bug on " \
230
231
"https://github.com/wang0618/PyWebIO/issues"
231
- return res ['data' ]
232
+ data = res ['data' ]
233
+ assert not strict or data , 'pin widget doesn\' t exist.'
234
+ return (data or {}).get ('value' )
232
235
233
236
234
237
class Pin_ :
238
+ _strict = False
239
+
240
+ def use_strict (self ):
241
+ """
242
+ Enable strict mode for getting pin widget value.
243
+ An AssertionError will be raised when try to get value of pin widgets that are currently not in the page.
244
+ """
245
+ self ._strict = True
235
246
236
247
def __getattr__ (self , name ):
237
248
"""__getattr__ is only invoked if the attribute wasn't found the usual ways"""
238
249
check_name (name )
239
250
send_msg ('pin_value' , spec = dict (name = name ))
240
- return get_client_val ()
251
+ return get_client_val (self . _strict )
241
252
242
253
def __getitem__ (self , name ):
243
254
return self .__getattr__ (name )
@@ -246,6 +257,9 @@ def __setattr__(self, name, value):
246
257
"""
247
258
__setattr__ will be invoked regardless of whether the attribute be found
248
259
"""
260
+ if name == '_strict' :
261
+ return object .__setattr__ (self , name , value )
262
+
249
263
check_name (name )
250
264
send_msg ('pin_update' , spec = dict (name = name , attributes = {"value" : value }))
251
265
0 commit comments