add validator support
This commit is contained in:
parent
aeaa7095cf
commit
07d9725ce4
@ -97,18 +97,31 @@ class ConditionalFilter(YieldFilter):
|
|||||||
All derived filters must provide a test_record(self, record) that
|
All derived filters must provide a test_record(self, record) that
|
||||||
returns True or False -- True indicating that the record should be
|
returns True or False -- True indicating that the record should be
|
||||||
passed through, and False preventing pass through.
|
passed through, and False preventing pass through.
|
||||||
|
|
||||||
|
If validator is True then raises a ValidationError instead of
|
||||||
|
silently dropping records that fail test_record.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
|
validator = False
|
||||||
|
|
||||||
def process_record(self, record):
|
def process_record(self, record):
|
||||||
""" Yields all records for which self.test_record is true """
|
""" Yields all records for which self.test_record is true """
|
||||||
|
|
||||||
if self.test_record(record):
|
if self.test_record(record):
|
||||||
yield record
|
yield record
|
||||||
|
elif self.validator:
|
||||||
|
raise ValidationError(record)
|
||||||
|
|
||||||
def test_record(self, record):
|
def test_record(self, record):
|
||||||
""" Given a record, return True iff it should be passed on """
|
""" Given a record, return True iff it should be passed on """
|
||||||
raise NotImplementedError('test_record not defined in ' +
|
raise NotImplementedError('test_record not defined in ' +
|
||||||
self.__class__.__name__)
|
self.__class__.__name__)
|
||||||
|
|
||||||
|
class ValidationError(Exception):
|
||||||
|
def __init__(self, record):
|
||||||
|
super(ValidationError, self).__init__(repr(record))
|
||||||
|
self.record = record
|
||||||
|
|
||||||
#####################
|
#####################
|
||||||
## Generic Filters ##
|
## Generic Filters ##
|
||||||
#####################
|
#####################
|
||||||
@ -339,6 +352,10 @@ class Unique(ConditionalFilter):
|
|||||||
else:
|
else:
|
||||||
return False
|
return False
|
||||||
|
|
||||||
|
class UniqueValidator(Unique):
|
||||||
|
validator = True
|
||||||
|
|
||||||
|
|
||||||
class UniqueID(ConditionalFilter):
|
class UniqueID(ConditionalFilter):
|
||||||
""" Filter that ensures that all records through have a unique ID.
|
""" Filter that ensures that all records through have a unique ID.
|
||||||
|
|
||||||
@ -360,6 +377,10 @@ class UniqueID(ConditionalFilter):
|
|||||||
else:
|
else:
|
||||||
return False
|
return False
|
||||||
|
|
||||||
|
class UniqueIDValidator(UniqueID):
|
||||||
|
validator = True
|
||||||
|
|
||||||
|
|
||||||
class UnicodeFilter(Filter):
|
class UnicodeFilter(Filter):
|
||||||
""" Convert all str elements in the record to Unicode.
|
""" Convert all str elements in the record to Unicode.
|
||||||
"""
|
"""
|
||||||
|
Loading…
Reference in New Issue
Block a user