Preview in web application.

This commit is contained in:
Aldo Cortesi 2012-04-29 21:15:02 +12:00
parent 37e880b399
commit 1431b36c4a
9 changed files with 111 additions and 44 deletions

View File

@ -6,6 +6,7 @@ class _Page(tornado.web.RequestHandler):
def render(self, name, **kwargs):
tornado.web.RequestHandler.render(self, name + ".html", **kwargs)
class Index(_Page):
name = "index"
section = "main"
@ -16,8 +17,29 @@ class Index(_Page):
class Preview(_Page):
name = "preview"
section = "main"
SANITY = 1024*1024
def get(self):
self.render(self.name, section=self.section)
spec = self.get_argument("spec", None)
args = dict(
spec = spec,
section = self.section,
syntaxerror = None,
error = None
)
try:
r = rparse.parse(self.application.settings, spec)
except rparse.ParseException, v:
args["syntaxerror"] = str(v)
args["marked"] = v.marked()
return self.render(self.name, **args)
if r.length() > self.SANITY:
error = "Refusing to preview a response of %s bytes. This is for your own good."%r.length()
args["error"] = error
else:
d = utils.DummyRequest()
r.serve(d)
args["output"] = d.getvalue()
self.render(self.name, **args)
class Help(_Page):

4
libpathod/static/jquery-1.7.2.min.js vendored Normal file

File diff suppressed because one or more lines are too long

View File

