Attachment 'xml_parser.py'

Download

   1 import xml.dom.minidom as minidom
   2 import xml.dom as dom
   3 import models
   4 
   5 def make_dom():
   6     dom = minidom.parse('cheat_sheet.xml')
   7     return dom
   8 
   9 def parse_data(text):
  10     text = text.replace('<code>', '{{{')
  11     text = text.replace('</code>', '}}}')
  12     text = text.replace('<a href="', '[[')
  13     text = text.replace('">', '|')
  14     text = text.replace('</a>', ']]')
  15     text = text.replace('&quot;', '"')
  16     return text
  17 
  18 
  19 def parse_text(node):
  20     data = ''.join([n.toxml() for n in node.childNodes])
  21     return parse_data(data)
  22 
  23 def parse_args(args_element, wxmethod):
  24     for arg_element in args_element.getElementsByTagName('arg'):
  25         wiki_text = parse_text(arg_element)
  26         wxmethod.args.append(wiki_text)
  27         if arg_element.getAttribute('emphasize') == 'True':
  28             wxmethod.args_of_interest.append(wiki_text)
  29 
  30 
  31 def parse_styles(styles_element, wxmethod):
  32     for style_element in styles_element.getElementsByTagName('style'):
  33         wiki_text = parse_text(style_element)
  34         if style_element.getAttribute('default') == 'True':
  35             wxmethod.default_styles.append(wiki_text)
  36         else:
  37             wxmethod.styles.append(wiki_text)
  38             
  39 
  40 def parse_method(method_element, wxclass):
  41     name = method_element.getAttribute('name')
  42     print '  ' + name
  43     wxmethod = models.wxMethod(name, wxclass)
  44     #print ' '*8 + wxmethod.name
  45     for child in method_element.childNodes:
  46         if child.nodeName == 'description':
  47             wxmethod.description = parse_text(child)
  48             #print ' '*12 + wxmethod.description
  49         elif child.nodeName == 'args':
  50             parse_args(child, wxmethod)
  51         elif child.nodeName == 'styles':
  52             parse_styles(child, wxmethod)
  53         elif child.nodeName == 'notes':
  54             note_elements = child.getElementsByTagName('note')
  55             wxmethod.notes = [parse_text(e) for e in note_elements]
  56     return wxmethod
  57 
  58 def parse_methods(methods_element, wxclass):
  59     method_elements = methods_element.getElementsByTagName('method')
  60     return [parse_method(e, wxclass) for e in method_elements]
  61 
  62 
  63 def parse_wxclass(wxclass_element):
  64     name = wxclass_element.getAttribute('name')
  65     print name
  66     wxclass = models.wxClass(name)
  67     #print '    Class: ' + wxclass.name
  68     for child in wxclass_element.childNodes:
  69         if child.nodeName == 'description':
  70             wxclass.description = parse_text(child)
  71             #print '    ' + wxclass.description
  72         elif child.nodeName == 'events':
  73             event_elements = child.getElementsByTagName('event')
  74             for e in event_elements:
  75                 wxclass.events = [parse_text(e) for e in event_elements]
  76             #print '    Events: ' + str(wxclass.events)
  77         elif child.nodeName == 'notes':
  78             note_elements = child.getElementsByTagName('note')
  79             wxclass.notes = [parse_text(e) for e in note_elements]
  80             #print '    Notes: ' + str(wxclass.notes)            
  81         elif child.nodeName == 'methods':
  82             wxclass.methods = parse_methods(child, wxclass)
  83 
  84     return wxclass
  85                 
  86             
  87 def parse_category(cat_element):
  88     name = cat_element.getAttribute('name')
  89     #print 'Category: ' + name
  90     wxclasses = cat_element.getElementsByTagName('wxclass')
  91     wxclasses = [parse_wxclass(c) for c in wxclasses]
  92 
  93     return (name, wxclasses)
  94 
  95 
  96 def parse_xml():
  97     dom = make_dom()
  98     categories = dom.getElementsByTagName('category')
  99     categories = [parse_category(cat) for cat in categories]
 100 
 101     return categories
 102 
 103 
 104 def make_wxclass_toc(sheet):
 105     s = ''
 106     for category in sheet:
 107         name = category[0]
 108         wxclasses = category[1]
 109         s += '{{{wx.' + name + '}}}:\n '
 110         s += ', '.join([c.toc_str() for c in wxclasses])
 111         s += '<<BR>>\n'
 112     return s
 113 
 114 
 115 def make_wxclass_notes(sheet):
 116     s = ''
 117     for category in sheet:
 118         name = category[0]
 119         wxclasses = category[1]
 120         s += '== ' + name + ' classes ==\n\n'
 121         for c in wxclasses:
 122             s += str(c)
 123     return s
 124 
 125 
 126 def make_sheet():
 127     s = '###\n###DO NOT EDIT\n###\n'
 128     s += '= wxPython Classes Cheat Sheet =\n\n'
 129     s += 'A wikipage to help you write scripts which was generated by a script'
 130     s += ' attatched to the wikipage.\n\n'
 131     s += 'NB: Think of this page as a collection of shortcuts to the docs.'
 132     s += ' It is not comprehensive in any way. There are many more classes'
 133     s += ' in the wx library than listed here, and most classes have many more'
 134     s += ' methods.\nI only add to this page as I encounter new features that I'
 135     s += " need in my projects, and I've just discovered PySide. - Anthony.\n\n"
 136     s +=  '<<Anchor(Top)>>'
 137     s += "'''Contents:'''\n\n"
 138 
 139     sheet = parse_xml()
 140     s += make_wxclass_toc(sheet)
 141     s += make_wxclass_notes(sheet)
 142 
 143     s += '----\n'
 144     s += 'This page was created by AnthonyGlaser.\n\n'
 145     s += "Please don't edit this page directly. Instead, download "
 146     s += "[[attachment:cheat_sheet.xml]], [[attachment:models.py]] "
 147     s += ", and [[attachment:xml_parser.py]]. Modify cheat_sheet.xml "
 148     s += "and run the function {{{write_sheet}}} in xml_parser.py. "
 149     s += " Upload the output and attach any changed files.\n\nOr just "
 150     s += "[[http://www.youtube.com/watch?v=9hmDZz5pDOQ|stay tuned]]"
 151     s += " for updates."""
 152     return s
 153 
 154 def write_sheet():
 155     with open('WxClassesCheatSheet.txt', 'w') as f:
 156         f.write(make_sheet())
 157 
 158 
 159 if __name__ == '__main__':
 160 
 161     write_sheet()

Attached Files

To refer to attachments on a page, use attachment:filename, as shown below in the list of files. Do NOT use the URL of the [get] link, since this is subject to change and can break easily.
  • [get | view] (2013-07-14 17:57:12, 33.3 KB) [[attachment:cheat_sheet.xml]]
  • [get | view] (2013-07-09 15:32:33, 6.8 KB) [[attachment:models.py]]
  • [get | view] (2013-07-14 17:56:55, 5.4 KB) [[attachment:xml_parser.py]]
 All files | Selected Files: delete move to page copy to page

You are not allowed to attach a file to this page.

NOTE: To edit pages in this wiki you must be a member of the TrustedEditorsGroup.