From efea8b4700749b1a3d985f42e30442c5e49c7271 Mon Sep 17 00:00:00 2001 From: blob8108 Date: Tue, 7 Jan 2014 01:25:17 +0000 Subject: Make `problem` and `reset` accessible in Snap! too Fixes #7 --- blockext/__init__.py | 23 +++++++++++++++++++++++ blockext/scratch.py | 14 +------------- example.py | 3 ++- 3 files changed, 26 insertions(+), 14 deletions(-) diff --git a/blockext/__init__.py b/blockext/__init__.py index c1779a5..86b6e74 100644 --- a/blockext/__init__.py +++ b/blockext/__init__.py @@ -321,6 +321,29 @@ reporter = _shape("reporter") predicate = _shape("predicate") +# Decorators (for special-in-Scratch features) + +def problem(func): + @predicate("{name} is working?") + def _no_problem(): + return not func() + + @reporter("problem with {name}") + def _problem(): + return func() + + return _problem + +problem(lambda: None) + +def reset(func): + @command("reset {name}") + def reset_all(): + func() + + return reset_all + + def run(name="", filename="extension", port=8080): blocking_reporters = [b.text for b in Blockext.blocks.values() if b.shape != "command" and diff --git a/blockext/scratch.py b/blockext/scratch.py index 402ccb0..ace80c8 100644 --- a/blockext/scratch.py +++ b/blockext/scratch.py @@ -33,6 +33,7 @@ def generate_s2e(): } for name, block in Blockext.blocks.items(): if block.is_hidden: continue + if name in ("_problem", "_no_problem", "reset_all"): continue shape = BLOCK_SHAPES[block.shape] if block.shape == "command" and block.is_blocking: shape = "w" @@ -71,16 +72,3 @@ def poll(is_browser=False): @reporter("_busy", hidden=True) def _busy(): return " ".join(map(str, Blockext.requests)) - -# Decorators - -def problem(func): - @reporter("_problem", hidden=True) - def _problem(): - return func() - -def reset(func): - @command("reset_all", hidden=True) - def reset_all(): - func() - diff --git a/example.py b/example.py index e066c05..baff568 100644 --- a/example.py +++ b/example.py @@ -36,7 +36,8 @@ def id(text): @problem def my_problem(): - return "The Scratch Sensor board is not connected. Foo." + if time.time() % 8 > 4: + return "The Scratch Sensor board is not connected. Foo." @reset def my_reset(): -- cgit v1.3.1