new package roa / resource-oriented architecture implementation, using JSON format for message exchange
git-svn-id: svn://svn.cy55.de/Zope3/src/cybertools/trunk@3524 fd906abe-77d9-0310-91a1-e0d9ade77398
This commit is contained in:
parent
96e9c47a1a
commit
e9f4bf4b1b
7 changed files with 183 additions and 0 deletions
23
roa/README.txt
Normal file
23
roa/README.txt
Normal file
|
@ -0,0 +1,23 @@
|
|||
==============================
|
||||
Resource-oriented Architecture
|
||||
==============================
|
||||
|
||||
($Id$)
|
||||
|
||||
>>> from zope import component
|
||||
>>> from zope.publisher.browser import TestRequest
|
||||
|
||||
|
||||
Providing object data in JSON format
|
||||
====================================
|
||||
|
||||
>>> class Demo(object):
|
||||
... def __init__(self, name):
|
||||
... self.name = name
|
||||
|
||||
>>> from cybertools.roa.json import JSONView
|
||||
|
||||
>>> obj = Demo('test')
|
||||
>>> jsv = JSONView(obj, TestRequest())
|
||||
>>> jsv()
|
||||
'{"name": "test"}'
|
3
roa/__init__.py
Normal file
3
roa/__init__.py
Normal file
|
@ -0,0 +1,3 @@
|
|||
"""
|
||||
$Id$
|
||||
"""
|
40
roa/json.py
Normal file
40
roa/json.py
Normal file
|
@ -0,0 +1,40 @@
|
|||
#
|
||||
# Copyright (c) 2009 Helmut Merz helmutm@cy55.de
|
||||
#
|
||||
# This program is free software; you can redistribute it and/or modify
|
||||
# it under the terms of the GNU General Public License as published by
|
||||
# the Free Software Foundation; either version 2 of the License, or
|
||||
# (at your option) any later version.
|
||||
#
|
||||
# This program is distributed in the hope that it will be useful,
|
||||
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
# GNU General Public License for more details.
|
||||
#
|
||||
# You should have received a copy of the GNU General Public License
|
||||
# along with this program; if not, write to the Free Software
|
||||
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
||||
#
|
||||
|
||||
"""
|
||||
Adapter(s)/view(s) for providing object attributes and other data in JSON format.
|
||||
|
||||
$Id$
|
||||
"""
|
||||
|
||||
from cybertools.util import json
|
||||
|
||||
|
||||
class JSONView(object):
|
||||
|
||||
def __init__(self, context, request):
|
||||
self.context = context
|
||||
self.request = request
|
||||
|
||||
def __call__(self):
|
||||
return json.dumps(self.context.__dict__)
|
||||
|
||||
def traverse(self, name):
|
||||
# To be implemented by subclass
|
||||
print '*** traversing', self.context, name
|
||||
return None
|
30
roa/tests.py
Executable file
30
roa/tests.py
Executable file
|
@ -0,0 +1,30 @@
|
|||
#! /usr/bin/python
|
||||
|
||||
"""
|
||||
Tests for the 'cybertools.roa' (Resource-oriented Architecture) package.
|
||||
|
||||
$Id$
|
||||
"""
|
||||
|
||||
import unittest, doctest
|
||||
from zope.testing.doctestunit import DocFileSuite
|
||||
from zope import component
|
||||
from cybertools.roa import json
|
||||
|
||||
|
||||
class Test(unittest.TestCase):
|
||||
"Basic tests for the rest package."
|
||||
|
||||
def testBasicStuff(self):
|
||||
pass
|
||||
|
||||
|
||||
def test_suite():
|
||||
flags = doctest.NORMALIZE_WHITESPACE | doctest.ELLIPSIS
|
||||
return unittest.TestSuite((
|
||||
unittest.makeSuite(Test),
|
||||
DocFileSuite('README.txt', optionflags=flags),
|
||||
))
|
||||
|
||||
if __name__ == '__main__':
|
||||
unittest.main(defaultTest='test_suite')
|
52
roa/traversal.py
Normal file
52
roa/traversal.py
Normal file
|
@ -0,0 +1,52 @@
|
|||
#
|
||||
# Copyright (c) 2009 Helmut Merz - helmutm@cy55.de
|
||||
#
|
||||
# This program is free software; you can redistribute it and/or modify
|
||||
# it under the terms of the GNU General Public License as published by
|
||||
# the Free Software Foundation; either version 2 of the License, or
|
||||
# (at your option) any later version.
|
||||
#
|
||||
# This program is distributed in the hope that it will be useful,
|
||||
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
# GNU General Public License for more details.
|
||||
#
|
||||
# You should have received a copy of the GNU General Public License
|
||||
# along with this program; if not, write to the Free Software
|
||||
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
||||
#
|
||||
"""
|
||||
IPublishTraverse adapter that checks the 'Accept' header.
|
||||
|
||||
$Id$
|
||||
"""
|
||||
|
||||
from zope.app.container.traversal import ItemTraverser
|
||||
from zope import component
|
||||
|
||||
|
||||
jsonMimeTypes = ('application/json',)
|
||||
|
||||
|
||||
class CheckJSONTraverser(ItemTraverser):
|
||||
|
||||
def publishTraverse(self, request, name):
|
||||
if self.isJSONRequest(request):
|
||||
return self.jsonTraverse(request, name)
|
||||
return self.defaultTraverse(request, name)
|
||||
|
||||
def isJSONRequest(self, request):
|
||||
return request.get('CONTENT_TYPE') in jsonMimeTypes
|
||||
|
||||
def jsonTraverse(self, request, name):
|
||||
item = self.context.get(name)
|
||||
if item is not None:
|
||||
return item
|
||||
# TODO: specify provides=IJSONView
|
||||
view = component.getMultiAdapter((self.context, request), name='json')
|
||||
if view is None:
|
||||
return self.defaultTraverse(request, name)
|
||||
return view.traverse(name)
|
||||
|
||||
def defaultTraverse(self, request, name):
|
||||
return super(CheckAcceptTraverser, self).publishTraverse(request, name)
|
3
roa/z2/__init__.py
Normal file
3
roa/z2/__init__.py
Normal file
|
@ -0,0 +1,3 @@
|
|||
"""
|
||||
$Id$
|
||||
"""
|
32
roa/z2/traversal.py
Normal file
32
roa/z2/traversal.py
Normal file
|
@ -0,0 +1,32 @@
|
|||
#
|
||||
# Copyright (c) 2009 Helmut Merz - helmutm@cy55.de
|
||||
#
|
||||
# This program is free software; you can redistribute it and/or modify
|
||||
# it under the terms of the GNU General Public License as published by
|
||||
# the Free Software Foundation; either version 2 of the License, or
|
||||
# (at your option) any later version.
|
||||
#
|
||||
# This program is distributed in the hope that it will be useful,
|
||||
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
# GNU General Public License for more details.
|
||||
#
|
||||
# You should have received a copy of the GNU General Public License
|
||||
# along with this program; if not, write to the Free Software
|
||||
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
||||
#
|
||||
"""
|
||||
IPublishTraverse adapter for Zope 2 that checks the 'Accept' header.
|
||||
|
||||
$Id$
|
||||
"""
|
||||
|
||||
from ZPublisher.BaseRequest import DefaultPublishTraverse
|
||||
from cybertools.roa.traversal import CheckJSONTraverser as BaseTraverser
|
||||
|
||||
|
||||
class CheckJSONTraverser(BaseTraverser, DefaultPublishTraverse):
|
||||
|
||||
defaultTraverse = DefaultPublishTraverse.publishTraverse
|
||||
browserDefault = DefaultPublishTraverse.browserDefault
|
||||
|
Loading…
Add table
Reference in a new issue