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

Backport PLIP 1406 for plone.app.registry 1.2.x #25

Merged
merged 5 commits into from
Feb 21, 2017
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
11 changes: 10 additions & 1 deletion CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,16 @@ Changelog
1.2.5 (unreleased)
------------------

- Nothing changed yet.
New features:

- Add support for *have* and *have-not* import conditions in
registry.xml
[datakurre]

- Add support for optional condition attribute in registry.xml entries
to allow conditional importing of records. Conditions themselves are
not import (nor exported).
[datakurre]


1.2.4 (2015-05-04)
Expand Down
32 changes: 32 additions & 0 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,38 @@ pairs. They can be configured like so::
</value>
</record>


Conditional records
~~~~~~~~~~~~~~~~~~~

Importable records in ``registry.xml`` can be marked conditional with
``condition`` attribute, which supports the following condition values:

* ``installed my.package``, which causes record to be imported only when
python module ``my.package`` is available to be imported.

* ``not-installed my.package``, which causes record to be imported only when
python module ``my.package`` is *not* available to be imported:

* ``have my-feature``, which causes record to be imported only when
ZCML feature flag ``my-feature`` has been registered (Zope2 only)

* ``not-have my-feature``, which causes record to be imported only when
ZCML feature flag ``my-feature`` has *not* been registered (Zope2 only)

For example, the following ``registry.xml`` step at the GenericSetup profile of
your policy product, would only import records when module ``my.package`` is
available::

<registry>
<records interface="my.package.interfaces.IZooSettings"
condition="installed my.package">
<value key="entryPrice">40</value>
<value key="messageOfTheDay">We've got lions and tigers!</value>
</records>
</registry>


Field references
~~~~~~~~~~~~~~~~

Expand Down
27 changes: 27 additions & 0 deletions plone/app/registry/exportimport/handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,33 @@

from plone.supermodel.utils import prettyXML, elementToValue, valueToElement, ns

from zope.configuration import config
from zope.configuration import xmlconfig

_marker = object()


def evaluateCondition(expression):
"""Evaluate import condition.

``expression`` is a string of the form "verb arguments".

Currently the supported verbs are 'have', 'not-have',
'installed' and 'not-installed'.

The 'have' verb takes one argument: the name of a feature.
"""
try:
import Zope2.App.zcml
context = Zope2.App.zcml._context or config.ConfigurationMachine()
except ImportError:
context = config.ConfigurationMachine()

handler = xmlconfig.ConfigurationHandler(context)
return handler.evaluateCondition(expression)



def shouldPurgeList(value_node, key):
for child in value_node:
attrib = child.attrib
Expand Down Expand Up @@ -88,6 +112,9 @@ def importDocument(self, document):
for node in tree:
if not isinstance(node.tag, str):
continue
condition = node.attrib.get('condition', None)
if condition and not evaluateCondition(condition):
continue
if node.tag.lower() == 'record':
self.importRecord(node)
elif node.tag.lower() == 'records':
Expand Down
133 changes: 129 additions & 4 deletions plone/app/registry/tests/test_exportimport.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,9 @@
from plone.app.registry.tests import data

configuration = """\
<configure xmlns="http://namespaces.zope.org/zope">
<configure xmlns="http://namespaces.zope.org/zope"
xmlns:meta="http://namespaces.zope.org/meta">
<meta:provides feature="plone" />
<include package="zope.component" file="meta.zcml" />
<include package="plone.registry" />
<include package="plone.app.registry.exportimport" file="handlers.zcml" />
Expand All @@ -43,7 +45,20 @@ def setUp(self):
self.site = ObjectManager('plone')
self.registry = Registry('portal_registry')
provideUtility(provides=IRegistry, component=self.registry)
xmlconfig.xmlconfig(StringIO(configuration))
context = xmlconfig.string(configuration, execute=True)
try:
import Zope2.App.zcml
self._context = Zope2.App.zcml._context
Zope2.App.zcml._context = context
except ImportError:
pass

def tearDown(self):
try:
import Zope2.App.zcml
Zope2.App.zcml._context = self._context
except ImportError:
pass

def assertXmlEquals(self, expected, actual):

Expand Down Expand Up @@ -293,6 +308,29 @@ def test_import_records_with_values(self):
self.assertEqual(self.registry['plone.app.registry.tests.data.SomethingElse.name'], 'Magic')
self.assertEqual(self.registry['plone.app.registry.tests.data.SomethingElse.age'], 42)

def test_import_records_nonexistant_interface(self):
xml = """\
<registry>
<records interface="non.existant.ISchema" />
</registry>
"""
context = DummyImportContext(self.site, purge=False)
context._files = {'registry.xml': xml}

self.assertRaises(ImportError, importRegistry, context)

def test_import_records_nonexistant_interface_condition_not_installed(self): # noqa
xml = """\
<registry>
<records interface="non.existant.ISchema"
condition="not-installed non" />
</registry>
"""
context = DummyImportContext(self.site, purge=False)
context._files = {'registry.xml': xml}

self.assertRaises(ImportError, importRegistry, context)

def test_import_value_only(self):
xml = """\
<registry>
Expand All @@ -310,8 +348,95 @@ def test_import_value_only(self):
importRegistry(context)

self.assertEquals(1, len(self.registry.records))
self.assertEquals(u"Simple record", self.registry.records['test.export.simple'].field.title)
self.assertEquals(u"Imported value", self.registry['test.export.simple'])
self.assertEquals(
u"Simple record",
self.registry.records['test.export.simple'].field.title
)
self.assertEquals(
u"Imported value",
self.registry['test.export.simple']
)

def test_import_value_only_condition_installed(self):
xml = """\
<registry>
<record name="test.export.simple"
condition="installed non">
<value>Imported value</value>
</record>
</registry>
"""
context = DummyImportContext(self.site, purge=False)
context._files = {'registry.xml': xml}

self.registry.records['test.export.simple'] = \
Record(field.TextLine(title=u"Simple record", default=u"N/A"),
value=u"Sample value")
importRegistry(context)

self.assertEquals(1, len(self.registry.records))
self.assertEquals(
u"Simple record",
self.registry.records['test.export.simple'].field.title
)
self.assertEquals(
u"Sample value",
self.registry['test.export.simple']
)

def test_import_value_only_condition_have(self):
xml = """\
<registry>
<record name="test.export.simple"
condition="have plone">
<value>Imported value</value>
</record>
</registry>
"""
context = DummyImportContext(self.site, purge=False)
context._files = {'registry.xml': xml}

self.registry.records['test.export.simple'] = \
Record(field.TextLine(title=u"Simple record", default=u"N/A"),
value=u"Sample value")
importRegistry(context)

self.assertEquals(1, len(self.registry.records))
self.assertEquals(
u"Simple record",
self.registry.records['test.export.simple'].field.title
)
self.assertEquals(
u"Imported value",
self.registry['test.export.simple']
)

def test_import_value_only_condition_not_have(self):
xml = """\
<registry>
<record name="test.export.simple"
condition="not-have plone">
<value>Imported value</value>
</record>
</registry>
"""
context = DummyImportContext(self.site, purge=False)
context._files = {'registry.xml': xml}

self.registry.records['test.export.simple'] = \
Record(field.TextLine(title=u"Simple record", default=u"N/A"),
value=u"Sample value")
importRegistry(context)

self.assertEquals(1, len(self.registry.records))
self.assertEquals(
u"Simple record",
self.registry.records['test.export.simple'].field.title
)
self.assertEquals(
u"Sample value",
self.registry['test.export.simple']
)

def test_import_interface_and_value(self):
xml = """\
Expand Down