Add tools to convert ini files to JSON format.

master
per 2013-12-31 15:55:10 +01:00
parent 0ad9c4d1fb
commit 9954fdaebf
3 changed files with 149 additions and 0 deletions

View File

@ -0,0 +1,24 @@
#!/bin/bash
# Convert all stats to JSON format. Run it from its directory.
#
# Missing : bodypropulsionimd.ini propulsionsounds.ini propulsiontype.ini structuremodifier.ini weaponmodifier.ini
#
BASE="../../../data"
function generic
{
python ini2json_generic.py ${BASE}/base/stats/$1.ini > ${BASE}/base/stats/$1.json
python ini2json_generic.py ${BASE}/mp/stats/$1.ini > ${BASE}/mp/stats/$1.json
}
generic ecm
generic sensor
generic weapons
generic repair
generic construction
generic propulsion
generic structure
generic body
generic templates
python ini2json_research.py ${BASE}/base/stats/research_cam1.ini > ${BASE}/base/stats/research_cam1.json
python ini2json_research.py ${BASE}/base/stats/research_cam2.ini > ${BASE}/base/stats/research_cam2.json
python ini2json_research.py ${BASE}/base/stats/research_cam3.ini > ${BASE}/base/stats/research_cam3.json
python ini2json_research.py ${BASE}/mp/stats/research.ini > ${BASE}/mp/stats/research.json

View File

@ -0,0 +1,53 @@
import ConfigParser
import json
import sys
if len(sys.argv) < 2:
print 'Need file parameter'
sys.exit(1)
config = ConfigParser.ConfigParser()
config.optionxform = str # stop making keys lowercase
config.read(sys.argv[1])
def is_number(s):
try:
int(s)
return True
except ValueError:
return False
translation = {
# templates
'compBody' : 'body', 'compPropulsion' : 'propulsion', 'compSensor' : 'sensor',
'compConstruct' : 'construct', 'compRepair' : 'repair', 'compBrain' : 'brain'
# heat vs thermal vs ... TODO
}
data = {}
for section in config.sections():
name = config.get(section, 'name')
if name.startswith('"') and name.endswith('"'):
name = name[1:-1]
entry = {}
for opt in config.items(section):
if opt[0] == 'name': continue
key = opt[0]
if key in translation:
key = translation[key]
value = opt[1]
if value.startswith('"') and value.endswith('"'):
value = value[1:-1]
value = value.split(',')
accum = []
for result in value: # convert numbers
if is_number(result):
accum.append(int(result))
else:
accum.append(result)
if len(accum) == 1 and key != 'weapons':
entry[key] = accum[0]
else:
entry[key] = accum
data[name] = entry
print json.dumps(data, indent=4, separators=(',', ': '))

View File

@ -0,0 +1,72 @@
import ConfigParser
import json
import sys
if len(sys.argv) < 2:
print 'Need file parameter'
sys.exit(1)
config = ConfigParser.ConfigParser()
config.read(sys.argv[1])
def is_number(s):
try:
int(s)
return True
except ValueError:
return False
data = {}
opts = ['researchPoints', 'researchPower', 'keyTopic', 'msgName', 'iconID', 'statID', 'requiredResearch', 'resultComponents',
'techCode', 'imdName', 'subgroupIconID', 'requiredStructures', 'redStructures', 'redComponents', 'resultComponents',
'replacedComponents']
for section in config.sections():
name = config.get(section, 'name')
if name.startswith('"') and name.endswith('"'):
name = name[1:-1]
entry = {}
for opt in opts:
if config.has_option(section, opt):
value = config.get(section, opt).strip()
if value.startswith('"') and value.endswith('"'):
value = value[1:-1]
value = value.split(',')
accum = []
for result in value: # convert numbers
if is_number(result):
accum.append(int(result))
else:
accum.append(result)
if len(accum) == 1:
entry[opt] = accum[0]
else:
entry[opt] = accum
if config.has_option(section, 'results'):
value = config.get(section, 'results')
value = value.split(',')
accum = []
for result in value: # convert custom format
result = result.strip()
if result.startswith('"'): result = result[1:]
if result.endswith('"'): result = result[:-1]
tmp = {}
comps = result.split(':')
if comps[0].isupper():
tmp['weaponSubClass'] = comps[0]
elif comps[0] in ['Cyborgs', 'Droids', 'Transport']:
tmp['bodyClass'] = comps[0]
elif comps[0] in ['Wall', 'Structure', 'Defense']:
tmp['structureType'] = comps[0]
elif comps[0] in ['RearmPoints', 'ProductionPoints', 'ResearchPoints', 'RepairPoints', 'Sensor', 'ECM', 'PowerPoints', 'Construct']:
pass # affects a global state
else: # forgot something...
print 'Unknown filter: %s' % comps[0]
sys.exit(1)
if len(comps) > 2:
tmp[comps[1]] = int(comps[2])
else:
tmp[comps[0]] = int(comps[1])
accum.append(tmp)
entry['results'] = accum
data[name] = entry
print json.dumps(data, indent=4, separators=(',', ': '))