Game Files/Scripts/get-wikified-data.py

Generates a wiki page from a data file. An optional params file can be included to retain data previously entered into the page.

Usage

 * PAGE_OUTPUT: The output file that will contain the wiki-formatted page
 * DATA_FILE: The data file to generate the page from
 * PARAMS_FILES: Params file generated from parse-wiki-data.py that fills in data parsed from an existing page (optional)

Code
import sys import os import re import json
 * 1) !/usr/bin/python

if len(sys.argv) < 3: sys.exit("Usage: get-wikified-data.py PAGE_OUTPUT DATA_FILE (PARAMS_FILE)");

page_output = sys.argv[1] data_file = sys.argv[2] params = False

if len(sys.argv) > 3: params = json.load(open(sys.argv[3]))
 * 1) Load params if a param file is given

try: data = json.load(open(data_file)) except: sys.exit('Failed to parse JSON from data file')
 * 1) Attempt to parse the data file

output = '' # Data that will get written to the output file

try: output += '{}\n'.format(params['filedesc']) except: pass
 * 1) Print the file description if params were given

for objtype in data: output += '\n== {} ==\n'.format(objtype)
 * 1) Loop through each object type and print relevant data

# Print the object type's description if the param was given try: output += '\n{}\n'.format(params[objtype]['desc']) except: pass if not isinstance(data[objtype][0], dict): continue

# Add all unique attributes into a set attrs = set for obj in data[objtype]: for attr in obj.keys: attr_name = re.sub('\d+', 'X', attr) attrs.add(attr_name)

# Start the attribute table output += ''' {| class="wikitable" !Attribute !Description !Notes '''

# Print the data for the attributes, using params if given for attr in sorted(attrs): try: params[objtype]['attrs'][attr] except: attr_desc = '' attr_notes = '' else: attr_desc = params[objtype]['attrs'][attr]['desc'] attr_notes = params[objtype]['attrs'][attr]['notes'] output += '|-\n|{}\n|{}\n|{}\n'.format(attr, attr_desc, attr_notes)

# End the attribute table output += '|}\n' print(output, file=open(page_output, 'w'))
 * 1) Write the page file