Skip to content

Commit

Permalink
Backport warnings-as-log-msgs from trunk.
Browse files Browse the repository at this point in the history
  • Loading branch information
ulif committed May 27, 2010
1 parent 26b94dc commit e5c74db
Show file tree
Hide file tree
Showing 6 changed files with 95 additions and 49 deletions.
9 changes: 9 additions & 0 deletions CHANGES.txt
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,15 @@ Changes

- 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)
-------------------
Expand Down
31 changes: 26 additions & 5 deletions src/grokcore/view/templatereg.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,30 @@
import logging
import os
import warnings
import zope.component
import grokcore.component
import grokcore.view
from martian.error import GrokError
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):
Expand Down Expand Up @@ -55,9 +73,11 @@ def findFilesystem(self, module_info):
# 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)
Expand Down Expand Up @@ -88,7 +108,8 @@ def checkUnassociated(self, module_info):
"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):
Expand Down
21 changes: 11 additions & 10 deletions src/grokcore/view/tests/view/dirtemplatesonly.py
Original file line number Diff line number Diff line change
@@ -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,
Expand All @@ -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
Expand Down
23 changes: 13 additions & 10 deletions src/grokcore/view/tests/view/inline_unassociated.py
Original file line number Diff line number Diff line change
@@ -1,19 +1,22 @@
"""
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
Expand Down
36 changes: 22 additions & 14 deletions src/grokcore/view/tests/view/shared_template_dir.py
Original file line number Diff line number Diff line change
@@ -1,24 +1,32 @@
"""
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
Expand Down
24 changes: 14 additions & 10 deletions src/grokcore/view/tests/view/unassociated.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,28 +2,32 @@
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
Expand Down

0 comments on commit e5c74db

Please sign in to comment.