Debian Codegraph @

stetl/filters/packetwriter.py

raw
1 # Writes the payload of a packet as a string to a file.
2 # Based on outputs.fileoutput.FileOutput.
3 #
4 # Author: Frank Steggink
5 #
6 import os
7
8 from stetl.component import Config
9 from stetl.filter import Filter
10 from stetl.util import Util
11 from stetl.packet import FORMAT
12
13 log = Util.get_log('packetwriter')
14
15
16 class PacketWriter(Filter):
17 """
18 Writes the payload of a packet as a string to a file.
19
20 consumes=FORMAT.any, produces=FORMAT.string
21 """
22
23 # Start attribute config meta
24 @Config(ptype=str, default=None, required=True)
25 def file_path(self):
26 """
27 File path to write content to.
28 """
29 pass
30
31 # End attribute config meta
32
33 # Constructor
34 def __init__(self, configdict, section):
35 Filter.__init__(self, configdict, section, consumes=FORMAT.any, produces=FORMAT.string)
36 log.info("working dir %s" % os.getcwd())
37
38 def invoke(self, packet):
39 if packet.data is None:
40 return packet
41
42 file_path = self.cfg.get('file_path')
43 return self.write_file(packet, file_path)
44
45 def write_file(self, packet, file_path):
46 log.info('writing to file %s' % file_path)
47 out_file = open(file_path, 'w')
48
49 out_file.write(packet.to_string())
50
51 out_file.close()
52 log.info("written to %s" % file_path)
53
54 packet.data = file_path
55 return packet