euscan: Added XML output

Signed-off-by: volpino <fox91@anche.no>
This commit is contained in:
volpino 2012-09-02 18:39:04 +02:00
parent 59b5eb77ac
commit 332b5d7c80
3 changed files with 36 additions and 1 deletions

2
TODO
View File

@ -13,7 +13,7 @@ euscan
### Command line interface ### Command line interface
- html and xml output - html output
### Misc ### Misc

View File

@ -2,6 +2,7 @@ import os
import re import re
import errno import errno
import urllib2 import urllib2
from xml.dom.minidom import Document
import portage import portage
from portage import dep from portage import dep
@ -451,3 +452,33 @@ def parse_mirror(uri):
return None return None
return uri return uri
def dict_to_xml(data, indent):
doc = Document()
root = doc.createElement("euscan")
doc.appendChild(root)
def _set_value(parent, value):
if isinstance(value, dict):
for k, v in value.iteritems():
node = doc.createElement(k)
_set_value(node, v)
parent.appendChild(node)
elif isinstance(value, list):
for item in value:
node = doc.createElement("value")
text = doc.createTextNode(item)
node.appendChild(text)
parent.appendChild(node)
else:
text = doc.createTextNode(unicode(value))
parent.appendChild(text)
for key, value in data.iteritems():
node = doc.createElement("package")
node.setAttribute("name", key)
_set_value(node, value)
root.appendChild(node)
return doc.toprettyxml(indent=" " * indent)

View File

@ -10,6 +10,8 @@ import portage
from portage.output import EOutput, TermProgressBar from portage.output import EOutput, TermProgressBar
from gentoolkit import pprinter as pp from gentoolkit import pprinter as pp
from euscan.helpers import dict_to_xml
mirrors_ = None mirrors_ = None
@ -200,6 +202,8 @@ class EuscanOutput(object):
format_ = format_ or self.config["format"] format_ = format_ or self.config["format"]
if format_.lower() == "json": if format_.lower() == "json":
return json.dumps(data, indent=self.config["indent"]) return json.dumps(data, indent=self.config["indent"])
elif format_.lower() == "xml":
return dict_to_xml(data, indent=self.config["indent"])
elif format_.lower() == "dict": elif format_.lower() == "dict":
return data return data
else: else: