Replace simplegeneric with singledispatch (PEP 443).
Closes-Bug: #1226813 Change-Id: I1ba149ac00353460065d4cddbe3ccd8782aaa611
This commit is contained in:
@@ -9,9 +9,7 @@ JSON. To get started, create a file in your project called
|
||||
``json.py`` and import it in your project's ``app.py``.
|
||||
|
||||
Your ``json`` module will contain a series of rules for generating
|
||||
JSON from objects you return in your controller, utilizing
|
||||
"generic" function support from the
|
||||
`simplegeneric <http://pypi.python.org/pypi/simplegeneric>`_ library.
|
||||
JSON from objects you return in your controller.
|
||||
|
||||
Let's say that we have a controller in our Pecan application which
|
||||
we want to use to return JSON output for a :class:`User` object::
|
||||
@@ -35,7 +33,7 @@ rule in your ``json.py``::
|
||||
from pecan.jsonify import jsonify
|
||||
from myproject import model
|
||||
|
||||
@jsonify.when_type(model.User)
|
||||
@jsonify.register(model.User)
|
||||
def jsonify_user(user):
|
||||
return dict(
|
||||
name = user.name,
|
||||
|
||||
@@ -33,9 +33,10 @@ def expose(template=None,
|
||||
directory.
|
||||
:param content_type: The content-type to use for this template.
|
||||
:param generic: A boolean which flags this as a "generic" controller,
|
||||
which uses generic functions based upon ``simplegeneric``
|
||||
generic functions. Allows you to split a single
|
||||
controller into multiple paths based upon HTTP method.
|
||||
which uses generic functions based upon
|
||||
``functools.singledispatch`` generic functions. Allows you
|
||||
to split a single controller into multiple paths based upon
|
||||
HTTP method.
|
||||
'''
|
||||
|
||||
if template == 'json':
|
||||
|
||||
@@ -17,18 +17,24 @@ except ImportError: # pragma no cover
|
||||
webob_dicts = (MultiDict,)
|
||||
|
||||
import six
|
||||
from simplegeneric import generic
|
||||
try:
|
||||
from functools import singledispatch
|
||||
except ImportError: # pragma: no cover
|
||||
from singledispatch import singledispatch
|
||||
|
||||
try:
|
||||
from sqlalchemy.engine.base import ResultProxy, RowProxy
|
||||
from sqlalchemy.engine.result import ResultProxy, RowProxy
|
||||
except ImportError: # pragma no cover
|
||||
# dummy classes since we don't have SQLAlchemy installed
|
||||
try:
|
||||
from sqlalchemy.engine.base import ResultProxy, RowProxy
|
||||
except ImportError: # pragma no cover
|
||||
# dummy classes since we don't have SQLAlchemy installed
|
||||
|
||||
class ResultProxy: # noqa
|
||||
pass
|
||||
class ResultProxy(object): # noqa
|
||||
pass
|
||||
|
||||
class RowProxy: # noqa
|
||||
pass
|
||||
class RowProxy(object): # noqa
|
||||
pass
|
||||
|
||||
|
||||
#
|
||||
@@ -105,11 +111,17 @@ class GenericJSON(JSONEncoder):
|
||||
else:
|
||||
return JSONEncoder.default(self, obj)
|
||||
|
||||
|
||||
_default = GenericJSON()
|
||||
|
||||
|
||||
@generic
|
||||
def with_when_type(f):
|
||||
# Add some backwards support for simplegeneric's API
|
||||
f.when_type = f.register
|
||||
return f
|
||||
|
||||
|
||||
@with_when_type
|
||||
@singledispatch
|
||||
def jsonify(obj):
|
||||
return _default.default(obj)
|
||||
|
||||
|
||||
@@ -1,5 +1,4 @@
|
||||
WebOb>=1.2dev
|
||||
Mako>=0.4.0
|
||||
WebTest>=1.3.1
|
||||
simplegeneric>=0.8.1
|
||||
six
|
||||
|
||||
13
setup.py
13
setup.py
@@ -39,10 +39,21 @@ except:
|
||||
#
|
||||
requirements.append('argparse')
|
||||
|
||||
try:
|
||||
from functools import singledispatch # noqa
|
||||
except:
|
||||
#
|
||||
# This was introduced in Python 3.4 - the singledispatch package contains
|
||||
# a backported replacement for 2.6 through 3.3
|
||||
#
|
||||
requirements.append('singledispatch')
|
||||
|
||||
|
||||
tests_require = requirements + [
|
||||
'virtualenv',
|
||||
'gunicorn',
|
||||
'mock'
|
||||
'mock',
|
||||
'sqlalchemy'
|
||||
]
|
||||
if sys.version_info < (2, 7):
|
||||
tests_require += ['unittest2']
|
||||
|
||||
Reference in New Issue
Block a user