File: stringfilter.py

package info (click to toggle)
python-stetl 2.1-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 90,360 kB
  • sloc: python: 5,618; xml: 707; sql: 430; makefile: 147; sh: 71
file content (110 lines) | stat: -rw-r--r-- 3,180 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
# String filtering.
#
# Author:Just van den Broecke

from stetl.component import Config
from stetl.util import Util
from stetl.filter import Filter
from stetl.packet import FORMAT

log = Util.get_log("stringfilter")


class StringFilter(Filter):
    """
    Base class for any string filtering
    """

    # Constructor
    def __init__(self, configdict, section, consumes, produces):
        Filter.__init__(self, configdict, section, consumes, produces)

    def invoke(self, packet):
        if packet.data is None:
            return packet
        return self.filter_string(packet)

    def filter_string(self, packet):
        pass


class StringSubstitutionFilter(StringFilter):
    """
    String filtering using Python advanced String formatting.
    String should have substitutable values like {schema} {foo}
    format_args should be of the form format_args = schema:test foo:bar ...

    consumes=FORMAT.string, produces=FORMAT.string
    """

    @Config(ptype=str, default=None, required=True)
    def format_args(self):
        """
        Provides a list of format arguments used by the string substitution filter. Formatting of content according to Python String.format().
        String should have substitutable values like {schema} {foo}.

        Example: format_args = schema:test foo:bar
        """
        pass

    @Config(ptype=str, default=':', required=False)
    def separator(self):
        """
        Provides the separator to split the format argument names from their values.
        """
        pass

    # Constructor
    def __init__(self, configdict, section):
        StringFilter.__init__(self, configdict, section, consumes=FORMAT.string, produces=FORMAT.string)

        # Convert string to dict: http://stackoverflow.com/a/1248990
        self.format_args_dict = Util.string_to_dict(self.format_args, self.separator)

    def filter_string(self, packet):
        # String substitution based on Python String.format()
        packet.data = packet.data.format(**self.format_args_dict)
        return packet


class StringConcatFilter(StringFilter):
    """
    Concatenates a specified string with the input string (packet.data) either/or as prefix (prepend)
    or postfix (append) and outputs that concatenation.

    consumes=FORMAT.string, produces=FORMAT.string
    """

    @Config(ptype=str, default=None, required=False)
    def append_string(self):
        """
        String to be appended.

        Example: append_string = /002PND150904.xml
        """
        pass

    @Config(ptype=str, default=None, required=False)
    def prepend_string(self):
        """
        String to be prepended.

        Example: prepend_string = /vsizip/
        """
        pass

    # Constructor
    def __init__(self, configdict, section):
        StringFilter.__init__(self, configdict, section, consumes=FORMAT.string, produces=FORMAT.string)

    def filter_string(self, packet):
        if not packet.data:
            return

        if self.append_string:
            packet.data = packet.data + self.append_string

        if self.prepend_string:
            packet.data = self.prepend_string + packet.data

        return packet