XML output for "be show"
authorThomas Habets <thomas@habets.pp.se>
Wed, 7 Jan 2009 18:39:32 +0000 (19:39 +0100)
committerThomas Habets <thomas@habets.pp.se>
Wed, 7 Jan 2009 18:39:32 +0000 (19:39 +0100)
becommands/show.py
libbe/bug.py
libbe/comment.py

index abec81319cb959951581dea54eecc20dd0fe99d8..1ee354cf770c144e50e8b937a223dc357e83e459 100644 (file)
@@ -41,10 +41,15 @@ def execute(args):
     bd = bugdir.BugDir(from_disk=True)
     for bugid in args:
         bug = bd.bug_from_shortname(bugid)
-        print bug.string(show_comments=True)
+        if options.dumpXML:
+            print bug.xml(show_comments=True)
+        else:
+            print bug.string(show_comments=True)
 
 def get_parser():
-    parser = cmdutil.CmdOptionParser("be show BUG-ID [BUG-ID ...]")
+    parser = cmdutil.CmdOptionParser("be show [options] BUG-ID [BUG-ID ...]")
+    parser.add_option("-x", "--xml", action="store_true",
+                      dest='dumpXML', help="Dump as XML")
     return parser
 
 longhelp="""
index c75c968c29993677244a5b13bc26d69149d7794d..afa9e09b4fb39c834c24316bbf062cc2c5fe3d21 100644 (file)
@@ -128,6 +128,43 @@ class Bug(object):
     def __repr__(self):
         return "Bug(uuid=%r)" % self.uuid
 
+    def xml(self, show_comments=False):
+        if self.bugdir == None:
+            shortname = self.uuid
+        else:
+            shortname = self.bugdir.bug_shortname(self)
+
+        if self.time == None:
+            timestring = ""
+        else:
+            htime = utility.handy_time(self.time)
+            ftime = utility.time_to_str(self.time)
+            timestring = "%s (%s)" % (htime, ftime)
+
+        info = [("uuid", self.uuid),
+                ("short-name", shortname),
+                ("severity", self.severity),
+                ("status", self.status),
+                ("assigned", self.assigned),
+                ("target", self.target),
+                ("creator", self.creator),
+                ("created", timestring),
+                ("summary", self.summary)]
+        ret = '<bug>\n'
+        for (k,v) in info:
+            if v is not None:
+                ret += '  <%s>%s</%s>\n' % (k,v,k)
+
+        if show_comments:
+            if self._comments_loaded == False:
+                self.load_comments()
+            comout = self.comment_root.xml_thread(auto_name_map=True,
+                                                  bug_shortname=shortname)
+            ret += comout
+
+        ret += '</bug>'
+        return ret
+
     def string(self, shortlist=False, show_comments=False):
         if self.bugdir == None:
             shortname = self.uuid
index c89fd9d836755d1459785bce5513dce15e8fc1a2..87c1de044b051c8098e661f714fd61e113437204 100644 (file)
@@ -130,6 +130,20 @@ class Comment(Tree):
             return ""
         return value
 
+    def xml(self, indent=0, shortname=None):
+        if shortname == None:
+            shortname = self.uuid
+        ret = """<comment>
+  <name>%s</name>
+  <from>%s</from>
+  <date>%s</date>
+  <body>%s</body>
+</comment>\n""" % (shortname,
+                   self._clean_string(self.From),
+                   utility.time_to_str(self.time),
+                   self.body.rstrip('\n'))
+        return ret
+
     def string(self, indent=0, shortname=None):
         """
         >>> comm = Comment(bug=None, body="Some\\ninsightful\\nremarks\\n")
@@ -320,6 +334,23 @@ class Comment(Tree):
             stringlist.append(comment.string(indent=ind, shortname=sname))
         return '\n'.join(stringlist)
 
+    def xml_thread(self, name_map={}, indent=0,
+                   auto_name_map=False, bug_shortname=None):
+        if auto_name_map == True:
+            name_map = {}
+            for shortname,comment in self.comment_shortnames(bug_shortname):
+                name_map[comment.uuid] = shortname
+        stringlist = []
+        for depth,comment in self.thread(flatten=True):
+            ind = 2*depth+indent
+            if comment.uuid in name_map:
+                sname = name_map[comment.uuid]
+            else:
+                sname = None
+            stringlist.append(comment.xml(indent=ind, shortname=sname))
+        return '\n'.join(stringlist)
+        
+
     def comment_shortnames(self, bug_shortname=""):
         """
         Iterate through (id, comment) pairs, in time order.