Skip to content

Commit

Permalink
migrate imports from zope.app.component to zope.site
Browse files Browse the repository at this point in the history
  • Loading branch information
kteague committed Dec 14, 2009
1 parent 6399980 commit 88bb218
Show file tree
Hide file tree
Showing 6 changed files with 283 additions and 3 deletions.
2 changes: 1 addition & 1 deletion CHANGES.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ Changes
1.2 (unreleased)
----------------

* ...
* Migrated imports from zope.app.component to zope.site.

1.1 (2009-09-18)
----------------
Expand Down
141 changes: 141 additions & 0 deletions src/grokcore/site/ftests/utility/local.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,141 @@
"""
Local Utilities can be registered on subclasses of grok.Site using
grok.local_utility:
>>> cave = Cave()
>>> getRootFolder()["cave"] = cave
>>> from zope import component
>>> from zope.site.hooks import getSite, setSite
>>> setSite(cave)
>>> fireplace = component.getUtility(IFireplace)
>>> IFireplace.providedBy(fireplace)
True
>>> isinstance(fireplace, Fireplace)
True
>>> club = component.getUtility(IClub)
>>> IClub.providedBy(club)
True
>>> isinstance(club, Club)
True
>>> spiky = component.getUtility(IClub, name='spiky')
>>> IClub.providedBy(spiky)
True
>>> isinstance(spiky, SpikyClub)
True
>>> mammoth = component.getUtility(IMammoth)
>>> IMammoth.providedBy(mammoth)
True
>>> isinstance(mammoth, Mammoth)
True
>>> tiger = component.getUtility(IMammoth, name='tiger')
>>> IMammoth.providedBy(tiger)
True
>>> isinstance(tiger, SabretoothTiger)
True
>>> painting = component.getUtility(IPainting, name='blackandwhite')
>>> IPainting.providedBy(painting)
True
>>> isinstance(painting, CavePainting)
True
>>> colored = component.getUtility(IPainting, name='color')
>>> IPainting.providedBy(colored)
True
>>> isinstance(colored, ColoredCavePainting)
True
Since it is a local utility, it is not available outside its site:
>>> setSite(None)
>>> component.getUtility(IFireplace)
Traceback (most recent call last):
...
ComponentLookupError: (<InterfaceClass grokcore.site.ftests.utility.local.IFireplace>, '')
>>> component.getUtility(IClub)
Traceback (most recent call last):
...
ComponentLookupError: (<InterfaceClass grokcore.site.ftests.utility.local.IClub>, '')
>>> component.getUtility(IClub, name='spiky')
Traceback (most recent call last):
...
ComponentLookupError: (<InterfaceClass grokcore.site.ftests.utility.local.IClub>, 'spiky')
>>> component.getUtility(IMammoth)
Traceback (most recent call last):
...
ComponentLookupError: (<InterfaceClass grokcore.site.ftests.utility.local.IMammoth>, '')
>>> component.getUtility(IMammoth, name='tiger')
Traceback (most recent call last):
...
ComponentLookupError: (<InterfaceClass grokcore.site.ftests.utility.local.IMammoth>, 'tiger')
>>> component.getUtility(IPainting, name='blackandwhite')
Traceback (most recent call last):
...
ComponentLookupError: (<InterfaceClass grokcore.site.ftests.utility.local.IPainting>, 'blackandwhite')
>>> component.getUtility(IPainting, name='color')
Traceback (most recent call last):
...
ComponentLookupError: (<InterfaceClass grokcore.site.ftests.utility.local.IPainting>, 'color')
"""
import grokcore.site
from zope import interface
import persistent

class IFireplace(interface.Interface):
pass

class IClub(interface.Interface):
pass

class ISpiky(interface.Interface):
pass

class IMammoth(interface.Interface):
pass

class Fireplace(grokcore.site.LocalUtility):
interface.implements(IFireplace)

class Club(object):
interface.implements(IClub)

class SpikyClub(object):
interface.implements(IClub, ISpiky)

class Mammoth(grokcore.site.LocalUtility):
interface.implements(IMammoth, IClub)

class SabretoothTiger(grokcore.site.LocalUtility):
interface.implements(IMammoth, IClub)
grokcore.site.provides(IMammoth)

class IPainting(persistent.interfaces.IPersistent):
pass

class CavePainting(grokcore.site.LocalUtility):
interface.implements(IPainting)

class ColoredCavePainting(grokcore.site.LocalUtility):
interface.implements(IPainting)
grokcore.site.provides(IPainting)

