| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
from xml.dom import minidom |
| 9 |
import os |
| 10 |
import glob |
| 11 |
import sys |
| 12 |
from pathlib import Path |
| 13 |
|
| 14 |
from categories import categories |
| 15 |
|
| 16 |
import argparse |
| 17 |
|
| 18 |
parser = argparse.ArgumentParser() |
| 19 |
parser.add_argument("-o", "--outfile", default="opcodes.xml") |
| 20 |
parser.add_argument("--manual", default='', help='The root to the manual repository') |
| 21 |
parser.add_argument("--xo", action="store_true") |
| 22 |
|
| 23 |
args = parser.parse_args() |
| 24 |
outfilename = args.outfile |
| 25 |
XO = args.xo |
| 26 |
|
| 27 |
manualroot = Path(args.manual) |
| 28 |
|
| 29 |
opcodelist = [] |
| 30 |
|
| 31 |
|
| 32 |
quickref = open(outfilename, 'w') |
| 33 |
quickref.write("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<!-- Don't modify this file. It is generated automatically by opcodeparser.py\nThis file is distributed under the GNU Free Documentation Licence-->") |
| 34 |
quickref.write('''<opcodes>\n''') |
| 35 |
|
| 36 |
entries = [] |
| 37 |
for i in categories: |
| 38 |
entries.append([]) |
| 39 |
|
| 40 |
manualxml = manualroot/'manual.xml' |
| 41 |
|
| 42 |
if not manualxml.exists(): |
| 43 |
print(f'\nManual file "{manualxml}" not found', file=sys.stderr) |
| 44 |
print('Use the --manual option to set the path to the manual repository') |
| 45 |
sys.exit(-1) |
| 46 |
|
| 47 |
manual = open(manualxml, 'r') |
| 48 |
text = manual.read() |
| 49 |
manual.close() |
| 50 |
|
| 51 |
files = list(manualroot.glob('opcodes/*.xml')) |
| 52 |
files.extend(manualroot.glob('opcodes/*/*.xml')) |
| 53 |
files.extend(manualroot.glob('vectorial/*.xml')) |
| 54 |
files.extend(manualroot.glob('utility/*.xml')) |
| 55 |
|
| 56 |
files = list(set(files)) |
| 57 |
files.sort() |
| 58 |
|
| 59 |
files = [f for f in files if 'topXO.xml' not in str(f)] |
| 60 |
|
| 61 |
headerText = text[:text.find('<book id="index"')] |
| 62 |
|
| 63 |
|
| 64 |
fixes = { |
| 65 |
'adds.xml': '<synopsis>a <opcodename>+</opcodename> b (any rate)</synopsis>\n', |
| 66 |
'dollar.xml': '<synopsis><opcodename>$NAME</opcodename></synopsis>\n<para/>', |
| 67 |
'divides.xml': '<synopsis>a <opcodename>/</opcodename> b (any rate)</synopsis>\n', |
| 68 |
'modulus.xml': '<synopsis>a <opcodename>%</opcodename> b (any rate)</synopsis>\n', |
| 69 |
'multiplies.xml': '<synopsis>a <opcodename>*</opcodename> b (any rate)</synopsis>\n', |
| 70 |
'opbitor.xml': '<synopsis>a <opcodename>'+'|</opcodename> b (bitwise OR)</synopsis>\n', |
| 71 |
'opor.xml': '<synopsis>a <opcodename>'+'||</opcodename> b (logical OR; not audio-rate)</synopsis>\n', |
| 72 |
'raises.xml': '<synopsis>a <opcodename>^</opcodename> b (b not audio-rate)</synopsis>\n', |
| 73 |
'subtracts.xml': '<synopsis>a <opcodename>-</opcodename> b (any rate)</synopsis>\n', |
| 74 |
'ifdef.xml': |
| 75 |
'<synopsis><opcodename>#ifndef</opcodename> NAME</synopsis><synopsis> ...</synopsis>' + |
| 76 |
'<synopsis><opcodename>#else</opcodename></synopsis><synopsis> ...</synopsis>' + |
| 77 |
'<synopsis><opcodename>#end</opcodename></synopsis>\n', |
| 78 |
'define.xml': |
| 79 |
'<synopsis><opcodename>#define</opcodename> NAME # replacement text #</synopsis>\n' + |
| 80 |
'<synopsis><opcodename>#define</opcodename> NAME(a' b' c') # replacement text #</synopsis>\n', |
| 81 |
'include.xml': '<synopsis><opcodename>#include</opcodename> "filename"</synopsis>\n', |
| 82 |
'undef.xml': '<synopsis><opcodename>#undef</opcodename> NAME</synopsis>\n', |
| 83 |
'0dbfs.xml': '<synopsis><opcodename>0dbfs</opcodename> = iarg</synopsis>\n' |
| 84 |
} |
| 85 |
|
| 86 |
fixes2 = {os.path.join('opcodes', key):entry for key, entry in fixes.items()} |
| 87 |
|
| 88 |
for i, filename in enumerate(files): |
| 89 |
print("filename", filename) |
| 90 |
entry = '' |
| 91 |
source = open(filename) |
| 92 |
entryText = source.read().replace("\xef\xbb\xbf","") |
| 93 |
newfile = headerText + '<book id="index" lang="en">' + entryText + '</book>' |
| 94 |
newfile = newfile.replace("\r", "") |
| 95 |
source.close() |
| 96 |
xmldoc = minidom.parseString(newfile) |
| 97 |
xmldocId = xmldoc.documentElement.getAttribute('id') |
| 98 |
|
| 99 |
if filename in fixes2: |
| 100 |
entry = fixes2[filename] |
| 101 |
else: |
| 102 |
synopsis = xmldoc.getElementsByTagName('synopsis') |
| 103 |
if not synopsis: |
| 104 |
entry = '' |
| 105 |
else: |
| 106 |
|
| 107 |
for synops in synopsis: |
| 108 |
tmp = synops.toxml() |
| 109 |
if XO: |
| 110 |
opcodename = tmp[tmp.find('<command>') + 9:tmp.find('</command>')] |
| 111 |
else: |
| 112 |
opcodename = "" |
| 113 |
tmp = tmp.replace('<command>', '<opcodename>') |
| 114 |
entry += tmp.replace('</command>', '</opcodename>') |
| 115 |
|
| 116 |
info = xmldoc.getElementsByTagName('refentryinfo') |
| 117 |
if info and entry: |
| 118 |
category = info[0].toxml() |
| 119 |
category = category[21:-23] |
| 120 |
else: |
| 121 |
print(f"no refentryinfo tag for file {filename}") |
| 122 |
category = "Miscellaneous" |
| 123 |
if (entry!=''): |
| 124 |
print(filename + " sent to Miscellaneous") |
| 125 |
desc = xmldoc.getElementsByTagName('refpurpose') |
| 126 |
description = "" |
| 127 |
if (len(desc)!=0 and entry != ''): |
| 128 |
description = desc[0].firstChild.toxml().strip() |
| 129 |
else: |
| 130 |
print(f"no refpurpose tag for file {filename}") |
| 131 |
match = False |
| 132 |
for j, thiscategory in enumerate(categories): |
| 133 |
if (category == thiscategory): |
| 134 |
entries[j].append([entry, description]) |
| 135 |
match = True |
| 136 |
if not match: |
| 137 |
print(str(filename) + "---- WARNING! No Category Match!") |
| 138 |
|
| 139 |
for i in range(len(categories)): |
| 140 |
if (len(entries[i])==0): |
| 141 |
print("No entries for category: "+categories[i]+"...Skipping") |
| 142 |
continue |
| 143 |
|
| 144 |
quickref.write("<category name=\"" + categories[i] + "\">\n") |
| 145 |
count = 0 |
| 146 |
for j in range(len(entries[i])): |
| 147 |
newentry = entries[i].pop(0) |
| 148 |
|
| 149 |
|
| 150 |
quickref.write("<opcode><desc>" + description) |
| 151 |
quickref.write(newentry[1] + "</desc>") |
| 152 |
quickref.write(newentry[0] + "</opcode>\n") |
| 153 |
count += 1 |
| 154 |
quickref.write("</category>\n") |
| 155 |
print(str(count) + " entries in category: " + categories[i]) |
| 156 |
|
| 157 |
quickref.write('</opcodes>\n') |
| 158 |
quickref.close() |
| 159 |
print(entries) |