~yasumoto7/five-a-day/icon_countdown

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
import textwrap
import time
import sys
import os

from gettext import gettext as _

import files

from core import Core
c = Core()

def get_affected_srcpkgs(bug):
    lpbugs = False
    try:
        import launchpadbugs.connector as Connector
        Bug = Connector.ConnectBug(method="Text")
        lpbugs = True
    except:
        print _("""Warning: Please install  python-launchpad-bugs
    It will add more information about the bug to your signature.""")

    try:
        affects = set(map(lambda a: a.affects.longname.lower(), 
                          Bug(int(bug)).infotable))
        affects = filter(lambda a: a.count("ubuntu"), affects)
        affects = map(lambda a: a.replace(" (ubuntu)", ""), affects)
    except:
        affects = None

    return affects    


def get_int(s):
    i = None
    try:
        i = int(s)
    except:
        pass
    return i


def generate_signature(log, date):
    d = time.localtime(date)
    date_str = "%s-%s-%s" % (d[0], d[1], d[2])
    is_right_date = lambda a: str(a).startswith(date_str) or \
                              (get_int(a.split()[0]) and \
                               get_int(a.split()[0]) > date-24*60*60)
    log = set(filter(is_right_date, log))
    if len(log)>5:
        log = list(log)[-5:]
    text = "My 5 today: "
    html = """
<font color='lightslategray'>-- <br />My 5 today: """
    for line in log:
        bugnr = line.split()[1]
        srcpkgs = ""
        affects = get_affected_srcpkgs(bugnr)
        if affects:
            srcpkgs = str(", ".join(affects))
            text += "#%s (%s), " % (bugnr, srcpkgs)
            html += "#<a href='https://launchpad.net/bugs/%s'>%s</a> (%s), " % (bugnr, bugnr, srcpkgs)
        else:
            text += "#%s, " % (bugnr)
            html += "#<a href='https://launchpad.net/bugs/%s'>%s</a>, " % (bugnr, bugnr)
    text = "\n".join(textwrap.wrap(text[:-2], 70))
    html = html[:-2]
    text += """
Do 5 a day - every day! https://wiki.ubuntu.com/5-A-Day\n"""
    html += """<br />Do 5 a day - every day! <a href='https://wiki.ubuntu.com/5-A-Day'>https://wiki.ubuntu.com/5-A-Day</a><br /></font>"""
    
    return (text, html)


def update(show_html, yesterday):
    log = files.LogFile()
    if not log:
        print >> sys.stderr, \
_("You need to use  5-a-day --add  first to add a few bug reports to your list.")
        sys.exit(1)
    files.preserve_old_signature()

    if yesterday:
        date = time.time()-24*3600
    else:
        date = time.time()
    
    (text, html) = generate_signature(log.entries, date)
    f = open(c.signature_file, "a")
    if os.path.exists(c.old_signature_file):
        f.write(open(c.old_signature_file).read())
        f.write("\n")
    f.write(text)
    f.close()

    if show_html:
        print html

    return 0