Newer
Older
Import / projects / Gameloft / bne_lib / build / scripts / ExtractGameDBSchema.py
#!/usr/bin/python


#
# Extracts the schema from the gamedb.json
# It takes its input from reading a json file and outputs to stdout.
#
# Example usage:
#
#       ./ExtractGameDBSchema.py gameDB.json > schema.txt
#       ./ExtractGameDBSchema.py gameDB.json --keepindices > schema.txt
#
# Example output:
#
#             BuildingType.uid
#             BuildingType.text.descr
#             BuildingType.text.name
#             BuildingType.type
#             Ingredient.uid
#             Ingredient.cost
#             Ingredient.text.descr
#             Ingredient.text.name
#             Ingredient.type
#


import sys, json
import GameDBUtils

gamedb = json.load(open(sys.argv[1], 'rb'));

gamedbkeys = gamedb.keys()
gamedbkeys.sort()

KeepArrayIndices = False
if len(sys.argv)>=3:
  param = str(sys.argv[2])
  if param=="--keepindices":
    KeepArrayIndices=True

# Iterate the tables of the GameDB
for gamedbtable in gamedbkeys:
  rows = gamedb[gamedbtable]
  if len(rows) > 0:
    keySet = {}
    for item in rows:
      # Use a slightly different function that will only 
      # output array names not a long list of all arrays indices
      if KeepArrayIndices:
        itemFlat = GameDBUtils.FlattenJson(item, False)
      else:
        itemFlat = GameDBUtils.FlattenJsonWithSimpleArray(item)
      for k in itemFlat.keys():
        keySet[k] = k

    # Make uid the first column
    keys = keySet.keys()
    if 'uid' in keys:
      keys.remove('uid')
      keys.sort()
      keys = ['uid'] + keys

    # Print out the columns of the table
    for item in keys:
      print gamedbtable + '.' + item + '	D'