Author: jmorliaguet
Date: Thu Dec  8 23:55:01 2005
New Revision: 2001

Modified:
   cpsskins/branches/jmo-perspectives/browser/rendering/interfaces.py
   cpsskins/branches/jmo-perspectives/browser/rendering/viewer.py
   cpsskins/branches/jmo-perspectives/setup/configure.zcml
   cpsskins/branches/jmo-perspectives/setup/registration.py
Log:

- implemented an automatic refresh of settings in dev mode (i.e. when the
  settings files are modified)



Modified: cpsskins/branches/jmo-perspectives/browser/rendering/interfaces.py
==============================================================================
--- cpsskins/branches/jmo-perspectives/browser/rendering/interfaces.py  
(original)
+++ cpsskins/branches/jmo-perspectives/browser/rendering/interfaces.py  Thu Dec 
 8 23:55:01 2005
@@ -48,3 +48,6 @@
     def update(info):
         """This method is called when the element is updated."""
 
+class IViewNodeEvent(Interface):
+    """Event triggered when a node is being viewed."""
+

Modified: cpsskins/branches/jmo-perspectives/browser/rendering/viewer.py
==============================================================================
--- cpsskins/branches/jmo-perspectives/browser/rendering/viewer.py      
(original)
+++ cpsskins/branches/jmo-perspectives/browser/rendering/viewer.py      Thu Dec 
 8 23:55:01 2005
@@ -24,13 +24,14 @@
 from zope.app.traversing.api import getParent
 from zope.app.zapi import getMultiAdapter
 from zope.component import adapts, getUtility
+from zope.event import notify
 from zope.interface import implements
 from zope.publisher.interfaces import IRequest
 
 from cpsskins.browser.negociation.interfaces import INegociation
 from cpsskins.browser.rendering.context import getContexts, ContextInfo
 from cpsskins.browser.rendering.interfaces import (
-    IUpdateData, IViewer, IRendererView)
+    IUpdateData, IViewer, IRendererView, IViewNodeEvent)
 from cpsskins.configuration.interfaces import IRegistry
 from cpsskins.elements.interfaces import (
     INodeTraverser, INode, IElement, ISlot, IDisplayable)
@@ -41,6 +42,12 @@
 
 accesskey_registry = getUtility(IRegistry, 'accesskeys')
 
+class ViewNodeEvent(object):
+    implements(IViewNodeEvent)
+
+    def __init__(self, object):
+        self.object = object
+
 class Viewer(BrowserView):
     """An element viewer provides a view of an element which is displayed
     in two phases:
@@ -63,6 +70,8 @@
         request = self.request
         contexts = getContexts(request)
 
+        notify(ViewNodeEvent(element))
+
         relations = getUtility(IRelationStorage, context=element)
         negociation = getMultiAdapter((element, request),
                                       INegociation, 'negociation')

Modified: cpsskins/branches/jmo-perspectives/setup/configure.zcml
==============================================================================
--- cpsskins/branches/jmo-perspectives/setup/configure.zcml     (original)
+++ cpsskins/branches/jmo-perspectives/setup/configure.zcml     Thu Dec  8 
23:55:01 2005
@@ -1,6 +1,7 @@
 <configure
     xmlns="http://namespaces.zope.org/zope";
     xmlns:i18n="http://namespaces.zope.org/i18n";
+    xmlns:zcml="http://namespaces.zope.org/zcml";
     i18n_domain="cpsskins"
     >
 
@@ -29,5 +30,11 @@
       for="zope.app.appsetup.IDatabaseOpenedEvent"
   />
 
+  <!-- refresh settings in dev mode -->
+  <subscriber
+      handler=".registration.refreshSettings"
+      for="cpsskins.browser.rendering.interfaces.IViewNodeEvent"
+      zcml:condition="have devmode"
+  />
 
 </configure>

Modified: cpsskins/branches/jmo-perspectives/setup/registration.py
==============================================================================
--- cpsskins/branches/jmo-perspectives/setup/registration.py    (original)
+++ cpsskins/branches/jmo-perspectives/setup/registration.py    Thu Dec  8 
23:55:01 2005
@@ -19,6 +19,7 @@
 
 import os
 import sys
+import time
 import logging
 
 from xml.dom.minidom import parse
@@ -34,18 +35,27 @@
 WARN = logging.WARN
 logger = logging.getLogger()
 
-filenames = []
+files = []
+
+#######################################################################
+# Registering and loading settings
+#######################################################################
 
 def register(profile, file):
     base_path = os.path.dirname(sys.modules.get(profile).__file__)
-    filenames.append(os.path.join(base_path, file))
+    files.append({'filename': os.path.join(base_path, file)})
+
+#######################################################################
+# Events
+#######################################################################
 
 def loadSettings(event):
     """Load all registered settings.
     """
     resources = getUtility(IResourceManager)
 
-    for filename in filenames:
+    for file in files:
+        filename = file['filename']
         dom = parse(filename)
         for setting in dom.getElementsByTagName('setting'):
             name = setting.getAttribute('name')
@@ -54,7 +64,7 @@
             resource = setting.getElementsByTagName('*')
             if not resource:
                 logger.log(DEBUG,
-                    "no resource found for: %s" % name)
+                    "no resource found for %s" % name)
                 continue
             resource = resource[0]
             factory = queryUtility(IFactory, resource.tagName)
@@ -71,25 +81,47 @@
             resources.register(name=name, title=title, 
resource=resource_object,
                                filename=filename)
 
-        logger.log(DEBUG, "loaded settings in: %s" % filename)
+            file['refresh_date'] = time.time()
 
+        logger.log(DEBUG, "loaded settings in %s" % filename)
+
+def refreshSettings(event):
+    """Refresh all settings that have been modified on the filesystem.
+    """
+    for file in files:
+        refresh_date = file['refresh_date']
+        filename = file['filename']
+        if refresh_date > os.path.getmtime(filename):
+            continue
+        _reload(filename=filename)
 
-def refresh(name=u''):
+#######################################################################
+# Reloading settings
+#######################################################################
 
+def reloadSetting(name=u''):
+    """Reload a setting by name.
+    """
     setting = getUtility(ISetting, name)
     filename = setting.filename
 
     if not filename:
         raise TypeError("The setting is not a file-system setting.")
 
+    _reload(name=name, filename=filename)
+
+def _reload(name=u'', filename=None):
+    """Reload the settings in a file
+    """
     try:
         dom = parse(filename)
     except ExpatError:
-        logger.log(WARN, "could not parse %s. not reloading" % filename)
+        logger.log(WARN, "could not parse %s - not reloading" % filename)
         return
 
     for setting_node in dom.getElementsByTagName('setting'):
-        if setting_node.getAttribute('name') != name:
+        setting_name = setting_node.getAttribute('name')
+        if name and setting_name != name:
             continue
 
         resource = setting_node.getElementsByTagName('*')
@@ -97,8 +129,12 @@
             break
         resource = resource[0]
 
+        setting = getUtility(ISetting, setting_name)
         resource_object = IResource(setting).getResource()
         IResourceImporter(resource_object).load(resource.toxml())
 
-        logger.log(DEBUG, "reloaded setting: %s (in %s)" % (name, filename))
+        if name:
+            logger.log(DEBUG, "reloaded setting %s in %s" % (name, filename))
 
+    if not name:
+        logger.log(DEBUG, "reloaded settings in %s" % filename)
-- 
http://lists.nuxeo.com/mailman/listinfo/z3lab-checkins

Reply via email to