-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexport_to_lua.py
More file actions
837 lines (746 loc) · 28.8 KB
/
export_to_lua.py
File metadata and controls
837 lines (746 loc) · 28.8 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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
#!/usr/bin/env python3
"""Export aonprd-parse DuckDB database to Factorio-style data:extend() Lua files.
Usage:
cd ~/aonprd-parse && uv run python export_to_lua.py --output ~/pathfinder-api/rules/data/aonprd/
"""
import argparse
import json
import os
import re
import sys
from collections import defaultdict
from urllib.parse import parse_qs, urlparse
import inflect
# ---------------------------------------------------------------------------
# Configuration
# ---------------------------------------------------------------------------
SCHEMA = "marts"
SKIP_TABLES = {
"build_metadata", "indices",
# Backward-compat views — creatures is the primary table
"monsters", "npcs", "mythic_monsters",
# Backward-compat junction views — creature_* are primary
"monster_feats", "monster_attacks", "monster_immunities",
"monster_resistances", "monster_senses", "monster_weaknesses",
"monster_special_abilities", "monster_spell_like_abilities",
}
STRIP_COLUMNS = {"source_file", "parse_coverage", "structure_hash", "creature_type"}
# Junction table → (parent_table, fk_column, embed_field, embed_style)
# embed_style: "dict_kv", "list_strings", "list_dicts", "single_string"
JUNCTION_CONFIG = {
# --- Unified creature junctions (all creature types) ---
"creature_feats": {
"parent": "creatures",
"fk": "creature_id",
"field": "feat_list",
"style": "list_strings",
"val_col": "feat_name",
},
"creature_attacks": {
"parent": "creatures",
"fk": "creature_id",
"field": "attack_list",
"style": "list_dicts",
"cols": ["attack_name", "attack_type", "count", "attack_bonus", "damage"],
},
"creature_immunities": {
"parent": "creatures",
"fk": "creature_id",
"field": "immunity_list",
"style": "list_strings",
"val_col": "immunity",
},
"creature_resistances": {
"parent": "creatures",
"fk": "creature_id",
"field": "resistance_list",
"style": "list_dicts",
"cols": ["resistance", "value"],
},
"creature_senses": {
"parent": "creatures",
"fk": "creature_id",
"field": "sense_list",
"style": "list_dicts",
"cols": ["sense", "range_ft"],
},
"creature_special_abilities": {
"parent": "creatures",
"fk": "creature_id",
"field": "special_ability_list",
"style": "list_dicts",
"cols": ["ability_name", "ability_type", "description"],
},
"creature_spell_like_abilities": {
"parent": "creatures",
"fk": "creature_id",
"field": "sla_list",
"style": "list_dicts",
"cols": ["spell_name", "frequency", "caster_level", "dc"],
},
"creature_weaknesses": {
"parent": "creatures",
"fk": "creature_id",
"field": "weakness_list",
"style": "list_strings",
"val_col": "weakness",
},
# --- Non-creature junctions ---
"spell_class_levels": {
"parent": "spells",
"fk": "spell_id",
"field": "class_levels",
"style": "dict_kv",
"key_col": "class_name",
"val_col": "level",
},
"deity_domains": {
"parent": "deities",
"fk": "deity_id",
"field": "domain_list",
"style": "list_dicts",
"cols": ["domain_name", "is_subdomain", "parent_domain"],
},
"feat_prerequisites_parsed": {
"parent": "feats",
"fk": "feat_id",
"field": "prereqs_parsed",
"style": "list_dicts",
"cols": ["prereq_type", "prereq_value", "prereq_detail"],
},
"item_construction_requirements": {
"parent": "items",
"fk": "item_id",
"field": "construction_reqs",
"style": "list_dicts",
"cols": ["req_type", "req_value"],
},
"class_class_skills": {
"parent": "classes",
"fk": "class_id",
"field": "skill_list",
"style": "list_strings",
"val_col": "skill_name",
},
"bloodline_bonus_spells": {
"parent": "bloodlines",
"fk": "bloodline_id",
"field": "bonus_spell_list",
"style": "list_dicts",
"cols": ["spell_name", "class_level"],
},
"prestige_class_skill_requirements": {
"parent": "prestige_classes",
"fk": "class_id",
"field": "skill_reqs",
"style": "list_dicts",
"cols": ["skill_name", "ranks"],
},
"combat_stamina": {
"parent": "feats",
"fk": "related_feat", # matches by name, not by id
"fk_target": "name", # join on feats.name = combat_stamina.related_feat
"field": "stamina_trick",
"style": "single_string",
"val_col": "description",
},
# --- Phase 2 creature junctions ---
"creature_skills": {
"parent": "creatures",
"fk": "creature_id",
"field": "skill_list",
"style": "list_dicts",
"cols": ["skill_name", "bonus"],
},
"creature_speeds": {
"parent": "creatures",
"fk": "creature_id",
"field": "speed_list",
"style": "list_dicts",
"cols": ["movement_type", "speed_ft", "maneuverability"],
},
"creature_languages": {
"parent": "creatures",
"fk": "creature_id",
"field": "language_list",
"style": "list_dicts",
"cols": ["language", "modifier"],
},
# --- Phase 2 class junctions ---
"class_progression": {
"parent": "classes",
"fk": "class_id",
"field": "progression",
"style": "list_dicts",
"cols": ["level", "base_attack_bonus", "fort_save", "ref_save", "will_save", "special"],
},
"class_spells_per_day": {
"parent": "classes",
"fk": "class_id",
"field": "spells_per_day",
"style": "list_dicts",
"cols": ["class_level", "spell_level", "slots"],
},
"class_spells_known": {
"parent": "classes",
"fk": "class_id",
"field": "spells_known",
"style": "list_dicts",
"cols": ["class_level", "spell_level", "spells_known"],
},
"bloodline_bonus_feats": {
"parent": "bloodlines",
"fk": "bloodline_id",
"field": "bonus_feat_list",
"style": "list_strings",
"val_col": "feat_name",
},
"bloodrager_bonus_feats": {
"parent": "bloodrager_bloodlines",
"fk": "bloodline_id",
"field": "bonus_feat_list",
"style": "list_strings",
"val_col": "feat_name",
},
"mystery_bonus_spells": {
"parent": "mysteries",
"fk": "mystery_id",
"field": "bonus_spell_list",
"style": "list_dicts",
"cols": ["spell_name", "class_level"],
},
"mystery_class_skills": {
"parent": "mysteries",
"fk": "mystery_id",
"field": "class_skill_list",
"style": "list_strings",
"val_col": "skill_name",
},
"psychic_bonus_spells": {
"parent": "psychic_disciplines",
"fk": "discipline_id",
"field": "bonus_spell_list",
"style": "list_dicts",
"cols": ["spell_name", "class_level"],
},
"spirit_magic_spells": {
"parent": "shaman_spirits",
"fk": "spirit_id",
"field": "spirit_spell_list",
"style": "list_dicts",
"cols": ["spell_name", "spell_level"],
},
"monster_type_class_skills": {
"parent": "monster_types",
"fk": "type_id",
"field": "class_skill_list",
"style": "list_strings",
"val_col": "skill_name",
},
"spellbook_spells": {
"parent": "spellbooks",
"fk": "spellbook_id",
"field": "spell_list",
"style": "list_dicts",
"cols": ["spell_level", "spell_name"],
},
"kineticist_element_talents": {
"parent": "kineticist_elements",
"fk": "element_id",
"field": "talent_list",
"style": "list_dicts",
"cols": ["level", "category", "talent_name"],
},
# --- Embedding fix: animal_companion_attacks ---
"animal_companion_attacks": {
"parent": "animal_companions",
"fk": "companion_id",
"field": "attack_list",
"style": "list_dicts",
"cols": ["phase", "name", "count", "dice", "crit", "is_primary", "raw"],
},
# --- Phase 3 decompositions ---
"spell_descriptors": {
"parent": "spells",
"fk": "spell_id",
"field": "descriptor_list",
"style": "list_strings",
"val_col": "descriptor",
},
"item_aura_schools": {
"parent": "items",
"fk": "item_id",
"field": "aura_schools",
"style": "list_dicts",
"cols": ["aura_strength", "aura_school"],
},
"spell_data_tables": {
"parent": "spells",
"fk": "spell_id",
"field": "data_tables",
"style": "list_dicts",
"cols": ["table_index", "rows", "row_count", "column_count"],
},
}
CLASS_OPTIONS_TABLES = {
"alchemist_discoveries", "arcanist_exploits", "barbarian_rage_powers",
"barbarian_uc_rage_powers", "bard_masterpieces", "cavalier_orders",
"gunslinger_deeds", "hunter_animal_focus", "inquisitions",
"investigator_talents", "kineticist_talents", "kineticists", "magus_arcana",
"medium_spirits", "mesmerist_stares", "mesmerist_tricks", "monk_ki_powers",
"monk_style_strikes", "monk_vows", "ninja_tricks", "occultist_implements",
"oracle_curses", "paladin_divine_bonds", "paladin_mercies", "phantom_foci",
"phrenic_amplifications", "psychic_disciplines", "ranger_combat_styles",
"ranger_traps", "rogue_talents", "rogue_unchained_talents", "shaman_hexes",
"shaman_spirits", "shifter_aspects", "skald_sagas", "slayer_talents",
"summoner_evolutions", "summoner_uc_evolutions", "swashbuckler_deeds",
"vigilante_talents", "warpriest_blessings", "witch_hexes", "witch_patrons",
"wizard_arcane_discoveries", "wildblooded", "adv_versatile_performances",
"advanced_armor_training", "advanced_weapon_training", "annointings",
"bloodline_mutations", "cavalier_banners", "gunslinger_dares", "oaths",
"ward_aspects", "domain_granted_powers", "school_granted_powers",
"cleric_variant_channeling", "skill_unlocks", "eidolon_base_forms",
"eidolon_uc_base_forms", "eidolon_uc_subtypes", "elemental_augmentations",
"kineticist_elements", "unique_patrons", "hellknight_disciplines",
"hellknight_orders", "shadow_piercings",
}
# Tables whose "type" column conflicts with our Lua "type" field — rename to "subtype"
TABLES_WITH_TYPE_COLUMN = {
"construct_mods", "curses", "diseases", "drugs", "items",
"kineticists", "madnesses", "poisons", "traps",
}
# ---------------------------------------------------------------------------
# ID Slugification
# ---------------------------------------------------------------------------
def slugify_id(raw_id: str) -> str:
"""Convert a URL-style ID to a Lua-friendly slug.
Examples:
SpellDisplay.aspx?ItemName=Magic Missile → magic_missile
FeatDisplay.aspx?ItemName=Power Attack → power_attack
MagicWondrousDisplay.aspx?FinalName=Gloves of the Keen Evaluator
→ gloves_of_the_keen_evaluator
"""
if not raw_id:
return ""
# If it looks like a URL with query params, extract the name value
if "?" in raw_id:
qs = parse_qs(urlparse("http://x/" + raw_id).query)
# Try common parameter names
for param in ("ItemName", "FinalName"):
if param in qs:
raw_id = qs[param][0]
break
else:
# Fallback: use the first query value
for vals in qs.values():
raw_id = vals[0]
break
slug = raw_id.lower()
slug = re.sub(r"[\s\-',]+", "_", slug)
slug = re.sub(r"[^a-z0-9_]", "", slug)
slug = re.sub(r"_+", "_", slug)
slug = slug.strip("_")
return slug or "unnamed"
class SlugTracker:
"""Track slugs per type to handle duplicates."""
def __init__(self):
self._seen: dict[str, dict[str, int]] = defaultdict(dict)
def get_unique(self, lua_type: str, raw_id: str) -> str:
slug = slugify_id(raw_id)
counts = self._seen[lua_type]
if slug in counts:
counts[slug] += 1
return f"{slug}_{counts[slug]}"
counts[slug] = 1
return slug
# ---------------------------------------------------------------------------
# Table Name → Lua Type Singularization
# ---------------------------------------------------------------------------
_inflect_engine = inflect.engine()
# Overrides where inflect gets it wrong for compound Pathfinder names
_SINGULAR_OVERRIDES = {
"hunter_animal_focus": "hunter_animal_focus", # already singular
"phantom_foci": "phantom_focus", # Latin plural inflect may not handle in compounds
}
def singularize(table_name: str) -> str:
"""Convert a plural table name to a singular Lua type name using inflect."""
if table_name in _SINGULAR_OVERRIDES:
return _SINGULAR_OVERRIDES[table_name]
# For compound_names, singularize only the last word
parts = table_name.rsplit("_", 1)
if len(parts) == 2:
prefix, last = parts
singular = _inflect_engine.singular_noun(last)
if singular:
return f"{prefix}_{singular}"
return table_name # already singular
else:
singular = _inflect_engine.singular_noun(table_name)
return singular if singular else table_name
# ---------------------------------------------------------------------------
# Lua Serialization
# ---------------------------------------------------------------------------
def _is_valid_lua_ident(key: str) -> bool:
"""Check if a string is a valid Lua identifier (no reserved word check needed)."""
return bool(re.match(r"^[a-zA-Z_][a-zA-Z0-9_]*$", key))
def lua_string(s: str) -> str:
"""Serialize a Python string to a Lua string literal."""
if s is None:
return "nil"
s = str(s)
# Use long strings for anything with quotes, backslashes, or newlines
if '"' in s or "\\" in s or "\n" in s:
if "]]" not in s:
return f"[[{s}]]"
elif "]=]" not in s:
return f"[=[{s}]=]"
else:
return f"[==[{s}]==]"
# Escape backslashes and quotes for short strings
escaped = s.replace("\\", "\\\\").replace('"', '\\"')
return f'"{escaped}"'
def to_lua(value, indent: int = 2, depth: int = 0) -> str:
"""Recursively serialize a Python value to Lua syntax."""
if value is None:
return "nil"
if isinstance(value, bool):
return "true" if value else "false"
if isinstance(value, int):
return str(value)
if isinstance(value, float):
if value == int(value):
return str(int(value))
return str(value)
if isinstance(value, str):
return lua_string(value)
if isinstance(value, list):
if not value:
return "{}"
# Check if all items are simple scalars for single-line output
if all(isinstance(v, (str, int, float, bool)) for v in value) and len(value) <= 8:
items = ", ".join(to_lua(v, indent, depth + 1) for v in value)
if len(items) < 120:
return "{ " + items + " }"
# Multi-line list
pad = " " * (indent * (depth + 1))
lines = []
for v in value:
lines.append(f"{pad}{to_lua(v, indent, depth + 1)}")
inner = ",\n".join(lines)
close_pad = " " * (indent * depth)
return "{\n" + inner + "\n" + close_pad + "}"
if isinstance(value, dict):
if not value:
return "{}"
pad = " " * (indent * (depth + 1))
lines = []
for k, v in value.items():
if v is None:
continue
lua_val = to_lua(v, indent, depth + 1)
if isinstance(k, str) and _is_valid_lua_ident(k):
lines.append(f"{pad}{k} = {lua_val}")
else:
lua_key = to_lua(k, indent, depth + 1)
lines.append(f"{pad}[{lua_key}] = {lua_val}")
if not lines:
return "{}"
inner = ",\n".join(lines)
close_pad = " " * (indent * depth)
return "{\n" + inner + "\n" + close_pad + "}"
return lua_string(str(value))
def format_entry(entry: dict, indent: int = 2) -> str:
"""Format a single data:extend entry as a Lua table literal."""
pad = " " * indent
lines = []
# type and id first
lines.append(f'{pad}type = {to_lua(entry["type"], indent, 1)}')
lines.append(f'{pad}id = {to_lua(entry["id"], indent, 1)}')
# Then remaining fields in order
for k, v in entry.items():
if k in ("type", "id") or v is None:
continue
lua_val = to_lua(v, indent, 1)
if _is_valid_lua_ident(k):
lines.append(f"{pad}{k} = {lua_val}")
else:
lines.append(f'{pad}["{k}"] = {lua_val}')
return " { " + ",\n ".join(line.strip() for line in lines) + " }"
# ---------------------------------------------------------------------------
# Junction Data Loading
# ---------------------------------------------------------------------------
def load_junction_data(con, table_name: str, config: dict) -> dict:
"""Load junction table data, grouped by FK.
Returns: {fk_value: embedded_data}
"""
fk_col = config["fk"]
rows = con.execute(f'SELECT * FROM {SCHEMA}."{table_name}"').fetchall()
col_names = [desc[0] for desc in con.execute(f'SELECT * FROM {SCHEMA}."{table_name}" LIMIT 0').description]
fk_idx = col_names.index(fk_col)
grouped: dict = defaultdict(list)
for row in rows:
fk_val = row[fk_idx]
row_dict = dict(zip(col_names, row))
grouped[fk_val].append(row_dict)
result = {}
style = config["style"]
for fk_val, row_dicts in grouped.items():
if style == "dict_kv":
d = {}
for rd in row_dicts:
key = rd[config["key_col"]]
val = rd[config["val_col"]]
if key is not None:
d[key] = val
result[fk_val] = d
elif style == "list_strings":
vals = [rd[config["val_col"]] for rd in row_dicts if rd[config["val_col"]] is not None]
result[fk_val] = vals
elif style == "list_dicts":
items = []
for rd in row_dicts:
item = {}
for c in config["cols"]:
if rd.get(c) is not None:
item[c] = rd[c]
if item:
items.append(item)
result[fk_val] = items
elif style == "single_string":
# Take the first non-None value
for rd in row_dicts:
val = rd.get(config["val_col"])
if val is not None:
result[fk_val] = val
break
return result
# ---------------------------------------------------------------------------
# JSON Column Handling
# ---------------------------------------------------------------------------
def try_parse_json(value):
"""Try to parse a JSON string; return original value if not JSON."""
if value is None:
return None
if isinstance(value, str):
try:
return json.loads(value)
except (json.JSONDecodeError, ValueError):
return value
# DuckDB may return already-parsed dicts/lists for JSON columns
if isinstance(value, (dict, list)):
return value
return value
# ---------------------------------------------------------------------------
# Main Export Logic
# ---------------------------------------------------------------------------
def get_entity_tables(con) -> list[str]:
"""Get list of entity tables to export from marts schema."""
all_tables = [
r[0] for r in con.execute(
f"SELECT table_name FROM information_schema.tables "
f"WHERE table_schema = '{SCHEMA}' ORDER BY table_name"
).fetchall()
]
junction_names = set(JUNCTION_CONFIG.keys())
return [t for t in all_tables if t not in SKIP_TABLES and t not in junction_names]
def get_json_columns(con, table_name: str) -> set[str]:
"""Find columns with JSON data type in a table."""
cols = con.execute(
f"SELECT column_name FROM information_schema.columns "
f"WHERE table_schema = '{SCHEMA}' AND table_name = '{table_name}' AND data_type = 'JSON'"
).fetchall()
return {c[0] for c in cols}
def process_row(
row_dict: dict,
lua_type: str,
slug_tracker: SlugTracker,
json_cols: set[str],
junction_data: dict[str, dict],
table_name: str,
) -> dict | None:
"""Process a single database row into a Lua entry dict."""
raw_id = row_dict.get("id", "")
slug = slug_tracker.get_unique(lua_type, raw_id)
entry = {"type": lua_type, "id": slug}
for col, val in row_dict.items():
# Skip metadata columns and the raw 'id' (replaced by slug)
if col in STRIP_COLUMNS or col == "id":
continue
# Rename 'type' column to avoid clash with Lua type field
if col == "type" and table_name in TABLES_WITH_TYPE_COLUMN:
col = "subtype"
if val is None:
continue
# Parse JSON columns
if col in json_cols:
val = try_parse_json(val)
if val is None:
continue
entry[col] = val
# Embed junction data
for junc_table, junc_config in JUNCTION_CONFIG.items():
if junc_config["parent"] != table_name:
continue
field = junc_config["field"]
junc = junction_data.get(junc_table, {})
# Determine the lookup key
fk_target = junc_config.get("fk_target")
if fk_target == "name":
lookup_key = row_dict.get("name")
else:
lookup_key = raw_id
if lookup_key and lookup_key in junc:
embedded = junc[lookup_key]
if embedded: # Don't add empty lists/dicts
entry[field] = embedded
return entry
def write_lua_file(filepath: str, entries: list[dict]):
"""Write a list of entries as a data:extend() Lua file."""
os.makedirs(os.path.dirname(filepath), exist_ok=True)
with open(filepath, "w", encoding="utf-8") as f:
f.write("-- Auto-generated from aonprd-parse DuckDB. Do not edit manually.\n")
f.write("-- To regenerate: cd ~/aonprd-parse && uv run python export_to_lua.py\n\n")
f.write("data:extend({\n")
for i, entry in enumerate(entries):
f.write(format_entry(entry))
if i < len(entries) - 1:
f.write(",\n")
else:
f.write("\n")
f.write("})\n")
def export_database(db_path: str, output_dir: str):
"""Main export: read DuckDB, generate Lua files."""
import duckdb
con = duckdb.connect(db_path, read_only=True)
slug_tracker = SlugTracker()
total_entries = 0
total_files = 0
# --- Load all junction data up front ---
eprint("Loading junction tables...")
junction_data: dict[str, dict] = {}
for junc_table, config in JUNCTION_CONFIG.items():
try:
junction_data[junc_table] = load_junction_data(con, junc_table, config)
eprint(f" {junc_table}: {len(junction_data[junc_table])} parent groups")
except Exception as e:
eprint(f" WARNING: Failed to load {junc_table}: {e}")
junction_data[junc_table] = {}
# --- Process entity tables ---
entity_tables = get_entity_tables(con)
eprint(f"\nExporting {len(entity_tables)} entity tables...")
for table_name in entity_tables:
json_cols = get_json_columns(con, table_name)
# Read all rows
rows = con.execute(f'SELECT * FROM {SCHEMA}."{table_name}"').fetchall()
col_names = [desc[0] for desc in con.execute(f'SELECT * FROM {SCHEMA}."{table_name}" LIMIT 0').description]
if not rows:
eprint(f" {table_name}: 0 rows (skipped)")
continue
# --- Creatures: split by creature_type, each type gets its own lua_type ---
if table_name == "creatures":
by_type: dict[str, list[dict]] = defaultdict(list)
for row in rows:
row_dict = dict(zip(col_names, row))
ct = row_dict.get("creature_type", "monster")
lua_type = singularize(ct + "s") if ct != "npc" else "npc"
entry = process_row(row_dict, lua_type, slug_tracker, json_cols, junction_data, table_name)
if entry:
by_type[ct].append(entry)
# Monsters → split alphabetically
if "monster" in by_type:
by_letter: dict[str, list[dict]] = defaultdict(list)
for e in by_type["monster"]:
name = e.get("name", "")
letter = name[0].lower() if name else "other"
if not letter.isalpha():
letter = "other"
by_letter[letter].append(e)
for letter, letter_entries in sorted(by_letter.items()):
filepath = os.path.join(output_dir, "monsters", f"{letter}.lua")
write_lua_file(filepath, letter_entries)
total_files += 1
total_entries += len(letter_entries)
eprint(f" creatures/monster: {len(by_type['monster'])} entries → {len(by_letter)} letter files")
# NPCs → single file
if "npc" in by_type:
filepath = os.path.join(output_dir, "npcs.lua")
write_lua_file(filepath, by_type["npc"])
total_files += 1
total_entries += len(by_type["npc"])
eprint(f" creatures/npc: {len(by_type['npc'])} entries")
# Mythic monsters → single file
if "mythic_monster" in by_type:
filepath = os.path.join(output_dir, "mythic_monsters.lua")
write_lua_file(filepath, by_type["mythic_monster"])
total_files += 1
total_entries += len(by_type["mythic_monster"])
eprint(f" creatures/mythic_monster: {len(by_type['mythic_monster'])} entries")
continue
lua_type = singularize(table_name)
entries = []
for row in rows:
row_dict = dict(zip(col_names, row))
entry = process_row(row_dict, lua_type, slug_tracker, json_cols, junction_data, table_name)
if entry:
entries.append(entry)
if not entries:
continue
# --- File routing ---
if table_name == "spells":
# Split by school_parsed
by_school: dict[str, list[dict]] = defaultdict(list)
for e in entries:
school = e.get("school_parsed", "unknown")
if not school:
school = "unknown"
by_school[school].append(e)
for school, school_entries in sorted(by_school.items()):
filepath = os.path.join(output_dir, "spells", f"{school}.lua")
write_lua_file(filepath, school_entries)
total_files += 1
total_entries += len(school_entries)
eprint(f" spells: {len(entries)} entries → {len(by_school)} school files")
elif table_name in CLASS_OPTIONS_TABLES:
filepath = os.path.join(output_dir, "class_options", f"{table_name}.lua")
write_lua_file(filepath, entries)
total_files += 1
total_entries += len(entries)
eprint(f" {table_name}: {len(entries)} entries → class_options/")
else:
filepath = os.path.join(output_dir, f"{table_name}.lua")
write_lua_file(filepath, entries)
total_files += 1
total_entries += len(entries)
eprint(f" {table_name}: {len(entries)} entries")
con.close()
eprint(f"\nDone! {total_files} files, {total_entries} entries total.")
return total_files, total_entries
def eprint(*args, **kwargs):
"""Print to stderr."""
print(*args, file=sys.stderr, **kwargs)
# ---------------------------------------------------------------------------
# CLI
# ---------------------------------------------------------------------------
def main():
parser = argparse.ArgumentParser(
description="Export aonprd-parse DuckDB to Lua data:extend() files"
)
parser.add_argument(
"--output", "-o",
default="output/lua",
help="Output directory for Lua files (default: output/lua)",
)
parser.add_argument(
"--db",
default="pathfinder1e.duckdb",
help="Path to DuckDB database (default: pathfinder1e.duckdb)",
)
args = parser.parse_args()
if not os.path.exists(args.db):
eprint(f"Error: Database not found: {args.db}")
sys.exit(1)
# Clean output dir
os.makedirs(args.output, exist_ok=True)
export_database(args.db, args.output)
if __name__ == "__main__":
main()