Author: jvloothuis
Date: Sun Nov 11 12:28:39 2007
New Revision: 48553
Added:
kukit/kss.zope/trunk/kss/zope/commands.py
kukit/kss.zope/trunk/kss/zope/commandset.py
Modified:
kukit/kss.zope/trunk/kss/core/__init__.py
kukit/kss.zope/trunk/kss/core/commands.py
kukit/kss.zope/trunk/kss/core/configure.zcml
kukit/kss.zope/trunk/kss/core/kssview.py
kukit/kss.zope/trunk/kss/zope/__init__.py
kukit/kss.zope/trunk/kss/zope/configure.zcml
kukit/kss.zope/trunk/kss/zope/interfaces.py
kukit/kss.zope/trunk/kss/zope/registry.py
kukit/kss.zope/trunk/kss/zope/registry.txt
kukit/kss.zope/trunk/kss/zope/view.py
Log:
Added the start of a commandset system for the new infrastructure.
Modified: kukit/kss.zope/trunk/kss/core/__init__.py
==============================================================================
--- kukit/kss.zope/trunk/kss/core/__init__.py (original)
+++ kukit/kss.zope/trunk/kss/core/__init__.py Sun Nov 11 12:28:39 2007
@@ -27,7 +27,10 @@
mimetypes.types_map['.kkt'] = 'text/xml' # BBB legacy!
mimetypes.types_map['.kukit'] = 'text/xml'
-from kss.core.kssview import KSSView, CommandSet
+from kss.core.kssview import KSSView
+
+from kss.zope.commandset import ZopeCommandSet as CommandSet
+
from kss.core.actionwrapper import KSSExplicitError, kssaction
from kss.core.unicode_quirks import force_unicode, KSSUnicodeError
from kss.core.interfaces import ICommandSet
Modified: kukit/kss.zope/trunk/kss/core/commands.py
==============================================================================
--- kukit/kss.zope/trunk/kss/core/commands.py (original)
+++ kukit/kss.zope/trunk/kss/core/commands.py Sun Nov 11 12:28:39 2007
@@ -1,27 +1,6 @@
-# Copyright (c) 2005-2007
-# Authors: KSS Project Contributors (see docs/CREDITS.txt)
-#
-# This program is free software; you can redistribute it and/or modify
-# it under the terms of the GNU General Public License version 2 as published
-# by the Free Software Foundation.
-#
-# 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.
-
-'''\
-Marshal objects
-
-These build up the response and get marshalled to the client
-in the defined format
-'''
+from kss.zope.commands import KSSZopeCommands as KSSCommands
+# Old cruft
from zope.interface import implements
from zope.app.pagetemplate.viewpagetemplatefile import ViewPageTemplateFile
from interfaces import IKSSCommands, IKSSCommand, IKSSParam, IKSSCommandView
@@ -31,20 +10,6 @@
##from pluginregistry import checkRegisteredSelector
from pluginregistry import checkRegisteredSelector
-class KSSCommands(list):
- implements(IKSSCommands)
-
- def addCommand(self, command_name, selector=None, **kw):
- command = KSSCommand(command_name, selector=selector, **kw)
- self.append(command)
- return command
-
- def render(self, request):
- '''All methods must use this to return their command set
- '''
- adapter = zope.component.getMultiAdapter((self, request),
IKSSCommandView)
- return adapter.render()
-
class KSSParam:
implements(IKSSParam)
@@ -61,7 +26,7 @@
def getContent(self):
return self.content
-
+
class KSSCommand:
implements(IKSSCommand)
Modified: kukit/kss.zope/trunk/kss/core/configure.zcml
==============================================================================
--- kukit/kss.zope/trunk/kss/core/configure.zcml (original)
+++ kukit/kss.zope/trunk/kss/core/configure.zcml Sun Nov 11 12:28:39 2007
@@ -8,9 +8,7 @@
<!-- This include MUST come first! -->
<include package=".pluginregistry"/>
-
- <!-- Include the core plugins -->
- <include package=".plugins" />
+ <include package=".plugins"/>
<!-- javascript testing support
XXX disabled during the transition process
@@ -51,13 +49,6 @@
name="base2-dom-fp.js"
/>
- <!-- Adapter used to render KSS commands -->
- <adapter
- for=".interfaces.IKSSCommands
- zope.publisher.interfaces.browser.IBrowserRequest"
- provides=".interfaces.IKSSCommandView"
- factory=".commands.CommandView"
- />
<!-- (non-kss) browser view -->
<browser:page
Modified: kukit/kss.zope/trunk/kss/core/kssview.py
==============================================================================
--- kukit/kss.zope/trunk/kss/core/kssview.py (original)
+++ kukit/kss.zope/trunk/kss/core/kssview.py Sun Nov 11 12:28:39 2007
@@ -15,183 +15,7 @@
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
# 02111-1307, USA.
-'''\
-KSS Base view class
-
-All the implementations should create a specialization
-of this class when building their browser views.
-
-The policy is that a method should build up a command
-set on available methods and then return self.render().
-
-The default command set is implemented in the base class
-as well.
-'''
-
-import warnings
-
-try:
- from Products.Five import BrowserView
-except ImportError:
- from zope.publisher.browser import BrowserView
-
-from kss.core.commands import KSSCommands
-from kss.core.interfaces import IKSSView, ICommandSet
-from kss.core.pluginregistry.commandset import getRegisteredCommandSet
-
-from zope import component, interface, event
-from zope.interface.adapter import VerifyingAdapterRegistry
-from zope.component.globalregistry import BaseGlobalComponents
-from zope.component.interfaces import IObjectEvent
-from zope.app.component.interfaces import ISite
-from zope.app.publication.zopepublication import BeforeTraverseEvent
-
-HAS_FIVE_LSM = True
-try:
- from five.localsitemanager import registry
- from zope.component.registry import UtilityRegistration
-except ImportError:
- HAS_FIVE_LSM = False
-
-
-class SiteViewComponents(BaseGlobalComponents):
-
- def _init_registries(self):
- # This is why this class is needed: we can't work with a
- # regular AdapterRegistry because it wants to do funny things
- # with __bases__.
- self.adapters = VerifyingAdapterRegistry()
- self.utilities = VerifyingAdapterRegistry()
- if HAS_FIVE_LSM:
- self.utilities.LookupClass = registry.FiveVerifyingAdapterLookup
- self.utilities._createLookup()
- self.utilities.__parent__ = self
-
- if HAS_FIVE_LSM:
- def registeredUtilities(self):
- for ((provided, name), (component, info)
- ) in self._utility_registrations.iteritems():
- yield UtilityRegistration(self, provided, name,
- registry._wrap(component, self),
- info)
-
-class SiteView(BrowserView):
- """A browser view that is its own site
- """
- interface.implements(ISite)
-
- def __init__(self, context, request):
- super(SiteView, self).__init__(context, request)
-
- next = component.getSiteManager()
- self._sitemanager = SiteViewComponents('siteview')
- self._sitemanager.__bases__ = (next, )
- if HAS_FIVE_LSM:
- self._sitemanager.__parent__ = self
-
- # On Five, we should wrap it in the acquisition context
- # see, if self has aq_parent, it is done obligatoraly
- try:
- self.context.aq_parent
- except AttributeError:
- # Zope3 - No problem.
- wrapped_view = self
- else:
- wrapped_view = self.__of__(self.context)
-
- # register object event handler
- self._sitemanager.registerHandler(wrapped_view._eventRedispatcher)
-
- # Zope 2.10 doesn't send BeforeTraverseEvents for all objects,
- # only the ones for which you explicitly enable a before traverse
- # hook available from Five.component. Hence, this view won't
- # become the current site when it's traversed. This is bad,
- # hopefully Zope 2.11 will fix this. For now, we'll just take
- # care of it ourselves...
- def __before_publishing_traverse__(self, obj, request):
- event.notify(BeforeTraverseEvent(self, request))
-
- # ISite interface
-
- def getSiteManager(self):
- return self._sitemanager
-
- def setSiteManager(self, sm):
- raise TypeError("Site manager of SiteView can't be changed.")
-
- @component.adapter(IObjectEvent)
- def _eventRedispatcher(self, event):
- """This works similar to zope.component.event.objectEventNotify:
- It dispatches object events to subscribers that listen to
- (object, view, event)."""
- adapters = component.subscribers((event.object, self, event), None)
- for adapter in adapters:
- pass # getting them does the work
-
-class KSSView(SiteView):
- """KSS view
-
- This allows setting up the content of the response, and then
- generate it out.
- """
- interface.implements(IKSSView)
-
- def __init__(self, context, request):
- super(KSSView, self).__init__(context, request)
- self._initcommands()
-
- def _initcommands(self):
- self.commands = KSSCommands()
-
- # XXX avoid weird acquisition behaviour in Zope 2... this should
- # go away when Five views aren't Acquisition objects anymore.
- def _set_context(self, context):
- self._context = [context]
- def _get_context(self):
- return self._context[0]
- context = property(_get_context, _set_context)
-
- def render(self):
- """Views can use this to return their command set."""
- return self.commands.render(self.request)
-
- def cancelRedirect(self):
- if self.request.response.getStatus() in (302, 303):
- # Try to not redirect if requested
- self.request.response.setStatus(200)
-
- def getCommands(self):
- return self.commands
-
- def getCommandSet(self, name):
- commandset = getRegisteredCommandSet(name)
- # return the adapted view
- return commandset.provides(self)
-
-class CommandSet:
- interface.implements(ICommandSet)
-
- def __init__(self, view):
- self.view = view
- self.context = self.view.context
- self.request = self.view.request
- self.commands = self.view.commands
-
- def getCommandSet(self, name):
- return self.view.getCommandSet(name)
-
-# BBB deprecated
-class AzaxBaseView(KSSView):
- def __init__(self, *args, **kw):
- message = "'AzaxBaseView' is deprecated," \
- "use 'KSSView' instead."
- warnings.warn(message, DeprecationWarning, 2)
- KSSView.__init__(self, *args, **kw)
-
-class AzaxViewAdapter(CommandSet):
- def __init__(self, *args, **kw):
- message = "'AzaxViewAdapter' is deprecated," \
- "use 'CommandSet' instead."
- warnings.warn(message, DeprecationWarning, 2)
- CommandSet.__init__(self, *args, **kw)
+from kss.zope.view import KSSView
+from kss.zope.commandset import ZopeCommandSet as CommandSet
+AzaxBaseView = KSSView
Modified: kukit/kss.zope/trunk/kss/zope/__init__.py
==============================================================================
--- kukit/kss.zope/trunk/kss/zope/__init__.py (original)
+++ kukit/kss.zope/trunk/kss/zope/__init__.py Sun Nov 11 12:28:39 2007
@@ -0,0 +1 @@
+from kss.zope.view import KSSView
Added: kukit/kss.zope/trunk/kss/zope/commands.py
==============================================================================
--- (empty file)
+++ kukit/kss.zope/trunk/kss/zope/commands.py Sun Nov 11 12:28:39 2007
@@ -0,0 +1,140 @@
+from zope import component
+from kss.base import KSSCommands
+
+from kss.zope.interfaces import IKSSPluginRegistry, IZopeCommandSet
+
+class KSSZopeCommands(KSSCommands):
+ def __init__(self, view, context, request):
+ super(KSSZopeCommands, self).__init__()
+ self.view = view
+ self.context = context
+ self.request = request
+
+ def __getattr__(self, name):
+ registry = component.getUtility(IKSSPluginRegistry)
+ commandset_factory = registry.lookup_commandset(name)
+ if IZopeCommandSet.implementedBy(commandset_factory):
+ return
+ else:
+ return commandset_factory(self)
+
+ # Add something like this
+ def addCommand(self, command_name, selector=None, **kw):
+ command = KSSCommand(command_name, selector=selector, **kw)
+ self.append(command)
+ return command
+
+'''
+# BBB stuf
+class KSSParam:
+ implements(IKSSParam)
+
+ def __init__(self, name, content=''):
+ self.name = name
+ self.content = content
+
+ def force_content_unicode(self):
+ # Content must be str with ascii encoding, or unicode!
+ self.content = force_unicode(self.content)
+
+ def getName(self):
+ return self.name
+
+ def getContent(self):
+ return self.content
+
+class KSSCommand:
+ implements(IKSSCommand)
+
+ def __init__(self, command_name, selector=None, **kw):
+ try:
+ checkRegisteredCommand_old(command_name)
+ except KSSPluginError:
+ # we expect this is not registered as command, anyway
+ # so check it as an action.
+ checkRegisteredCommand(command_name)
+ else:
+ # ok. XXX this will be deprecated
+ # All registerCommand commands are obsolete, by default
+ import warnings, textwrap
+ warnings.warn(textwrap.dedent('
+ The usage of the kss command "%s" is deprecated'
+ % (command_name, )), DeprecationWarning, 2)
+ if selector is not None:
+ if isinstance(selector, basestring):
+ # the default selector - given just as a string
+ self.selector = selector
+ self.selectorType = ''
+ else:
+ checkRegisteredSelector(selector.type)
+ self.selector = selector.value
+ self.selectorType = selector.type
+ else:
+ self.selector = None
+ self.selectorType = None
+ self.name = command_name
+ self.params = []
+ # Add parameters passed in **kw
+ for key, value in kw.iteritems():
+ self.addParam(key, value)
+
+ # --
+ # Different parameter conversions
+ # --
+
+ # REMARK: with the jsonserver product present, you can
+ # just send complex data types directly with AddParam
+
+ def addParam(self, name, content=''):
+ 'Add the param as is'
+ param = KSSParam(name, content)
+ self.params.append(param)
+ return param
+
+ #
+ # Some helpers
+ #
+
+ def addUnicodeParam(self, name, content=''):
+ 'Add the param as unicode'
+ self.addParam(name, content=content)
+
+ def addStringParam(self, name, content='', encoding='utf'):
+ 'Add the param as an encoded string, by default UTF-8'
+ content = unicode(content, 'utf')
+ self.addParam(name, content=content)
+
+ def addHtmlParam(self, name, content=''):
+ 'Add the param as an HTML content.'
+ content = HtmlParser(content)().encode('ascii', 'xmlcharrefreplace')
+ ##self.addParam(name, content=content)
+ # add html as cdata!
+ self.addCdataParam(name, content=content)
+
+ def addXmlParam(self, name, content=''):
+ 'Add the param as XML content'
+ content = XmlParser(content)().encode('ascii', 'xmlcharrefreplace')
+ self.addParam(name, content=content)
+
+ def addCdataParam(self, name, content=''):
+ 'Add the param as a CDATA node'
+ content = '<![CDATA[%s]]>' % (content, )
+ self.addParam(name, content=content)
+
+
+ # --
+ # Accessors, not sure if we need them
+ # --
+
+ def getName(self):
+ return self.name
+
+ def getSelector(self):
+ return self.selector
+
+ def getSelectorType(self):
+ return self.selectorType
+
+ def getParams(self):
+ return self.params
+'''
Added: kukit/kss.zope/trunk/kss/zope/commandset.py
==============================================================================
--- (empty file)
+++ kukit/kss.zope/trunk/kss/zope/commandset.py Sun Nov 11 12:28:39 2007
@@ -0,0 +1,16 @@
+from zope import interface
+
+from kss.zope.interfaces import IZopeCommandSet
+
+class ZopeCommandSet:
+ interface.implements(IZopeCommandSet)
+
+ def __init__(self, view):
+ self.view = view
+ self.context = self.view.context
+ self.request = self.view.request
+ self.commands = self.view.commands
+
+ def getCommandSet(self, name):
+ return self.view.getCommandSet(name)
+
Modified: kukit/kss.zope/trunk/kss/zope/configure.zcml
==============================================================================
--- kukit/kss.zope/trunk/kss/zope/configure.zcml (original)
+++ kukit/kss.zope/trunk/kss/zope/configure.zcml Sun Nov 11 12:28:39 2007
@@ -8,7 +8,6 @@
<utility factory=".registry.GlobalPluginRegistry"
permission="zope.Public" />
-
<!-- Defining the kss concatenated javascript
as resources
-->
Modified: kukit/kss.zope/trunk/kss/zope/interfaces.py
==============================================================================
--- kukit/kss.zope/trunk/kss/zope/interfaces.py (original)
+++ kukit/kss.zope/trunk/kss/zope/interfaces.py Sun Nov 11 12:28:39 2007
@@ -41,6 +41,21 @@
def lookup_commandset(id):
'''Return a commandset class for the given id'''
+class ICommandSet(Interface):
+ 'Methods of this class implement a command set'
+
+ def __init__(self, commands):
+ '''Initialize a commandset'''
+
+ def getCommandSet(self, name): # TODO: Is this API used at all?
+ 'Returns the command set for a given name'
+
+class IZopeCommandSet(ICommandSet):
+ '''A Zope version of the commandset, has more stuff'''
+
+ def __init__(self, commands, view, context, request):
+ '''Initialize a commandset'''
+
# from kss.core
class IKSSCommands(Interface):
@@ -136,11 +151,3 @@
"""Return the commandset called ``name`` bound to the current
view."""
-# BBB deprecated
-IAzaxView = IKSSView
-
-class ICommandSet(Interface):
- 'Methods of this class implement a command set'
-
- def getCommandSet(self, name):
- 'Returns the command set for a given name'
Modified: kukit/kss.zope/trunk/kss/zope/registry.py
==============================================================================
--- kukit/kss.zope/trunk/kss/zope/registry.py (original)
+++ kukit/kss.zope/trunk/kss/zope/registry.py Sun Nov 11 12:28:39 2007
@@ -6,6 +6,12 @@
class GlobalPluginRegistry(object):
interface.implements(IKSSPluginRegistry)
+ def __init__(self):
+ self._commandsets = {}
+
+ for plugin_id, config in available_plugins():
+ self._commandsets.update(config.commandsets)
+
def plugins(self):
return available_plugins()
@@ -21,4 +27,8 @@
def lookup_commandset(self, id):
- pass
+ return self._commandsets[id]
+
+
+ def register_commandset(self, id, factory):
+ self._commandsets[id] = factory
Modified: kukit/kss.zope/trunk/kss/zope/registry.txt
==============================================================================
--- kukit/kss.zope/trunk/kss/zope/registry.txt (original)
+++ kukit/kss.zope/trunk/kss/zope/registry.txt Sun Nov 11 12:28:39 2007
@@ -1,3 +1,4 @@
+=================
Plugin registries
=================
@@ -35,3 +36,31 @@
>>> list(registry.extra_javascripts())
['.../3rd_party/base2-dom-fp.js', '.../3rd_party/sarissa.js']
+
+The registry can also be used to lookup a commandset. If a commandset
+is requested which is not available we will get a KeyError.
+
+ >>> registry.lookup_commandset('cheese')
+ Traceback (most recent call last):
+ ...
+ KeyError: 'cheese'
+
+Loading the commandset for the core should be possible.
+
+ >>> registry.lookup_commandset('core')
+ <class 'kss.base.corecommands.KSSCoreCommands'>
+
+As you can see it returns a factory (in this case the class) for the
+commandset.
+
+Next to automatically discovering commandsets (using the plugin
+system) there is support for registering a specific set. The
+registration is a simple call to the registry with a commandset
+factory.
+
+ >>> from kss.base.commands import KSSCommandSet
+ >>> class CheeseCommandSet(KSSCommandSet):
+ ... pass
+ >>> registry.register_commandset('cheese', CheeseCommandSet)
+ >>> registry.lookup_commandset('cheese')
+ <class 'CheeseCommandSet'>
\ No newline at end of file
Modified: kukit/kss.zope/trunk/kss/zope/view.py
==============================================================================
--- kukit/kss.zope/trunk/kss/zope/view.py (original)
+++ kukit/kss.zope/trunk/kss/zope/view.py Sun Nov 11 12:28:39 2007
@@ -28,17 +28,13 @@
as well.
'''
-import warnings
-
try:
from Products.Five import BrowserView
except ImportError:
from zope.publisher.browser import BrowserView
-from kss.base import KSSCommands
-# from kss.core.commands import KSSCommands
-from kss.zope.interfaces import IKSSView, ICommandSet
-from kss.core.pluginregistry.commandset import getRegisteredCommandSet
+from kss.zope.commands import KSSZopeCommands
+from kss.zope.interfaces import IKSSView
from zope import component, interface, event
from zope.interface.adapter import VerifyingAdapterRegistry
@@ -139,10 +135,7 @@
def __init__(self, context, request):
super(KSSView, self).__init__(context, request)
- self._initcommands()
-
- def _initcommands(self):
- self.commands = KSSCommands()
+ self.commands = KSSZopeCommands(self, context, request)
# XXX avoid weird acquisition behaviour in Zope 2... this should
# go away when Five views aren't Acquisition objects anymore.
@@ -154,7 +147,9 @@
def render(self):
"""Views can use this to return their command set."""
- return self.commands.render(self.request)
+ self.request.response.setHeader('Content-type',
+ 'text/xml;charset=utf-8')
+ return self.commands.render()
def cancelRedirect(self):
if self.request.response.getStatus() in (302, 303):
@@ -165,34 +160,4 @@
return self.commands
def getCommandSet(self, name):
- commandset = getRegisteredCommandSet(name)
- # return the adapted view
- return commandset.provides(self)
-
-class CommandSet:
- interface.implements(ICommandSet)
-
- def __init__(self, view):
- self.view = view
- self.context = self.view.context
- self.request = self.view.request
- self.commands = self.view.commands
-
- def getCommandSet(self, name):
- return self.view.getCommandSet(name)
-
-# BBB deprecated
-class AzaxBaseView(KSSView):
- def __init__(self, *args, **kw):
- message = "'AzaxBaseView' is deprecated," \
- "use 'KSSView' instead."
- warnings.warn(message, DeprecationWarning, 2)
- KSSView.__init__(self, *args, **kw)
-
-class AzaxViewAdapter(CommandSet):
- def __init__(self, *args, **kw):
- message = "'AzaxViewAdapter' is deprecated," \
- "use 'CommandSet' instead."
- warnings.warn(message, DeprecationWarning, 2)
- CommandSet.__init__(self, *args, **kw)
-
+ return getattr(self.commands, name)
_______________________________________________
Kukit-checkins mailing list
[email protected]
http://codespeak.net/mailman/listinfo/kukit-checkins