From 677f0e0580f92d1244285b9f0481c3ace3b14ee1 Mon Sep 17 00:00:00 2001 From: Aldo Cortesi Date: Tue, 30 Oct 2012 13:36:32 +1300 Subject: [PATCH] Change preview_safe to return a safe copy of the current message. --- libpathod/app.py | 9 ++++----- libpathod/language.py | 9 ++++----- libpathod/templates/request_preview.html | 4 +--- libpathod/templates/response_preview.html | 4 +--- test/test_language.py | 6 +++--- 5 files changed, 13 insertions(+), 19 deletions(-) diff --git a/libpathod/app.py b/libpathod/app.py index 38d0be33a..e073921cb 100644 --- a/libpathod/app.py +++ b/libpathod/app.py @@ -108,7 +108,6 @@ def _preview(is_request): section = "main", syntaxerror = None, error = None, - pauses = None ) if not spec.strip(): args["error"] = "Can't parse an empty spec." @@ -125,16 +124,16 @@ def _preview(is_request): return render(template, False, **args) 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: args["error"] = c return render(template, False, **args) 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: - r.serve(s, app.config["pathod"].request_settings) + safe.serve(s, app.config["pathod"].request_settings) args["output"] = utils.escape_unprintables(s.getvalue()) return render(template, False, **args) diff --git a/libpathod/language.py b/libpathod/language.py index f7909b81f..ba462abe0 100644 --- a/libpathod/language.py +++ b/libpathod/language.py @@ -167,7 +167,7 @@ class FileGenerator: class _Token(object): """ - A specification token. + A specification token. Tokens are immutable. """ __metaclass__ = abc.ABCMeta @abc.abstractmethod @@ -646,11 +646,10 @@ class _Message(object): 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)] - #self.actions = [i for i in self.actions if not isinstance(i, PauseAt)] - return pauses + tokens = [i for i in self.tokens if not isinstance(i, PauseAt)] + return self.__class__(tokens) def maximum_length(self, settings, request_host): """ diff --git a/libpathod/templates/request_preview.html b/libpathod/templates/request_preview.html index e4efd9d01..db08580e6 100644 --- a/libpathod/templates/request_preview.html +++ b/libpathod/templates/request_preview.html @@ -40,9 +40,7 @@
{{ output }}
- {% if pauses %} -

Note: pauses are skipped when generating previews!

- {% endif %} +

Note: pauses are skipped when generating previews!

{% endif %} diff --git a/libpathod/templates/response_preview.html b/libpathod/templates/response_preview.html index c725bcf8c..bb72513d4 100644 --- a/libpathod/templates/response_preview.html +++ b/libpathod/templates/response_preview.html @@ -40,9 +40,7 @@
{{ output }}
- {% if pauses %} -

Note: pauses are skipped when generating previews!

- {% endif %} +

Note: pauses are skipped when generating previews!

{% endif %} diff --git a/test/test_language.py b/test/test_language.py index 6d9fa31d8..d0f431986 100644 --- a/test/test_language.py +++ b/test/test_language.py @@ -606,9 +606,9 @@ class TestResponse: def test_render(self): r = language.parse_response({}, "400:p0,100:dr") - assert r.actions[0].spec() == "p0,100" - assert len(r.preview_safe()) == 1 - assert not r.actions[0].spec().startswith("p") + assert "p0" in r.spec() + s = r.preview_safe() + assert not "p0" in s.spec()