@ -4,6 +4,7 @@
<meta charset="utf-8">
<title>Pathod</title>
<link href="/static/bootstrap.min.css" rel="stylesheet">
<script src="/static/jquery-1.7.2.min.js"></script>
<style>
body {
padding-top: 45px;
@ -15,7 +16,7 @@
<div class="navbar navbar-fixed-top">
<div class="navbar-inner">
<div class="container">
<a class="brand" href="#">Pathod</a>
<a class="brand" href="/">Pathod</a>
<ul class="nav">
<li {% if section== "main" %} class="active" {% end %}><a href="/">Main</a></li>
<li {% if section== "log" %} class="active" {% end %}><a href="/log">Log</a></li>

View File

@ -1,12 +1,13 @@
{% extends frame.html %}
{% block body %}
<!-- Main hero unit for a primary marketing message or call to action -->
<div class="hero-unit">
<form>
<input>
<a type="submit" class="btn">explain</a>
<form class="well form-search" method="GET" action="/preview">
<input style="width: 30em;" name="spec" class="input-medium search-query">
<input type="submit" class="btn" value="preview">
<a href="#" class="btn">go</a>
</form>
</div>
<script>
$(function(){
console.log("pigs");
});
</script>
{% end %}

View File

@ -0,0 +1,28 @@
{% extends frame.html %}
{% block body %}
<h1>Preview</h1>
{% if syntaxerror %}
<h2> Error: {{ syntaxerror }} </h2>
<pre>{{ marked }}</pre>
{% elif error %}
<h2> Error </h2>
<p style="color: #ff0000">{{ error }}</p>
{% else %}
<h2>Spec:</h2>
<pre>{{ spec }}</pre>
<h2>Response:</h2>
<pre>{{ output }}</pre>
{% end %}
<form class="well form-search" method="GET" action="/preview">
<input style="width: 30em;" name="spec" class="input-medium search-query" value="{{spec}}">
<input type="submit" class="btn" value="preview">
<a href="#" class="btn">go</a>
</form>
{% end %}

View File

@ -1,8 +1,34 @@
import copy, os, re
import copy, os, re, StringIO
import rparse
class AnchorError(Exception): pass
class Sponge:
def __getattr__(self, x):
return Sponge()
def __call__(self, *args, **kwargs):
pass
class DummyRequest:
connection = Sponge()
def __init__(self):
self.buf = []
def write(self, d, callback=None):
self.buf.append(str(d))
if callback:
callback()
def getvalue(self):
return "".join(self.buf)
def finish(self):
return
def parse_anchor_spec(s, settings):
"""
For now, this is very simple, and you can't have an '=' in your regular

View File

@ -42,7 +42,9 @@ class uPages(libpry.AutoTree):
klass = h.handler_class
r = httpserver.HTTPRequest("GET", path)
del r.connection
return klass(a, r)
k = klass(a, r)
k._transforms = []
return k
def test_index(self):
page = self.dummy_page("/")
@ -58,5 +60,4 @@ class uPages(libpry.AutoTree):
tests = [
uApplication(),
uPages()
]

View File

@ -1,29 +1,10 @@
import StringIO, sys, os
import sys, os
import libpry
from libpathod import rparse
from libpathod import rparse, utils
rparse.TESTING = True
class Sponge:
def __getattr__(self, x):
return Sponge()
def __call__(self, *args, **kwargs):
pass
class DummyRequest(StringIO.StringIO):
connection = Sponge()
def write(self, d, callback=None):
StringIO.StringIO.write(self, d)
if callback:
callback()
def finish(self):
return
class uMisc(libpry.AutoTree):
def test_generators(self):
v = rparse.Value.parseString("'val'")[0]
@ -150,7 +131,7 @@ class uMisc(libpry.AutoTree):
assert r.msg.val == "Unknown code"
def test_internal_response(self):
d = DummyRequest()
d = utils.DummyRequest()
s = rparse.InternalResponse(400, "foo")
s.serve(d)
@ -226,6 +207,10 @@ class uparse(libpry.AutoTree):
r = rparse.parse({}, "400:p10,r")
assert ("r", "pause", 10) in r.actions
def test_parse_stress(self):
r = rparse.parse({}, "400:b@100g")
assert r.length()
class uResponse(libpry.AutoTree):
def dummy_response(self):
@ -260,7 +245,7 @@ class uResponse(libpry.AutoTree):
def test_write_values_disconnects(self):
r = self.dummy_response()
s = DummyRequest()
s = utils.DummyRequest()
tst = "foo"*100
r.write_values(s, [tst], [(0, "disconnect")], blocksize=5)
assert not s.getvalue()
@ -269,7 +254,7 @@ class uResponse(libpry.AutoTree):
tst = "foo"*1025
r = rparse.parse({}, "400'msg'")
s = DummyRequest()
s = utils.DummyRequest()
r.write_values(s, [tst], [])
assert s.getvalue() == tst
@ -278,29 +263,29 @@ class uResponse(libpry.AutoTree):
r = rparse.parse({}, "400'msg'")
for i in range(2, 10):
s = DummyRequest()
s = utils.DummyRequest()
r.write_values(s, [tst], [(2, "pause", 0), (1, "pause", 0)], blocksize=i)
assert s.getvalue() == tst
for i in range(2, 10):
s = DummyRequest()
s = utils.DummyRequest()
r.write_values(s, [tst], [(1, "pause", 0)], blocksize=i)
assert s.getvalue() == tst
tst = ["".join(str(i) for i in range(10))]*5
for i in range(2, 10):
s = DummyRequest()
s = utils.DummyRequest()
r.write_values(s, tst[:], [(1, "pause", 0)], blocksize=i)
assert s.getvalue() == "".join(tst)
def test_render(self):
s = DummyRequest()
s = utils.DummyRequest()
r = rparse.parse({}, "400'msg'")
assert r.serve(s)
def test_length(self):
def testlen(x):
s = DummyRequest()
s = utils.DummyRequest()
x.serve(s)
assert x.length() == len(s.getvalue())
testlen(rparse.parse({}, "400'msg'"))

3
todo
View File

@ -1,7 +1,7 @@
0.1:
- Test pause at 0 bytes
- Web status and explain
- setup.py
- API
- Logs, log reset, log retrieval
- Anchor management
@ -11,4 +11,3 @@
- Shortcuts for cookies, auth
- Various SSL errors (expired certs, etc.)
- Caching