From 2d989e2fbb8b87847c215a984bc8a010ce583a8e Mon Sep 17 00:00:00 2001 From: Aldo Cortesi Date: Sat, 28 Apr 2012 22:51:36 +1200 Subject: Add a shortcut for setting content-type. --- libpathod/__init__.py | 1 - libpathod/rparse.py | 23 ++++++++++++++++++++++- 2 files changed, 22 insertions(+), 2 deletions(-) (limited to 'libpathod') diff --git a/libpathod/__init__.py b/libpathod/__init__.py index 4ef52a84..ff1b88f8 100644 --- a/libpathod/__init__.py +++ b/libpathod/__init__.py @@ -19,4 +19,3 @@ def application(**settings): static_path = utils.data.path("static"), template_path = utils.data.path("templates"), ) - diff --git a/libpathod/rparse.py b/libpathod/rparse.py index 62f48d78..4f7cc12d 100644 --- a/libpathod/rparse.py +++ b/libpathod/rparse.py @@ -192,6 +192,25 @@ Value = pp.MatchFirst( ) +class ShortcutContentType: + def __init__(self, value): + self.value = value + + def mod_response(self, settings, r): + r.headers.append( + ( + LiteralGenerator("Content-Type"), + self.value.get_generator(settings) + ) + ) + + @classmethod + def expr(klass): + e = pp.Literal("c").suppress() + e = e + Value + return e.setParseAction(lambda x: klass(*x)) + + class Body: def __init__(self, value): self.value = value @@ -240,7 +259,6 @@ class PauseRandom(_Pause): r.actions.append(("random", "pause", self.value)) - class _Disconnect: def __init__(self, value): self.value = value @@ -313,6 +331,7 @@ class Response: PauseRandom, DisconnectBefore, DisconnectRandom, + ShortcutContentType, ) version = "HTTP/1.1" code = 200 @@ -364,8 +383,10 @@ class Response: def add_timeout(self, s, callback): if TESTING: callback() + # begin nocover else: tornado.ioloop.IOLoop.instance().add_timeout(time.time() + s, callback) + # end nocover def write_values(self, fp, vals, actions, sofar=0, skip=0, blocksize=BLOCKSIZE): while vals: -- cgit v1.2.3