convert.py
4.03 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
import untangle
import sys
import os
import argparse
import sqlite3
import shlex
# To Install:
# * Install python 2.7.X
# * pip install untangle
# Edit the fields below for each file. If there are other files just add them to the hash.
field_list = {
"items-armor.xml":
{"fields": ["id", "flags", "stack-size", "valid-targets", "name", "description", "log-name-singular", "log-name-plural", "level", "iLevel", "slots", "races", "jobs", "superior-level", "shield-size", "max-charges", "casting-time" ,"use-delay", "reuse-delay"],
"calculated": {"jobs": "divide_hex_by_2"}},
"items-armor2.xml":
{"fields": ["id", "flags", "stack-size", "valid-targets", "name", "description", "log-name-singular", "log-name-plural", "level", "iLevel", "slots", "races", "jobs", "superior-level", "shield-size", "max-charges", "casting-time" ,"use-delay", "reuse-delay"],
"calculated": {"jobs": "divide_hex_by_2"}},
"items-currency.xml":
{"fields": ["id", "flags", "name", "description", "log-name-singular", "log-name-plural"],
"calculated": {}},
"items-general.xml":
{"fields": ["id", "flags", "stack-size", "valid-targets", "name", "description", "log-name-singular", "log-name-plural", "element", "storage-slots"],
"calculated": {}},
"items-general2.xml":
{"fields": ["id", "flags", "stack-size", "valid-targets", "name", "description", "log-name-singular", "log-name-plural", "element", "storage-slots"],
"calculated": {}},
"items-puppet.xml":
{"fields": ["id", "flags", "stack-size", "valid-targets", "name", "description", "log-name-singular", "log-name-plural", "puppet-slot", "element-charge"],
"calculated": {}},
"items-usable.xml":
{"fields": ["id", "flags", "stack-size", "valid-targets", "name", "description", "log-name-singular", "log-name-plural", "activation-time"],
"calculated": {}},
"items-voucher-slip.xml":
{"fields": ["id", "flags", "stack-size", "valid-targets", "name", "description", "log-name-singular", "log-name-plural"],
"calculated": {}},
"items-weapons.xml":
{"fields": ["id", "flags", "stack-size", "valid-targets", "name", "description", "log-name-singular", "log-name-plural", "level", "iLevel", "slots", "races", "jobs", "superior-level", "damage", "delay", "dps", "skill", "jug-size", "max-charges", "casting-time", "use-delay", "reuse-delay"],
"calculated": {"jobs": "divide_hex_by_2"}},
}
# Takes in a hex string in the formats: 0xDEADBEEF or DEADBEEF
# Outputs integer of the hex string divided by 2
def divide_hex_by_2(hexstring):
return int(hexstring, 16) / 2
#print(divide_hex_by_2("0xDEADBEEF"))
#print(divide_hex_by_2("00080BE6"))
#quit()
parser = argparse.ArgumentParser()
parser.add_argument('-s', '--sourcexml', required=True, help="Source XML Directory")
parser.add_argument('-o', '--outputdb', required=True, help="Destination SQLite Database")
args = parser.parse_args()
print ("Source XML Directory: %s" % (args.sourcexml,))
print ("Destination SQLite DB: %s" % (args.outputdb,))
conn = sqlite3.connect(args.outputdb)
c = conn.cursor()
for filename in os.listdir(args.sourcexml):
if filename.endswith(".xml"):
try:
fields = (', '.join('"' + item + '"' for item in field_list[filename]["fields"]))
query = "CREATE TABLE \"%s\" (%s)" % (filename, fields)
c.execute(query)
conn.commit()
except sqlite3.OperationalError:
pass
source_file = os.path.join(args.sourcexml, filename)
print ("Parsing File: %s" % (source_file,))
doc = untangle.parse(source_file)
for item in doc.thing_list.thing:
row = []
for field_name in field_list[filename]["fields"]:
field_set = False
for field in item.children:
if field['name'] == field_name:
if field['name'] in field_list[filename]['calculated']:
function_name = field_list[filename]['calculated'][field['name']]
function = locals()[function_name]
row.append(function(field.cdata))
else:
row.append(field.cdata)
field_set = True
if not field_set:
row.append(0)
query = "INSERT INTO \"%s\" VALUES(%s)" % (filename, ','.join('?' * len(row)))
c.execute(query, row)
conn.commit()
c.close()