Files
dusk/tools/csvtoenum.py
Dominic Masters f71c271c97
Some checks failed
Build Dusk / run-tests (push) Failing after 1m31s
Build Dusk / build-linux (push) Failing after 1m8s
Build Dusk / build-psp (push) Failing after 1m32s
item
2026-01-25 15:01:25 -06:00

50 lines
1.5 KiB
Python

#!/usr/bin/env python3
"""
csvtoenum.py: Generate a C enum header from a CSV file.
Usage:
python csvtoenum.py <csv_file> <out_header> <c_type> <take_column> <prefix_column>
"""
import sys
import csv
def main():
if len(sys.argv) != 6:
print("Usage: csvtoenum.py <csv_file> <out_header> <c_type> <take_column> <prefix_column>", file=sys.stderr)
sys.exit(1)
csv_file, out_header, c_type, take_column, prefix_column = sys.argv[1:6]
with open(csv_file, newline='') as f:
reader = csv.DictReader(f)
if take_column not in reader.fieldnames:
print(f"TAKE_COLUMN '{take_column}' not found in CSV header", file=sys.stderr)
sys.exit(2)
if prefix_column not in reader.fieldnames:
print(f"PREFIX_COLUMN '{prefix_column}' not found in CSV header", file=sys.stderr)
sys.exit(2)
entries = []
for row in reader:
entry = row[take_column].strip()
prefix = row[prefix_column].strip()
if entry:
entries.append(f" {prefix}{entry},")
# Compose enum
enum_entries = [f" {prefix_column}NULL,"]
enum_entries.extend(entries)
enum_entries.append(f" {prefix_column}COUNT,")
enum_body = "\n".join(enum_entries)
header = f"""/**
* Auto-generated by csvtoenum.py
* Source: {csv_file}
*/
typedef enum {{
{enum_body}
}} {c_type};
"""
with open(out_header, 'w') as f:
f.write(header)
if __name__ == "__main__":
main()