starting to set up a REST module for all sorts of external communication

git-svn-id: svn://svn.cy55.de/Zope3/src/loops/trunk@1557 fd906abe-77d9-0310-91a1-e0d9ade77398
This commit is contained in:
helmutm 2007-01-19 15:22:09 +00:00
parent fc913ad638
commit 7425dca76f
10 changed files with 303 additions and 36 deletions

View file

@ -23,38 +23,4 @@
$Id$ $Id$
""" """
from zope.app import zapi from loops.base import Loops
from zope.app.folder.folder import Folder
from zope.interface import implements
from interfaces import ILoops
loopsPrefix = '.loops'
class Loops(Folder):
implements(ILoops)
_skinName = ''
def getSkinName(self): return self._skinName
def setSkinName(self, skinName): self._skinName = skinName
skinName = property(getSkinName, setSkinName)
def getLoopsRoot(self):
return self
def getConceptManager(self):
return self['concepts']
def getResourceManager(self):
return self['resources']
def getViewManager(self):
return self['views']
def getLoopsUri(self, obj):
return str(loopsPrefix + zapi.getPath(obj)[len(zapi.getPath(self)):])
def loopsTraverse(self, uri):
prefix = loopsPrefix + '/'
if uri.startswith(prefix):
return zapi.traverse(self, uri[len(prefix):])

63
base.py Normal file
View file

@ -0,0 +1,63 @@
# -*- coding: UTF-8 -*-
# -*- Mode: Python; py-indent-offset: 4 -*-
#
# Copyright (c) 2006 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
#
"""
$Id$
"""
from zope.app.folder.folder import Folder
from zope.app.traversing.api import getPath, traverse
from zope.interface import implements
from loops.interfaces import ILoops
loopsPrefix = '.loops'
class Loops(Folder):
implements(ILoops)
_skinName = ''
def getSkinName(self): return self._skinName
def setSkinName(self, skinName): self._skinName = skinName
skinName = property(getSkinName, setSkinName)
def getLoopsRoot(self):
return self
def getConceptManager(self):
return self['concepts']
def getResourceManager(self):
return self['resources']
def getViewManager(self):
return self['views']
def getLoopsUri(self, obj):
return str(loopsPrefix + getPath(obj)[len(getPath(self)):])
def loopsTraverse(self, uri):
prefix = loopsPrefix + '/'
if uri.startswith(prefix):
return traverse(self, uri[len(prefix):])

View file

@ -388,5 +388,6 @@
<include package=".search" /> <include package=".search" />
<include package=".browser" /> <include package=".browser" />
<include package=".xmlrpc" /> <include package=".xmlrpc" />
<include package=".rest" />
</configure> </configure>

77
rest/README.txt Executable file
View file

@ -0,0 +1,77 @@
===============================================================
loops.xmlrpc
===============================================================
($Id$)
Let's do some basic set up
>>> from zope.app.testing.setup import placefulSetUp, placefulTearDown
>>> site = placefulSetUp(True)
>>> from zope import component, interface
>>> from zope.publisher.browser import TestRequest
and setup a simple loops site with a concept manager and some concepts
(with all the type machinery, what in real life is done via standard
ZCML setup):
>>> from cybertools.relation.registry import DummyRelationRegistry
>>> component.provideUtility(DummyRelationRegistry())
>>> from cybertools.relation.tests import IntIdsStub
>>> component.provideUtility(IntIdsStub())
>>> from loops.type import ConceptType, TypeConcept
>>> component.provideAdapter(ConceptType)
>>> component.provideAdapter(TypeConcept)
>>> from loops import Loops
>>> loopsRoot = site['loops'] = Loops()
>>> from loops.setup import SetupManager
>>> setup = SetupManager(loopsRoot)
>>> concepts, resources, views = setup.setup()
Let's look what setup has provided us with:
>>> list(concepts)
[u'file', u'hasType', u'image', u'predicate', u'standard', u'textdocument', u'type']
loops Traversal
===============
The loops root object provides a traversal mechanism that goes beyond the
standard container traversal. One can directly access objects by their
unique id or via a symbolic name (like `startObject`); usually the traverser
returns a REST view of the object.
>>> from loops.rest.traversal import LoopsTraverser
>>> from zope.publisher.interfaces.browser import IBrowserPublisher
>>> component.provideAdapter(LoopsTraverser, provides=IBrowserPublisher)
>>> from loops.rest.common import ConceptView
>>> component.provideAdapter(ConceptView)
Navigation typically starts at a start object, which by default ist the
top-level type concept:
>>> request = TestRequest()
>>> obj = LoopsTraverser(loopsRoot, request).publishTraverse(request, 'startObject')
>>> obj
<loops.rest.common.ConceptView object at ...>
>>> obj.context.title
u'Type'
The traversal adapter returns a view that when called renders the
representation of its context object:
>>> obj()
'Hello REST'
Fin de partie
=============
>>> placefulTearDown()

