| 1 |
# Lets data Packets pass-through, "sieve", based on criteria in their data. |
| 2 |
# See issue: https://github.com/geopython/stetl/issues/78 |
| 3 |
# |
| 4 |
# A concrete example is AttrValueRecordSieve which sieves records matching |
| 5 |
# specific attribute values. One can also think of Sieves based on XPath expressions |
| 6 |
# (e.g. for XML, GML), or geospatial, based on for example WFS-like filters like bounding boxes. |
| 7 |
# |
| 8 |
# Author: Just van den Broecke |
| 9 |
# |
| 10 |
from stetl.component import Config |
| 11 |
from stetl.filter import Filter |
| 12 |
from stetl.util import Util |
| 13 |
from stetl.packet import FORMAT |
| 14 |
|
| 15 |
log = Util.get_log('Sieve') |
| 16 |
|
| 17 |
|
| 18 |
class Sieve(Filter): |
| 19 |
""" |
| 20 |
ABC for specific Sieves that pass-through, "sieve", Packets based on criteria in their data. |
| 21 |
""" |
| 22 |
|
| 23 |
def __init__(self, configdict, section, consumes, produces): |
| 24 |
Filter.__init__(self, configdict, section, consumes, produces) |
| 25 |
|
| 26 |
def invoke(self, packet): |
| 27 |
if packet.data is None: |
| 28 |
return packet |
| 29 |
return self.sieve(packet) |
| 30 |
|
| 31 |
def sieve(self, packet): |
| 32 |
""" |
| 33 |
To be implemented in subclasses. |
| 34 |
:param packet: |
| 35 |
:return: |
| 36 |
""" |
| 37 |
return packet |
| 38 |
|
| 39 |
|
| 40 |
class AttrValueRecordSieve(Sieve): |
| 41 |
""" |
| 42 |
Sieves by attr/value(s) in Record Packets. |
| 43 |
""" |
| 44 |
|
| 45 |
@Config(ptype=str, required=True) |
| 46 |
def attr_name(self): |
| 47 |
""" |
| 48 |
Name of attribute whose value(s) are to be sieved. |
| 49 |
""" |
| 50 |
pass |
| 51 |
|
| 52 |
@Config(ptype=list, default=list(), required=False) |
| 53 |
def attr_values(self): |
| 54 |
""" |
| 55 |
Value(s) for attribute to be to sieved. If empty any value is passed through (existence |
| 56 |
of attr_name is criterium). |
| 57 |
""" |
| 58 |
pass |
| 59 |
|
| 60 |
def __init__(self, configdict, section): |
| 61 |
Sieve.__init__(self, configdict, section, consumes=[FORMAT.record_array, FORMAT.record], produces=[FORMAT.record_array, FORMAT.record]) |
| 62 |
|
| 63 |
def sieve(self, packet): |
| 64 |
""" |
| 65 |
Filter out Packets that are not matching designated attr value(s). |
| 66 |
:param packet: |
| 67 |
:return: |
| 68 |
""" |
| 69 |
|
| 70 |
# Start with empty result: fill with matching records |
| 71 |
record_data = packet.data |
| 72 |
packet.data = None |
| 73 |
|
| 74 |
# Data can be list or single record |
| 75 |
if type(record_data) is list: |
| 76 |
packet.data = list() |
| 77 |
for record in record_data: |
| 78 |
if self.matches_attr(record): |
| 79 |
packet.data.append(record) |
| 80 |
elif type(record_data) is dict: |
| 81 |
if self.matches_attr(record_data): |
| 82 |
packet.data = record_data |
| 83 |
|
| 84 |
return packet |
| 85 |
|
| 86 |
def matches_attr(self, record): |
| 87 |
# Attr not even in record: no use going on |
| 88 |
if self.attr_name not in record: |
| 89 |
return False |
| 90 |
|
| 91 |
# Match if no value |
| 92 |
if not self.attr_values: |
| 93 |
return True |
| 94 |
|
| 95 |
return record[self.attr_name] in self.attr_values |