|
| 1 | +#!/usr/bin/env python3 |
| 2 | +import xml.etree.ElementTree as ET |
| 3 | +import urllib.request |
| 4 | +import os |
| 5 | +import hashlib |
| 6 | +from datetime import datetime |
| 7 | + |
| 8 | +# Official FIX Orchestra Repository URL (Latest EP) |
| 9 | +FIX_REPO_URL = "https://raw.githubusercontent.com/FIXTradingCommunity/orchestra/master/repository/FIX.latest.xml" |
| 10 | +OUTPUT_DIR = "QuanuX-Orchestra/include/quanux/orchestra" |
| 11 | +CONSTANTS_FILE = os.path.join(OUTPUT_DIR, "constants.hpp") |
| 12 | + |
| 13 | +def download_orchestra(): |
| 14 | + print(f"[*] Downloading FIX Orchestra from {FIX_REPO_URL}...") |
| 15 | + try: |
| 16 | + response = urllib.request.urlopen(FIX_REPO_URL) |
| 17 | + return response.read() |
| 18 | + except Exception as e: |
| 19 | + print(f"[!] Failed to download: {e}") |
| 20 | + # Try local fallback if internet is blocked |
| 21 | + if os.path.exists("QuanuX-Orchestra/scripts/fix_repository.xml"): |
| 22 | + with open("QuanuX-Orchestra/scripts/fix_repository.xml", "rb") as f: |
| 23 | + return f.read() |
| 24 | + return None |
| 25 | + |
| 26 | +def generate_checksum(data): |
| 27 | + return hashlib.sha256(data).hexdigest() |
| 28 | + |
| 29 | +def parse_and_generate(xml_data): |
| 30 | + print("[*] Parsing Orchestra XML...") |
| 31 | + root = ET.fromstring(xml_data) |
| 32 | + |
| 33 | + # Simple namespace handling if present |
| 34 | + ns = {'fixr': 'http://fixprotocol.io/2020/orchestra/repository'} |
| 35 | + |
| 36 | + fields = [] |
| 37 | + # Try finding fields with or without namespaces |
| 38 | + fields_xml = root.findall('.//fixr:fields/fixr:field', ns) |
| 39 | + if not fields_xml: |
| 40 | + fields_xml = root.findall('.//fields/field') |
| 41 | + |
| 42 | + if not fields_xml: |
| 43 | + print("[!] Could not parse fields. Check XML structure.") |
| 44 | + return None |
| 45 | + |
| 46 | + for field in fields_xml: |
| 47 | + tag = field.get('id') |
| 48 | + name = field.get('name') |
| 49 | + type_str = field.get('type') |
| 50 | + if tag and name: |
| 51 | + fields.append((tag, name, type_str)) |
| 52 | + |
| 53 | + # Add our compliance hook |
| 54 | + fields.append(("99999", "QuanuxUnmappedTag", "String")) |
| 55 | + |
| 56 | + print(f"[*] Extracted {len(fields)} fields.") |
| 57 | + |
| 58 | + codesets = [] |
| 59 | + codesets_xml = root.findall('.//fixr:codeSets/fixr:codeSet', ns) |
| 60 | + if not codesets_xml: |
| 61 | + codesets_xml = root.findall('.//codeSets/codeSet') |
| 62 | + |
| 63 | + for cs in codesets_xml: |
| 64 | + name = cs.get('name') |
| 65 | + cs_type = cs.get('type') |
| 66 | + codes = [] |
| 67 | + for code in cs.findall('.//fixr:code', ns) or cs.findall('.//code'): |
| 68 | + cname = code.get('name') |
| 69 | + cvalue = code.get('value') |
| 70 | + if cname and cvalue: |
| 71 | + codes.append((cname, cvalue)) |
| 72 | + if name and codes: |
| 73 | + codesets.append((name, cs_type, codes)) |
| 74 | + |
| 75 | + print(f"[*] Extracted {len(codesets)} codeSets.") |
| 76 | + return fields, codesets |
| 77 | + |
| 78 | +def write_hpp(fields, codesets, checksum): |
| 79 | + os.makedirs(OUTPUT_DIR, exist_ok=True) |
| 80 | + with open(CONSTANTS_FILE, 'w') as f: |
| 81 | + f.write("/**\n") |
| 82 | + f.write(" * QuanuX-Orchestra: The Rosetta Stone\n") |
| 83 | + f.write(" * Auto-generated FIX Orchestra Constants\n") |
| 84 | + f.write(f" * SHA-256 Checksum: {checksum}\n") |
| 85 | + f.write(f" * Generated: {datetime.utcnow().isoformat()}Z\n") |
| 86 | + f.write(" */\n\n") |
| 87 | + f.write("#pragma once\n\n") |
| 88 | + f.write("#include <cstdint>\n\n") |
| 89 | + f.write("namespace quanux {\n") |
| 90 | + f.write("namespace orchestra {\n\n") |
| 91 | + |
| 92 | + f.write("enum class FixTag : uint32_t {\n") |
| 93 | + for tag, name, _ in fields: |
| 94 | + # Handle standard names that might conflict with C++ keywords |
| 95 | + safe_name = name.replace("-", "_").replace(" ", "") |
| 96 | + f.write(f" {safe_name} = {tag},\n") |
| 97 | + f.write("};\n\n") |
| 98 | + |
| 99 | + for name, cs_type, codes in codesets: |
| 100 | + c_type = "char" if cs_type == "char" else "int" |
| 101 | + clean_name = name.replace("CodeSet", "") |
| 102 | + f.write(f"enum class {clean_name} : {c_type} {{\n") |
| 103 | + for cname, cval in codes: |
| 104 | + val_str = f"'{cval}'" if c_type == 'char' else cval |
| 105 | + f.write(f" {cname} = {val_str},\n") |
| 106 | + f.write("};\n\n") |
| 107 | + |
| 108 | + f.write("} // namespace orchestra\n") |
| 109 | + f.write("} // namespace quanux\n") |
| 110 | + print(f"[+] Successfully generated {CONSTANTS_FILE}") |
| 111 | + |
| 112 | +def main(): |
| 113 | + xml_data = download_orchestra() |
| 114 | + if not xml_data: |
| 115 | + print("[!] No XML data available. Exiting.") |
| 116 | + return |
| 117 | + |
| 118 | + checksum = generate_checksum(xml_data) |
| 119 | + print(f"[*] SHA-256 Checksum: {checksum}") |
| 120 | + |
| 121 | + fields, codesets = parse_and_generate(xml_data) |
| 122 | + if fields: |
| 123 | + write_hpp(fields, codesets, checksum) |
| 124 | + |
| 125 | +if __name__ == "__main__": |
| 126 | + main() |
0 commit comments