diff options
| author | blob8108 <spaaam42@gmail.com> | 2014-03-28 22:12:52 +0000 |
|---|---|---|
| committer | blob8108 <spaaam42@gmail.com> | 2014-03-28 22:12:52 +0000 |
| commit | 70d9d041236f36b9589c25e6f4e83af03263dd99 (patch) | |
| tree | 3d0df7020ff2db332adcb58b4671ef88031ae72c | |
| parent | a5e2a25139a03fece22a1447fc37c756154235dc (diff) | |
| download | blockext-70d9d041236f36b9589c25e6f4e83af03263dd99.tar.gz blockext-70d9d041236f36b9589c25e6f4e83af03263dd99.zip | |
Fix Unicode handling & investigate Scratch polling
| -rw-r--r-- | blockext/__init__.py | 16 | ||||
| -rw-r--r-- | blockext/scratch.py | 15 |
2 files changed, 23 insertions, 8 deletions
diff --git a/blockext/__init__.py b/blockext/__init__.py index aebbb03..79338bd 100644 --- a/blockext/__init__.py +++ b/blockext/__init__.py @@ -33,6 +33,11 @@ except NameError: unicode = str unichr = chr +def make_unicode(text): + """Fix strings passed in from extension code.""" + if isinstance(text, bytes): + text = text.decode("utf-8") + return unicode(text) def unquote_unicode(text): def unicode_unquoter(match): @@ -103,11 +108,6 @@ class Blockext(BaseHTTPRequestHandler): response = "ERROR: Not found" status = 404 - if isinstance(response, bytes): - response = response.decode("utf-8") - else: - response = unicode(response) - if is_browser and mime_type == "text/plain": # Some browsers seem to hate plain text. response = u"""<!DOCTYPE html> @@ -147,7 +147,7 @@ def handler(name, **kwargs): def menu(name, values): - Blockext.menus[name] = list(map(unicode, values)) + Blockext.menus[name] = list(map(make_unicode, values)) @handler("", hidden=True) @@ -246,7 +246,7 @@ class Block(object): if blocking and shape != "command": raise ValueError("only commands can be blocking") - self._text = text + self._text = make_unicode(text) self.shape = shape self.func = func self.is_blocking = blocking @@ -327,7 +327,7 @@ class Block(object): if self.shape == "command": result = None result = ("true" if result is True else "false" if result is False else - "" if result == None else unicode(result)) + "" if result == None else make_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 b4440ec..862bfc9 100644 --- a/blockext/scratch.py +++ b/blockext/scratch.py @@ -5,6 +5,17 @@ import json from blockext import * +def quote_unicode(text): + """Scratch expects non-standard unicode URL encoding.""" + def unicode_quoter(c): + v = ord(c) + if v < 128: + return quote(c) + else: + if v >= 65536: raise ValueError + return "%u" + hex(v)[:2] + return b"".join(map(unicode_quoter, text)) + CROSSDOMAIN_XML = """ <?xml version="1.0"?> @@ -66,6 +77,10 @@ def poll(is_browser=False): lines += "{path} {result}\n".format( path="/".join([name] + args), result=block(*args).replace("\n", " "), + # TODO Scratch's url-decoding is buggy. + #path="/".join(quote_unicode(p).replace("/", "%2F") + # for p in [name] + args) + #result=quote_unicode(block(*args).replace("\n", " ")), ) return ("text/plain", lines) |
