From 81c5ae716d177cc18fa36d63cb7d5c7435b199f9 Mon Sep 17 00:00:00 2001 From: root Date: Fri, 17 Jun 2011 18:44:59 +0800 Subject: add category title support to generate xml from all its members --- wiki_to_nitrate_xml.py | 51 ++++++++++++++++++++++++++++++-------------------- 1 file changed, 31 insertions(+), 20 deletions(-) diff --git a/wiki_to_nitrate_xml.py b/wiki_to_nitrate_xml.py index c1706c5..c962620 100755 --- a/wiki_to_nitrate_xml.py +++ b/wiki_to_nitrate_xml.py @@ -35,14 +35,14 @@ def parse_args(): optgrp = optparse.OptionGroup(parser, "Options for 'categorymembers' command:") optgrp.add_option('-c', '--category', dest="categories", default=[], action="append", - help='Wiki category name to query (accepts multiple values) - Not Finished Yet!') + help='Wiki category name to covert all its members to Nitrate xml file') parser.add_option_group(optgrp) # migration optgrp = optparse.OptionGroup(parser, "Options for 'migration':") optgrp.add_option('-t', '--title', default='', action='store', - help='test case page title to convert to Nitrate xml file') + help='Page title to convert the test case to Nitrate xml file') parser.add_option_group(optgrp) (opts, args) = parser.parse_args() @@ -101,7 +101,7 @@ def list_categorymembers(wiki, cat_page, limit=5): return members -def extract_html(string, titles): +def extract_to_dict(string, titles): '''extract wiki contents in html format and cache to table''' s_text = string.get('text',{}).get('*','') s_tag = string.get('categories',{}) @@ -124,11 +124,9 @@ def extract_html(string, titles): table['tag'] = tag return table -def nitratexml(table): +def nitrate_xml(table): '''generate Nitrate format xml from wiki test case''' - root = ET.Element("testopia") - root.attrib["version"] = "1.1" - head = ET.SubElement(root, "testcase") + head = ET.Element("testcase") head.attrib["author"] = "rhe@redhat.com" head.attrib["priority"] = "P1" head.attrib["automated"] = "" @@ -136,7 +134,7 @@ def nitratexml(table): title = ET.SubElement(head, "summary") title.text = table['title'] title = ET.SubElement(head, "categoryname") - title.text = "default" + title.text = "--default--" title = ET.SubElement(head, "defaulttester") title = ET.SubElement(head, "notes") title.text = str(table['description']) @@ -152,13 +150,20 @@ def nitratexml(table): title = ET.SubElement(head, "breakdown") title = ET.SubElement(head, "tag") title.text = str(table['tag'][0]) - - tree = ET.ElementTree(root) - tree.write("output.xml", encoding="UTF-8", xml_declaration=True) + return head + +def write_to_file(xmlcases): + '''write the xml contents to a file''' + root = ET.Element("testopia") + root.attrib["version"] = "1.1" + for case in xmlcases: + root.append(case) string = ET.tostring(root) xml_dom = xml.dom.minidom.parseString(string) - pretty_xml_as_string = xml_dom.toprettyxml() - return pretty_xml_as_string + pretty_xml = xml_dom.toprettyxml() + f = open('output.xml', 'w') + f.write(pretty_xml) + f.close() if __name__ == "__main__": (opts,action) = parse_args() @@ -170,18 +175,24 @@ if __name__ == "__main__": for cat_page in opts.categories: pages = list_categorymembers(wiki, cat_page, opts.limit) if pages: - print "\n".join(pages) + print "\n".join(pages) else: print "No data found for '%s'" % cat_page - + sys.exit(1) + elif action == 'migration': - string = parse(wiki, opts.title) - table = extract_html(string, opts.title) + pages = [] + pages.append(opts.title) if opts.debug: for key in table.keys(): print key, '\t', table[key] - pretty_xml_print = nitratexml(table) - print pretty_xml_print, '\n', '\"The xml file named output.xml is generated at ./\"' else: print "Unknown action requested '%s'" % action - + sys.exit(1) + + pagesxml = [] + for pagetitle in pages: + pagestring = parse(wiki, pagetitle) + pagetable = extract_to_dict(pagestring, pagetitle) + pagesxml.append(nitrate_xml(pagetable)) + write_to_file(pagesxml) -- cgit