-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
154 additions
and
56 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,10 @@ | ||
from __future__ import absolute_import | ||
|
||
from .exceptions import * | ||
from .provider import * | ||
from .server import * | ||
from .validators import * | ||
|
||
__all__ = [server.__all__, provider.__all__] | ||
__all__ = [exceptions.__all__, server.__all__, provider.__all__, validators.__all__] | ||
|
||
__version__ = "0.1.0" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
"""Collection of exceptions | ||
""" | ||
|
||
__all__ = ['ValidationError'] | ||
|
||
|
||
class ValidationError(Exception): | ||
pass |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
"""Collection of validators | ||
""" | ||
from __future__ import absolute_import | ||
|
||
from . import exceptions | ||
|
||
__all__ = ['BaronTypeValidator'] | ||
|
||
|
||
class ValidatorInterface(object): | ||
def __call__(self, red): | ||
"""Validate the given redbaron source | ||
:raises: :class:`ValidationError` | ||
""" | ||
pass | ||
|
||
|
||
class BaronTypeValidator(ValidatorInterface): | ||
def __init__(self, identifiers, single=False): | ||
"""Initialize the validator | ||
:param identifiers: allowed identifiers for the redbaron source | ||
:type identifiers: sequence of :class:`str` | ||
:param single: If more than one top level node is valid | ||
:type single: :class:`boolean` | ||
""" | ||
self.identifiers = identifiers | ||
self.single = single | ||
|
||
def __call__(self, red): | ||
"""Validate the given redbaron source | ||
:raises: :class:`ValidationError` | ||
""" | ||
if self.single: | ||
count = len(red) | ||
if count != 1: | ||
raise exceptions.ValidationError("Expected a single node but got %s" % count) | ||
for node in red: | ||
identifiers = node.generate_identifiers() | ||
if not any(i in identifiers for i in self.identifiers): | ||
raise exceptions.ValidationError("Expected identifier %s but got %s" % | ||
(self.identifiers, identifiers)) |