===============
``zope.site``
===============
.. image:: https://img.shields.io/pypi/v/zope.site.svg
:target: https://pypi.python.org/pypi/zope.site/
:alt: Latest release
.. image:: https://img.shields.io/pypi/pyversions/zope.site.svg
:target: https://pypi.org/project/zope.site/
:alt: Supported Python versions
.. image:: https://github.com/zopefoundation/zope.site/workflows/tests/badge.svg
:target: https://github.com/zopefoundation/zope.site/actions?query=workflow%3Atests
.. image:: https://coveralls.io/repos/github/zopefoundation/zope.site/badge.svg?branch=master
:target: https://coveralls.io/github/zopefoundation/zope.site?branch=master
.. image:: https://readthedocs.org/projects/zopesite/badge/?version=latest
:target: https://zopesite.readthedocs.io/en/latest/
:alt: Documentation Status
This package provides a local and persistent site manager
implementation, so that one can register local utilities and
adapters. It uses local adapter registries for its adapter and utility
registry. The module also provides some facilities to organize the
local software and ensures the correct behavior inside the ZODB.
Documentation is hosted at https://zopesite.readthedocs.io
Sites and Local Site Managers
=============================
This is an introduction of location-based component architecture.
Creating and Accessing Sites
----------------------------
*Sites* are used to provide custom component setups for parts of your
application or web site. Every folder:
>>> from zope.site import folder
>>> myfolder = folder.rootFolder()
has the potential to become a site:
>>> from zope.component.interfaces import ISite, IPossibleSite
>>> IPossibleSite.providedBy(myfolder)
True
but is not yet one:
>>> ISite.providedBy(myfolder)
False
If you would like your custom content component to be able to become a site,
you can use the `SiteManagerContainer` mix-in class:
>>> from zope import site
>>> class MyContentComponent(site.SiteManagerContainer):
... pass
>>> myContent = MyContentComponent()
>>> IPossibleSite.providedBy(myContent)
True
>>> ISite.providedBy(myContent)
False
To convert a possible site to a real site, we have to provide a site manager:
>>> sm = site.LocalSiteManager(myfolder)
>>> myfolder.setSiteManager(sm)
>>> ISite.providedBy(myfolder)
True
>>> myfolder.getSiteManager() is sm
True
Note that an event is generated when a local site manager is created:
>>> from zope.component.eventtesting import getEvents
>>> from zope.site.interfaces import INewLocalSite
>>> [event] = getEvents(INewLocalSite)
>>> event.manager is sm
True
If one tries to set a bogus site manager, a `ValueError` will be raised:
>>> myfolder2 = folder.Folder()
>>> myfolder2.setSiteManager(object)
Traceback (most recent call last):
...
ValueError: setSiteManager requires an IComponentLookup
If the possible site has been changed to a site already, a `TypeError`
is raised when one attempts to add a new site manager:
>>> myfolder.setSiteManager(site.LocalSiteManager(myfolder))
Traceback (most recent call last):
...
TypeError: Already a site
There is also an adapter you can use to get the next site manager from any
location:
>>> myfolder['mysubfolder'] = folder.Folder()
>>> import zope.interface.interfaces
>>> zope.interface.interfaces.IComponentLookup(myfolder['mysubfolder']) is sm
True
If the location passed is a site, the site manager of that site is returned:
>>> zope.interface.interfaces.IComponentLookup(myfolder) is sm
True
Using the Site Manager
----------------------
A site manager contains several *site management folders*, which are used to
logically organize the software. When a site manager is initialized, a default
site management folder is created:
>>> sm = myfolder.getSiteManager()
>>> default = sm['default']
>>> default.__class__
<class 'zope.site.site.SiteManagementFolder'>
However, you can tell not to create the default site manager folder on
LocalSiteManager creation:
>>> nodefault = site.LocalSiteManager(myfolder, default_folder=False)
>>> 'default' in nodefault
False
Also, note that when creating LocalSiteManager, its __parent__ is set to
site that was passed to constructor and the __name__ is set to ++etc++site.
>>> nodefault.__parent__ is myfolder
True
>>> nodefault.__name__ == '++etc++site'
True
You can easily create a new site management folder:
>>> sm['mySMF'] = site.SiteManagementFolder()
>>> sm['mySMF'].__class__
<class 'zope.site.site.SiteManagementFolder'>
Once you have your site management folder -- let's use the default one -- we
can register some components. Let's start with a utility (we define it
in a ``__module__`` that can be pickled):
>>> import zope.interface
>>> __name__ = 'zope.site.tests'
>>> class IMyUtility(zope.interface.Interface):
... pass
>>> import persistent
>>> from zope.container.contained import Contained
>>> @zope.interface.implementer(IMyUtility)
... class MyUtility(persistent.Persistent, Contained):
... def __init__(self, title):
... self.title = title
... def __repr__(self):
... return "%s('%s')" %(self.__class__.__name__, self.title)
Now we can create an instance of our utility and put it in the site
management folder and register it:
>>> myutil = MyUtility('My custom utility')
>>> default['myutil'] = myutil
>>> sm.registerUtility(myutil, IMyUtility, 'u1')
Now we can ask the site manager for the utility:
>>> sm.queryUtility(IMyUtility, 'u1')
MyUtility('My custom utility')
Of course, the local site manager has also access to the global component
registrations:
>>> gutil = MyUtility('Global Utility')
>>> from zope.component import getGlobalSiteManager
>>> gsm = getGlobalSiteManager()
>>> gsm.registerUtility(gutil, IMyUtility, 'gutil')
>>> sm.queryUtility(IMyUtility, 'gutil')
MyUtility('Global Utility')
Next let's see whether we can also successfully register an adapter as
well. Here the adapter will provide the size of a file:
>>> class IFile(zope.interface.Interface):
... pass
>>> class ISized(zope.interface.Interface):
... pass
>>> @zope.interface.implementer(IFile)
... class File(object):
... pass
>>> @zope.interface.implementer(ISized)
... class FileSize(object):
... def __init__(self, context):
... self.context = context
Now that we have the adapter we need to register it:
>>> sm.registerAdapter(FileSize, [IFile])
Finally, we can get the adapter for a file:
>>> file = File()
>>> size = sm.queryAdapter(file, ISized, name='')
>>> isinstance(size, FileSize)
True
>>> size.context is file
True
By the way, once you set a site
>>> from zope.component import hooks
>>> hooks.setSite(myfolder)
you can simply use the zope.component's `getSiteManager()` method to get
the nearest site manager:
>>> from zope.component import getSiteManager
>>> getSiteManager() is sm
True
This also means that you can simply use zope.component to look up your utility
>>> from zope.component import getUtility
>>> getUtility(IMyUtility, 'gutil')
MyUtility('Global Utility')
or the adapter via the interface's `__call__` method:
>>> size = ISized(file)
>>> isinstance(size, FileSize)
True
>>> size.context is file
True
Multiple Sites
--------------
Until now we have only dealt with one local and the global site. But things
really become interesting, once we have multiple sites. We can override other
local configuration.
This behaviour uses the notion of location, therefore we need to configure the
zope.location package first:
>>> import zope.configuration.xmlconfig
>>> _ = zope.configuration.xmlconfig.string("""
... <configure xmlns="http://namespaces.zope.org/zope">
... <include package="zope.component" file="meta.zcml"/>
... <include package="zope.location" />
... </configure>
... """)
Let's now create a new folder called `folder11`, add it to `myfolder` and make
it a site:
>>> myfolder11 = folder.Folder()
>>> myfolder['myfolder11'] = myfolder11
>>> myfolder11.setSiteManager(site.LocalSiteManager(myfolder11))
>>> sm11 = myfolder11.getSiteManager()
If we ask the second site manager for its next, we get
>>> sm11.__bases__ == (sm, )
True
and the first site manager should have the folling sub manager:
>>> sm.subs == (sm11,)
True
If we now register a second utility with the same name and interface with the
new site manager folder,
>>> default11 = sm11['default']
>>> myutil11 = MyUtility('Utility, uno & uno')
>>> default11['myutil'] = myutil11
>>> sm11.registerUtility(myutil11, IMyUtility, 'u1')
then it will will be available in the second site manager
>>> sm11.queryUtility(IMyUtility, 'u1')
MyUtility('Utility, uno & uno')
but not in the first one:
>>> sm.queryUtility(IMyUtility, 'u1')
MyUtility('My custom utility')
It is also interesting to look at the use cases of moving and copying a
site. To do that we create a second root folder and make it a site, so that
site hierarchy is as follows:
::
_____ global site _____
/ \
myfolder myfolder2
|
myfolder11
>>> myfolder2 = folder.rootFolder()
>>> myfolder2.setSiteManager(site.LocalSiteManager(myfolder2))
Before we can move or copy sites, we need to register two event subscribers
that manage the wiring of site managers after moving or copying:
>>> import zope.lifecycleevent.interfaces
>>> gsm.registerHandler(
... site.changeSiteConfigurationAfterMove,
... (ISite, zope.lifecycleevent.interfaces.IObjectMovedEvent),
... )
We only have to register one event listener, since the copy action causes an
`IObjectAddedEvent` to be created, which is just a special type of
`IObjectMovedEvent`.
First, make sure that everything is setup correctly in the first place:
>>> myfolder11.getSiteManager().__bases__ == (myfolder.getSiteManager(), )
True
>>> myfolder.getSiteManager().subs[0] is myfolder11.getSiteManager()
True
>>> myfolder2.getSiteManager().subs
()
Let's now move ``myfolder11`` from ``myfolder`` to ``myfolder2``:
>>> myfolder2['myfolder21'] = myfolder11
>>> del myfolder['myfolder11']
Now the next site manager for ``myfolder11``'s site manager should have changed:
>>> myfolder21 = myfolder11
>>> myfolder21.getSiteManager().__bases__ == (myfolder2.getSiteManager(), )
True
>>> myfolder2.getSiteManager().subs[0] is myfolder21.getSiteManager()
True
>>> myfolder.getSiteManager().subs
()
Make sure that our interfaces and classes are picklable:
>>> import sys
>>> sys.modules['zope.site.tests'].IMyUtility = IMyUtility
>>> sys.modules['zope.site.tests'].MyUtility = MyUtility
>>> from pickle import dumps, loads
>>> data = dumps(myfolder2['myfolder21'])
>>> myfolder['myfolder11'] = loads(data)
>>> myfolder11 = myfolder['myfolder11']
>>> myfolder11.getSiteManager().__bases__ == (myfolder.getSiteManager(), )
True
>>> myfolder.getSiteManager().subs[0] is myfolder11.getSiteManager()
True
>>> myfolder2.getSiteManager().subs[0] is myfolder21.getSiteManager()
True
Finally, let's check that everything works fine when our folder is moved
to the folder that doesn't contain any site manager. Our folder's
sitemanager's bases should be set to global site manager.
>>> myfolder11.getSiteManager().__bases__ == (myfolder.getSiteManager(), )
True
>>> nosm = folder.Folder()
>>> nosm['root'] = myfolder11
>>> myfolder11.getSiteManager().__bases__ == (gsm, )
True
Deleting a site unregisters its site manger from its parent site manager:
>>> del myfolder2['myfolder21']
>>> myfolder2.getSiteManager().subs
()
The removed site manager now has no bases:
>>> myfolder21.getSiteManager().__bases__
()
=========
Changes
=========
6.0 (2025-09-12)
================
- Replace ``pkg_resources`` namespace with PEP 420 native namespace.
5.1 (2025-02-14)
================
- Add support for Python 3.12, 3.13.
- Drop support for Python 3.7, 3.8.
5.0 (2023-06-30)
================
- Drop support for Python 2.7, 3.5, 3.6.
- Add support for Python 3.11.
4.6.1 (2022-09-02)
==================
- Fix more deprecation warnings.
4.6 (2022-08-23)
================
- Add support for Python 3.9, 3.10.
- Fix deprecation warning.
4.5.0 (2021-03-04)
==================
- Fix the interface definition of ``IRootFolder`` to give ``IRoot``
higher priority than the folder and container interfaces. This is
what is usually expected, but not what the code defined. Commonly,
in the past, this problem was hidden because the factory function
``rootFolder()`` re-arranged the interfaces to put ``IRoot`` at the
front. Under zope.interface 5's C3 resolution order, however, this
rearrangement was not taking place; thus, looking up adapters for a
``rootFolder()`` object was likely to find adapters for
``IItemContainer`` instead of adapters for ``IRoot`` as intended.
With this change, users of ``rootFolder()`` should notice no changes
compared with zope.interface 4. Code that has classes defined to
implement ``IRootFolder`` directly, though, may notice a different
resolution order on those objects (consistent with what
``rootFolder()`` generates).
See `issue 17 <https://github.com/zopefoundation/zope.site/issues/17>`_.
4.4.0 (2020-09-10)
==================
- On removal of a site, clear the bases of its site manager. This fixes a reference leak
from a parent site manager. See
`issue 1 <https://github.com/zopefoundation/zope.site/issues/1>`_.
4.3.0 (2020-04-01)
==================
- Add support for Python 3.8.
- Drop support for Python 3.4.
- Drop support for the deprecated ``python setup.py test`` command.
- Fix tests with zope.interface 5.0. See `issue 12
<https://github.com/zopefoundation/zope.site/issues/12>`_.
4.2.2 (2018-10-19)
==================
- Fix more ``DeprecationWarnings``. See `issue 10
<https://github.com/zopefoundation/zope.site/issues/10>`_.
4.2.1 (2018-10-11)
==================
- Use current import location for ``UtilityRegistration`` and ``IUtilityRegistration``
classes to avoid ``DeprecationWarning``.
4.2.0 (2018-10-09)
==================
- Add support for Python 3.7.
4.1.0 (2017-08-08)
==================
- Add support for Python 3.5 and 3.6.
- Drop support for Python 2.6 and 3.3.
- Deprecate ``zope.site.hooks.*``, ``zope.site.site.setSite``,
``zope.site.next.getNextUtility`` and ``zope.site.next.queryNextUtility``
with ``zope.deprecation``. These will be removed in version 5.0.
They all have replacements in ``zope.component``.
- Added implementation for _p_repr in LocalSiteManager. For further
information see `issue 8
<https://github.com/zopefoundation/zope.site/issues/8>`_.
- Reach 100% test coverage and ensure we remain there.
4.0.0 (2014-12-24)
==================
- Add support for PyPy.
- Add support for Python 3.4.
- Add support for testing on Travis.
4.0.0a1 (2013-02-20)
====================
- Added support for Python 3.3.
- Replaced deprecated ``zope.interface.implements`` usage with equivalent
``zope.interface.implementer`` decorator.
- Dropped support for Python 2.4 and 2.5.
- Include zcml dependencies in configure.zcml, added tests for zcml.
3.9.2 (2010-09-25)
==================
- Added not declared, but needed test dependency on `zope.testing`.
3.9.1 (2010-04-30)
==================
- Removed use of 'zope.testing.doctest' in favor of stdlib's 'doctest.
- Removed use of 'zope.testing.doctestunit' in favor of stdlib's 'doctest.
3.9.0 (2009-12-29)
==================
- Avoid a test dependency on zope.copypastemove by testing the correct
persistent behavior of a site manager using the normal pickle module.
3.8.0 (2009-12-15)
==================
- Removed functional testing setup and dependency on zope.app.testing.
3.7.1 (2009-11-18)
==================
- Moved the zope.site.hooks functionality to zope.component.hooks as it isn't
actually dealing with zope.site's concept of a site.
- Import ISite and IPossibleSite from zope.component after they were moved
there from zope.location.
3.7.0 (2009-09-29)
==================
- Cleaned up the undeclared dependency on zope.app.publication by moving the
two relevant subscriber registrations and their tests to that package.
- Dropped the dependency on zope.traversing which was only used to access
zope.location functionality. Configure zope.location for some tests.
- Demoted zope.configuration to a testing dependency.
3.6.4 (2009-09-01)
==================
- Set __parent__ and __name__ in the LocalSiteManager's constructor
after calling constructor of its superclasses, so __name__ doesn't
get overwritten with empty string by the Components constructor.
- Don't set __parent__ and __name__ attributes of site manager in
SiteManagerContainer's ``setSiteManager`` method, as they're
already set for LocalSiteManager. Other site manager implementations
are not required to have those attributes at all, so we're not
adding them anymore.
3.6.3 (2009-07-27)
==================
- Propagate an ObjectRemovedEvent to the SiteManager upon removal of a
SiteManagerContainer.
3.6.2 (2009-07-24)
==================
- Fixed tests to pass with latest packages.
- Removed failing test of persistent interfaces, since it did not test
anything in this package and used the deprecated ``zodbcode`` module.
- Fix NameError when calling ``zope.site.testing.siteSetUp(site=True)``.
- The ``getNextUtility`` and ``queryNextUtility`` functions was moved to
``zope.component``. While backward-compatibility imports are provided, it's
strongly recommended to update your imports.
3.6.1 (2009-02-28)
==================
- Import symbols moved from zope.traversing to zope.location from the new
location.
- Don't fail when changing component registry bases while moving ISite
object to non-ISite object.
- Allow specify whether to create 'default' SiteManagementFolder on
initializing LocalSiteManager. Use the ``default_folder`` argument.
- Add a containment constraint to the SiteManagementFolder that makes
it only available to be contained in ILocalSiteManagers and other
ISiteManagementFolders.
- Change package's mailing list address to zope-dev at zope.org, as
zope3-dev at zope.org is now retired.
- Remove old unused code. Update package description.
3.6.0 (2009-01-31)
==================
- Use zope.container instead of zope.app.container.
3.5.1 (2009-01-27)
==================
- Extracted from zope.app.component (trunk, 3.5.1 under development)
as part of an effort to clean up dependencies between Zope packages.
Raw data
{
"_id": null,
"home_page": "http://zopesite.readthedocs.io",
"name": "zope.site",
"maintainer": null,
"docs_url": null,
"requires_python": ">=3.9",
"maintainer_email": null,
"keywords": "zope component architecture local",
"author": "Zope Foundation and Contributors",
"author_email": "zope-dev@zope.dev",
"download_url": "https://files.pythonhosted.org/packages/fd/d1/e9d559d9b1af35c74ea5eaa8bdcc8a6910d304eaa1e13285bacbc92dcad3/zope_site-6.0.tar.gz",
"platform": null,
"description": "===============\n ``zope.site``\n===============\n\n.. image:: https://img.shields.io/pypi/v/zope.site.svg\n :target: https://pypi.python.org/pypi/zope.site/\n :alt: Latest release\n\n.. image:: https://img.shields.io/pypi/pyversions/zope.site.svg\n :target: https://pypi.org/project/zope.site/\n :alt: Supported Python versions\n\n.. image:: https://github.com/zopefoundation/zope.site/workflows/tests/badge.svg\n :target: https://github.com/zopefoundation/zope.site/actions?query=workflow%3Atests\n\n.. image:: https://coveralls.io/repos/github/zopefoundation/zope.site/badge.svg?branch=master\n :target: https://coveralls.io/github/zopefoundation/zope.site?branch=master\n\n.. image:: https://readthedocs.org/projects/zopesite/badge/?version=latest\n :target: https://zopesite.readthedocs.io/en/latest/\n :alt: Documentation Status\n\n\nThis package provides a local and persistent site manager\nimplementation, so that one can register local utilities and\nadapters. It uses local adapter registries for its adapter and utility\nregistry. The module also provides some facilities to organize the\nlocal software and ensures the correct behavior inside the ZODB.\n\nDocumentation is hosted at https://zopesite.readthedocs.io\n\n\nSites and Local Site Managers\n=============================\n\nThis is an introduction of location-based component architecture.\n\nCreating and Accessing Sites\n----------------------------\n\n*Sites* are used to provide custom component setups for parts of your\napplication or web site. Every folder:\n\n >>> from zope.site import folder\n >>> myfolder = folder.rootFolder()\n\nhas the potential to become a site:\n\n >>> from zope.component.interfaces import ISite, IPossibleSite\n >>> IPossibleSite.providedBy(myfolder)\n True\n\nbut is not yet one:\n\n >>> ISite.providedBy(myfolder)\n False\n\nIf you would like your custom content component to be able to become a site,\nyou can use the `SiteManagerContainer` mix-in class:\n\n >>> from zope import site\n >>> class MyContentComponent(site.SiteManagerContainer):\n ... pass\n\n >>> myContent = MyContentComponent()\n >>> IPossibleSite.providedBy(myContent)\n True\n >>> ISite.providedBy(myContent)\n False\n\nTo convert a possible site to a real site, we have to provide a site manager:\n\n >>> sm = site.LocalSiteManager(myfolder)\n >>> myfolder.setSiteManager(sm)\n >>> ISite.providedBy(myfolder)\n True\n >>> myfolder.getSiteManager() is sm\n True\n\nNote that an event is generated when a local site manager is created:\n\n >>> from zope.component.eventtesting import getEvents\n >>> from zope.site.interfaces import INewLocalSite\n >>> [event] = getEvents(INewLocalSite)\n >>> event.manager is sm\n True\n\nIf one tries to set a bogus site manager, a `ValueError` will be raised:\n\n >>> myfolder2 = folder.Folder()\n >>> myfolder2.setSiteManager(object)\n Traceback (most recent call last):\n ...\n ValueError: setSiteManager requires an IComponentLookup\n\nIf the possible site has been changed to a site already, a `TypeError`\nis raised when one attempts to add a new site manager:\n\n >>> myfolder.setSiteManager(site.LocalSiteManager(myfolder))\n Traceback (most recent call last):\n ...\n TypeError: Already a site\n\nThere is also an adapter you can use to get the next site manager from any\nlocation:\n\n >>> myfolder['mysubfolder'] = folder.Folder()\n >>> import zope.interface.interfaces\n >>> zope.interface.interfaces.IComponentLookup(myfolder['mysubfolder']) is sm\n True\n\nIf the location passed is a site, the site manager of that site is returned:\n\n >>> zope.interface.interfaces.IComponentLookup(myfolder) is sm\n True\n\n\nUsing the Site Manager\n----------------------\n\nA site manager contains several *site management folders*, which are used to\nlogically organize the software. When a site manager is initialized, a default\nsite management folder is created:\n\n >>> sm = myfolder.getSiteManager()\n >>> default = sm['default']\n >>> default.__class__\n <class 'zope.site.site.SiteManagementFolder'>\n\nHowever, you can tell not to create the default site manager folder on\nLocalSiteManager creation:\n\n >>> nodefault = site.LocalSiteManager(myfolder, default_folder=False)\n >>> 'default' in nodefault\n False\n\nAlso, note that when creating LocalSiteManager, its __parent__ is set to\nsite that was passed to constructor and the __name__ is set to ++etc++site.\n\n >>> nodefault.__parent__ is myfolder\n True\n >>> nodefault.__name__ == '++etc++site'\n True\n\nYou can easily create a new site management folder:\n\n >>> sm['mySMF'] = site.SiteManagementFolder()\n >>> sm['mySMF'].__class__\n <class 'zope.site.site.SiteManagementFolder'>\n\nOnce you have your site management folder -- let's use the default one -- we\ncan register some components. Let's start with a utility (we define it\nin a ``__module__`` that can be pickled):\n\n >>> import zope.interface\n >>> __name__ = 'zope.site.tests'\n >>> class IMyUtility(zope.interface.Interface):\n ... pass\n\n >>> import persistent\n >>> from zope.container.contained import Contained\n >>> @zope.interface.implementer(IMyUtility)\n ... class MyUtility(persistent.Persistent, Contained):\n ... def __init__(self, title):\n ... self.title = title\n ... def __repr__(self):\n ... return \"%s('%s')\" %(self.__class__.__name__, self.title)\n\nNow we can create an instance of our utility and put it in the site\nmanagement folder and register it:\n\n >>> myutil = MyUtility('My custom utility')\n >>> default['myutil'] = myutil\n >>> sm.registerUtility(myutil, IMyUtility, 'u1')\n\nNow we can ask the site manager for the utility:\n\n >>> sm.queryUtility(IMyUtility, 'u1')\n MyUtility('My custom utility')\n\nOf course, the local site manager has also access to the global component\nregistrations:\n\n >>> gutil = MyUtility('Global Utility')\n >>> from zope.component import getGlobalSiteManager\n >>> gsm = getGlobalSiteManager()\n >>> gsm.registerUtility(gutil, IMyUtility, 'gutil')\n\n >>> sm.queryUtility(IMyUtility, 'gutil')\n MyUtility('Global Utility')\n\nNext let's see whether we can also successfully register an adapter as\nwell. Here the adapter will provide the size of a file:\n\n >>> class IFile(zope.interface.Interface):\n ... pass\n\n >>> class ISized(zope.interface.Interface):\n ... pass\n\n >>> @zope.interface.implementer(IFile)\n ... class File(object):\n ... pass\n\n >>> @zope.interface.implementer(ISized)\n ... class FileSize(object):\n ... def __init__(self, context):\n ... self.context = context\n\nNow that we have the adapter we need to register it:\n\n >>> sm.registerAdapter(FileSize, [IFile])\n\nFinally, we can get the adapter for a file:\n\n >>> file = File()\n >>> size = sm.queryAdapter(file, ISized, name='')\n >>> isinstance(size, FileSize)\n True\n >>> size.context is file\n True\n\nBy the way, once you set a site\n\n >>> from zope.component import hooks\n >>> hooks.setSite(myfolder)\n\nyou can simply use the zope.component's `getSiteManager()` method to get\nthe nearest site manager:\n\n >>> from zope.component import getSiteManager\n >>> getSiteManager() is sm\n True\n\nThis also means that you can simply use zope.component to look up your utility\n\n >>> from zope.component import getUtility\n >>> getUtility(IMyUtility, 'gutil')\n MyUtility('Global Utility')\n\nor the adapter via the interface's `__call__` method:\n\n >>> size = ISized(file)\n >>> isinstance(size, FileSize)\n True\n >>> size.context is file\n True\n\n\nMultiple Sites\n--------------\n\nUntil now we have only dealt with one local and the global site. But things\nreally become interesting, once we have multiple sites. We can override other\nlocal configuration.\n\nThis behaviour uses the notion of location, therefore we need to configure the\nzope.location package first:\n\n >>> import zope.configuration.xmlconfig\n >>> _ = zope.configuration.xmlconfig.string(\"\"\"\n ... <configure xmlns=\"http://namespaces.zope.org/zope\">\n ... <include package=\"zope.component\" file=\"meta.zcml\"/>\n ... <include package=\"zope.location\" />\n ... </configure>\n ... \"\"\")\n\nLet's now create a new folder called `folder11`, add it to `myfolder` and make\nit a site:\n\n >>> myfolder11 = folder.Folder()\n >>> myfolder['myfolder11'] = myfolder11\n >>> myfolder11.setSiteManager(site.LocalSiteManager(myfolder11))\n >>> sm11 = myfolder11.getSiteManager()\n\nIf we ask the second site manager for its next, we get\n\n >>> sm11.__bases__ == (sm, )\n True\n\nand the first site manager should have the folling sub manager:\n\n >>> sm.subs == (sm11,)\n True\n\nIf we now register a second utility with the same name and interface with the\nnew site manager folder,\n\n >>> default11 = sm11['default']\n >>> myutil11 = MyUtility('Utility, uno & uno')\n >>> default11['myutil'] = myutil11\n\n >>> sm11.registerUtility(myutil11, IMyUtility, 'u1')\n\nthen it will will be available in the second site manager\n\n >>> sm11.queryUtility(IMyUtility, 'u1')\n MyUtility('Utility, uno & uno')\n\nbut not in the first one:\n\n >>> sm.queryUtility(IMyUtility, 'u1')\n MyUtility('My custom utility')\n\nIt is also interesting to look at the use cases of moving and copying a\nsite. To do that we create a second root folder and make it a site, so that\nsite hierarchy is as follows:\n\n::\n\n _____ global site _____\n / \\\n myfolder myfolder2\n |\n myfolder11\n\n\n >>> myfolder2 = folder.rootFolder()\n >>> myfolder2.setSiteManager(site.LocalSiteManager(myfolder2))\n\nBefore we can move or copy sites, we need to register two event subscribers\nthat manage the wiring of site managers after moving or copying:\n\n >>> import zope.lifecycleevent.interfaces\n >>> gsm.registerHandler(\n ... site.changeSiteConfigurationAfterMove,\n ... (ISite, zope.lifecycleevent.interfaces.IObjectMovedEvent),\n ... )\n\nWe only have to register one event listener, since the copy action causes an\n`IObjectAddedEvent` to be created, which is just a special type of\n`IObjectMovedEvent`.\n\nFirst, make sure that everything is setup correctly in the first place:\n\n >>> myfolder11.getSiteManager().__bases__ == (myfolder.getSiteManager(), )\n True\n >>> myfolder.getSiteManager().subs[0] is myfolder11.getSiteManager()\n True\n >>> myfolder2.getSiteManager().subs\n ()\n\nLet's now move ``myfolder11`` from ``myfolder`` to ``myfolder2``:\n\n >>> myfolder2['myfolder21'] = myfolder11\n >>> del myfolder['myfolder11']\n\nNow the next site manager for ``myfolder11``'s site manager should have changed:\n\n >>> myfolder21 = myfolder11\n >>> myfolder21.getSiteManager().__bases__ == (myfolder2.getSiteManager(), )\n True\n >>> myfolder2.getSiteManager().subs[0] is myfolder21.getSiteManager()\n True\n >>> myfolder.getSiteManager().subs\n ()\n\nMake sure that our interfaces and classes are picklable:\n\n >>> import sys\n >>> sys.modules['zope.site.tests'].IMyUtility = IMyUtility\n >>> sys.modules['zope.site.tests'].MyUtility = MyUtility\n\n >>> from pickle import dumps, loads\n >>> data = dumps(myfolder2['myfolder21'])\n >>> myfolder['myfolder11'] = loads(data)\n\n >>> myfolder11 = myfolder['myfolder11']\n >>> myfolder11.getSiteManager().__bases__ == (myfolder.getSiteManager(), )\n True\n >>> myfolder.getSiteManager().subs[0] is myfolder11.getSiteManager()\n True\n >>> myfolder2.getSiteManager().subs[0] is myfolder21.getSiteManager()\n True\n\nFinally, let's check that everything works fine when our folder is moved\nto the folder that doesn't contain any site manager. Our folder's\nsitemanager's bases should be set to global site manager.\n\n >>> myfolder11.getSiteManager().__bases__ == (myfolder.getSiteManager(), )\n True\n\n >>> nosm = folder.Folder()\n >>> nosm['root'] = myfolder11\n >>> myfolder11.getSiteManager().__bases__ == (gsm, )\n True\n\nDeleting a site unregisters its site manger from its parent site manager:\n\n >>> del myfolder2['myfolder21']\n >>> myfolder2.getSiteManager().subs\n ()\n\nThe removed site manager now has no bases:\n\n >>> myfolder21.getSiteManager().__bases__\n ()\n\n\n=========\n Changes\n=========\n\n6.0 (2025-09-12)\n================\n\n- Replace ``pkg_resources`` namespace with PEP 420 native namespace.\n\n\n5.1 (2025-02-14)\n================\n\n- Add support for Python 3.12, 3.13.\n\n- Drop support for Python 3.7, 3.8.\n\n\n5.0 (2023-06-30)\n================\n\n- Drop support for Python 2.7, 3.5, 3.6.\n\n- Add support for Python 3.11.\n\n\n4.6.1 (2022-09-02)\n==================\n\n- Fix more deprecation warnings.\n\n\n4.6 (2022-08-23)\n================\n\n- Add support for Python 3.9, 3.10.\n\n- Fix deprecation warning.\n\n\n4.5.0 (2021-03-04)\n==================\n\n- Fix the interface definition of ``IRootFolder`` to give ``IRoot``\n higher priority than the folder and container interfaces. This is\n what is usually expected, but not what the code defined. Commonly,\n in the past, this problem was hidden because the factory function\n ``rootFolder()`` re-arranged the interfaces to put ``IRoot`` at the\n front. Under zope.interface 5's C3 resolution order, however, this\n rearrangement was not taking place; thus, looking up adapters for a\n ``rootFolder()`` object was likely to find adapters for\n ``IItemContainer`` instead of adapters for ``IRoot`` as intended.\n\n With this change, users of ``rootFolder()`` should notice no changes\n compared with zope.interface 4. Code that has classes defined to\n implement ``IRootFolder`` directly, though, may notice a different\n resolution order on those objects (consistent with what\n ``rootFolder()`` generates).\n\n See `issue 17 <https://github.com/zopefoundation/zope.site/issues/17>`_.\n\n\n4.4.0 (2020-09-10)\n==================\n\n- On removal of a site, clear the bases of its site manager. This fixes a reference leak\n from a parent site manager. See\n `issue 1 <https://github.com/zopefoundation/zope.site/issues/1>`_.\n\n\n4.3.0 (2020-04-01)\n==================\n\n- Add support for Python 3.8.\n\n- Drop support for Python 3.4.\n\n- Drop support for the deprecated ``python setup.py test`` command.\n\n- Fix tests with zope.interface 5.0. See `issue 12\n <https://github.com/zopefoundation/zope.site/issues/12>`_.\n\n\n4.2.2 (2018-10-19)\n==================\n\n- Fix more ``DeprecationWarnings``. See `issue 10\n <https://github.com/zopefoundation/zope.site/issues/10>`_.\n\n\n4.2.1 (2018-10-11)\n==================\n\n- Use current import location for ``UtilityRegistration`` and ``IUtilityRegistration``\n classes to avoid ``DeprecationWarning``.\n\n\n4.2.0 (2018-10-09)\n==================\n\n- Add support for Python 3.7.\n\n\n4.1.0 (2017-08-08)\n==================\n\n- Add support for Python 3.5 and 3.6.\n\n- Drop support for Python 2.6 and 3.3.\n\n- Deprecate ``zope.site.hooks.*``, ``zope.site.site.setSite``,\n ``zope.site.next.getNextUtility`` and ``zope.site.next.queryNextUtility``\n with ``zope.deprecation``. These will be removed in version 5.0.\n They all have replacements in ``zope.component``.\n\n- Added implementation for _p_repr in LocalSiteManager. For further\n information see `issue 8\n <https://github.com/zopefoundation/zope.site/issues/8>`_.\n\n- Reach 100% test coverage and ensure we remain there.\n\n\n4.0.0 (2014-12-24)\n==================\n\n- Add support for PyPy.\n\n- Add support for Python 3.4.\n\n- Add support for testing on Travis.\n\n\n4.0.0a1 (2013-02-20)\n====================\n\n- Added support for Python 3.3.\n\n- Replaced deprecated ``zope.interface.implements`` usage with equivalent\n ``zope.interface.implementer`` decorator.\n\n- Dropped support for Python 2.4 and 2.5.\n\n- Include zcml dependencies in configure.zcml, added tests for zcml.\n\n\n3.9.2 (2010-09-25)\n==================\n\n- Added not declared, but needed test dependency on `zope.testing`.\n\n3.9.1 (2010-04-30)\n==================\n\n- Removed use of 'zope.testing.doctest' in favor of stdlib's 'doctest.\n\n- Removed use of 'zope.testing.doctestunit' in favor of stdlib's 'doctest.\n\n3.9.0 (2009-12-29)\n==================\n\n- Avoid a test dependency on zope.copypastemove by testing the correct\n persistent behavior of a site manager using the normal pickle module.\n\n3.8.0 (2009-12-15)\n==================\n\n- Removed functional testing setup and dependency on zope.app.testing.\n\n3.7.1 (2009-11-18)\n==================\n\n- Moved the zope.site.hooks functionality to zope.component.hooks as it isn't\n actually dealing with zope.site's concept of a site.\n\n- Import ISite and IPossibleSite from zope.component after they were moved\n there from zope.location.\n\n3.7.0 (2009-09-29)\n==================\n\n- Cleaned up the undeclared dependency on zope.app.publication by moving the\n two relevant subscriber registrations and their tests to that package.\n\n- Dropped the dependency on zope.traversing which was only used to access\n zope.location functionality. Configure zope.location for some tests.\n\n- Demoted zope.configuration to a testing dependency.\n\n3.6.4 (2009-09-01)\n==================\n\n- Set __parent__ and __name__ in the LocalSiteManager's constructor\n after calling constructor of its superclasses, so __name__ doesn't\n get overwritten with empty string by the Components constructor.\n\n- Don't set __parent__ and __name__ attributes of site manager in\n SiteManagerContainer's ``setSiteManager`` method, as they're\n already set for LocalSiteManager. Other site manager implementations\n are not required to have those attributes at all, so we're not\n adding them anymore.\n\n3.6.3 (2009-07-27)\n==================\n\n- Propagate an ObjectRemovedEvent to the SiteManager upon removal of a\n SiteManagerContainer.\n\n3.6.2 (2009-07-24)\n==================\n\n- Fixed tests to pass with latest packages.\n\n- Removed failing test of persistent interfaces, since it did not test\n anything in this package and used the deprecated ``zodbcode`` module.\n\n- Fix NameError when calling ``zope.site.testing.siteSetUp(site=True)``.\n\n- The ``getNextUtility`` and ``queryNextUtility`` functions was moved to\n ``zope.component``. While backward-compatibility imports are provided, it's\n strongly recommended to update your imports.\n\n3.6.1 (2009-02-28)\n==================\n\n- Import symbols moved from zope.traversing to zope.location from the new\n location.\n\n- Don't fail when changing component registry bases while moving ISite\n object to non-ISite object.\n\n- Allow specify whether to create 'default' SiteManagementFolder on\n initializing LocalSiteManager. Use the ``default_folder`` argument.\n\n- Add a containment constraint to the SiteManagementFolder that makes\n it only available to be contained in ILocalSiteManagers and other\n ISiteManagementFolders.\n\n- Change package's mailing list address to zope-dev at zope.org, as\n zope3-dev at zope.org is now retired.\n\n- Remove old unused code. Update package description.\n\n3.6.0 (2009-01-31)\n==================\n\n- Use zope.container instead of zope.app.container.\n\n3.5.1 (2009-01-27)\n==================\n\n- Extracted from zope.app.component (trunk, 3.5.1 under development)\n as part of an effort to clean up dependencies between Zope packages.\n",
"bugtrack_url": null,
"license": "ZPL-2.1",
"summary": "Local registries for zope component architecture",
"version": "6.0",
"project_urls": {
"Homepage": "http://zopesite.readthedocs.io"
},
"split_keywords": [
"zope",
"component",
"architecture",
"local"
],
"urls": [
{
"comment_text": null,
"digests": {
"blake2b_256": "b0d02363e2e12f2483b82c0f9358b7a789be5b04a6112a98e0c91390d6da13e2",
"md5": "a007bb48ba0401dc045f2011162d971a",
"sha256": "6b0dfed73f4c2ec88665989bf188f1a3b20eaa60107d895e195fea109d4db7c4"
},
"downloads": -1,
"filename": "zope_site-6.0-py3-none-any.whl",
"has_sig": false,
"md5_digest": "a007bb48ba0401dc045f2011162d971a",
"packagetype": "bdist_wheel",
"python_version": "py3",
"requires_python": ">=3.9",
"size": 29773,
"upload_time": "2025-09-12T07:43:39",
"upload_time_iso_8601": "2025-09-12T07:43:39.620097Z",
"url": "https://files.pythonhosted.org/packages/b0/d0/2363e2e12f2483b82c0f9358b7a789be5b04a6112a98e0c91390d6da13e2/zope_site-6.0-py3-none-any.whl",
"yanked": false,
"yanked_reason": null
},
{
"comment_text": null,
"digests": {
"blake2b_256": "fdd1e9d559d9b1af35c74ea5eaa8bdcc8a6910d304eaa1e13285bacbc92dcad3",
"md5": "a943f59e8dbd71f929102d72cf0be7c0",
"sha256": "d8139a3ad0f3e51732082dc98db8de29c834a6594126e63f25be6ea2ebc4064b"
},
"downloads": -1,
"filename": "zope_site-6.0.tar.gz",
"has_sig": false,
"md5_digest": "a943f59e8dbd71f929102d72cf0be7c0",
"packagetype": "sdist",
"python_version": "source",
"requires_python": ">=3.9",
"size": 38592,
"upload_time": "2025-09-12T07:43:40",
"upload_time_iso_8601": "2025-09-12T07:43:40.765554Z",
"url": "https://files.pythonhosted.org/packages/fd/d1/e9d559d9b1af35c74ea5eaa8bdcc8a6910d304eaa1e13285bacbc92dcad3/zope_site-6.0.tar.gz",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2025-09-12 07:43:40",
"github": false,
"gitlab": false,
"bitbucket": false,
"codeberg": false,
"lcname": "zope.site"
}