Update import-nethack-logfile script

This commit is contained in:
Eryn Wells 2022-04-24 17:08:21 -07:00
parent 9efb210508
commit 8a939f5b45

View file

@ -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