Fixes for Python 3 and refactoring: Merge generic Output class into specialized output classes as some need to write files in binary mode.

This commit is contained in:
Stefan Rado
2014-02-15 21:37:47 +01:00
parent 1a7518a422
commit d30335cb3f
7 changed files with 65 additions and 50 deletions
+1
View File
@@ -1,3 +1,4 @@
parameters.wiki parameters.wiki
parameters.xml parameters.xml
parameters.wikirpc.xml
cookies.txt cookies.txt
-5
View File
@@ -1,5 +0,0 @@
class Output(object):
def Save(self, groups, fn):
data = self.Generate(groups)
with open(fn, 'w') as f:
f.write(data)
@@ -1,7 +1,6 @@
import output
class DokuWikiOutput(output.Output): class DokuWikiListingsOutput():
def Generate(self, groups): def __init__(self, groups):
result = "" result = ""
for group in groups: for group in groups:
result += "==== %s ====\n\n" % group.GetName() result += "==== %s ====\n\n" % group.GetName()
@@ -24,4 +23,8 @@ class DokuWikiOutput(output.Output):
if def_val is not None: if def_val is not None:
result += "* Default value: %s\n" % def_val result += "* Default value: %s\n" % def_val
result += "\n" result += "\n"
return result self.output = result
def Save(self, filename):
with open(filename, 'w') as f:
f.write(self.output)
@@ -1,21 +1,8 @@
import output
from xml.sax.saxutils import escape from xml.sax.saxutils import escape
class DokuWikiOutput(output.Output): class DokuWikiTablesOutput():
def Generate(self, groups): def __init__(self, groups):
pre_text = """<?xml version='1.0'?> result = "====== Parameter Reference ======\nThis list is auto-generated every few minutes and contains the most recent parameter names and default values.\n\n"
<methodCall>
<methodName>wiki.putPage</methodName>
<params>
<param>
<value>
<string>:firmware:parameters</string>
</value>
</param>
<param>
<value>
<string>"""
result = "====== Parameter Reference ======\nThis list is auto-generated every few minutes and contains the most recent parameter names and default values."
for group in groups: for group in groups:
result += "==== %s ====\n\n" % group.GetName() result += "==== %s ====\n\n" % group.GetName()
result += "|< 100% 20% 20% 10% 10% 10% 30%>|\n" result += "|< 100% 20% 20% 10% 10% 10% 30%>|\n"
@@ -54,15 +41,35 @@ class DokuWikiOutput(output.Output):
result += "\n" result += "\n"
result += "\n" result += "\n"
post_text = """</string> self.output = result;
</value>
</param> def Save(self, filename):
<param> with open(filename, 'w') as f:
<value> f.write(self.output)
<name>sum</name>
<string>Updated parameters automagically from code.</string> def SaveRpc(self, filename):
</value> with open(filename, 'w') as f:
</param> f.write("""<?xml version='1.0'?>
</params> <methodCall>
</methodCall>""" <methodName>wiki.putPage</methodName>
return pre_text + escape(result) + post_text <params>
<param>
<value>
<string>:firmware:parameters</string>
</value>
</param>
<param>
<value>
<string>""")
f.write(escape(self.output))
f.write("""</string>
</value>
</param>
<param>
<value>
<name>sum</name>
<string>Updated parameters automagically from code.</string>
</value>
</param>
</params>
</methodCall>""")
@@ -1,8 +1,7 @@
import output
from xml.dom.minidom import getDOMImplementation from xml.dom.minidom import getDOMImplementation
class XMLOutput(output.Output): class XMLOutput():
def Generate(self, groups): def __init__(self, groups):
impl = getDOMImplementation() impl = getDOMImplementation()
xml_document = impl.createDocument(None, "parameters", None) xml_document = impl.createDocument(None, "parameters", None)
xml_parameters = xml_document.documentElement xml_parameters = xml_document.documentElement
@@ -19,4 +18,8 @@ class XMLOutput(output.Output):
xml_param.appendChild(xml_field) xml_param.appendChild(xml_field)
xml_value = xml_document.createTextNode(value) xml_value = xml_document.createTextNode(value)
xml_field.appendChild(xml_value) xml_field.appendChild(xml_value)
return xml_document.toprettyxml(indent=" ", newl="\n", encoding="utf-8") self.output = xml_document.toprettyxml(indent=" ", newl="\n", encoding="utf-8")
def Save(self, filename):
with open(filename, 'wb') as f:
f.write(self.output)
+14 -8
View File
@@ -41,8 +41,9 @@
import scanner import scanner
import srcparser import srcparser
import xmlout import output_xml
import dokuwikiout import output_dokuwiki_tables
import output_dokuwiki_listings
# Initialize parser # Initialize parser
prs = srcparser.Parser() prs = srcparser.Parser()
@@ -50,12 +51,17 @@ prs = srcparser.Parser()
# Scan directories, and parse the files # Scan directories, and parse the files
sc = scanner.Scanner() sc = scanner.Scanner()
sc.ScanDir("../../src", prs) sc.ScanDir("../../src", prs)
output = prs.GetParamGroups() groups = prs.GetParamGroups()
# Output into XML # Output into XML
out = xmlout.XMLOutput() out = output_xml.XMLOutput(groups)
out.Save(output, "parameters.xml") out.Save("parameters.xml")
# Output into DokuWiki # Output to DokuWiki listings
out = dokuwikiout.DokuWikiOutput() #out = output_dokuwiki_listings.DokuWikiListingsOutput(groups)
out.Save(output, "parameters.wiki") #out.Save("parameters.wiki")
# Output to DokuWiki tables
out = output_dokuwiki_tables.DokuWikiTablesOutput(groups)
out.Save("parameters.wiki")
out.SaveRpc("parameters.wikirpc.xml")
+1 -1
View File
@@ -2,4 +2,4 @@ python px_process_params.py
rm cookies.txt rm cookies.txt
curl --cookie cookies.txt --cookie-jar cookies.txt --user-agent Mozilla/4.0 --data "u=$XMLRPCUSER&p=$XMLRPCPASS" https://pixhawk.org/start?do=login curl --cookie cookies.txt --cookie-jar cookies.txt --user-agent Mozilla/4.0 --data "u=$XMLRPCUSER&p=$XMLRPCPASS" https://pixhawk.org/start?do=login
curl -k --cookie cookies.txt -H "Content-Type: application/xml" -X POST --data-binary @parameters.wiki "https://pixhawk.org/lib/exe/xmlrpc.php" curl -k --cookie cookies.txt -H "Content-Type: application/xml" -X POST --data-binary @parameters.wikirpc.xml "https://pixhawk.org/lib/exe/xmlrpc.php"