4
rest/__init__.py Normal file
View file

@ -0,0 +1,4 @@
"""
$Id$
"""

59
rest/common.py Normal file
View file

@ -0,0 +1,59 @@
#
# Copyright (c) 2007 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
#
"""
Common stuff for REST (REpresentational State Transfer) views.
$Id$
"""
from zope.interface import implements
from zope.component import adapts
from zope.publisher.interfaces.browser import IBrowserRequest, IBrowserPublisher
from loops.interfaces import IConcept
# interfaces
class IRESTView(IBrowserPublisher):
""" The basic interface for all REST views.
"""
def __call__():
""" Render the representation.
"""
class ConceptView(object):
""" A REST view for a concept.
"""
implements(IRESTView)
adapts(IConcept, IBrowserRequest)
def __init__(self, context, request):
self.context = self.__parent__ = context
self.request = request
def __call__(self):
return 'Hello REST'
def browserDefault(self, request):
""" Make the publisher happy.
"""
return self, ()

21
rest/configure.zcml Normal file
View file

@ -0,0 +1,21 @@
<!-- $Id$ -->
<configure
xmlns="http://namespaces.zope.org/zope"
xmlns:browser="http://namespaces.zope.org/browser"
i18n_domain="zope">
<adapter
factory="loops.rest.traversal.LoopsTraverser"
for="loops.interfaces.ILoops
zope.publisher.interfaces.browser.IBrowserRequest"
provides="zope.publisher.interfaces.browser.IBrowserPublisher"
permission="zope.Public" />
<adapter factory="loops.rest.common.ConceptView" />
<class class="loops.rest.common.ConceptView">
<require permission="zope.View"
interface="loops.rest.common.IRESTView" />
</class>
</configure>

22
rest/tests.py Executable file
View file

@ -0,0 +1,22 @@
# $Id$
import unittest, doctest
from zope.testing.doctestunit import DocFileSuite
class Test(unittest.TestCase):
"Basic tests for the loops.rest package."
def testBasics(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')

54
rest/traversal.py Normal file
View file

@ -0,0 +1,54 @@
# -*- coding: UTF-8 -*-
# -*- Mode: Python; py-indent-offset: 4 -*-
#
# Copyright (c) 2006 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
#
"""
$Id$
"""
from zope import component
from zope.component import adapts
from zope.app.container.traversal import ItemTraverser
from zope.publisher.interfaces.browser import IBrowserRequest
from loops.interfaces import ILoops
from loops import util
from loops.rest.common import IRESTView
class LoopsTraverser(ItemTraverser):
adapts(ILoops, IBrowserRequest)
restProperties = ('startObject', 'typePredicate',)
def publishTraverse(self, request, name):
if name in self.restProperties:
obj = getattr(self, name)
return component.getMultiAdapter((obj, request), IRESTView)
if name.isdigit():
obj = util.getObjectForUid(int(name))
return component.getMultiAdapter((obj, request), IRESTView)
return super(LoopsTraverser, self).publishTraverse(request, name)
@property
def startObject(self):
cm = self.context.getConceptManager()
return cm.get('domain', cm.getTypeConcept())

View file

@ -17,7 +17,7 @@
# #
""" """
Tournament and Assessment views. XML-RPC views.
$Id$ $Id$
""" """