Split main parsing code out of network-dependent Launchpad code.
authorJelmer Vernooij <jelmer@samba.org>
Fri, 20 Jun 2008 14:08:45 +0000 (16:08 +0200)
committerJelmer Vernooij <jelmer@samba.org>
Fri, 20 Jun 2008 14:08:45 +0000 (16:08 +0200)
remote/launchpad.py

index 44add7f80f81bfa4a3eeab450476fbb888b6de0d..da1a0ddc9845005e7bfc2c68d1522ffce8720a9f 100644 (file)
@@ -32,7 +32,31 @@ import urllib, urlparse, re
 from __init__ import *
 import rfc822
 
-class LaunchpadData:
+
+class LaunchpadTask:
+    def __init__(self, msg):
+        self.product = msg["task"]
+        self.status = msg["status"]
+
+
+class LaunchpadBug:
+    def __init__(self, text):
+        bug = rfc822.Message(text)
+        self.duplicate_of = bug.get("duplicate-of", None)
+        if self.duplicate_of == "":
+            self.duplicate_of = None
+        self.id = int(bug["bug"])
+        self.tasks = {}
+
+        # Find the task related to the product we're dealing 
+        # with at the moment
+        task = rfc822.Message(text)
+        while "task" in task:
+            self.tasks[task["task"]] = LaunchpadTask(task)
+            task = rfc822.Message(text)
+
+
+class RemoteLaunchpadData:
     def __init__(self, uri, id):
 
         text = urllib.urlopen(urlparse.urljoin(uri+"/", "+text"))
@@ -43,30 +67,32 @@ class LaunchpadData:
         else:
             # https://bugs.launchpad.net/bugs/XXXXXX
             product = None
+        self.resolution = None
 
-        bug = rfc822.Message(text)
-        if bug.get("duplicate-of", "") != "":
+        lp_bug = LaunchpadBug(text)
+        self.id = lp_bug.id
+
+        if lp_bug.duplicate_of is not None:
             raise DupeExn(uri)
 
-        self.id = int(bug["bug"])
-        self.resolution = None
+        if product is None: 
+            if len(lp_bug.tasks) != 1:
+                raise ParseExn(uri, 
+                    "No product specified but bug affects multiple products")
+            else:
+                self.status = lp_bug.tasks[lp_bugs.keys()[0]].status
 
-        # Find the task related to the product we're dealing 
-        # with at the moment
-        task = rfc822.Message(text)
-        while "task" in task:
-            if product is None or task["task"] == product:
-                self.status = task["status"]
-                return
-            task = rfc822.Message(text)
-        failwith(uri, "Not a bug in %s" % product)
+        if product not in lp_bug.tasks:
+            raise ParseExn(uri, "No task affecting product %s" % product)
+
+        self.status = lp_bug.tasks[product].status
 
 
 class RemoteLaunchpad(RemoteBts):
     def __init__(self, cnf):
         bugre  = r"^%(uri)s/(?:.*/)?\+bug/([0-9]+)$"
         urifmt = "%(uri)s/\+bug/%(id)s"
-        RemoteBts.__init__(self, cnf, bugre, urifmt, LaunchpadData)
+        RemoteBts.__init__(self, cnf, bugre, urifmt, RemoteLaunchpadData)
 
     def isClosing(self, status, resolution):
         return status == "Fix-Released" or status == "Fix-Committed"