From 36a916a557ed6bec888a9054d9c5e1d7580f0287 Mon Sep 17 00:00:00 2001 From: Dave Shawley Date: Mon, 1 Feb 2016 09:07:56 -0500 Subject: [PATCH] add_transcoder: Change parameter order. This is the first part of making the content type parameter optional --- README.rst | 4 ++-- examples.py | 8 ++++---- sprockets/mixins/mediatype/content.py | 14 ++++++++------ 3 files changed, 14 insertions(+), 12 deletions(-) diff --git a/README.rst b/README.rst index 4601b70..ff6d16d 100644 --- a/README.rst +++ b/README.rst @@ -52,8 +52,8 @@ types: # insert your handlers here ]) - content.add_transcoder(application, 'application/json', - transcoders.JSONTranscoder()) + content.add_transcoder(application, transcoders.JSONTranscoder(), + 'application/json') return application diff --git a/examples.py b/examples.py index db6ea78..5e07470 100644 --- a/examples.py +++ b/examples.py @@ -18,10 +18,10 @@ def make_application(**settings): application = web.Application([web.url(r'/', SimpleHandler)], **settings) content.set_default_content_type(application, 'application/json', encoding='utf-8') - content.add_transcoder(application, 'application/msgpack', - transcoders.MsgPackTranscoder()) - content.add_transcoder(application, 'application/json', - transcoders.JSONTranscoder()) + content.add_transcoder(application, transcoders.MsgPackTranscoder(), + content_type='application/msgpack') + content.add_transcoder(application, transcoders.JSONTranscoder(), + content_type='application/json') return application diff --git a/sprockets/mixins/mediatype/content.py b/sprockets/mixins/mediatype/content.py index 7675b7e..1633dae 100644 --- a/sprockets/mixins/mediatype/content.py +++ b/sprockets/mixins/mediatype/content.py @@ -131,8 +131,9 @@ def add_binary_content_type(application, content_type, pack, unpack): dictionary. ``unpack(bytes) -> dict`` """ - add_transcoder(application, content_type, - handlers.BinaryContentHandler(content_type, pack, unpack)) + add_transcoder(application, + handlers.BinaryContentHandler(content_type, pack, unpack), + content_type) def add_text_content_type(application, content_type, default_encoding, @@ -155,19 +156,20 @@ def add_text_content_type(application, content_type, default_encoding, parsed = headers.parse_content_type(content_type) parsed.parameters.pop('charset', None) normalized = str(parsed) - add_transcoder(application, normalized, + add_transcoder(application, handlers.TextContentHandler(normalized, dumps, loads, - default_encoding)) + default_encoding), + normalized) -def add_transcoder(application, content_type, transcoder): +def add_transcoder(application, transcoder, content_type): """ Register a transcoder for a specific content type. :param tornado.web.Application application: the application to modify - :param str content_type: the content type to add :param transcoder: object that translates between :class:`bytes` and :class:`object` instances + :param str content_type: the content type to add The `transcoder` instance is required to implement the following simple protocol: