From 8a939f5b45d24cdaa258eeee99706ad56967cfac Mon Sep 17 00:00:00 2001 From: Eryn Wells Date: Sun, 24 Apr 2022 17:08:21 -0700 Subject: [PATCH] Update import-nethack-logfile script --- scripts/import-nethack-logfile.py | 24 ++++++++++++++++++++---- 1 file changed, 20 insertions(+), 4 deletions(-) diff --git a/scripts/import-nethack-logfile.py b/scripts/import-nethack-logfile.py index c8701bc..6591c7d 100755 --- a/scripts/import-nethack-logfile.py +++ b/scripts/import-nethack-logfile.py @@ -12,6 +12,7 @@ logfiles. import argparse import datetime import json +from locale import normalize import os.path import subprocess import sys @@ -63,6 +64,7 @@ ALIGNMENTS = { def parse_args(argv, *a, **kw): parser = argparse.ArgumentParser(*a, **kw) + parser.add_argument('-o', '--output',help='Path to the output file') parser.add_argument('logfile', help='Path to the Nethack log file to convert') args = parser.parse_args(argv) return args @@ -78,7 +80,7 @@ def main(argv): records = [] with open(args.logfile) as logfile: - for record in logfile.readlines(): + for record in logfile: fields = record.split(maxsplit=15) dungeon_number = int(fields[2]) @@ -110,7 +112,7 @@ def main(argv): 'start_date': start_date, 'character': { 'name': name, - 'abbreviated': f'{name}-{role}-{race}-{gender}-{alignment}', + 'descriptor': f'{name}-{role}-{race}-{gender}-{alignment}', 'max_level': int(fields[4]), 'hp': {'n': int(fields[5]), 'max': int(fields[6])}, 'role': {'short': role, 'descriptive': ROLES[role]}, @@ -126,8 +128,22 @@ def main(argv): } }) - json.dump(records, sys.stdout, indent=2) - print('\n') + output_object = { + 'generated': datetime.datetime.now().isoformat(), + 'logfile': records, + } + + output_path = args.output + if output_path and output_path != '-': + output_file = open(output_path, 'w') + else: + output_file = sys.stdout + + json.dump(output_object, output_file, indent=2) + output_file.write('\n') + output_file.close() + + return 0 if __name__ == '__main__': import sys