Commit b25f83e8 authored by Chris McDonough's avatar Chris McDonough

Moved warnfilter to Zope.Startup (from zLOG package).

Added tests.
parent 335c08d2
##############################################################################
#
# Copyright (c) 2003 Zope Corporation and Contributors.
# All Rights Reserved.
#
# This software is subject to the provisions of the Zope Public License,
# Version 2.0 (ZPL). A copy of the ZPL should accompany this distribution.
# THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
# WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
# WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
# FOR A PARTICULAR PURPOSE.
#
##############################################################################
"""Test that the warning filter works."""
import os
import sys
import cStringIO
import tempfile
import unittest
import warnings
import ZConfig
import Zope.Startup
from Zope.Startup import datatypes
from App.config import getConfiguration
TEMPNAME = tempfile.mktemp()
TEMPPRODUCTS = os.path.join(TEMPNAME, "Products")
def getSchema():
startup = os.path.dirname(os.path.realpath(Zope.Startup.__file__))
schemafile = os.path.join(startup, 'zopeschema.xml')
return ZConfig.loadSchema(schemafile)
class TestSchemaWarning(Warning):
pass
class TestWarnFilter(unittest.TestCase):
schema = None
def setUp(self):
if self.schema is None:
TestWarnFilter.schema = getSchema()
def tearDown(self):
warnings.resetwarnings()
def load_config_text(self, text):
# We have to create a directory of our own since the existence
# of the directory is checked. This handles this in a
# platform-independent way.
schema = self.schema
sio = cStringIO.StringIO(
text.replace("<<INSTANCE_HOME>>", TEMPNAME))
os.mkdir(TEMPNAME)
os.mkdir(TEMPPRODUCTS)
try:
conf, handler = ZConfig.loadConfigFile(schema, sio)
finally:
os.rmdir(TEMPPRODUCTS)
os.rmdir(TEMPNAME)
self.assertEqual(conf.instancehome, TEMPNAME)
return conf, handler
def test_behavior(self):
conf, handler = self.load_config_text("""\
instancehome <<INSTANCE_HOME>>
<warnfilter>
action error
message .*test.*
category test_warnfilter.TestSchemaWarning
module .*test_warnfilter.*
lineno 0
</warnfilter>
<warnfilter>
action error
message .*test.*
</warnfilter>
""")
self.assertRaises(TestSchemaWarning, self._dowarning1)
self.assertRaises(UserWarning, self._dowarning2)
def _dowarning1(self):
warnings.warn('This is only a test.', TestSchemaWarning)
def _dowarning2(self):
warnings.warn('This is another test.')
def test_warn_action(self):
self.assertRaises(ZConfig.ConfigurationSyntaxError,
self._badwarnaction)
def _badwarnaction(self):
conf, handler = self.load_config_text("""\
instancehome <<INSTANCE_HOME>>
<warnfilter>
action wontwork
category Zope.Startup.tests.test_schema.TestSchemaWarning
</warnfilter>
""")
def test_warn_category(self):
self.assertRaises(ZConfig.ConfigurationSyntaxError,
self._badwarncategory)
def _badwarncategory(self):
conf, handler = self.load_config_text("""\
instancehome <<INSTANCE_HOME>>
<warnfilter>
action error
category A.Module.That.Doesnt.Exist
</warnfilter>
""")
def test_suite():
return unittest.makeSuite(TestWarnFilter)
if __name__ == "__main__":
unittest.main(defaultTest="test_suite")
##############################################################################
#
# Copyright (c) 2003 Zope Corporation and Contributors.
# All Rights Reserved.
#
# This software is subject to the provisions of the Zope Public License,
# Version 2.0 (ZPL). A copy of the ZPL should accompany this distribution.
# THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
# WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
# WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
# FOR A PARTICULAR PURPOSE.
#
##############################################################################
"""Datatypes for warning filter component """
def warn_category(category):
import re, types
if not category:
return Warning
if re.match("^[a-zA-Z0-9_]+$", category):
try:
cat = eval(category)
except NameError:
raise ValueError("unknown warning category: %s" % `category`)
else:
i = category.rfind(".")
module = category[:i]
klass = category[i+1:]
try:
m = __import__(module, None, None, [klass])
except ImportError:
raise ValueError("invalid module name: %s" % `module`)
try:
cat = getattr(m, klass)
except AttributeError:
raise ValueError("unknown warning category: %s" % `category`)
if (not isinstance(cat, types.ClassType) or
not issubclass(cat, Warning)):
raise ValueError("invalid warning category: %s" % `category`)
return cat
def warn_action(val):
OK = ("error", "ignore", "always", "default", "module", "once")
if val not in OK:
raise ValueError, "warning action %s not one of %s" % (val, OK)
return val
def warning_filter_handler(section):
import warnings
# add the warning filter
warnings.filterwarnings(section.action, section.message, section.category,
section.module, section.lineno, 1)
return section
<component prefix="Zope.Startup.warnfilter">
<sectiontype name="warnfilter" datatype=".warning_filter_handler">
<key name="action" datatype=".warn_action" default="default"/>
<key name="message" default=""/>
<key name="category" datatype=".warn_category" default="Warning"/>
<key name="module" default=""/>
<key name="lineno" datatype="integer" default="0"/>
</sectiontype>
</component>
......@@ -8,6 +8,7 @@
<import package="ZODB"/>
<import package="ZServer"/>
<import package="tempstorage"/>
<import package="Zope.Startup" file="warnfilter.xml"/>
<sectiontype name="logger" datatype=".LoggerFactory">
<description>
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment