#!/usr/bin/python
# -*- coding: utf-8 -*-
#
# Copyright 2007, Canonical, Daniel Holbach
#
# GPL 3
#
# hugdaylist <url>
# - produces lists like https://wiki.ubuntu.com/UbuntuBugDay/20070912?action=raw
#
# hugdaylist -n <howmany> <url>
# - will only list <howmany> URLs
#

import re
import os
import sys
import string

try:
    import launchpadbugs.connector as Connector
    BugList = Connector.ConnectBugList()
    Bug = Connector.ConnectBug(method="Text")
except:
    print >> sys.stderr, \
	"You need  python-launchpad-bugs (>= 0.2.25)  installed to use hugdaylist."
    sys.exit(1)


USAGE = "hugdaylist [-n <howmany>] <URL>"

def check_args():
    howmany = -1
    url = ""
    
    if len(sys.argv) < 2:
        print >> sys.stderr, USAGE
        sys.exit(1)
    
    if sys.argv[1] == "-n":
        howmany = int(sys.argv[2])
        if len(sys.argv) < 4:
        	print USAGE
        	sys.exit(1)
        url = sys.argv[3]
    else:
        url = sys.argv[1]
    
    return (howmany, url)

def filter_unsolved(b):
    bug = Bug(int(b))
    return filter(lambda a: a.status != 'Fix Committed' and \
			 (a.assignee in ['motu','desktop-bugs'] or \
			  not a.assignee), bug.infotable) and \
	'ubuntu-main-sponsors' not in [str(s) for s in bug.subscribers] and \
	'ubuntu-universe-sponsors' not in [str(s) for s in bug.subscribers]

def main(): 
    (howmany, url) = check_args()
    
    bl = BugList(url)
    l = filter(filter_unsolved, bl)
    
    if not l:
        print "BugList of %s is empty." % url
        sys.exit(0)
    if howmany == -1:
        howmany = len(l)

    print """
## ||<rowbgcolor="#CCFFCC"> This task is done || somebody || ||
## ||<rowbgcolor="#FFFFCC"> This task is assigned || somebody || <status> ||
## ||<rowbgcolor="#FFEBBB"> This task isn't || ... || ||
## ||<rowbgcolor="#FFCCCC"> This task is blocked on something || somebody || <explanation> ||
        
|| Bug || Subject || Triager ||"""

    for i in list(l)[:howmany]:
        print '||<rowbgcolor="#FFEBBB"> [%s %s] || %s || ||' % \
            (i.url, i.bugnumber, i.summary)


if __name__ == '__main__':
	main()

