plonegenericsetup

How to remove an element from a list type Resource Registry record using Generic Setup


In the current Plone 5 coredev buildout, I am trying to write a GenericSetup uninstall profile for an add-on that registers some css in cssregistry.xml. In Plone 5, portal_css and portal_javascripts are empty, with all those resources now automatically loaded in the Resource Registry instead. But there is no corresponding uninstall. If I have a single css resource, I get the following records in the Resource Registry:

  <record name="plone.resources/resource-myaddon-stylesheets.conf">...</record>
  <record name="plone.resources/resource-myaddon-stylesheets.css">...</record>
  <record name="plone.resources/resource-myaddon-stylesheets.deps">...</record>
  <record name="plone.resources/resource-myaddon-stylesheets.export">...</record>
  <record name="plone.resources/resource-myaddon-stylesheets.init">...</record>
  <record name="plone.resources/resource-myaddon-stylesheets.js">...</record>
  <record name="plone.resources/resource-myaddon-stylesheets.url">...</record>

(I get all these even though I have no js resources, and they all have an empty value, except for the css record.)

In addition, there is a new <element> in the following record:

<record name="plone.bundles/plone-legacy.resources" interface="Products.CMFPlone.interfaces.resources.IBundleRegistry" field="resources">

    ...

    <value>
      ...
      <element>resource-myaddon-stylesheets</element>
    </value>
</record>

As I create my GS uninstall profile, it's simple enough to remove the former 7 records in registry.xml. But how do I remove the single <element> from the latter record? I looked through the test in plone.app.registry, but removing an element does not seem to be covered.

Ultimately, it would be great if the uninstallation could be handled automatically, just like the installation is.


Solution

  • Seems like the workaround is to add something like this to Extensions/Install.py:

    def _removeBundleFromRegistry():
        logger.info('Removing bundle reference from registry')
    
        record = 'plone.bundles/plone-legacy.resources'
        resources = api.portal.get_registry_record(record)
        if u'resource-myaddon-stylesheets' in resources:
            resources.remove(u'resource-myaddon-stylesheets')
    
    def uninstall(portal, reinstall=False):
        if not reinstall:
            ...
            _removeBundleFromRegistry()
            ...
    

    Too bad.