Back to index

plone3  3.1.7
registry.py
Go to the documentation of this file.
00001 from zope.interface import implements
00002 from zope.component import (
00003     adapter, 
00004     getSiteManager,
00005     )
00006 from interfaces import (
00007     IKSSDemoRegistry,
00008     IKSSSeleniumTestRegistry,
00009     IKSSDemoResource,
00010     IKSSSeleniumTestResource,
00011     IKSSDemoRegistrationEvent,
00012     IKSSDemoRegistryEvent,
00013     )
00014 from zope.component.interfaces import (
00015     IUtilityRegistration,
00016     IRegistered,
00017     IUnregistered,
00018     )
00019 from kss.demo.selenium_utils.builder import getSeleniumTestsFromSuite
00020 
00021 # --
00022 # Registry implementation for use with Zope
00023 # --
00024 
00025 
00026 # Create a mesh of provided interfaces
00027 # This is needed, because an utility must have a single interface.
00028 class IRegistry(IKSSDemoRegistry, IKSSSeleniumTestRegistry):
00029     pass
00030 
00031 class KSSDemoRegistry(object):
00032     """KSS demo registry.
00033     """
00034     implements(IRegistry)
00035 
00036     def __init__(self):
00037         # We will set up my handlers to get notified of new plugins
00038         # (works via redispatching by events.py)
00039         site = getSiteManager()
00040         # registry for demos
00041         self.demos_dict = {}
00042         self.demos = []
00043         self.demos_are_sorted = False
00044         site.registerHandler(self.registerDemosFromPlugin)
00045         site.registerHandler(self.unregisterDemosFromPlugin)
00046         site.registerHandler(self.registerEarlierDemos)
00047         # registry for selenium tests
00048         self.selenium_tests = []
00049         site.registerHandler(self.registerSeleniumTestsFromPlugin)
00050         site.registerHandler(self.unregisterSeleniumTestsFromPlugin)
00051         # ... the rest of setup will be done from registerEarlierDemos
00052 
00053     @adapter(IKSSDemoRegistry, IUtilityRegistration, IRegistered, IKSSDemoRegistryEvent)
00054     def registerEarlierDemos(self, registry, registration=None, event=None, new_event=None):
00055         """Make sure that the resources registered earlier, are added
00056          (so this is a listener to the registration of myself
00057          which is needed because CA is not ready at time of the __init__)
00058         """
00059         if registry != self:
00060             # The utility only register on itself.
00061             return
00062         site = getSiteManager()
00063         for name, plugin in site.getUtilitiesFor(IKSSDemoResource):
00064             for demo in plugin.demos:
00065                 self.registerDemo(demo)
00066             for test_filename in self._getSeleniumTestsFromPlugin(plugin):
00067                 self.registerSeleniumTestFile(test_filename)
00068 
00069     @adapter(IKSSDemoResource, IUtilityRegistration, IRegistered, IKSSDemoRegistrationEvent)
00070     def registerDemosFromPlugin(self, plugin, registration=None, event=None, new_event=None):
00071         """Add a demo collection to the registry.
00072         """
00073         for demo in plugin.demos:
00074             self.registerDemo(demo)
00075 
00076     @adapter(IKSSDemoResource, IUtilityRegistration, IUnregistered, IKSSDemoRegistrationEvent)
00077     def unregisterDemosFromPlugin(self, plugin, registration=None, event=None, new_event=None):
00078         """Remove a demo collection from the registry.
00079         """
00080         for demo in plugin.demos:
00081             self.unregisterDemo(demo)
00082 
00083     def registerDemo(self, demo):
00084         """Register a demo
00085 
00086         It has the attributes specified in IKSSDemo:
00087 
00088         plugin_namespace - string with the name of the plugin.
00089                            Or: "" when it is the core part.
00090 
00091         category         - text that will appear as the title of the
00092                            category. "" if out of category.
00093 
00094         demo_page        - (relative) url of the demo page. This should
00095                            traverse on ISimpleContent.
00096 
00097         title            - Title of the demo. This also identifies it
00098                            for removal.
00099         """
00100         key = demo.plugin_namespace, demo.category, demo.page_url
00101         if key in self.demos:
00102             raise Exception, 'The demo for %s has already been registered. Cannot add.' % (key, )
00103         self.demos_dict[key] = demo
00104         self.demos.append(demo)
00105         self.demos_are_sorted = False
00106 
00107     def unregisterDemo(self, demo):
00108         """Unregister the given demo."""
00109         key = demo.plugin_namespace, demo.category, demo.demo_page
00110         try:
00111             value = self.demos_dict[key]
00112         except KeyError:
00113             raise Exception, 'The demo for %s is yet unregistered. Cannot remove.' % (key, )
00114         del self.demos_dict[key]
00115         self.demos.remove(value)
00116 
00117     def getSortedDemos(self):
00118         """Get the (sorted) list of demos"""
00119         if not self.demos_are_sorted:
00120             self.demos.sort(key=lambda demo: (
00121                 demo.plugin_namespace,
00122                 demo.category,
00123                 ))
00124             self.demos_are_sorted = True
00125         return list(self.demos)
00126 
00127     # --
00128     # Selenium tests
00129     # --
00130 
00131     @staticmethod
00132     def _getSeleniumTestsFromPlugin(plugin):
00133         test_filenames = []
00134         for selenium_test_suite in plugin.selenium_tests:
00135             filenames = getSeleniumTestsFromSuite(plugin,  selenium_test_suite)
00136             test_filenames.extend(filenames)
00137         return test_filenames
00138 
00139     @adapter(IKSSSeleniumTestResource, IUtilityRegistration, IRegistered, IKSSDemoRegistrationEvent)
00140     def registerSeleniumTestsFromPlugin(self, plugin, registration=None, event=None, new_event=None):
00141         """Add a demo collection to the registry.
00142         """
00143         test_filenames = self._getSeleniumTestsFromPlugin(plugin)
00144         for test_filename in test_filenames:
00145             self.registerSeleniumTestFile(test_filename)
00146 
00147     @adapter(IKSSSeleniumTestResource, IUtilityRegistration, IUnregistered, IKSSDemoRegistrationEvent)
00148     def unregisterSeleniumTestsFromPlugin(self, plugin, registration=None, event=None, new_event=None):
00149         """Remove a demo collection from the registry.
00150         """
00151         for test_filename in self._getSeleniumTestsFromPlugin(plugin):
00152             self.registerSeleniumTestFile(test_filename)
00153 
00154     def registerSeleniumTestFile(self, test_filename):
00155         """Register a selenium test by absolute filename
00156         """
00157         if test_filename in self.selenium_tests:
00158             raise Exception, 'The selenium test for %s has already been registered. Cannot add.' % (test_filename, )
00159         self.selenium_tests.append(test_filename)
00160 
00161     def unregisterSeleniumTestFile(self, test_filename):
00162         """Unregister the given selenium test."""
00163         try:
00164             del self.selenium_tests[test_filename]
00165         except KeyError:
00166             raise Exception, 'The selenium test for %s is yet unregistered. Cannot remove.' % (test_filename, )