Update import-nethack-logfile script
This commit is contained in:
parent
9efb210508
commit
8a939f5b45
1 changed files with 20 additions and 4 deletions
|
@ -12,6 +12,7 @@ logfiles.
|
||||||
import argparse
|
import argparse
|
||||||
import datetime
|
import datetime
|
||||||
import json
|
import json
|
||||||
|
from locale import normalize
|
||||||
import os.path
|
import os.path
|
||||||
import subprocess
|
import subprocess
|
||||||
import sys
|
import sys
|
||||||
|
@ -63,6 +64,7 @@ ALIGNMENTS = {
|
||||||
|
|
||||||
def parse_args(argv, *a, **kw):
|
def parse_args(argv, *a, **kw):
|
||||||
parser = argparse.ArgumentParser(*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')
|
parser.add_argument('logfile', help='Path to the Nethack log file to convert')
|
||||||
args = parser.parse_args(argv)
|
args = parser.parse_args(argv)
|
||||||
return args
|
return args
|
||||||
|
@ -78,7 +80,7 @@ def main(argv):
|
||||||
|
|
||||||
records = []
|
records = []
|
||||||
with open(args.logfile) as logfile:
|
with open(args.logfile) as logfile:
|
||||||
for record in logfile.readlines():
|
for record in logfile:
|
||||||
fields = record.split(maxsplit=15)
|
fields = record.split(maxsplit=15)
|
||||||
|
|
||||||
dungeon_number = int(fields[2])
|
dungeon_number = int(fields[2])
|
||||||
|
@ -110,7 +112,7 @@ def main(argv):
|
||||||
'start_date': start_date,
|
'start_date': start_date,
|
||||||
'character': {
|
'character': {
|
||||||
'name': name,
|
'name': name,
|
||||||
'abbreviated': f'{name}-{role}-{race}-{gender}-{alignment}',
|
'descriptor': f'{name}-{role}-{race}-{gender}-{alignment}',
|
||||||
'max_level': int(fields[4]),
|
'max_level': int(fields[4]),
|
||||||
'hp': {'n': int(fields[5]), 'max': int(fields[6])},
|
'hp': {'n': int(fields[5]), 'max': int(fields[6])},
|
||||||
'role': {'short': role, 'descriptive': ROLES[role]},
|
'role': {'short': role, 'descriptive': ROLES[role]},
|
||||||
|
@ -126,8 +128,22 @@ def main(argv):
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
json.dump(records, sys.stdout, indent=2)
|
output_object = {
|
||||||
print('\n')
|
'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__':
|
if __name__ == '__main__':
|
||||||
import sys
|
import sys
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue