aboutsummaryrefslogtreecommitdiff
path: root/pym
diff options
context:
space:
mode:
authorvolpino <fox91@anche.no>2012-09-02 18:39:04 +0200
committervolpino <fox91@anche.no>2012-09-02 18:39:04 +0200
commit332b5d7c80f7af68c2f581dae5d70b8c78ec3ddd (patch)
treea511cf6c341ad79cfd919a45ee49fa5bae999659 /pym
parentMerge branch 'master' of https://github.com/iksaif/euscan (diff)
downloadeuscan-332b5d7c80f7af68c2f581dae5d70b8c78ec3ddd.tar.gz
euscan-332b5d7c80f7af68c2f581dae5d70b8c78ec3ddd.tar.bz2
euscan-332b5d7c80f7af68c2f581dae5d70b8c78ec3ddd.zip
euscan: Added XML output
Signed-off-by: volpino <fox91@anche.no>
Diffstat (limited to 'pym')
-rw-r--r--pym/euscan/helpers.py31
-rw-r--r--pym/euscan/out.py4
2 files changed, 35 insertions, 0 deletions
diff --git a/pym/euscan/helpers.py b/pym/euscan/helpers.py
index f748c2e..5e3e6ea 100644
--- a/pym/euscan/helpers.py
+++ b/pym/euscan/helpers.py
@@ -2,6 +2,7 @@ import os
import re
import errno
import urllib2
+from xml.dom.minidom import Document
import portage
from portage import dep
@@ -451,3 +452,33 @@ def parse_mirror(uri):
return None
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)
diff --git a/pym/euscan/out.py b/pym/euscan/out.py
index dcdd5f0..e6e6ebf 100644
--- a/pym/euscan/out.py
+++ b/pym/euscan/out.py
@@ -10,6 +10,8 @@ import portage
from portage.output import EOutput, TermProgressBar
from gentoolkit import pprinter as pp
+from euscan.helpers import dict_to_xml
+
mirrors_ = None
@@ -200,6 +202,8 @@ class EuscanOutput(object):
format_ = format_ or self.config["format"]
if format_.lower() == "json":
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":
return data
else: