diff --git a/README.rst b/README.rst index ff6d16d..56bb13c 100644 --- a/README.rst +++ b/README.rst @@ -52,8 +52,7 @@ types: # insert your handlers here ]) - content.add_transcoder(application, transcoders.JSONTranscoder(), - 'application/json') + content.add_transcoder(application, transcoders.JSONTranscoder()) return application diff --git a/examples.py b/examples.py index 5e07470..bef1f27 100644 --- a/examples.py +++ b/examples.py @@ -18,10 +18,8 @@ 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, transcoders.MsgPackTranscoder(), - content_type='application/msgpack') - content.add_transcoder(application, transcoders.JSONTranscoder(), - content_type='application/json') + content.add_transcoder(application, transcoders.MsgPackTranscoder()) + content.add_transcoder(application, transcoders.JSONTranscoder()) return application diff --git a/sprockets/mixins/mediatype/content.py b/sprockets/mixins/mediatype/content.py index 1633dae..b0e69a2 100644 --- a/sprockets/mixins/mediatype/content.py +++ b/sprockets/mixins/mediatype/content.py @@ -132,8 +132,7 @@ def add_binary_content_type(application, content_type, pack, unpack): """ add_transcoder(application, - handlers.BinaryContentHandler(content_type, pack, unpack), - content_type) + handlers.BinaryContentHandler(content_type, pack, unpack)) def add_text_content_type(application, content_type, default_encoding, @@ -158,22 +157,28 @@ def add_text_content_type(application, content_type, default_encoding, normalized = str(parsed) add_transcoder(application, handlers.TextContentHandler(normalized, dumps, loads, - default_encoding), - normalized) + default_encoding)) -def add_transcoder(application, transcoder, content_type): +def add_transcoder(application, transcoder, content_type=None): """ Register a transcoder for a specific content type. :param tornado.web.Application application: the application to modify :param transcoder: object that translates between :class:`bytes` and :class:`object` instances - :param str content_type: the content type to add + :param str content_type: the content type to add. If this is + unspecified or :data:`None`, then the transcoder's ``content_type`` + attribute is used. The `transcoder` instance is required to implement the following simple protocol: + .. attribute:: transcoder.content_type + + :class:`str` that identifies the MIME type that the transcoder + implements. + .. method:: transcoder.to_bytes(inst_data, encoding=None) -> bytes :param object inst_data: the object to encode @@ -188,7 +193,7 @@ def add_transcoder(application, transcoder, content_type): """ settings = ContentSettings.from_application(application) - settings[content_type] = transcoder + settings[content_type or transcoder.content_type] = transcoder def set_default_content_type(application, content_type, encoding=None):