Change preview_safe to return a safe copy of the current message.

This commit is contained in:
Aldo Cortesi 2012-10-30 13:36:32 +13:00
parent a1f782b543
commit 677f0e0580
5 changed files with 13 additions and 19 deletions

View File

@ -108,7 +108,6 @@ def _preview(is_request):
section = "main", section = "main",
syntaxerror = None, syntaxerror = None,
error = None, error = None,
pauses = None
) )
if not spec.strip(): if not spec.strip():
args["error"] = "Can't parse an empty spec." args["error"] = "Can't parse an empty spec."
@ -125,16 +124,16 @@ def _preview(is_request):
return render(template, False, **args) return render(template, False, **args)
s = cStringIO.StringIO() s = cStringIO.StringIO()
args["pauses"] = r.preview_safe() safe = r.preview_safe()
c = app.config["pathod"].check_policy(r, app.config["pathod"].request_settings) c = app.config["pathod"].check_policy(safe, app.config["pathod"].request_settings)
if c: if c:
args["error"] = c args["error"] = c
return render(template, False, **args) return render(template, False, **args)
if is_request: if is_request:
r.serve(s, app.config["pathod"].request_settings, host="example.com") safe.serve(s, app.config["pathod"].request_settings, host="example.com")
else: else:
r.serve(s, app.config["pathod"].request_settings) safe.serve(s, app.config["pathod"].request_settings)
args["output"] = utils.escape_unprintables(s.getvalue()) args["output"] = utils.escape_unprintables(s.getvalue())
return render(template, False, **args) return render(template, False, **args)

View File

@ -167,7 +167,7 @@ class FileGenerator:
class _Token(object): class _Token(object):
""" """
A specification token. A specification token. Tokens are immutable.
""" """
__metaclass__ = abc.ABCMeta __metaclass__ = abc.ABCMeta
@abc.abstractmethod @abc.abstractmethod
@ -646,11 +646,10 @@ class _Message(object):
def preview_safe(self): def preview_safe(self):
""" """
Modify this message to be safe for previews. Returns a list of elided actions. Return a copy of this message that issafe for previews.
""" """
pauses = [i for i in self.actions if isinstance(i, PauseAt)] tokens = [i for i in self.tokens if not isinstance(i, PauseAt)]
#self.actions = [i for i in self.actions if not isinstance(i, PauseAt)] return self.__class__(tokens)
return pauses
def maximum_length(self, settings, request_host): def maximum_length(self, settings, request_host):
""" """

View File

@ -40,9 +40,7 @@
</div> </div>
<div class="span10"> <div class="span10">
<pre>{{ output }}</pre> <pre>{{ output }}</pre>
{% if pauses %} <p>Note: pauses are skipped when generating previews!</p>
<p>Note: pauses are skipped when generating previews!</p>
{% endif %}
</div> </div>
</div> </div>
{% endif %} {% endif %}

View File

@ -40,9 +40,7 @@
</div> </div>
<div class="span10"> <div class="span10">
<pre>{{ output }}</pre> <pre>{{ output }}</pre>
{% if pauses %} <p>Note: pauses are skipped when generating previews!</p>
<p>Note: pauses are skipped when generating previews!</p>
{% endif %}
</div> </div>
</div> </div>
{% endif %} {% endif %}

View File

@ -606,9 +606,9 @@ class TestResponse:
def test_render(self): def test_render(self):
r = language.parse_response({}, "400:p0,100:dr") r = language.parse_response({}, "400:p0,100:dr")
assert r.actions[0].spec() == "p0,100" assert "p0" in r.spec()
assert len(r.preview_safe()) == 1 s = r.preview_safe()
assert not r.actions[0].spec().startswith("p") assert not "p0" in s.spec()