-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
migrate imports from zope.app.component to zope.site
- Loading branch information
Showing
6 changed files
with
283 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters