Skip to content

Commit

Permalink
Add ability to exclude more than one module or package using ``<grok:…
Browse files Browse the repository at this point in the history
…grok exclude="<names>" />`` and allow to use unix shell-style wildcards within.
  • Loading branch information
Michael Howitz committed Feb 14, 2016
1 parent 2c50001 commit 50d87a6
Show file tree
Hide file tree
Showing 7 changed files with 73 additions and 7 deletions.
4 changes: 3 additions & 1 deletion CHANGES.rst
Expand Up @@ -4,7 +4,9 @@ Changes
2.7 (unreleased)
----------------

- Nothing changed yet.
- Add ability to exclude more than one module or package using
``<grok:grok exclude="<names>" />`` and allow to use unix shell-style
wildcards within.


2.6.1 (2016-01-29)
Expand Down
4 changes: 4 additions & 0 deletions README.rst
Expand Up @@ -49,6 +49,10 @@ To sum up, your ``site.zcml`` file should look like something like this::

</configure>

There is an optional ``exclude`` on the `grok` directive. It allows to specify
names of packages or modules that if encountered won't be grokked. These
names might contain unix shell-style wildcards.

Examples
========

Expand Down
50 changes: 50 additions & 0 deletions src/grokcore/component/tests/zcml/excludemany.py
@@ -0,0 +1,50 @@
"""
It allows to exclude a many packages or modules from beeing grokked.
These packages or modules can be specified using unix shell-style wildcards
There is a NameError in `.excludemanypkg.file_1` which is raised when this
module is not excluded:
>>> xmlconfig.string('''
... <configure xmlns:grok="http://namespaces.zope.org/grok">
... <include package="grokcore.component" file="meta.zcml"/>
... <grok:grok package="." />
... </configure>''', context)
Traceback (most recent call last):
ZopeXMLConfigurationError: File "<string>", line 4.6-4.31
NameError: name 'asdf' is not defined
There is a NameError in `.excludemanypkg.test_asdf`, too which is raised when
this module is not excluded:
>>> xmlconfig.string('''
... <configure xmlns:grok="http://namespaces.zope.org/grok">
... <include package="grokcore.component" file="meta.zcml"/>
... <grok:grok package="."
... exclude="file_*" />
... </configure>''', context)
Traceback (most recent call last):
ZopeXMLConfigurationError: File "<string>", line 4.6-5.36
NameError: name 'qwe' is not defined
Excluding both 'file_1` and `test_asdf`allows to successfully grok the module:
>>> xmlconfig.string('''
... <configure xmlns:grok="http://namespaces.zope.org/grok">
... <include package="grokcore.component" file="meta.zcml"/>
... <grok:grok package="."
... exclude="file_*
... *asdf" />
... </configure>''', context)
<zope.configuration.config.ConfigurationMachine ...>
"""

from zope.configuration import xmlconfig
from grokcore.component.tests.zcml import excludemanypkg

context = xmlconfig.ConfigurationMachine()
xmlconfig.registerCommonDirectives(context)
context.package = excludemanypkg
@@ -0,0 +1 @@
# this is a package
1 change: 1 addition & 0 deletions src/grokcore/component/tests/zcml/excludemanypkg/file_1.py
@@ -0,0 +1 @@
asdf # This leads to a NameError if exclude does not work correctly.
@@ -0,0 +1 @@
qwe # This leads to a NameError if exclude does not work correctly.
19 changes: 13 additions & 6 deletions src/grokcore/component/zcml.py
Expand Up @@ -14,10 +14,11 @@
"""Grok ZCML directives."""

from zope.interface import Interface
from zope.configuration.fields import GlobalObject
from zope.configuration.fields import GlobalObject, Tokens
from zope.schema import TextLine

import martian
import fnmatch


class IGrokDirective(Interface):
Expand All @@ -28,10 +29,12 @@ class IGrokDirective(Interface):
description=u"The package or module to be analyzed by grok.",
required=False)

exclude = TextLine(
exclude = Tokens(
title=u"Exclude",
description=u"Name to exclude in the grokking process.",
required=False)
description=u"Names (which might contain unix shell-style wildcards) "
u"to be excluded in the grokking process.",
required=False,
value_type=TextLine())


# add a cleanup hook so that grok will bootstrap itself again whenever
Expand Down Expand Up @@ -61,8 +64,12 @@ def do_grok(dotted_name, config, extra_exclude=None):
if extra_exclude is not None:

def exclude_filter(name):
return skip_tests(name) or extra_exclude == name

if skip_tests(name):
return True
for exclude in extra_exclude:
if fnmatch.fnmatch(name, exclude):
return True
return False
else:
exclude_filter = skip_tests

Expand Down

0 comments on commit 50d87a6

Please sign in to comment.