try:
  import json
except ImportError:
  import simplejson as json

# TODO split functions: get/parse, and all/project

def get_all(baseurl, opener = None):
    """
    Get branch informations for all projects

    baseurl is the root URL of Hudson.

    opener is a function to call to open the URL, by default urllib2.urlopen.
    It can be useful to change it to handle authentication.

    Returns a dictionary.
    """
    if opener is None:
        from urllib2 import urlopen
        opener = urlopen

    # FIXME is Hudson's encoding always this?
    data = json.load(
        opener(baseurl+"/api/json?depth=2"),
        encoding="iso-8859-1"
    )

    infos = []

    for job in data["jobs"]:
      info = {}
      info["name"] = job["name"]
      info["url"] = job["url"]

      results = {}
      for build in job["builds"]:
        result = build["result"]
        if result:
          for action in build["actions"]:
              if action.has_key("lastBuiltRevision") \
              and action["lastBuiltRevision"].has_key("branch"):
                for branch in action["lastBuiltRevision"]["branch"]:
                  if not results.has_key(branch["name"]):
                    results[branch["name"]] = result

      info["branches"] = results

      infos.append(info)

    return infos

@@ -699,24 +699,26 @@
 bs%22%5D:%0A      
+  
 info = %7B%7D%0A  
@@ -711,24 +711,26 @@
   info = %7B%7D%0A
+  
       info%5B%22
@@ -752,24 +752,26 @@
 ame%22%5D%0A      
+ 
+ 
 info%5B%22url%22%5D 
@@ -786,24 +786,26 @@
 rl%22%5D%0A%0A      
+  
 results = %7B%7D
@@ -807,24 +807,26 @@
  = %7B%7D%0A      
+ 
+ 
 for build in
@@ -845,24 +845,28 @@
 %22%5D:%0A        
+    
 result = bui
@@ -878,16 +878,20 @@
 esult%22%5D%0A
+    
         
@@ -907,24 +907,30 @@
 :%0A          
+      
 for action i
@@ -945,24 +945,30 @@
 %22actions%22%5D:%0A
+      
             
@@ -1024,16 +1024,22 @@
         
+     
+ 
 and acti
@@ -1081,16 +1081,24 @@
 anch%22):%0A
+        
         
@@ -1176,16 +1176,26 @@
         
+         
+ 
 if not r
@@ -1242,24 +1242,36 @@
             
+        
+    
 results%5Bbran
@@ -1294,24 +1294,26 @@
 sult%0A%0A      
+  
 info%5B%22branch
@@ -1324,24 +1324,26 @@
  = results%0A%0A
+  
       infos.

Fix wrong indentation