From 457674c8dc0a070d63e7992fb1b69694418e6f3a Mon Sep 17 00:00:00 2001 From: blob8108 Date: Sun, 5 Jan 2014 17:06:21 +0000 Subject: Fix blocking reporters --- blockext/__init__.py | 28 +++++++++++++--------------- blockext/scratch.py | 6 ++++-- blockext/snap.py | 2 ++ example.py | 4 ++-- 4 files changed, 21 insertions(+), 19 deletions(-) diff --git a/blockext/__init__.py b/blockext/__init__.py index adba2cf..3c4a028 100644 --- a/blockext/__init__.py +++ b/blockext/__init__.py @@ -135,6 +135,7 @@ def index(is_browser=False): body { font-family: sans-serif; } a { color: #20e; text-decoration: none; } a:hover { text-decoration: underline; } + ul { padding-left: 1em; } """ @@ -155,12 +156,15 @@ def index(is_browser=False): for name in sorted(Blockext.blocks): block = Blockext.blocks[name] if block.is_hidden: continue + parts = [name] + block.defaults + if block.is_blocking: parts.insert(1, "-") html += '
  • {text}'.format( - path=escape("/".join(map(unicode, - [name] + block.defaults))), - text=escape(unicode(block)), + path=escape("/".join(map(unicode, parts))), + text=escape(unicode(block) + + (" *" if block.is_blocking else "")), ) html += "" + html += "* = blocking" return ("text/html", html) else: @@ -214,10 +218,6 @@ class Block(object): shape_defaults = map(Block.INPUT_DEFAULTS.get, self.arg_shapes) self.defaults = [a or b for (a, b) in zip(defaults, shape_defaults)] - if (not all(shape[0] in "md" for shape in self.arg_shapes) and - shape != "reporter"): - self.is_blocking = True - def __repr__(self): return "" % self.text @@ -254,6 +254,7 @@ class Block(object): return arg def __call__(self, *args): + request_id = None if self.is_blocking: request_id = args[0] args = args[1:] @@ -261,16 +262,13 @@ class Block(object): args = [Block.convert_arg(a, t) for (a, t) in zip(args, self.arg_shapes)] result = self.func(*args) + if self.shape == "command": result = None result = ("true" if result is True else "false" if result is False else - "" if result == None else result) - if self.is_blocking: - if request_id in Blockext.requests: - del Blockext.requests[request_id] - if self.shape in ("reporter", "predicate"): - return result - else: - return "" + "" if result == None else unicode(result)) + if request_id and request_id in Blockext.requests: + del Blockext.requests[request_id] + return result diff --git a/blockext/scratch.py b/blockext/scratch.py index 584b2c2..e296e8e 100644 --- a/blockext/scratch.py +++ b/blockext/scratch.py @@ -1,3 +1,5 @@ +from __future__ import unicode_literals + import json from blockext import * @@ -62,13 +64,13 @@ def poll(is_browser=False): for args in menu_permutations(block.arg_shapes): lines += "{path} {result}\n".format( path="/".join([name] + args), - result=block(*args) + result=block(*args).replace("\n", " "), ) return ("text/plain", lines) @reporter("_problem", hidden=True) def _problem(): - return "The Scratch Sensor board is not connected.\xe2\x80\xa8 Foo." + return "The Scratch Sensor board is not connected.\n Foo." @reporter("_busy", hidden=True) def _busy(): diff --git a/blockext/snap.py b/blockext/snap.py index da7a4bb..2c3eb33 100644 --- a/blockext/snap.py +++ b/blockext/snap.py @@ -1,3 +1,5 @@ +from __future__ import unicode_literals + from xml.etree import ElementTree from xml.etree.ElementTree import Element, SubElement diff --git a/example.py b/example.py index 07838cf..2a6adc1 100644 --- a/example.py +++ b/example.py @@ -8,14 +8,14 @@ from blockext import * def not_(value): return not value -@command("say %s for %n secs") +@command("say %s for %n secs", blocking=True) def say_for_secs(text="Hello", duration=5): import time print(text) time.sleep(duration) -@command("play note %n", blocking=True) +@command("play note %n") def play_note(note): print("DING {note}".format(note=note)) time.sleep(2) -- cgit v1.3.1