# -*- coding: utf-8 -*-
# This script generates the misc/quickref.html file
# by Andres Cabrera June 2006
# 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, glob, sys
# categories holds the list of valid categories for opcodes
from categories import categories
XO = False
opcodelist = []
if sys.argv.count("-x"):
XO = True
file = open("opcode_listXO.txt", 'r')
removedopcodes = file.read().split()
file.close()
entries = []
example_cats = []
for i in categories:
entries.append([])
example_cats.append([])
# Generate a file listing all examples
outfilename = 'misc/examples.xml'
examples = open(outfilename,'w')
examples.write("\n\n")
examples.write('''
List of examples''')
files = glob.glob('opcodes/*.xml')
try:
files.remove('opcodes/template.xml')
except:
pass
files.sort()
for i,filename in enumerate(files):
entry = ''
#print filename
source = open(filename, 'r')
text = source.read()
source.close()
cat_text = text[text.find("") + 21: text.find("")]
if categories.count(cat_text) > 0:
cat = categories.index(cat_text);
#print cat_text, cat
while text.find("examples-xml/") != -1:
file_to_add = text[text.find("examples-xml/"):text.find(".csd",text.find("examples-xml/") )+ 4]
if len(file_to_add) < 45: #yes, very flaky, I know...
entries[cat].append(file_to_add)
else:
print file_to_add
text = text[text.find("examples-xml/") + 10:]
for i,catname in enumerate(categories):
print catname
if len(entries[i]) > 0:
title =""
title += catname
#title += "\n"
title += "\n"
examples.write(title)
for j,ex in enumerate(entries[i]):
if ex != "":
line = '' + ex.replace("examples-xml/",'')
print line
#line += "\n"
line += "\n"
examples.write(line)
examples.write("\n")
examples.write('\n')
examples.close()
#Now generate the quickref file
#Reset entries
entries = []
for i in categories:
entries.append([])
outfilename = 'misc/quickref.xml' if not XO else 'misc/quickrefXO.xml'
quickref = open(outfilename,'w')
quickref.write("\n\n")
quickref.write('''
Opcode Quick Reference
Opcode Quick Reference''')
manualfilename = 'manual.xml' if not XO else 'manualXO.xml'
manual = open(manualfilename, 'r')
text = manual.read()
manual.close()
files = glob.glob('opcodes/*.xml')
try:
files.remove('opcodes/template.xml')
except:
pass
files[len(files):]=glob.glob('opcodes/*/*.xml')
files[len(files):]=glob.glob('vectorial/*.xml')
files[len(files):]=glob.glob('utility/*.xml')
files.sort()
headerText = text[0:text.find(' 0):
# print 'Trimming file: ', filename, ' ', refStart
newfile = newfile[refStart:]
# Necessary to define entities
newfile = headerText + newfile
#print text
try:
xmldoc = minidom.parseString(newfile)
except:
print '>>> Failed to parse:',filename
continue
xmldocId = xmldoc.documentElement.getAttribute('id')
# Some files need special treatment (adds, dollar, divides, modulus, multiplies,
# opbitor, opor, raises, subtracts, assign, ifdef, ifndef, define, include, undef)
# There must be a better way to avoid loosing the entities when parsing the XML
# file. Anyone???
if (filename == 'opcodes' + os.sep + 'adds.xml'):
entry = 'a '+'+ b (no rate restriction)\n'
elif (filename == 'opcodes' + os.sep + 'dollar.xml'):
entry = ''+'$NAME \n'
elif (filename == 'opcodes' + os.sep + 'divides.xml'):
entry = 'a '+'/ b (no rate restriction)\n'
elif (filename == 'opcodes' + os.sep + 'modulus.xml'):
entry = 'a '+'% b (no rate restriction)\n'
elif (filename == 'opcodes' + os.sep + 'multiplies.xml'):
entry = 'a '+'* b (no rate restriction)\n'
elif (filename == 'opcodes' + os.sep + 'opbitor.xml'):
entry = 'a '+'| b (bitwise OR)\n'
elif (filename == 'opcodes' + os.sep + 'opor.xml'):
entry = 'a '+'|| b (logical OR; not audio-rate)\n'
elif (filename == 'opcodes' + os.sep + 'raises.xml'):
entry = 'a '+'ˆ b (b not audio-rate)\n'
elif (filename == 'opcodes' + os.sep + 'subtracts.xml'):
entry = 'a '+'− b (no rate restriction)\n'
# elif (filename == 'opcodes' + os.sep + 'assign.xml'):
# entry = ''' '+'−a (no rate restriction)
# '+'+a (no rate restriction)\n'''
elif (filename == 'opcodes' + os.sep + 'ifdef.xml'):
entry = ''+'#ifdef NAME ....' + \
''+'#else ....' + \
''+'#end \n'
elif (filename == 'opcodes' + os.sep + 'ifndef.xml'):
entry=''+'#ifndef NAME ....' + \
''+'#else ....' + \
''+'#end \n'
elif (filename == 'opcodes' + os.sep + 'define.xml'):
entry=''+'#define NAME # replacement text #\n' + \
''+'#define NAME(a' b' c') # replacement text #\n'
elif (filename == 'opcodes' + os.sep + 'include.xml'):
entry=''+'#include "filename"\n'
elif (filename == 'opcodes' + os.sep + 'undef.xml'):
entry=''+'#undef NAME\n'
else:
synopsis = xmldoc.getElementsByTagName('synopsis')
if (len(synopsis) != 0):
# There can be more than 1 synopsis per file
for num in range(len(synopsis)):
tmp = synopsis[num].toxml()
if XO:
opcodename = tmp[tmp.find('') + 9:tmp.find('')]
else:
opcodename = ""
if XO and removedopcodes.count(opcodename) == 0:
print "Removed ----------------------", opcodename
else:
if tmp[-21:] == "": # no arg, insert nbsp
tmp = tmp[:-11] + " "
tmp = tmp.replace('', '')
entry += tmp.replace('', '')
if entry != '':
entry += ''
else:
#print "no synopsis tag for file: " + file
entry = ''
#print "Entry ------ ", entry
info = xmldoc.getElementsByTagName('refentryinfo')
if (len(info)!=0 and entry != ''):
category = info[0].toxml()
category = category[21:-23]
print category
else:
print "no refentryinfo tag for file " + filename
category = "Miscellaneous"
if (entry!=''):
print filename + " sent to Miscellaneous"
#print category
match = False
for j, thiscategory in enumerate(categories):
if (category == thiscategory):
entries[j].append(entry+ '\n')
match = True
if match == False:
print 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(""+ categories[i] + "\n\n\n")
count = 0
for j in range(len(entries[i])):
quickref.write(entries[i].pop(0)) # + '\n')
count += 1
quickref.write("\n")
print str(count) + " entries in category: " + categories[i]
quickref.write('\n')
quickref.close()
print entries