Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Prefer component (over factory) in the serialization of utility registrations #128

Merged
merged 2 commits into from
May 17, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,10 @@ Changelog
3.0.2 (unreleased)
------------------

- Nothing changed yet.
- Use ``component`` in the serialization of utility registrations
if the registered component is a "global object".
Fixes
`#6 <https://github.com/zopefoundation/Products.GenericSetup/issues/6>_`.


3.0.1 (2023-03-03)
Expand Down
3 changes: 3 additions & 0 deletions src/Products/GenericSetup/components.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
from .interfaces import ISetupEnviron
from .utils import XMLAdapterBase
from .utils import _getDottedName
from .utils import _isGlobalObject
from .utils import _resolveDottedName


Expand Down Expand Up @@ -495,6 +496,8 @@ def _extractUtilities(self):
# This is a five.localsitemanager wrapped utility
factory = _getDottedName(type(aq_base(comp)))
child.setAttribute('factory', factory)
elif _isGlobalObject(comp):
child.setAttribute('component', _getDottedName(comp))
else:
factory = _getDottedName(type(comp))
child.setAttribute('factory', factory)
Expand Down
42 changes: 42 additions & 0 deletions src/Products/GenericSetup/tests/test_components.py
Original file line number Diff line number Diff line change
Expand Up @@ -239,6 +239,20 @@ def getExcludedInterfaces(self):
"""


_INTERFACE_COMPONENT = b"""\
<?xml version="1.0" encoding="utf-8"?>
<componentregistry>
<adapters/>
<subscribers/>
<utilities>
<utility name="test_interface"
component="Products.GenericSetup.tests.test_components.ITestInterface"
interface="Products.GenericSetup.tests.test_components.ITestInterfaceType"/>
</utilities>
</componentregistry>
"""


class ComponentRegistryXMLAdapterTests(BodyAdapterTestCase, unittest.TestCase):

layer = ExportImportZCMLLayer
Expand Down Expand Up @@ -434,6 +448,26 @@ def test_remove_components(self):
util = queryUtility(IDummyInterface2, name='dummy tool name2')
self.assertFalse(util is None)

def test_export_interface_component(self):
sm = self._obj
sm.registerUtility(ITestInterface,
ITestInterfaceType,
"test_interface")
context = DummySetupEnviron()
adapted = getMultiAdapter((sm, context), IBody)
body = adapted.body
self.assertEqual(body, _INTERFACE_COMPONENT)

def test_import_interface_component(self):
from ..components import importComponentRegistry
sm = self._obj
context = DummyImportContext(sm, False)
context._files['componentregistry.xml'] = _INTERFACE_COMPONENT
importComponentRegistry(context)
self.assertIs(
sm.queryUtility(ITestInterfaceType, name="test_interface"),
ITestInterface)

def setUp(self):
# Create and enable a local component registry
site = Folder()
Expand All @@ -459,6 +493,14 @@ def tearDown(self):
name='dummy')


class ITestInterface(Interface):
"""interface registered as utility."""


class ITestInterfaceType(Interface):
"""interface provided by ``ITestInterface``."""


if PersistentComponents is not None:
def test_suite():
# reimport to make sure tests are run from Products
Expand Down
9 changes: 9 additions & 0 deletions src/Products/GenericSetup/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,15 @@ def _resolveDottedName(dotted):
return


def _isGlobalObject(obj):
"""Is *obj* identified by a dotted name?"""
try:
dn = _getDottedName(obj)
return obj is _resolveDottedName(dn)
except Exception:
return False


def _extractDocstring(func, default_title, default_description):
try:
doc = getdoc(func)
Expand Down