[Checkins] SVN: grokcore.view/branches/1.13/ Backport warnings-as-log-msgs from trunk.

Uli Fouquet uli at gnufix.de
Wed May 26 20:36:44 EDT 2010


Log message for revision 112745:
  Backport warnings-as-log-msgs from trunk.

Changed:
  U   grokcore.view/branches/1.13/CHANGES.txt
  U   grokcore.view/branches/1.13/src/grokcore/view/templatereg.py
  U   grokcore.view/branches/1.13/src/grokcore/view/tests/view/dirtemplatesonly.py
  U   grokcore.view/branches/1.13/src/grokcore/view/tests/view/inline_unassociated.py
  U   grokcore.view/branches/1.13/src/grokcore/view/tests/view/shared_template_dir.py
  U   grokcore.view/branches/1.13/src/grokcore/view/tests/view/unassociated.py

-=-
Modified: grokcore.view/branches/1.13/CHANGES.txt
===================================================================
--- grokcore.view/branches/1.13/CHANGES.txt	2010-05-26 18:56:41 UTC (rev 112744)
+++ grokcore.view/branches/1.13/CHANGES.txt	2010-05-27 00:36:43 UTC (rev 112745)
@@ -10,7 +10,16 @@
 
 - Update tsets not to require zope.app.zcmlfiles anymore.
 
+- Warnings are now emitted as log messages with level
+  ``logging.WARNING`` to a logger named ``grokcore.view`` with level
+  ``logging.ERROR``. That means that by default no warnings are
+  emitted anymore (while errors will still appear).
 
+  You have to tweak the mentioned logger to get the warnings back. For
+  instance you can set its level to ``logging.INFO`` or similar. This
+  has to be done before code is grokked using `grokcore.view`.
+
+
 1.13.2 (2010-01-09)
 -------------------
 

Modified: grokcore.view/branches/1.13/src/grokcore/view/templatereg.py
===================================================================
--- grokcore.view/branches/1.13/src/grokcore/view/templatereg.py	2010-05-26 18:56:41 UTC (rev 112744)
+++ grokcore.view/branches/1.13/src/grokcore/view/templatereg.py	2010-05-27 00:36:43 UTC (rev 112745)
@@ -1,5 +1,5 @@
+import logging
 import os
-import warnings
 import zope.component
 import grokcore.component
 import grokcore.view
@@ -7,6 +7,24 @@
 from grokcore.view.interfaces import ITemplateFileFactory, ITemplate
 from grokcore.view.components import PageTemplate
 
+def get_logger():
+    """Setup a 'grokcore.view' logger if none is already defined.
+
+    Return the defined one else.
+
+    We set the logger level to ``logging.ERROR``, which means that by
+    default warning messages will not be displayed.
+    """
+    logger = logging.getLogger('grokcore.view')
+    if len(logger.handlers) > 0:
+        return logger
+    logger.setLevel(logging.ERROR)
+    handler = logging.StreamHandler()
+    formatter = logging.Formatter("%(levelname)s: %(message)s")
+    handler.setFormatter(formatter)
+    logger.addHandler(handler)
+    return logger
+
 class TemplateRegistry(object):
 
     def __init__(self):
@@ -55,9 +73,11 @@
                 # Warning when importing files. This should be
                 # allowed because people may be using editors that generate
                 # '.bak' files and such.
-                warnings.warn("File '%s' has an unrecognized extension in "
-                              "directory '%s'" %
-                              (template_file, template_dir), UserWarning, 2)
+                logger = get_logger()
+                msg = ("File '%s' has an unrecognized extension in "
+                       "directory '%s'" %
+                       (template_file, template_dir))
+                logger.warn(msg)
                 continue
 
             inline_template = self.get(template_name)
@@ -88,7 +108,8 @@
                 "grokking %r: %s.  Define view classes inheriting "
                 "from grok.View to enable the template(s)." % (
                 module_info.dotted_name, ', '.join(unassociated)))
-            warnings.warn(msg, UserWarning, 1)
+            logger = get_logger()
+            logger.warn(msg)
 
     def checkTemplates(self, module_info, factory, component_name,
                        has_render, has_no_render):

