Zope Site

6.0 · active · verified Fri Apr 17

zope.site is a foundational Python library that provides local registries for the Zope Component Architecture (ZCA). It enables the creation and retrieval of localized component managers (site managers) for specific object contexts, allowing components to be registered and looked up dynamically based on their position in an object hierarchy. The current version is 6.0, and its release cadence is tied to the broader Zope ecosystem.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to use `zope.site.site.provideSite` to establish local component registries for different contexts. It shows how local registrations are isolated to their respective sites, while global registrations remain accessible from within local sites. Note the use of `zope.location.Located` for context objects, which is common in Zope applications.

import zope.component
import zope.interface
import zope.site.site
from zope.location import Located

# 1. Define an interface for your context and services
class IMyContext(zope.interface.Interface):
    """An interface for objects that can serve as local site contexts."""

class IMyService(zope.interface.Interface):
    """An interface for a service."""

# 2. Implement the context and service
@zope.interface.implementer(IMyContext)
class MyContext(Located): # Inherit from Located for Zope site integration
    """A sample context object that can host a local site."""
    def __init__(self, name="Default"):
        self.name = name

@zope.interface.implementer(IMyService)
class MyService:
    """A sample service implementation."""
    def __init__(self, name):
        self.name = name
    def __repr__(self):
        return f"<MyService: {self.name}>"

# 3. Register a global service
print("--- Global Scope ---")
zope.component.provideUtility(IMyService, MyService("Global Service"), name="shared_service")
print(f"Globally available: {zope.component.getUtility(IMyService, name='shared_service')}")

# 4. Create a context instance and establish a local site
context_a = MyContext("Context A")
print(f"\n--- Inside Local Site for {context_a.name} ---")
with zope.site.site.provideSite(context_a):
    # Register a service locally for Context A
    zope.component.provideUtility(IMyService, MyService("Local Service A"), name="my_local_service")
    print(f"Local to {context_a.name}: {zope.component.getUtility(IMyService, name='my_local_service')}")
    # Global service is still accessible
    print(f"Global from {context_a.name}: {zope.component.getUtility(IMyService, name='shared_service')}")

# 5. Create another context and establish a different local site
context_b = MyContext("Context B")
print(f"\n--- Inside Local Site for {context_b.name} ---")
with zope.site.site.provideSite(context_b):
    # Register a service locally for Context B (different from Context A's local service)
    zope.component.provideUtility(IMyService, MyService("Local Service B"), name="my_local_service")
    print(f"Local to {context_b.name}: {zope.component.getUtility(IMyService, name='my_local_service')}")
    # Global service is still accessible
    print(f"Global from {context_b.name}: {zope.component.getUtility(IMyService, name='shared_service')}")

# 6. Outside any local site
print("\n--- Outside any Local Site ---")
try:
    # Attempting to access a local service will fail
    zope.component.getUtility(IMyService, name="my_local_service")
except zope.component.ComponentLookupError as e:
    print(f"Error: {e} (local service not available globally)")

print(f"Globally available: {zope.component.getUtility(IMyService, name='shared_service')}")

view raw JSON →