From 39f2b5cc6c5f51923c2225a53798f4155bb187a2 Mon Sep 17 00:00:00 2001 From: Ryan Mclean Date: Tue, 15 Mar 2016 13:53:46 -0400 Subject: [PATCH] Add installation method Move application initializations out of initialize and into ``sprockets.mixins.avro_publisher.install`` Add sprockets.mixins.amqp.install to setup amqp for application Bump version to 1.0.0 --- README.rst | 19 ++++++++------ requires/python2.txt | 2 +- requires/python3.txt | 2 +- setup.py | 2 +- sprockets/mixins/avro_publisher/__init__.py | 28 ++++++++++----------- 5 files changed, 29 insertions(+), 24 deletions(-) diff --git a/README.rst b/README.rst index 729c7ef..d54d845 100644 --- a/README.rst +++ b/README.rst @@ -16,7 +16,7 @@ and can be installed via ``pip`` or ``easy_install``: Requirements ------------ -- sprockets.mixins.amqp>=0.1.1 +- sprockets.mixins.amqp>=1.0.0 Example ------- @@ -34,6 +34,16 @@ This examples demonstrates the most basic usage of ``sprockets.mixins.avro-publi from tornado import web from sprockets.mixins import avro_publisher + def make_app(**settings): + settings = {'avro_schema_uri_format': 'http://my-schema-repository/%(name)s.avsc'} + application = web.Application( + [ + web.url(r'/', RequestHandler), + ], **settings) + + avro_publisher.install(application) + return application + class RequestHandler(avro_publisher.AvroPublishingMixin, web.RequestHandler): @gen.coroutine @@ -43,13 +53,8 @@ This examples demonstrates the most basic usage of ``sprockets.mixins.avro-publi {'content_type': avro_publisher.DATUM_MIME_TYPE, 'type': 'avro-schema-name'}) - settings = {'avro_schema_uri_format': 'http://my-schema-repository/%(name)s.avsc'} - application = web.Application([(r"/", RequestHandler),], - debug=True, - **settings) - - if __name__ == "__main__": + application = make_app() application.listen(8888) logging.basicConfig(level=logging.INFO) ioloop.IOLoop.current().start() diff --git a/requires/python2.txt b/requires/python2.txt index 4fdd545..8953202 100644 --- a/requires/python2.txt +++ b/requires/python2.txt @@ -1,2 +1,2 @@ -sprockets.mixins.amqp>=0.1.1,<1 +sprockets.mixins.amqp>=1.0.0,<2 avro>=1.7.7,<2 diff --git a/requires/python3.txt b/requires/python3.txt index a4ad115..8804715 100644 --- a/requires/python3.txt +++ b/requires/python3.txt @@ -1,2 +1,2 @@ -sprockets.mixins.amqp>=0.1.1,<1 +sprockets.mixins.amqp>=1.0.0,<2 avro-python3>=1.7.7,<2 diff --git a/setup.py b/setup.py index 99d0020..7b304fa 100644 --- a/setup.py +++ b/setup.py @@ -9,7 +9,7 @@ with open(requires) as handle: setuptools.setup( name='sprockets.mixins.avro-publisher', - version='0.1.1', + version='1.0.0', description='Mixin for publishing events to RabbitMQ as avro datums', long_description=open('README.rst').read(), url='https://github.com/sprockets/sprockets.mixins.avro-publisher', diff --git a/sprockets/mixins/avro_publisher/__init__.py b/sprockets/mixins/avro_publisher/__init__.py index d14dd1c..bbd204d 100644 --- a/sprockets/mixins/avro_publisher/__init__.py +++ b/sprockets/mixins/avro_publisher/__init__.py @@ -28,7 +28,7 @@ from tornado import httpclient import avro.io import avro.schema -version_info = (0, 1, 0) +version_info = (1, 0, 0) __version__ = '.'.join(str(v) for v in version_info) LOGGER = logging.getLogger(__name__) @@ -52,17 +52,6 @@ class AvroPublishingMixin(amqp.PublishingMixin): DATUM_MIME_TYPE = 'application/vnd.apache.avro.datum' DEFAULT_SCHEMA_URI_FORMAT = 'http://localhost/avro/%(name)s.avsc' - def initialize(self): - """Initialize the RequestHandler ensuring there is an a dict for - caching avro schemas. - - """ - super(AvroPublishingMixin, self).initialize() - if 'avro_schema_uri_format' not in self.application.settings: - LOGGER.warning('avro_schema_uri_format is not set, using default') - if not hasattr(self.application, 'avro'): - self.application.avro = {} - @gen.coroutine def amqp_publish(self, exchange, routing_key, body, properties): """Publish the message to RabbitMQ @@ -73,8 +62,8 @@ class AvroPublishingMixin(amqp.PublishingMixin): :param dict properties: The message properties """ - if ('content_type' in properties and - properties['content_type']) == self.DATUM_MIME_TYPE: + if (('content_type' in properties and + properties['content_type']) == self.DATUM_MIME_TYPE): body = yield self._avro_serialize(properties['type'], body) yield self.application.amqp.publish(exchange, routing_key, body, properties) @@ -146,3 +135,14 @@ class AvroPublishingMixin(amqp.PublishingMixin): except avro.io.AvroTypeException as error: raise ValueError(error) raise gen.Return(bytes_io.getvalue()) + + +def install(application, **kwargs): + """Call this to install avro publishing for the Tornado application.""" + amqp.install(application, **kwargs) + + if 'avro_schema_uri_format' not in application.settings: + LOGGER.warning('avro_schema_uri_format is not set, using default') + + setattr(application, 'avro', {}) + return True