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
@@ -231,13 +232,28 @@ def get_client_val():
231
232
return res ['data' ]
232
233
233
234
235
+ @chose_impl
236
+ def get_pin_value (name , strict ):
237
+ send_msg ('pin_value' , spec = dict (name = name ))
238
+ data = yield get_client_val ()
239
+ assert not strict or data , 'pin widget "%s" doesn\' t exist.' % name
240
+ return (data or {}).get ('value' )
241
+
242
+
234
243
class Pin_ :
244
+ _strict = False
245
+
246
+ def use_strict (self ):
247
+ """
248
+ Enable strict mode for getting pin widget value.
249
+ An AssertionError will be raised when try to get value of pin widgets that are currently not in the page.
250
+ """
251
+ self ._strict = True
235
252
236
253
def __getattr__ (self , name ):
237
254
"""__getattr__ is only invoked if the attribute wasn't found the usual ways"""
238
255
check_name (name )
239
- send_msg ('pin_value' , spec = dict (name = name ))
240
- return get_client_val ()
256
+ return get_pin_value (name , self ._strict )
241
257
242
258
def __getitem__ (self , name ):
243
259
return self .__getattr__ (name )
@@ -246,6 +262,9 @@ def __setattr__(self, name, value):
246
262
"""
247
263
__setattr__ will be invoked regardless of whether the attribute be found
248
264
"""
265
+ if name == '_strict' :
266
+ return object .__setattr__ (self , name , value )
267
+
249
268
check_name (name )
250
269
send_msg ('pin_update' , spec = dict (name = name , attributes = {"value" : value }))
251
270
0 commit comments