class Cave(grokcore.site.Site):
grokcore.site.local_utility(Fireplace)
grokcore.site.local_utility(Club)
grokcore.site.local_utility(SpikyClub, provides=IClub, name='spiky')
grokcore.site.local_utility(Mammoth, provides=IMammoth)
grokcore.site.local_utility(SabretoothTiger, name='tiger')
grokcore.site.local_utility(CavePainting, name='blackandwhite', provides=IPainting)
grokcore.site.local_utility(ColoredCavePainting, name='color')
38 changes: 38 additions & 0 deletions src/grokcore/site/ftests/utility/local_override.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
"""
Local Utilities can be registered on subclasses of grok.Site using
grok.local_utility:
>>> cave = SpikyCave()
>>> getRootFolder()['cave'] = cave
>>> from zope import component
>>> from zope.site.hooks import getSite, setSite
>>> setSite(cave)
>>> club = component.getUtility(IClub)
>>> IClub.providedBy(club)
True
>>> isinstance(club, SpikyClub)
True
>>> list(cave.getSiteManager().keys())
[u'SpikyClub']
"""
import grokcore.site
from zope import interface

class IClub(interface.Interface):
pass

class Club(grokcore.site.LocalUtility):
interface.implements(IClub)

class SpikyClub(grokcore.site.LocalUtility):
interface.implements(IClub)

class Cave(grokcore.site.Site):
grokcore.site.local_utility(Club)

class SpikyCave(Cave):
grokcore.site.local_utility(SpikyClub)

2 changes: 1 addition & 1 deletion src/grokcore/site/ftests/utility/public.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
>>> getRootFolder()["cave"] = cave
>>> from zope import component
>>> from zope.app.component.hooks import getSite, setSite
>>> from zope.site.hooks import getSite, setSite
>>> setSite(cave)
>>> cave['fireplace'] is component.getUtility(IFireplace)
True
Expand Down
101 changes: 101 additions & 0 deletions src/grokcore/site/ftests/utility/subclass.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
"""
Subclassed sites inherit all local utilities of their base classes:
>>> cave = BigCave()
>>> getRootFolder()["cave"] = cave
>>> from zope import component
>>> from zope.site.hooks import getSite, setSite
>>> setSite(cave)
>>> fireplace = component.getUtility(IFireplace)
>>> IFireplace.providedBy(fireplace)
True
>>> isinstance(fireplace, Fireplace)
True
Additional utilities can be registered in the subclass:
>>> hollow = HollowCave()
>>> getRootFolder()["hollow"] = hollow
>>> setSite(hollow)
>>> fireplace = component.getUtility(IFireplace)
>>> IFireplace.providedBy(fireplace)
True
>>> isinstance(fireplace, Fireplace)
True
>>> painting = component.getUtility(IPainting)
>>> IPainting.providedBy(painting)
True
>>> isinstance(painting, Painting)
True
Those do not influence the base class:
>>> setSite(cave)
>>> painting = component.getUtility(IPainting)
Traceback (most recent call last):
...
ComponentLookupError: (<InterfaceClass grokcore.site.ftests.utility.subclass.IPainting>, '')
This works various levels of inheritance deep:
>>> very_hollow = VeryHollowCave()
>>> getRootFolder()['very_hollow'] = very_hollow
>>> setSite(very_hollow)
>>> fireplace = component.getUtility(IFireplace)
>>> painting = component.getUtility(IPainting)
>>> great_painting = component.getUtility(IPainting, 'great')
>>> bad_painting = component.getUtility(IPainting, 'bad')
And with inheritance hierarchies where a base class is inherited multiple
times through different routes:
>>> scary = ScaryCave()
>>> getRootFolder()['scary'] = scary
>>> setSite(scary)
>>> fireplace = component.getUtility(IFireplace)
>>> painting = component.getUtility(IPainting)
>>> great_painting = component.getUtility(IPainting, 'great')
>>> bad_painting = component.getUtility(IPainting, 'bad')
"""
import grokcore.site
from zope import interface

class IFireplace(interface.Interface):
pass

class IPainting(interface.Interface):
pass

class Fireplace(grokcore.site.LocalUtility):
interface.implements(IFireplace)

class Painting(grokcore.site.LocalUtility):
interface.implements(IPainting)

class Cave(grokcore.site.Site):
# we use name_in_container here to prevent multiple registrations
# since storing the utilities multiple times under the same name
# would raise a DuplicationError
grokcore.site.local_utility(Fireplace, name_in_container='fireplace')

class BigCave(Cave):
pass

class HollowCave(Cave):
grokcore.site.local_utility(Painting)

class VeryHollowCave(HollowCave):
grokcore.site.local_utility(Painting, name='great')
grokcore.site.local_utility(Painting, name='bad')

# this cave subclasses from Cave twice
class ScaryCave(VeryHollowCave, Cave):
pass

2 changes: 1 addition & 1 deletion src/grokcore/site/subscriber.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
##############################################################################

from zope.lifecycleevent.interfaces import IObjectAddedEvent
from zope.app.component.site import LocalSiteManager
from zope.site import LocalSiteManager

import grokcore.component
from grokcore.site.components import Site
Expand Down

0 comments on commit 88bb218

Please sign in to comment.