Skip to content

Commit

Permalink
add pin.pin.use_strict() to enable strict pin value getting mode (#241
Browse files Browse the repository at this point in the history
)
  • Loading branch information
wang0618 committed Dec 22, 2021
1 parent fae206b commit 4fd622b
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 4 deletions.
23 changes: 21 additions & 2 deletions pywebio/pin.py
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,7 @@

def check_name(name):
assert all(i in _html_value_chars for i in name), "pin `name` can only contain letters, digits and underscore"
assert name != 'use_strict', "'use_strict' is a reserve name, can't use as pin widget name"
assert name[0] in string.ascii_letters, "pin `name` can only starts with letters"


Expand Down Expand Up @@ -231,13 +232,28 @@ def get_client_val():
return res['data']


@chose_impl
def get_pin_value(name, strict):
send_msg('pin_value', spec=dict(name=name))
data = yield get_client_val()
assert not strict or data, 'pin widget "%s" doesn\'t exist.' % name
return (data or {}).get('value')


class Pin_:
_strict = False

def use_strict(self):
"""
Enable strict mode for getting pin widget value.
An AssertionError will be raised when try to get value of pin widgets that are currently not in the page.
"""
self._strict = True

def __getattr__(self, name):
"""__getattr__ is only invoked if the attribute wasn't found the usual ways"""
check_name(name)
send_msg('pin_value', spec=dict(name=name))
return get_client_val()
return get_pin_value(name, self._strict)

def __getitem__(self, name):
return self.__getattr__(name)
Expand All @@ -246,6 +262,9 @@ def __setattr__(self, name, value):
"""
__setattr__ will be invoked regardless of whether the attribute be found
"""
if name == '_strict':
return object.__setattr__(self, name, value)

check_name(name)
send_msg('pin_update', spec=dict(name=name, attributes={"value": value}))

Expand Down
5 changes: 3 additions & 2 deletions webiojs/src/handlers/pin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,9 @@ export class PinHandler implements CommandHandler {

handle_message(msg: Command) {
if (msg.command === 'pin_value') {
let val = GetPinValue(msg.spec.name)
state.CurrentSession.send_message({event: "js_yield", task_id: msg.task_id, data: val});
let val = GetPinValue(msg.spec.name);
let data = val===undefined? null : {value: val};
state.CurrentSession.send_message({event: "js_yield", task_id: msg.task_id, data: data});
} else if (msg.command === 'pin_update') {
PinUpdate(msg.spec.name, msg.spec.attributes);
} else if (msg.command === 'pin_wait') {
Expand Down
2 changes: 2 additions & 0 deletions webiojs/src/models/pin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import {AfterCurrentOutputWidgetShow} from "../handlers/output";
let name2input: { [k: string]: InputItem } = {};

export function GetPinValue(name: string) {
if(!document.contains(name2input[name].element[0]))
return undefined;
return name2input[name].get_value();
}

Expand Down

0 comments on commit 4fd622b

Please sign in to comment.