Visit Mozilla.org

XUL Parser in Python/source

From MDC

Source code for the XUL Parser in Python.

  import sys, glob, xmllib
  import string, os, re
  el_list = {}
  w = open('res.html', 'w')
  
  # Unfortunately, I had to put this hack in here to suppress the printing out of the resolved namespace: 
  # "xmlns="http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul window", etc.
 
  def strip(snip):
  	t = re.sub('http://.*?\s', '', snip)
  	return t
  		
  class XULParser(xmllib.XMLParser):
  	def unknown_starttag(self, t, a):
  		name = strip(t)
  		if not el_list.has_key(name): el_list[name] = {}
  		for attr in a.keys():
  			el_list[name][strip(attr)] = strip(a[attr])
  			
  	def syntax_error(self, message):
  		pass
  	
  p = XULParser()
  
  cmd = 'dir /s /b *.xul'
  CHROME_DIR = 'C:\Program Files\Netscape\Netscape 6\chrome' 
  os.chdir(CHROME_DIR)
  files = os.popen(cmd).readlines()
  for file in files:
  	file = string.strip(file)
  	print '** ' + file + ' **'
  	data = open(file).read()
  	p.feed(data)
  	
  w.write('<html><h3>Periodic Table of XUL Elements</h3>')
  w.write('<table><style>.head {font-weight: bold; background-color: lightgrey;}</style>')
  
  elements = el_list.keys()
  elements.sort()
  for item in elements:
  	w.write('<tr><td class="head">' + item + '</td></tr>\n')
  	for a in el_list[item].keys():
  		w.write('<tr><td class="at">' + a + '</td>')
  	
  w.write('</table></html>\n')
  w.close()