diff options
| author | blob8108 <blob8108@gmail.com> | 2014-01-07 10:57:43 +0000 |
|---|---|---|
| committer | blob8108 <blob8108@gmail.com> | 2014-01-07 10:57:43 +0000 |
| commit | eb25dfb720b6cae0e806eb230c01e18ba7f0f1cb (patch) | |
| tree | df65fe85b18820bce0cfe1835ca7e22b4a890c0b | |
| parent | a35e39e2af6cd1ab29cb8cc51718f6dbd4464e8e (diff) | |
| download | blockext-eb25dfb720b6cae0e806eb230c01e18ba7f0f1cb.tar.gz blockext-eb25dfb720b6cae0e806eb230c01e18ba7f0f1cb.zip | |
Make Snap! predicates actually return <true> and <false>
Fixes #8
| -rw-r--r-- | blockext/snap.py | 79 |
1 files changed, 70 insertions, 9 deletions
diff --git a/blockext/snap.py b/blockext/snap.py index b18a9c0..ba4f56e 100644 --- a/blockext/snap.py +++ b/blockext/snap.py @@ -74,17 +74,78 @@ def generate_s2e(): encode = SubElement(list_, "block", s="reportTextFunction") l = SubElement(encode, "l") SubElement(l, "option").text = "encode URI component" - SubElement(encode, "block", var=name) + join = SubElement(encode, "block", s="reportJoinWords") + SubElement(join, "block", var=name) - script = SubElement(defn, "script") if block.shape == "command": - selector = "doRun" if block.is_blocking else "fork" - run = SubElement(script, "block", s=selector) - ring = SubElement(run, "block", s="reifyReporter") - lambda_ = SubElement(ring, "autolambda") - lambda_.append(http_block) - else: # reporter, predicate - SubElement(script, "block", s="doReport").append(http_block) + script_xml = """ + <script> + <block s="{selector}"> + <block s="reifyReporter"> + <autolambda> + {http_block_xml} + </autolambda> + </block> + </block> + </script> + """.format( + selector="doRun" if block.is_blocking else "fork", + http_block_xml="{http_block_xml}", + ) + elif block.shape == "predicate": + script_xml = """ + <script> + <block s="doDeclareVariables"> + <list> + <l>result</l> + </list> + </block> + <block s="doSetVar"> + <l>result</l> + {http_block_xml} + </block> + <block s="doIf"> + <block s="reportEquals"> + <block var="result"/> + <l>true</l> + </block> + <script> + <block s="doSetVar"> + <l>result</l> + <block s="reportTrue"/> + </block> + </script> + </block> + <block s="doIf"> + <block s="reportEquals"> + <block var="result"/> + <l>false</l> + </block> + <script> + <block s="doSetVar"> + <l>result</l> + <block s="reportFalse"/> + </block> + </script> + </block> + <block s="doReport"> + <block var="result"/> + </block> + </script> + """ + elif block.shape == "reporter": + script_xml = """ + <script> + <block s="doReport"> + {http_block_xml} + </block> + </script> + """ + + script = ElementTree.fromstring(script_xml.format( + http_block_xml=ElementTree.tostring(http_block), + )) + defn.append(script) # It's useful to change this to "application/xml" while debugging. return ("application/octet-stream", ElementTree.tostring(root)) |
