#!/usr/bin/env python3 # This script generates the opcode.xml file # by Andres Cabrera June 2006-2010 # Licensed under the GPL licence version 3 or later # modification for empty arg in command and links on opcodes by Francois Pinot February 2007 from xml.dom import minidom import os import glob import sys from pathlib import Path # categories holds the list of valid categories for opcodes from categories import categories import argparse parser = argparse.ArgumentParser() parser.add_argument("-o", "--outfile", default="opcodes.xml") parser.add_argument("--manual", default='', help='The root to the manual repository') parser.add_argument("--xo", action="store_true") args = parser.parse_args() outfilename = args.outfile XO = args.xo manualroot = Path(args.manual) opcodelist = [] # outfilename = 'opcodes.xml' quickref = open(outfilename, 'w') quickref.write("\n") quickref.write('''\n''') entries = [] for i in categories: entries.append([]) manualxml = manualroot/'manual.xml' if not manualxml.exists(): print(f'\nManual file "{manualxml}" not found', file=sys.stderr) print('Use the --manual option to set the path to the manual repository') sys.exit(-1) manual = open(manualxml, 'r') text = manual.read() manual.close() files = list(manualroot.glob('opcodes/*.xml')) files.extend(manualroot.glob('opcodes/*/*.xml')) files.extend(manualroot.glob('vectorial/*.xml')) files.extend(manualroot.glob('utility/*.xml')) files = list(set(files)) files.sort() files = [f for f in files if 'topXO.xml' not in str(f)] headerText = text[:text.find('a + b (any rate)\n', 'dollar.xml': '$NAME\n', 'divides.xml': 'a / b (any rate)\n', 'modulus.xml': 'a % b (any rate)\n', 'multiplies.xml': 'a * b (any rate)\n', 'opbitor.xml': 'a '+'| b (bitwise OR)\n', 'opor.xml': 'a '+'|| b (logical OR; not audio-rate)\n', 'raises.xml': 'a ^ b (b not audio-rate)\n', 'subtracts.xml': 'a - b (any rate)\n', 'ifdef.xml': '#ifndef NAME ...' + '#else ...' + '#end\n', 'define.xml': '#define NAME # replacement text #\n' + '#define NAME(a' b' c') # replacement text #\n', 'include.xml': '#include "filename"\n', 'undef.xml': '#undef NAME\n', '0dbfs.xml': '0dbfs = iarg\n' } fixes2 = {os.path.join('opcodes', key):entry for key, entry in fixes.items()} for i, filename in enumerate(files): print("filename", filename) entry = '' source = open(filename) entryText = source.read().replace("\xef\xbb\xbf","") newfile = headerText + '' + entryText + '' newfile = newfile.replace("\r", "") source.close() xmldoc = minidom.parseString(newfile) xmldocId = xmldoc.documentElement.getAttribute('id') if filename in fixes2: entry = fixes2[filename] else: synopsis = xmldoc.getElementsByTagName('synopsis') if not synopsis: entry = '' else: # There can be more than 1 synopsis per file for synops in synopsis: tmp = synops.toxml() if XO: opcodename = tmp[tmp.find('') + 9:tmp.find('')] else: opcodename = "" tmp = tmp.replace('', '') entry += tmp.replace('', '') info = xmldoc.getElementsByTagName('refentryinfo') if info and entry: category = info[0].toxml() category = category[21:-23] else: print(f"no refentryinfo tag for file {filename}") category = "Miscellaneous" if (entry!=''): print(filename + " sent to Miscellaneous") desc = xmldoc.getElementsByTagName('refpurpose') description = "" if (len(desc)!=0 and entry != ''): description = desc[0].firstChild.toxml().strip() else: print(f"no refpurpose tag for file {filename}") match = False for j, thiscategory in enumerate(categories): if (category == thiscategory): entries[j].append([entry, description]) match = True if not match: print(str(filename) + "---- WARNING! No Category Match!") for i in range(len(categories)): if (len(entries[i])==0): print("No entries for category: "+categories[i]+"...Skipping") continue # quickref.write("\n") quickref.write("\n") count = 0 for j in range(len(entries[i])): newentry = entries[i].pop(0) # entry = entry.replace("$", "$") # entry = entry.replace(" ", " ") quickref.write("" + description) quickref.write(newentry[1] + "") quickref.write(newentry[0] + "\n") count += 1 quickref.write("\n") print(str(count) + " entries in category: " + categories[i]) quickref.write('\n') quickref.close() print(entries)