Format pass
This commit is contained in:
parent
305d44bc1b
commit
eb20e0561d
1 changed files with 44 additions and 50 deletions
94
main.py
94
main.py
|
@ -1,59 +1,52 @@
|
|||
#!/usr/bin/env python3
|
||||
|
||||
__version__ = '0.1'
|
||||
__author__ = 'Trysdyn Black'
|
||||
__version__ = "0.1"
|
||||
__author__ = "Trysdyn Black"
|
||||
|
||||
import json
|
||||
import sys
|
||||
|
||||
|
||||
def parse_CHARACTERS(data):
|
||||
replacements = {
|
||||
'Looks like': 'looks',
|
||||
'World of Ruin location': 'wor_location',
|
||||
'Notable equipment': 'equipment'
|
||||
}
|
||||
replacements = {"Looks like": "looks", "World of Ruin location": "wor_location", "Notable equipment": "equipment"}
|
||||
|
||||
result = {}
|
||||
|
||||
for c_data in data.split('\n\n')[1:-1]:
|
||||
info = {
|
||||
'stats': {},
|
||||
'spells': {},
|
||||
'natural_magic': False
|
||||
}
|
||||
for c_data in data.split("\n\n")[1:-1]:
|
||||
info = {"stats": {}, "spells": {}, "natural_magic": False}
|
||||
|
||||
for line in c_data.split('\n'):
|
||||
for line in c_data.split("\n"):
|
||||
# Name
|
||||
if line[0:2].isdigit():
|
||||
name = line[4:]
|
||||
|
||||
# Stat chart rows
|
||||
elif line.startswith('|'):
|
||||
for stat in line[1:-1].split('|'):
|
||||
if ':' in stat:
|
||||
stat_name, stat_value = stat.split(':')
|
||||
stat_name = stat_name.replace('.', '').strip().lower()
|
||||
info['stats'][stat_name] = int(stat_value)
|
||||
elif line.startswith("|"):
|
||||
for stat in line[1:-1].split("|"):
|
||||
if ":" in stat:
|
||||
stat_name, stat_value = stat.split(":")
|
||||
stat_name = stat_name.replace(".", "").strip().lower()
|
||||
info["stats"][stat_name] = int(stat_value)
|
||||
|
||||
# Spell learnset rows
|
||||
elif line.startswith(' LV'):
|
||||
spell_level, spell_name = line.split('-', 1)
|
||||
info['spells'][spell_name.strip()] = int(spell_level.strip().split(' ')[1])
|
||||
elif line.startswith(" LV"):
|
||||
spell_level, spell_name = line.split("-", 1)
|
||||
info["spells"][spell_name.strip()] = int(spell_level.strip().split(" ")[1])
|
||||
|
||||
# Special k=v strings with comma-delimited lists
|
||||
elif line.startswith('Commands:'):
|
||||
info['commands'] = [command.strip() for command in line.split(':')[1].split(',')]
|
||||
elif line.startswith("Commands:"):
|
||||
info["commands"] = [command.strip() for command in line.split(":")[1].split(",")]
|
||||
|
||||
elif line.startswith('Notable'):
|
||||
info['equipment'] = [eq.strip() for eq in line.split(':')[1].split(',')]
|
||||
elif line.startswith("Notable"):
|
||||
info["equipment"] = [eq.strip() for eq in line.split(":")[1].split(",")]
|
||||
|
||||
# Special bare strings
|
||||
elif line.startswith('Has natural'):
|
||||
info['natural_magic'] = True
|
||||
elif line.startswith("Has natural"):
|
||||
info["natural_magic"] = True
|
||||
|
||||
# Everything else: normal k=v colon strings
|
||||
elif ':' in line:
|
||||
field, value = line.split(':', 1)
|
||||
elif ":" in line:
|
||||
field, value = line.split(":", 1)
|
||||
if field in replacements.keys():
|
||||
field = replacements[field]
|
||||
field = field.lower()
|
||||
|
@ -71,24 +64,24 @@ def parse_COMMANDS(data):
|
|||
# As a result we have to pull the last line from each block and remember
|
||||
# it as the name of the command in the next block. Blorf :)
|
||||
next_command_name = None
|
||||
for c_data in data.split('\n-------\n'):
|
||||
c_data_lines = [c_data_line.strip() for c_data_line in c_data.split('\n')]
|
||||
if '' in c_data_lines:
|
||||
c_data_lines.remove('')
|
||||
for c_data in data.split("\n-------\n"):
|
||||
c_data_lines = [c_data_line.strip() for c_data_line in c_data.split("\n")]
|
||||
if "" in c_data_lines:
|
||||
c_data_lines.remove("")
|
||||
if next_command_name:
|
||||
command_string = '; '.join(c_data_lines[:-1])
|
||||
command_string = "; ".join(c_data_lines[:-1])
|
||||
|
||||
# Clip trailing junk from inconsistent spoiler log generation
|
||||
# as well as the join above
|
||||
if command_string.endswith('; '):
|
||||
if command_string.endswith("; "):
|
||||
command_string = command_string[:-2]
|
||||
if command_string.endswith('.'):
|
||||
if command_string.endswith("."):
|
||||
command_string = command_string[:-1]
|
||||
|
||||
# Clean up a couple of clumsy string cases from the join above
|
||||
command_string = command_string.replace('.; ', ': ')
|
||||
command_string = command_string.replace(' ', ' ')
|
||||
command_string = command_string.replace(':;', ':')
|
||||
command_string = command_string.replace(".; ", ": ")
|
||||
command_string = command_string.replace(" ", " ")
|
||||
command_string = command_string.replace(":;", ":")
|
||||
|
||||
# Commit the command to the dict
|
||||
commands[next_command_name] = command_string
|
||||
|
@ -100,8 +93,8 @@ def parse_COMMANDS(data):
|
|||
|
||||
def load(filename):
|
||||
# Load our file, tokenize by section header (starting with ====)
|
||||
with open(filename, 'r') as infile:
|
||||
tok_data = infile.read().split('============================================================\n')
|
||||
with open(filename, "r") as infile:
|
||||
tok_data = infile.read().split("============================================================\n")
|
||||
|
||||
sections = {}
|
||||
|
||||
|
@ -109,16 +102,17 @@ def load(filename):
|
|||
for s in tok_data:
|
||||
# The top section needs special handling and contains only seed code
|
||||
if top_section:
|
||||
sections["SEED"] = s.split('\n', 1)[0][12:]
|
||||
sections["SEED"] = s.split("\n", 1)[0][12:]
|
||||
top_section = False
|
||||
continue
|
||||
|
||||
# Everything else we just dump into named sections for now
|
||||
section_header, section_data = s.split('\n', 1)
|
||||
section_header, section_data = s.split("\n", 1)
|
||||
sections[section_header[5:]] = section_data
|
||||
|
||||
return sections
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
sections = load(sys.argv[1])
|
||||
|
||||
|
@ -135,14 +129,14 @@ if __name__ == "__main__":
|
|||
# Subkey CHARACTERS commands with COMMANDS data
|
||||
# This turns lists of commands each character has into hashes where
|
||||
# Command name => Textual desc of command
|
||||
for character, c_data in data['CHARACTERS'].items():
|
||||
for character, c_data in data["CHARACTERS"].items():
|
||||
new_commands = {}
|
||||
for command in c_data['commands']:
|
||||
if command in data['COMMANDS']:
|
||||
new_commands[command] = data['COMMANDS'][command]
|
||||
for command in c_data["commands"]:
|
||||
if command in data["COMMANDS"]:
|
||||
new_commands[command] = data["COMMANDS"][command]
|
||||
else:
|
||||
new_commands[command] = command
|
||||
c_data['commands'] = new_commands
|
||||
c_data["commands"] = new_commands
|
||||
|
||||
# Barf this pile of trash out
|
||||
print(json.dumps(data))
|
||||
|
|
Loading…
Add table
Reference in a new issue