Add `pkg_resources` lookup.

This commit is contained in:
Dave Shawley 2017-02-19 21:43:16 -05:00
parent 0f63e9675b
commit 81b2161c47
2 changed files with 62 additions and 6 deletions

View file

@ -1,4 +1,5 @@
import json
import pkg_resources
from tornado import web
@ -6,6 +7,22 @@ from tornado import web
class StatusHandler(web.RequestHandler):
"""
Simple handler that returns application status information.
The easiest way to use this handler is to pass your package's
name as the ``package`` kwarg to :class:`tornado.web.URLSpec`::
app = web.Application([
web.url('/status', StatusHandler, {'package': 'my-package'}),
])
If your application is not bundled as a python package, then you can
specify the name and version explicitly::
app = web.Application([
web.url('/status', StatusHandler,
{'name': 'my-package', 'version': '0.0.1'}),
])
"""
def __init__(self, *args, **kwargs):
@ -14,16 +31,28 @@ class StatusHandler(web.RequestHandler):
self.status = 'ok'
super(StatusHandler, self).__init__(*args, **kwargs)
def initialize(self, name, version):
def initialize(self, **kwargs):
"""
Sets the static information.
:param str name: name of the application
:param str version: application version number
:keyword str package: name of the package to retrieve
metadata from
:keyword str name: name of the application
:keyword str version: application version number
If the ``package`` keyword is specified, then the distribution
is retrieved using :py:mod:`pkg_resources` and used as a source
of status information. Otherwise, the ``name`` and ``version``
keywords are used.
"""
self.name = name
self.version = version
if 'package' in kwargs:
pkg_info = pkg_resources.get_distribution(kwargs['package'])
self.name = pkg_info.project_name
self.version = pkg_info.version
else:
self.name = self.name or kwargs.get('name')
self.version = self.version or kwargs.get('version')
def get(self):
"""

View file

@ -1,8 +1,10 @@
import json
import pkg_resources
from tornado import testing
from tornado import testing, web
import examples.app
import sprockets_status.handlers
class SimpleStatusTests(testing.AsyncHTTPTestCase):
@ -33,3 +35,28 @@ class SimpleStatusTests(testing.AsyncHTTPTestCase):
self.assertEqual(response.code, 200)
body = json.loads(response.body.decode('utf-8'))
self.assertEqual(body['status'], 'ok')
class PackageLookupTests(testing.AsyncHTTPTestCase):
def setUp(self):
super(PackageLookupTests, self).setUp()
self.package_info = pkg_resources.get_distribution('tornado')
def get_app(self):
return web.Application([
web.url('/status', sprockets_status.handlers.StatusHandler,
{'package': 'tornado'}),
])
def test_that_application_name_is_included(self):
response = self.fetch('/status')
self.assertEqual(response.code, 200)
body = json.loads(response.body.decode('utf-8'))
self.assertEqual(body['name'], self.package_info.project_name)
def test_that_application_version_is_included(self):
response = self.fetch('/status')
self.assertEqual(response.code, 200)
body = json.loads(response.body.decode('utf-8'))
self.assertEqual(body['version'], self.package_info.version)