[Zope3-checkins] CVS: Zope3/src/zope/app/module - __init__.py:1.1 configure.zcml:1.1 interfaces.py:1.1

Stephan Richter srichter at cosmos.phy.tufts.edu
Wed Mar 10 12:00:54 EST 2004


Update of /cvs-repository/Zope3/src/zope/app/module
In directory cvs.zope.org:/tmp/cvs-serv6102/src/zope/app/module

Added Files:
	__init__.py configure.zcml interfaces.py 
Log Message:


Moved persistent module (manager) code to zope.app.module. I did not provide
any module aliases, since noone uses this feature yet based on a recent ML
poll. If someone really needs the aliases, please let me know and I will add
them.


=== Added File Zope3/src/zope/app/module/__init__.py ===
##############################################################################
#
# Copyright (c) 2002 Zope Corporation and Contributors.
# All Rights Reserved.
#
# This software is subject to the provisions of the Zope Public License,
# Version 2.0 (ZPL).  A copy of the ZPL should accompany this distribution.
# THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
# WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
# WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
# FOR A PARTICULAR PURPOSE.
#
##############################################################################
"""Manager for persistent modules associated with a service manager.

$Id: __init__.py,v 1.1 2004/03/10 17:00:54 srichter Exp $
"""
from persistent import Persistent
from zodbcode.module import PersistentModule, compileModule
from zope.interface import implements
from zope.security.proxy import trustedRemoveSecurityProxy

from zope.app.event import function
from zope.app.interfaces.annotation import IAttributeAnnotatable
from zope.app.interfaces.file import IFileFactory
from zope.app.module.interfaces import IModuleManager
from zope.fssync.server.entryadapter import ObjectEntryAdapter, AttrMapping
from zope.fssync.server.interfaces import IObjectFile
from zope.app.container.contained import Contained

class Manager(Persistent, Contained):

    implements(IModuleManager, IAttributeAnnotatable)

    def __init__(self, name, source):
        self.name = name
        self._source = None
        self.source = source

    def __setstate__(self, state):
        manager = state.get('_manager')
        if manager is None:
            return Persistent.__setstate__(self, state)

        # We need to convert an old-style manager
        self._module = manager._module
        self.name = manager.name
        self._source = manager.source
        self._recompile = False

    def execute(self):
        try:
            mod = self._module
        except AttributeError:
            mod = self._module = PersistentModule(self.name)


        folder = self.__parent__

        # XXX
        # We are currently only supporting trusted code.
        # We don't want the folder to be proxied because, if it is, then
        # the modules will be proxied.
        # When we do support untrusted code, we're going to have to do
        # something different.
        folder = trustedRemoveSecurityProxy(folder)

        compileModule(mod, folder, self.source)
        self._recompile = False

    def getModule(self):
        if self._recompile:
            self.execute()
        return self._module

    
    def _get_source(self):
        return self._source
    def _set_source(self, source):
        if self._source != source:
            self._source = source
            self._recompile = True
    source = property(_get_source, _set_source)


# Hack to allow unpickling of old Managers to get far enough for __setstate__
# to do it's magic:
Registry = Manager

class ModuleAdapter(ObjectEntryAdapter):

    implements(IObjectFile)

    def getBody(self):
        return self.context.source

    def setBody(self, source):
        self.context.update(source)

    def extra(self):
        return AttrMapping(self.context, ("name",))


class ModuleFactory(object):

    implements(IFileFactory)

    def __init__(self, context):
        self.context = context

    def __call__(self, name, content_type, data):
        assert name.endswith(".py")
        name = name[:-3]
        m = Manager(name, data)
        m.__parent__ = self.context
        m.execute()
        return m


# Installer function that can be called from ZCML.
# This installs an import hook necessary to support persistent modules.

def installPersistentModuleImporter(event):
    from zodbcode.module import PersistentModuleImporter
    PersistentModuleImporter().install()

installPersistentModuleImporter = function.Subscriber(
    installPersistentModuleImporter)


=== Added File Zope3/src/zope/app/module/configure.zcml ===
<configure
    xmlns="http://namespaces.zope.org/zope"
    xmlns:event="http://namespaces.zope.org/event"
    xmlns:fssync="http://namespaces.zope.org/fssync"
    >

  <content class=".Manager">
    <require
        permission="zope.ManageCode"
        interface=".interfaces.IModuleManager"
        set_schema=".interfaces.IModuleManager"
        />
  </content>

  <fssync:adapter
      class=".Manager"
      factory=".ModuleAdapter"
      />

  <adapter
      for="zope.app.interfaces.services.folder.ISiteManagementFolder"
      provides="zope.app.interfaces.file.IFileFactory"
      name=".py"
      factory=".ModuleFactory"
      permission="zope.ManageContent"
      />

  <!-- Enable import of persistent modules -->
  <event:subscribe
      subscriber=".installPersistentModuleImporter"
      event_types="zope.app.event.interfaces.IProcessStartingEvent"
      />

  <include package=".browser" />

</configure>


=== Added File Zope3/src/zope/app/module/interfaces.py ===
##############################################################################
#
# Copyright (c) 2003 Zope Corporation and Contributors.
# All Rights Reserved.
#
# This software is subject to the provisions of the Zope Public License,
# Version 2.0 (ZPL).  A copy of the ZPL should accompany this distribution.
# THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
# WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
# WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
# FOR A PARTICULAR PURPOSE.
#
##############################################################################
"""Interfaces needed by the module service.

XXX There is no module service yet; instead, the service manager
currently implements it.  This should change.

$Id: interfaces.py,v 1.1 2004/03/10 17:00:54 srichter Exp $
"""

from zope.interface import Interface
from zope.schema import Bytes, ASCII, BytesLine


class IModuleManager(Interface):
    """Content object providing management support for persistent modules."""

    def execute():
        """Recompile the module source and initialize the module."""

    def getModule():
        """Return the module object that can be used from Python.

        If the module has not been initialized from the source text,
        or the source text has changed, the source will be executed by
        this method.
        """

    
    name = BytesLine(title=u"The module's name.", readonly=True)

    source = ASCII(title=u"The module's source code.")


class IModuleService(Interface):
    """Objects that can resolve dotted names to objects
    """

    def resolve(dotted_name):
        """Resolve the given dotted name to a module global variable.

        If the name ends with a trailing dot, the last name segment
        may be repeated.

        If the dotted name cannot be resolved, an ImportError is raised.
        """




More information about the Zope3-Checkins mailing list