Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ability to split output into numbered files #182

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 39 additions & 3 deletions bin/aisdecode
Original file line number Diff line number Diff line change
Expand Up @@ -70,18 +70,54 @@ Available opptions:

--uscg=False
--validate_checksum=False

--file=NAME Write to named file, not stdout
--split=NUM Write NUM lines of output to each file, appending an
index to the filename

""")
sys.exit(0)

file_name = args.pop('file', 'None')
if file_name is not None:
file_base, file_ext = file_name.rsplit(".", 1)
split = int(args.pop('split', 'None'))

lines_in_current_file = 0
current_file = None
current_file_idx = 0
def get_file():
global lines_in_current_file
global current_file
global current_file_idx

if file_name is None:
return sys.stdout
if split is None:
if current_file is None:
current_file = open(file_name, "w")
return current_file
else:
if current_file is None or lines_in_current_file >= split:
if current_file:
current_file.close()
current_file = open("%s-%s.%s" % (file_base, current_file_idx, file_ext), "w")
current_file_idx += 1
lines_in_current_file = 1
else:
lines_in_current_file += 1
return current_file

encoding = args.pop('encoding', 'json')
if encoding == 'json':
def write(msg):
json.dump(msg, sys.stdout)
sys.stdout.write("\n")
out = get_file()
json.dump(msg, out)
out.write("\n")

if encoding == 'msgpack':
def write(msg):
msgpack.pack(msg, sys.stdout)
msgpack.pack(msg, get_file())

use_gpsd = args.pop("gpsd", False)
copy_tagblock_timestamp = args.pop("copy-tagblock-timestamp", True)
Expand Down