| 1 |
# String filtering. |
| 2 |
# |
| 3 |
# Author:Just van den Broecke |
| 4 |
|
| 5 |
from stetl.component import Config |
| 6 |
from stetl.util import Util |
| 7 |
from stetl.filter import Filter |
| 8 |
from stetl.packet import FORMAT |
| 9 |
|
| 10 |
log = Util.get_log("stringfilter") |
| 11 |
|
| 12 |
|
| 13 |
class StringFilter(Filter): |
| 14 |
""" |
| 15 |
Base class for any string filtering |
| 16 |
""" |
| 17 |
|
| 18 |
# Constructor |
| 19 |
def __init__(self, configdict, section, consumes, produces): |
| 20 |
Filter.__init__(self, configdict, section, consumes, produces) |
| 21 |
|
| 22 |
def invoke(self, packet): |
| 23 |
if packet.data is None: |
| 24 |
return packet |
| 25 |
return self.filter_string(packet) |
| 26 |
|
| 27 |
def filter_string(self, packet): |
| 28 |
pass |
| 29 |
|
| 30 |
|
| 31 |
class StringSubstitutionFilter(StringFilter): |
| 32 |
""" |
| 33 |
String filtering using Python advanced String formatting. |
| 34 |
String should have substitutable values like {schema} {foo} |
| 35 |
format_args should be of the form format_args = schema:test foo:bar ... |
| 36 |
|
| 37 |
consumes=FORMAT.string, produces=FORMAT.string |
| 38 |
""" |
| 39 |
|
| 40 |
@Config(ptype=str, default=None, required=True) |
| 41 |
def format_args(self): |
| 42 |
""" |
| 43 |
Provides a list of format arguments used by the string substitution filter. Formatting of content according to Python String.format(). |
| 44 |
String should have substitutable values like {schema} {foo}. |
| 45 |
|
| 46 |
Example: format_args = schema:test foo:bar |
| 47 |
""" |
| 48 |
pass |
| 49 |
|
| 50 |
@Config(ptype=str, default=':', required=False) |
| 51 |
def separator(self): |
| 52 |
""" |
| 53 |
Provides the separator to split the format argument names from their values. |
| 54 |
""" |
| 55 |
pass |
| 56 |
|
| 57 |
# Constructor |
| 58 |
def __init__(self, configdict, section): |
| 59 |
StringFilter.__init__(self, configdict, section, consumes=FORMAT.string, produces=FORMAT.string) |
| 60 |
|
| 61 |
# Convert string to dict: http://stackoverflow.com/a/1248990 |
| 62 |
self.format_args_dict = Util.string_to_dict(self.format_args, self.separator) |
| 63 |
|
| 64 |
def filter_string(self, packet): |
| 65 |
# String substitution based on Python String.format() |
| 66 |
packet.data = packet.data.format(**self.format_args_dict) |
| 67 |
return packet |
| 68 |
|
| 69 |
|
| 70 |
class StringConcatFilter(StringFilter): |
| 71 |
""" |
| 72 |
Concatenates a specified string with the input string (packet.data) either/or as prefix (prepend) |
| 73 |
or postfix (append) and outputs that concatenation. |
| 74 |
|
| 75 |
consumes=FORMAT.string, produces=FORMAT.string |
| 76 |
""" |
| 77 |
|
| 78 |
@Config(ptype=str, default=None, required=False) |
| 79 |
def append_string(self): |
| 80 |
""" |
| 81 |
String to be appended. |
| 82 |
|
| 83 |
Example: append_string = /002PND150904.xml |
| 84 |
""" |
| 85 |
pass |
| 86 |
|
| 87 |
@Config(ptype=str, default=None, required=False) |
| 88 |
def prepend_string(self): |
| 89 |
""" |
| 90 |
String to be prepended. |
| 91 |
|
| 92 |
Example: prepend_string = /vsizip/ |
| 93 |
""" |
| 94 |
pass |
| 95 |
|
| 96 |
# Constructor |
| 97 |
def __init__(self, configdict, section): |
| 98 |
StringFilter.__init__(self, configdict, section, consumes=FORMAT.string, produces=FORMAT.string) |
| 99 |
|
| 100 |
def filter_string(self, packet): |
| 101 |
if not packet.data: |
| 102 |
return |
| 103 |
|
| 104 |
if self.append_string: |
| 105 |
packet.data = packet.data + self.append_string |
| 106 |
|
| 107 |
if self.prepend_string: |
| 108 |
packet.data = self.prepend_string + packet.data |
| 109 |
|
| 110 |
return packet |