Skip to content
This repository has been archived by the owner on Feb 10, 2023. It is now read-only.

Commit

Permalink
Move zope.conf warnfilter into ZServer.
Browse files Browse the repository at this point in the history
  • Loading branch information
hannosch committed Aug 7, 2016
1 parent b5a8b6f commit bdd16a5
Show file tree
Hide file tree
Showing 3 changed files with 190 additions and 0 deletions.
123 changes: 123 additions & 0 deletions src/ZServer/Zope2/Startup/tests/test_warnfilter.py
@@ -0,0 +1,123 @@
##############################################################################
#
# Copyright (c) 2003 Zope Foundation and Contributors.
# All Rights Reserved.
#
# This software is subject to the provisions of the Zope Public License,
# Version 2.1 (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.
#
##############################################################################

import os
import cStringIO
import sys
import tempfile
import unittest
import warnings

import ZConfig
import Zope2.Startup
import Products

TEMPNAME = tempfile.mktemp()
TEMPPRODUCTS = os.path.join(TEMPNAME, "Products")


def getSchema():
startup = os.path.dirname(os.path.realpath(Zope2.Startup.__file__))
schemafile = os.path.join(startup, 'wsgischema.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()
# There is no official API to restore warning filters to a previous
# state. Here we cheat.
self.original_warning_filters = warnings.filters[:]

def tearDown(self):
warnings.filters[:] = self.original_warning_filters
Products.__path__ = [d for d in Products.__path__
if os.path.exists(d)]

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

if sys.version_info < (2, 7):
def test_behavior(self):
conf, handler = self.load_config_text("""\
instancehome <<INSTANCE_HOME>>
<warnfilter>
action error
message .*test.*
category Zope2.Startup.tests.test_warnfilter.TestSchemaWarning
module .*test_warnfilter.*
lineno 0
</warnfilter>
<warnfilter>
action error
message .*test.*
</warnfilter>
""")
self.assertEqual(len(conf.warnfilters), 2)
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 Zope2.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>
""")
58 changes: 58 additions & 0 deletions src/ZServer/Zope2/Startup/warnfilter.py
@@ -0,0 +1,58 @@
##############################################################################
#
# Copyright (c) 2003 Zope Foundation and Contributors.
# All Rights Reserved.
#
# This software is subject to the provisions of the Zope Public License,
# Version 2.1 (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 """

import re
import warnings


def warn_category(category):
if not category:
return Warning
if re.match("^[a-zA-Z0-9_]+$", category):
try:
cat = eval(category)
except NameError:
raise ValueError("unknown warning category: %r" % 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: %r" % module)
try:
cat = getattr(m, klass)
except AttributeError:
raise ValueError("unknown warning category: %r" % category)
if (not isinstance(cat, type(Warning)) or
not issubclass(cat, Warning)):
raise ValueError("invalid warning category: %r" % 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):
# add the warning filter
warnings.filterwarnings(section.action, section.message, section.category,
section.module, section.lineno)
return section
9 changes: 9 additions & 0 deletions src/ZServer/Zope2/Startup/warnfilter.xml
@@ -0,0 +1,9 @@
<component prefix="ZServer.Zope2.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>

0 comments on commit bdd16a5

Please sign in to comment.