vendor/Five/1.2b-r20590

view monkey.py @ 0:3673ed425f80

Vendor import of Five 1.2b+ (r20590)
author fguillaume
date Fri, 02 Dec 2005 20:25:42 +0000
parents
children
line source
1 ##############################################################################
2 #
3 # Copyright (c) 2004, 2005 Zope Corporation and Contributors.
4 # All Rights Reserved.
5 #
6 # This software is subject to the provisions of the Zope Public License,
7 # Version 2.1 (ZPL). A copy of the ZPL should accompany this distribution.
8 # THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
9 # WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
10 # WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
11 # FOR A PARTICULAR PURPOSE.
12 #
13 ##############################################################################
14 """Bad monkey!
16 $Id: monkey.py 20590 2005-12-02 20:23:17Z efge $
17 """
18 def monkeyPatch():
19 """Trigger all monkey patches needed to make Five work.
21 This adjusts Zope 2 classes to make them work with Zope 3.
23 Monkey patches are kept to a minimum level.
24 """
26 from Products.Five import interfaces, i18n
27 interfaces.monkey()
28 i18n.monkey()
29 localsites_monkey()
30 skins_monkey()
31 zope3_monkey()
33 def localsites_monkey():
34 from Acquisition import aq_inner, aq_parent
35 from zope.component.interfaces import IServiceService
36 from zope.component.exceptions import ComponentLookupError
38 def getLocalServices(context):
39 """Returns the service manager that contains `context`.
41 If `context` is a local service, returns the service manager
42 that contains that service. If `context` is a service manager,
43 returns `context`.
45 Otherwise, raises ``ComponentLookupError('Services')``
47 Basically, this overrides the one in Zope X3 3.0 so that it
48 uses acquisition instead of looking up __parent__. Monkey
49 patching Zope 3 sucks, but X3 3.0 leaves us no choice, really.
50 Fortunately, this will disappear in Zope 3.2, so we won't wet
51 our pants about this now..."""
52 # IMPORTANT
53 #
54 # This is not allowed to use any services to get its job done!
56 while not (context is None or
57 IServiceService.providedBy(context)):
58 context = getattr(context, '__parent__', aq_parent(aq_inner(context)))
59 if context is None:
60 raise ComponentLookupError('Services')
61 else:
62 return context
64 from zope.app.component import localservice
65 localservice.getLocalServices = getLocalServices
67 from zope.event import notify
68 from zope.app.publication.interfaces import EndRequestEvent
69 def close(self):
70 self.other.clear()
71 self._held=None
72 notify(EndRequestEvent(None, self))
74 from ZPublisher.BaseRequest import BaseRequest
75 BaseRequest.close = close
77 def skins_monkey():
78 """Monkey HTTPRequest, from Zope > 2.8.4
79 """
80 def shiftNameToApplication(self):
81 """see zope.publisher.interfaces.http.IVirtualHostRequest"""
82 # this is needed for ++skin++
83 from ZPublisher.HTTPRequest import HTTPRequest
84 HTTPRequest.shiftNameToApplication = shiftNameToApplication
86 def zope3_monkey():
87 """Zope 3 monkeys to get some Zope 3.2 features.
89 - Added ContainerModifiedEvent.
91 - Added `original` parameter to ObjectCopiedEvent.
92 """
93 import warnings
94 from zope.event import notify
95 from zope.interface import implements
96 from zope.app.event.objectevent import ObjectModifiedEvent
97 from zope.app.event.objectevent import ObjectCopiedEvent
98 from zope.app.event.interfaces import IObjectModifiedEvent
100 class IContainerModifiedEvent(IObjectModifiedEvent):
101 """The container has been modified.
103 This event is specific to "containerness" modifications, which
104 means addition, removal or reordering of sub-objects.
105 """
107 class ContainerModifiedEvent(ObjectModifiedEvent):
108 """The container has been modified."""
109 implements(IContainerModifiedEvent)
111 def notifyContainerModified(object, *descriptions):
112 """Notify that the container was modified."""
113 notify(ContainerModifiedEvent(object, *descriptions))
115 def ObjectCopiedEvent_init(self, object, original=None):
116 super(ObjectCopiedEvent, self).__init__(object)
117 self.original = original
118 # BBB goes away in 3.3
119 if original is None:
120 warnings.warn(
121 "%s with no original is deprecated and will no-longer "
122 "be supported starting in Zope 3.3."
123 % self.__class__.__name__,
124 DeprecationWarning, stacklevel=2)
126 from zope.app.container import contained
127 from zope.app.container import interfaces
128 interfaces.IContainerModifiedEvent = IContainerModifiedEvent
129 contained.ContainerModifiedEvent = ContainerModifiedEvent
130 contained.notifyContainerModified = notifyContainerModified
131 ObjectCopiedEvent.__init__ = ObjectCopiedEvent_init