Update convert.py
Showing
1 changed file
with
39 additions
and
13 deletions
... | @@ -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", "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"], |
17 | "calculated": {"jobs": "divide_hex_by_2"}}, | ||
17 | "items-armor2.xml": | 18 | "items-armor2.xml": |
18 | ["id", "flags", "name", "description", "level", "iLevel", "slots", "races"], | 19 | {"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"], |
20 | "calculated": {"jobs": "divide_hex_by_2"}}, | ||
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", "stack-size", "valid-targets", "name", "description", "log-name-singular", "log-name-plural", "element", "storage-slots"], |
26 | "calculated": {}}, | ||
23 | "items-general2.xml": | 27 | "items-general2.xml": |
24 | ["id", "flags", "name", "description"], | 28 | {"fields": ["id", "flags", "stack-size", "valid-targets", "name", "description", "log-name-singular", "log-name-plural", "element", "storage-slots"], |
29 | "calculated": {}}, | ||
25 | "items-puppet.xml": | 30 | "items-puppet.xml": |
26 | ["id", "flags", "name", "description", "puppet-slot", "element-charge"], | 31 | {"fields": ["id", "flags", "stack-size", "valid-targets", "name", "description", "log-name-singular", "log-name-plural", "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", "stack-size", "valid-targets", "name", "description", "log-name-singular", "log-name-plural", "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", "stack-size", "valid-targets", "name", "description", "log-name-singular", "log-name-plural"], |
38 | "calculated": {}}, | ||
31 | "items-weapons.xml": | 39 | "items-weapons.xml": |
32 | ["id", "flags", "name", "description", "level", "iLevel", "slots", "races", "damage"], | 40 | {"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"], |
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 = [] |
79 | for field_name in field_list[filename]["fields"]: | ||
80 | field_set = False | ||
62 | for field in item.children: | 81 | for field in item.children: |
63 | if field['name'] in field_list[filename]: | 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: | ||
64 | row.append(field.cdata) | 88 | row.append(field.cdata) |
65 | 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() | ... | ... |
-
Please register or sign in to post a comment