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

From The Alchemist Code Wiki
Jump to: navigation, search

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

Usage[edit | edit source]

get-wikified-data.py PAGE_OUTPUT DATA_FILE PARAMS_FILE

  • 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[edit | edit source]

#!/usr/bin/python
import sys
import os
import re
import json

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

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

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

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

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

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

    # 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'
        
# Write the page file
print(output, file=open(page_output, 'w'))