| 1 |
# -*- coding: utf-8 -*- |
| 2 |
# |
| 3 |
# Example of user-defined component. |
| 4 |
# |
| 5 |
# Author:Just van den Broecke |
| 6 |
|
| 7 |
from stetl.util import Util |
| 8 |
from stetl.filter import Filter |
| 9 |
from stetl.packet import FORMAT |
| 10 |
|
| 11 |
log = Util.get_log("myfilter") |
| 12 |
|
| 13 |
|
| 14 |
class MyFilter(Filter): |
| 15 |
# Constructor |
| 16 |
def __init__(self, configdict, section): |
| 17 |
Filter.__init__(self, configdict, section, consumes=FORMAT.etree_doc, produces=FORMAT.etree_doc) |
| 18 |
|
| 19 |
def invoke(self, packet): |
| 20 |
if packet.data is None: |
| 21 |
return packet |
| 22 |
return self.do_something(packet) |
| 23 |
|
| 24 |
def do_something(self, packet): |
| 25 |
log.info("CALLING MyFilter OK!!!!") |
| 26 |
data = packet.data |
| 27 |
self.show_data(data) |
| 28 |
|
| 29 |
return packet |
| 30 |
|
| 31 |
def show_data(self, data): |
| 32 |
try: |
| 33 |
from lxml import etree |
| 34 |
except ImportError: |
| 35 |
# No lxml, so just print data |
| 36 |
print(data) |
| 37 |
else: |
| 38 |
root = data.getroot() |
| 39 |
for city in root.iterchildren(): |
| 40 |
city_name = city.find('name').text |
| 41 |
print('Welcome to {c} !'.format(c=city_name)) |
| 42 |
|