52662843 by Barry

Updated convert to handle "jobs" field with a divide by 2.

Added fields and calculated to the hash input.
Fixed a bug with the fields where in cases where the order of fields was
not sequential it would cause the wrong data to be entered into another
column
Fixed a bug where if the value did not exist or the field was not in the
xml the insert would fail. Now a 0 is inserted instead.
1 parent a3a7a20c
......@@ -13,26 +13,44 @@ import shlex
field_list = {
"items-armor.xml":
["id", "flags", "name", "description", "level", "iLevel", "slots", "races"],
{"fields": ["id", "flags", "name", "description", "level", "iLevel", "slots", "races"],
"calculated": {}},
"items-armor2.xml":
["id", "flags", "name", "description", "level", "iLevel", "slots", "races"],
{"fields": ["id", "flags", "name", "description", "level", "iLevel", "slots", "races"],
"calculated": {}},
"items-currency.xml":
["id", "flags", "name", "description", "log-name-singular", "log-name-plural"],
{"fields": ["id", "flags", "name", "description", "log-name-singular", "log-name-plural"],
"calculated": {}},
"items-general.xml":
["id", "flags", "name", "description"],
{"fields": ["id", "flags", "name", "description"],
"calculated": {}},
"items-general2.xml":
["id", "flags", "name", "description"],
{"fields": ["id", "flags", "name", "description"],
"calculated": {}},
"items-puppet.xml":
["id", "flags", "name", "description", "puppet-slot", "element-charge"],
{"fields": ["id", "flags", "name", "description", "puppet-slot", "element-charge"],
"calculated": {}},
"items-usable.xml":
["id", "flags", "name", "description", "activation-time"],
{"fields": ["id", "flags", "name", "description", "activation-time"],
"calculated": {}},
"items-voucher-slip.xml":
["id", "flags", "name", "description", "valid-targets"],
{"fields": ["id", "flags", "name", "description", "valid-targets"],
"calculated": {}},
"items-weapons.xml":
["id", "flags", "name", "description", "level", "iLevel", "slots", "races", "damage"],
{"fields": ["id", "flags", "name", "description", "level", "iLevel", "slots", "races", "damage", "jobs"],
"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")
......@@ -47,9 +65,8 @@ 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 = (', '.join('"' + item + '"' for item in field_list[filename]["fields"]))
query = "CREATE TABLE \"%s\" (%s)" % (filename, fields)
#print (query)
c.execute(query)
conn.commit()
except sqlite3.OperationalError:
......@@ -59,10 +76,19 @@ for filename in os.listdir(args.sourcexml):
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'] in field_list[filename]:
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()
......
No preview for this file type