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