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 ...@@ -13,26 +13,44 @@ import shlex
13 13
14 field_list = { 14 field_list = {
15 "items-armor.xml": 15 "items-armor.xml":
16 ["id", "flags", "name", "description", "level", "iLevel", "slots", "races"], 16 {"fields": ["id", "flags", "name", "description", "level", "iLevel", "slots", "races"],
17 "calculated": {}},
17 "items-armor2.xml": 18 "items-armor2.xml":
18 ["id", "flags", "name", "description", "level", "iLevel", "slots", "races"], 19 {"fields": ["id", "flags", "name", "description", "level", "iLevel", "slots", "races"],
20 "calculated": {}},
19 "items-currency.xml": 21 "items-currency.xml":
20 ["id", "flags", "name", "description", "log-name-singular", "log-name-plural"], 22 {"fields": ["id", "flags", "name", "description", "log-name-singular", "log-name-plural"],
23 "calculated": {}},
21 "items-general.xml": 24 "items-general.xml":
22 ["id", "flags", "name", "description"], 25 {"fields": ["id", "flags", "name", "description"],
26 "calculated": {}},
23 "items-general2.xml": 27 "items-general2.xml":
24 ["id", "flags", "name", "description"], 28 {"fields": ["id", "flags", "name", "description"],
29 "calculated": {}},
25 "items-puppet.xml": 30 "items-puppet.xml":
26 ["id", "flags", "name", "description", "puppet-slot", "element-charge"], 31 {"fields": ["id", "flags", "name", "description", "puppet-slot", "element-charge"],
32 "calculated": {}},
27 "items-usable.xml": 33 "items-usable.xml":
28 ["id", "flags", "name", "description", "activation-time"], 34 {"fields": ["id", "flags", "name", "description", "activation-time"],
35 "calculated": {}},
29 "items-voucher-slip.xml": 36 "items-voucher-slip.xml":
30 ["id", "flags", "name", "description", "valid-targets"], 37 {"fields": ["id", "flags", "name", "description", "valid-targets"],
38 "calculated": {}},
31 "items-weapons.xml": 39 "items-weapons.xml":
32 ["id", "flags", "name", "description", "level", "iLevel", "slots", "races", "damage"], 40 {"fields": ["id", "flags", "name", "description", "level", "iLevel", "slots", "races", "damage", "jobs"],
41 "calculated": {"jobs": "divide_hex_by_2"}}
42
33 43
34 } 44 }
35 45
46 # Takes in a hex string in the formats: 0xDEADBEEF or DEADBEEF
47 # Outputs integer of the hex string divided by 2
48 def divide_hex_by_2(hexstring):
49 return int(hexstring, 16) / 2
50 #print(divide_hex_by_2("0xDEADBEEF"))
51 #print(divide_hex_by_2("00080BE6"))
52 #quit()
53
36 parser = argparse.ArgumentParser() 54 parser = argparse.ArgumentParser()
37 parser.add_argument('-s', '--sourcexml', required=True, help="Source XML Directory") 55 parser.add_argument('-s', '--sourcexml', required=True, help="Source XML Directory")
38 parser.add_argument('-o', '--outputdb', required=True, help="Destination SQLite Database") 56 parser.add_argument('-o', '--outputdb', required=True, help="Destination SQLite Database")
...@@ -47,9 +65,8 @@ c = conn.cursor() ...@@ -47,9 +65,8 @@ c = conn.cursor()
47 for filename in os.listdir(args.sourcexml): 65 for filename in os.listdir(args.sourcexml):
48 if filename.endswith(".xml"): 66 if filename.endswith(".xml"):
49 try: 67 try:
50 fields = (', '.join('"' + item + '"' for item in field_list[filename])) 68 fields = (', '.join('"' + item + '"' for item in field_list[filename]["fields"]))
51 query = "CREATE TABLE \"%s\" (%s)" % (filename, fields) 69 query = "CREATE TABLE \"%s\" (%s)" % (filename, fields)
52 #print (query)
53 c.execute(query) 70 c.execute(query)
54 conn.commit() 71 conn.commit()
55 except sqlite3.OperationalError: 72 except sqlite3.OperationalError:
...@@ -59,10 +76,19 @@ for filename in os.listdir(args.sourcexml): ...@@ -59,10 +76,19 @@ for filename in os.listdir(args.sourcexml):
59 doc = untangle.parse(source_file) 76 doc = untangle.parse(source_file)
60 for item in doc.thing_list.thing: 77 for item in doc.thing_list.thing:
61 row = [] 78 row = []
62 for field in item.children: 79 for field_name in field_list[filename]["fields"]:
63 if field['name'] in field_list[filename]: 80 field_set = False
64 row.append(field.cdata) 81 for field in item.children:
65 82 if field['name'] == field_name:
83 if field['name'] in field_list[filename]['calculated']:
84 function_name = field_list[filename]['calculated'][field['name']]
85 function = locals()[function_name]
86 row.append(function(field.cdata))
87 else:
88 row.append(field.cdata)
89 field_set = True
90 if not field_set:
91 row.append(0)
66 query = "INSERT INTO \"%s\" VALUES(%s)" % (filename, ','.join('?' * len(row))) 92 query = "INSERT INTO \"%s\" VALUES(%s)" % (filename, ','.join('?' * len(row)))
67 c.execute(query, row) 93 c.execute(query, row)
68 conn.commit() 94 conn.commit()
......
No preview for this file type