-
Notifications
You must be signed in to change notification settings - Fork 34
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #28 from fsteggink/nlextract_update
Bedankt! Various Stetl updates for NLExtract
- Loading branch information
Showing
6 changed files
with
371 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
# -*- coding: utf-8 -*- | ||
# | ||
# Writes the payload of a packet as a string to a file. | ||
# Based on outputs.fileoutput.FileOutput. | ||
# | ||
# Author: Frank Steggink | ||
# | ||
from stetl.component import Config | ||
from stetl.filter import Filter | ||
from stetl.util import Util | ||
from stetl.packet import FORMAT | ||
|
||
import os | ||
|
||
log = Util.get_log('packetwriter') | ||
|
||
|
||
class PacketWriter(Filter): | ||
""" | ||
Writes the payload of a packet as a string to a file. | ||
consumes=FORMAT.any, produces=FORMAT.string | ||
""" | ||
|
||
# Start attribute config meta | ||
@Config(ptype=str, default=None, required=True) | ||
def file_path(self): | ||
""" | ||
File path to write content to. | ||
Required: True | ||
Default: None | ||
""" | ||
pass | ||
|
||
# End attribute config meta | ||
|
||
# Constructor | ||
def __init__(self, configdict, section): | ||
Filter.__init__(self, configdict, section, consumes=FORMAT.any, produces=FORMAT.string) | ||
log.info("working dir %s" % os.getcwd()) | ||
|
||
def invoke(self, packet): | ||
if packet.data is None: | ||
return packet | ||
|
||
file_path = self.cfg.get('file_path') | ||
return self.write_file(packet, file_path) | ||
|
||
def write_file(self, packet, file_path): | ||
log.info('writing to file %s' % file_path) | ||
out_file = open(file_path, 'w') | ||
|
||
out_file.write(packet.to_string()) | ||
|
||
out_file.close() | ||
log.info("written to %s" % file_path) | ||
|
||
packet.data = file_path | ||
return packet |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
# -*- coding: utf-8 -*- | ||
# | ||
# Extracts a file from a ZIP file, and saves it as the given file name. | ||
# | ||
# Author: Frank Steggink | ||
# | ||
from stetl.component import Config | ||
from stetl.filter import Filter | ||
from stetl.util import Util | ||
from stetl.packet import FORMAT | ||
|
||
log = Util.get_log('zipfileextractor') | ||
|
||
|
||
class ZipFileExtractor(Filter): | ||
""" | ||
Extracts a file from a ZIP file, and saves it as the given file name. | ||
consumes=FORMAT.record, produces=FORMAT.string | ||
""" | ||
|
||
# Start attribute config meta | ||
@Config(ptype=str, default=None, required=True) | ||
def file_path(self): | ||
""" | ||
File name to write the extracted file to. | ||
Required: True | ||
Default: None | ||
""" | ||
pass | ||
|
||
# End attribute config meta | ||
|
||
# Constructor | ||
def __init__(self, configdict, section): | ||
Filter.__init__(self, configdict, section, consumes=FORMAT.record, produces=FORMAT.string) | ||
self.cur_file_path = self.cfg.get('file_path') | ||
|
||
def invoke(self, packet): | ||
event = None | ||
|
||
if packet.data is None: | ||
log.info("No file name given") | ||
return packet | ||
|
||
import os | ||
import zipfile | ||
|
||
with zipfile.ZipFile(packet.data['file_path']) as z: | ||
with open(self.cur_file_path, 'wb') as f: | ||
f.write(z.read(packet.data['name'])) | ||
|
||
packet.data = self.cur_file_path | ||
return packet | ||
|
||
def after_chain_invoke(self, packet): | ||
import os.path | ||
if os.path.isfile(self.cur_file_path): | ||
os.remove(self.cur_file_path) | ||
|
||
return True |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.