Reformat the logfile

This commit is contained in:
Eryn Wells 2022-04-16 18:20:59 +00:00
parent fde5bf81ea
commit a5a226a5b3
2 changed files with 308 additions and 313 deletions

View file

@ -13,6 +13,7 @@ import argparse
import datetime
import json
import os.path
import subprocess
import sys
DUNGEONS = {
@ -73,6 +74,8 @@ def main(argv):
print('Given path is not a real file!', file=sys.stderr)
return -1
hostname = subprocess.check_output(['hostname', '-s']).decode('ascii').strip()
records = []
with open(args.logfile) as logfile:
for record in logfile.readlines():
@ -90,34 +93,40 @@ def main(argv):
start_date = datetime.datetime.strptime(fields[9], '%Y%m%d').strftime('%Y-%m-%d')
end_date = datetime.datetime.strptime(fields[8], '%Y%m%d').strftime('%Y-%m-%d')
name, reason = fields[15].split(',', maxsplit=1)
name, cause_of_death = (s.strip() for s in fields[15].split(',', maxsplit=1))
role = fields[11]
race = fields[12]
gender = fields[13]
alignment = fields[14]
records.append({
'nethack_version': fields[0],
'score': int(fields[1]),
'dungeon_number': int(fields[2]),
'dungeon_name': dungeon_name,
'level': dungeon_level,
'level_descriptive': dungeon_level_descriptive,
'max_level': int(fields[4]),
'hit_points': int(fields[5]),
'max_hit_points': int(fields[6]),
'number_of_deaths': int(fields[7]),
'dungeon': {
'n': int(fields[2]),
'name': dungeon_name,
'level': {'n': dungeon_level, 'descriptive': dungeon_level_descriptive},
},
'end_date': end_date,
'start_date': start_date,
'user_id': int(fields[10]),
'role': ROLES[fields[11]],
'race': RACES[fields[12]],
'gender': GENDERS[fields[13]],
'alignment': ALIGNMENTS[fields[14]],
'name': name.strip(),
'reason_for_death': reason.strip(),
'character': {
'name': name,
'abbreviated': 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]},
'race': {'short': race, 'descriptive': RACES[race]},
'gender': {'short': gender, 'descriptive': GENDERS[gender]},
'alignment': {'short': alignment, 'descriptive': ALIGNMENTS[alignment]},
},
'death': {'n': int(fields[7]), 'cause': cause_of_death},
'system': {
'hostname': hostname,
'user_id': int(fields[10]),
'nethack_version': fields[0],
}
})
output_object = {
'records': records,
}
json.dump(output_object, sys.stdout, indent=2)
json.dump(records, sys.stdout, indent=2)
print('\n')
if __name__ == '__main__':