Modified: grokcore.view/branches/1.13/src/grokcore/view/tests/view/dirtemplatesonly.py
===================================================================
--- grokcore.view/branches/1.13/src/grokcore/view/tests/view/dirtemplatesonly.py	2010-05-26 18:56:41 UTC (rev 112744)
+++ grokcore.view/branches/1.13/src/grokcore/view/tests/view/dirtemplatesonly.py	2010-05-27 00:36:43 UTC (rev 112745)
@@ -1,15 +1,15 @@
 """
 A template directory may only contain recognized template files::
 
-  >>> from grokcore.view.testing import warn, lastwarning
-  >>> import warnings
-  >>> saved_warn = warnings.warn
-  >>> warnings.warn = warn
+  >>> from zope.testing.loggingsupport import InstalledHandler
+  >>> handler = InstalledHandler('grokcore.view')
+  >>> handler.clear()
 
   >>> grok.testing.grok(__name__)
-  From grok.testing's warn():
-  ... UserWarning: File 'invalid.txt' has an unrecognized extension in
-  directory '...dirtemplatesonly_templates'...
+  >>> print handler
+  grokcore.view WARNING
+      File 'invalid.txt' has an unrecognized extension in directory
+      '...dirtemplatesonly_templates'
 
 Files ending with '.cache' are generated on the fly by some template
 engines. Although they provide no valid template filename extension,
@@ -18,15 +18,16 @@
 There is a 'template' ``ignored.cache`` in our template dir, which
 emits no warning::
 
-  >>> 'ignored.cache' in lastwarning
+  >>> #'ignored.cache' in lastwarning
+  >>> 'ignored.cache' in handler.records[0].msg
   False
 
 The same applies to files and directories ending with '~' or starting
 with a dot ('.').
 
-Restore the warning machinery::
+Restore the logging machinery::
 
-  >>> warnings.warn = saved_warn
+  >>> handler.uninstall()
 
 """
 import grokcore.view as grok

Modified: grokcore.view/branches/1.13/src/grokcore/view/tests/view/inline_unassociated.py
===================================================================
--- grokcore.view/branches/1.13/src/grokcore/view/tests/view/inline_unassociated.py	2010-05-26 18:56:41 UTC (rev 112744)
+++ grokcore.view/branches/1.13/src/grokcore/view/tests/view/inline_unassociated.py	2010-05-27 00:36:43 UTC (rev 112745)
@@ -1,20 +1,23 @@
 """
 Inline templates that are not associated with a view class will
-provoke an error:
+provoke a log message on warning level to ``grokcore.view`` logger:
 
-  >>> from grokcore.view.testing import warn
-  >>> import warnings
-  >>> saved_warn = warnings.warn
-  >>> warnings.warn = warn
+  >>> from zope.testing.loggingsupport import InstalledHandler
+  >>> handler = InstalledHandler('grokcore.view')
+  >>> handler.clear() # Make sure there are no old msgs stored...
 
   >>> grok.testing.grok(__name__)
-  From grok.testing's warn():
-  ...UserWarning: Found the following unassociated template(s) when grokking
-  'grokcore.view.tests.view.inline_unassociated': club. Define view classes inheriting
-  from grok.View to enable the template(s)...
+  >>> print handler
+  grokcore.view WARNING
+      Found the following unassociated template(s) when grokking
+      'grokcore.view.tests.view.inline_unassociated': club.  Define
+      view classes inheriting from grok.View to enable the
+      template(s).
 
-  >>> warnings.warn = saved_warn
+Restore logging machinery:
 
+  >>> handler.uninstall()
+
 """
 import grokcore.view as grok
 

Modified: grokcore.view/branches/1.13/src/grokcore/view/tests/view/shared_template_dir.py
===================================================================
--- grokcore.view/branches/1.13/src/grokcore/view/tests/view/shared_template_dir.py	2010-05-26 18:56:41 UTC (rev 112744)
+++ grokcore.view/branches/1.13/src/grokcore/view/tests/view/shared_template_dir.py	2010-05-27 00:36:43 UTC (rev 112745)
@@ -1,25 +1,33 @@
 """
-When modules share a template directory, templates that have not been associated with any view class of a given module issue a UserWarning:
+When modules share a template directory, templates that have not been
+associated with any view class of a given module issue a log message
+with level ``logging.WARNING`` to a ``grokcore.view`` logger:
 
-  >>> from grokcore.view.testing import warn
-  >>> import warnings
-  >>> saved_warn = warnings.warn
-  >>> warnings.warn = warn
+  >>> from zope.testing.loggingsupport import InstalledHandler
+  >>> handler = InstalledHandler('grokcore.view')
 
   >>> import shared_template_dir_fixture
 
+  >>> handler.clear()  # Make sure no old log msgs are stored...
   >>> grok.testing.grok(__name__)
-  From grok.testing's warn():
-  ...UserWarning: Found the following unassociated template(s) when grokking
-  'grokcore.view.tests.view.shared_template_dir': food, unassociated.  Define view classes inheriting from
-  grok.View to enable the template(s)...
-
+  >>> print handler
+  grokcore.view WARNING
+      Found the following unassociated template(s) when grokking
+      'grokcore.view.tests.view.shared_template_dir': food,
+      unassociated.  Define view classes inheriting from grok.View to
+      enable the template(s).
+  
+  >>> handler.clear()  # Make sure no old log msgs are stored...
   >>> grok.testing.grok(shared_template_dir_fixture.__name__)
-  From grok.testing's warn():
-  ...UserWarning: Found the following unassociated template(s) when grokking
-  'grokcore.view.tests.view.shared_template_dir_fixture': cavepainting, unassociated.  Define view classes inheriting from
-  grok.View to enable the template(s)...
+  >>> print handler
+  grokcore.view WARNING
+        Found the following unassociated template(s) when grokking
+      'grokcore.view.tests.view.shared_template_dir_fixture':
+      cavepainting, unassociated.  Define view classes inheriting from
+      grok.View to enable the template(s).
 
+  >>> handler.uninstall()
+
 """
 import grokcore.view as grok
 

Modified: grokcore.view/branches/1.13/src/grokcore/view/tests/view/unassociated.py
===================================================================
--- grokcore.view/branches/1.13/src/grokcore/view/tests/view/unassociated.py	2010-05-26 18:56:41 UTC (rev 112744)
+++ grokcore.view/branches/1.13/src/grokcore/view/tests/view/unassociated.py	2010-05-27 00:36:43 UTC (rev 112745)
@@ -2,29 +2,33 @@
 Templates that are not associated with a view class will provoke an
 error:
 
-  >>> from grokcore.view.testing import warn
-  >>> import warnings
-  >>> saved_warn = warnings.warn
-  >>> warnings.warn = warn
+  >>> from zope.testing.loggingsupport import InstalledHandler
+  >>> handler = InstalledHandler('grokcore.view')
 
   >>> grok.testing.grok(__name__)
-  From grok.testing's warn():
-  ...UserWarning: Found the following unassociated template(s) when grokking
-  'grokcore.view.tests.view.unassociated': index.  Define view classes inheriting from
-  grok.View to enable the template(s)...
+  >>> print handler
+  grokcore.view WARNING
+      Found the following unassociated template(s) when grokking
+      'grokcore.view.tests.view.unassociated': index.  Define view
+      classes inheriting from grok.View to enable the template(s).
 
 Also templates of modules named equally as the package name the module
 resides in, should be found without error or warning. We check this
 with the local package `modequalspkgname`::
 
-  >>> warnings.warn = warn
+  >>> handler.clear() # Make sure no old log msgs are stored...
 
   >>> pkg = __name__.rsplit('.', 1)[0] + '.modequalspkgname'
   >>> grok.testing.grok(pkg) is None
   True
 
-  >>> warnings.warn = saved_warn
+No log messages were emitted:
 
+  >>> handler.records
+  []
+
+  >>> handler.uninstall()
+
 """
 import grokcore.view as grok
 



More information about the checkins mailing list