Skip to content

Commit

Permalink
Simplify maxOccurs handling.
Browse files Browse the repository at this point in the history
Fixes #15.
  • Loading branch information
jamadden committed Jun 22, 2017
1 parent 7de4975 commit f15662d
Show file tree
Hide file tree
Showing 6 changed files with 19 additions and 17 deletions.
2 changes: 1 addition & 1 deletion .travis.yml
Expand Up @@ -6,7 +6,7 @@ python:
- 3.4
- 3.5
- 3.6
- pypy-5.4.1
- pypy-5.6.0
- pypy3.3-5.2-alpha1
install:
- pip install -U pip setuptools
Expand Down
3 changes: 3 additions & 0 deletions CHANGES.rst
Expand Up @@ -24,6 +24,9 @@ Change History for ZConfig
documenting ZConfig components using their description and examples
in Sphinx documentation.

- Simplify internal schema processing of max and min occurrence
values. See https://github.com/zopefoundation/ZConfig/issues/15.

3.1.0 (2015-10-17)
------------------

Expand Down
19 changes: 9 additions & 10 deletions ZConfig/info.py
Expand Up @@ -64,13 +64,15 @@ class BaseInfo(object):

def __init__(self, name, datatype, minOccurs, maxOccurs, handler,
attribute):
if maxOccurs is not None:
if maxOccurs < 1:
raise ZConfig.SchemaError(
"maxOccurs must be at least 1")
if minOccurs is not None and minOccurs > maxOccurs:
raise ZConfig.SchemaError(
"minOccurs cannot be more than maxOccurs")
assert maxOccurs is not None, "Use Unbounded for an upper bound, not None"
assert minOccurs is not None, "Use 0 for a lower bound, not None"

if maxOccurs < 1:
raise ZConfig.SchemaError(
"maxOccurs must be at least 1")
if minOccurs > maxOccurs:
raise ZConfig.SchemaError(
"minOccurs cannot be more than maxOccurs")
self.name = name
self.datatype = datatype
self.minOccurs = minOccurs
Expand Down Expand Up @@ -98,7 +100,6 @@ class BaseKeyInfo(AbstractBaseClass, BaseInfo):

def __init__(self, name, datatype, minOccurs, maxOccurs, handler,
attribute):
assert minOccurs is not None
BaseInfo.__init__(self, name, datatype, minOccurs, maxOccurs,
handler, attribute)
self._finished = False
Expand Down Expand Up @@ -221,8 +222,6 @@ def __init__(self, name, sectiontype, minOccurs, maxOccurs, handler,
# handler - handler name called when value(s) must take effect,
# or None
# attribute - name of the attribute on the SectionValue object
assert maxOccurs is not None
# because we compare it to 1 which is a Py3 error
if maxOccurs > 1:
if name not in ('*', '+'):
raise ZConfig.SchemaError(
Expand Down
4 changes: 2 additions & 2 deletions ZConfig/schema.py
Expand Up @@ -367,7 +367,7 @@ def end_sectiontype(self):
def start_section(self, attrs):
sectiontype = self.get_sectiontype(attrs)
handler = self.get_handler(attrs)
min = self.get_required(attrs) and 1 or 0
min = 1 if self.get_required(attrs) else 0
any, name, attribute = self.get_name_info(attrs, "section", "*")
if any and not attribute: # pragma: no cover
# It seems like this is handled by get_name_info.
Expand Down Expand Up @@ -410,7 +410,7 @@ def end_abstracttype(self):

def start_key(self, attrs):
name, datatype, handler, attribute = self.get_key_info(attrs, "key")
min = self.get_required(attrs) and 1 or 0
min = 1 if self.get_required(attrs) else 0
key = info.KeyInfo(name, datatype, min, handler, attribute)
if "default" in attrs:
if min:
Expand Down
4 changes: 2 additions & 2 deletions ZConfig/tests/test_info.py
Expand Up @@ -38,7 +38,7 @@ class InfoMixin(object):
Class = None

default_kwargs = {'name': '', 'datatype': None, 'handler': None,
'minOccurs': None, 'maxOccurs': None, 'attribute': None}
'minOccurs': 0, 'maxOccurs': Unbounded, 'attribute': None}

def make_one(self, **kwargs):
args = self.default_kwargs.copy()
Expand All @@ -54,7 +54,7 @@ def test_constructor_error(self):
self.assertRaisesRegexp(SchemaError,
'maxOccurs',
self.make_one,
maxOccurs=0)
maxOccurs=0, minOccurs=0)

# This case doesn't really make sense
self.assertRaisesRegexp(SchemaError,
Expand Down
4 changes: 2 additions & 2 deletions tox.ini
@@ -1,12 +1,12 @@
[tox]
envlist = py27,py33,py34,py35,py36
envlist = py27,py33,py34,py35,py36,pypy

[testenv]
commands =
python setup.py -q test -q
# without explicit deps, setup.py test will download a bunch of eggs into $PWD
deps =
zope.testrunner
.[test]

[testenv:coverage]
basepython =
Expand Down

0 comments on commit f15662d

Please sign in to comment.