summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorblob8108 <blob8108@gmail.com>2014-01-05 13:19:26 +0000
committerblob8108 <spaaam42@gmail.com>2014-01-05 15:50:51 +0000
commitc2065edb2845a3cfb40685cced16f003e694f10e (patch)
tree0c4f34ef310f63ea1cef7d1bf07cd01adf7f9ae7
parent6ae298c8904ff03d014bcf70a26f0dedca949d3b (diff)
downloadblockext-c2065edb2845a3cfb40685cced16f003e694f10e.tar.gz
blockext-c2065edb2845a3cfb40685cced16f003e694f10e.zip
Scratch blocking reporters
-rw-r--r--blockext/__init__.py26
-rw-r--r--blockext/scratch.py5
-rw-r--r--blockext/snap.py2
3 files changed, 30 insertions, 3 deletions
diff --git a/blockext/__init__.py b/blockext/__init__.py
index 74bd7f8..adba2cf 100644
--- a/blockext/__init__.py
+++ b/blockext/__init__.py
@@ -47,6 +47,8 @@ class Blockext(BaseHTTPRequestHandler):
name = "A blockext extension"
port = 8080
+ requests = {}
+
def log_message(self, format, *args):
if isinstance(args[0], str) and args[0].startswith("GET /poll"):
return
@@ -189,6 +191,9 @@ class Block(object):
}
def __init__(self, text, shape, func, blocking=False, hidden=False):
+ if blocking and shape != "command":
+ raise ValueError("only commands can be blocking")
+
self.text = text
self.shape = shape
self.func = func
@@ -209,6 +214,10 @@ 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 "<Block(%r)>" % self.text
@@ -240,16 +249,28 @@ class Block(object):
arg = True if arg == "true" else False if arg == "false" else None
elif shape == "m":
menu_name = arg[2:]
+ # TODO check in menu options?
+ # shape = "d" ?
return arg
def __call__(self, *args):
+ if self.is_blocking:
+ request_id = args[0]
+ args = args[1:]
+ Blockext.requests[request_id] = True
args = [Block.convert_arg(a, t) for (a, t)
in zip(args, self.arg_shapes)]
result = self.func(*args)
result = ("true" if result is True else
"false" if result is False else
"" if result == None else result)
- return 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 ""
@@ -271,7 +292,8 @@ predicate = _shape("predicate")
def run(name="", port=8080):
blocking_reporters = [b.text for b in Blockext.blocks.values()
- if b.shape == "reporter" and b.is_blocking]
+ if b.shape != "command" and
+ not all(shape[0] in "md" for shape in b.arg_shapes)]
if blocking_reporters:
print("WARNING: Scratch 2.0 doesn't support blocking reporters yet.")
print("Affects: " + "\n ".join(blocking_reporters))
diff --git a/blockext/scratch.py b/blockext/scratch.py
index b633a5d..584b2c2 100644
--- a/blockext/scratch.py
+++ b/blockext/scratch.py
@@ -66,7 +66,10 @@ def poll(is_browser=False):
)
return ("text/plain", lines)
-@reporter("", hidden=True)
+@reporter("_problem", hidden=True)
def _problem():
return "The Scratch Sensor board is not connected.\xe2\x80\xa8 Foo."
+@reporter("_busy", hidden=True)
+def _busy():
+ return " ".join(map(str, Blockext.requests))
diff --git a/blockext/snap.py b/blockext/snap.py
index 8230356..da7a4bb 100644
--- a/blockext/snap.py
+++ b/blockext/snap.py
@@ -62,6 +62,8 @@ def generate_s2e():
join_block = SubElement(http_block, "block", s="reportJoinWords")
list_ = SubElement(join_block, "list")
url = "localhost:{port}/{name}".format(port=Blockext.port, name=name)
+ if block.is_blocking:
+ url += "/-" # Blank request id
SubElement(list_, "l").text = url
input_names = inspect.getargspec(block.func).args
for name in input_names: