~5-a-day/five-a-day/trunk

« back to all changes in this revision

Viewing changes to fiveaday/files.py

  • Committer: Daniel Holbach
  • Date: 2009-02-27 09:27:36 UTC
  • Revision ID: daniel.holbach@canonical.com-20090227092736-lnndwvl2wansln5y
* Probably the last release of this package.
* Deprecate almost everything. The client and applet are not necessary any 
  more, just sign up for the ~5-a-day-participants team in Launchpad.
* Only leave "5-a-day --update" to generate a nice signature for you.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
import os
2
 
import sys
3
 
import time
4
 
 
5
 
from gettext import gettext as _
6
 
 
7
 
import lpteams
8
2
 
9
3
from core import Core
10
4
c = Core()
11
5
 
12
 
def get_tags():
13
 
    if os.path.exists(c.tag_file):
14
 
        return map(lambda a: a.strip(), open(c.tag_file).readlines())
15
 
    return []
16
 
 
17
 
def get_teams():
18
 
    if os.path.exists(c.team_file):
19
 
        return map(lambda a: a.strip(), open(c.team_file).readlines())
20
 
    return []
21
 
 
22
6
def preserve_old_signature():
23
7
    sig = c.signature_file
24
8
    oldsig = c.old_signature_file
28
12
            os.system("mv %s %s" % (sig, oldsig))
29
13
        else:
30
14
            os.remove(sig)
31
 
 
32
 
def get_old_log():
33
 
    if os.path.exists(c.log_file):
34
 
        lines = open(c.log_file).readlines()
35
 
        return filter(lambda b: b != "", map(lambda a: a.strip(), lines))
36
 
    else:
37
 
        return []
38
 
 
39
 
def first_element_is_int(s):
40
 
    a = s.split()[0]
41
 
    try:
42
 
        i = int(a)
43
 
        return True
44
 
    except:
45
 
        return False
46
 
 
47
 
class LogFile(object):
48
 
    def __init__(self):
49
 
        self.tags = get_tags()
50
 
        self.entries = get_old_log()
51
 
        if self.entries:
52
 
            # we used "2008-4-1 123456" entries before, now they're simply
53
 
            # <timestamp> <bug>
54
 
            self.correct_new_entries = filter(lambda a: first_element_is_int(a),
55
 
                                              self.entries)
56
 
        else:
57
 
            self.correct_new_entries = []
58
 
 
59
 
    def add(self, t, entries):
60
 
        # TODO: move parsing/validation of bug numbers to parser?!
61
 
        #       This method should only get a list of bug numbers probably.
62
 
 
63
 
        # Parse/validate bug arguments:
64
 
        bugs = []
65
 
        for entry in entries:
66
 
            for i in entry.split(","):
67
 
                n = i.strip(" #[]()")
68
 
                if n:
69
 
                    try:
70
 
                        number = int(n)
71
 
                    except:
72
 
                        print >> sys.stderr, _("'%s' is not a valid bug number. Aborting.") % n
73
 
                        return 105 #105: invalid bug number
74
 
                    if filter(lambda a: (int(t)-int(a.split()[0]) < 60*60*24) and \
75
 
                                        (a.split()[1]==str(n)), 
76
 
                              self.correct_new_entries):
77
 
                        print >> sys.stderr, _("Bug '%s' has already been added today. Aborting.") % n
78
 
                        return 106 #106: bug has already been added today
79
 
                    bugs.append(n)
80
 
        # Write bugs to file:
81
 
        if os.path.exists(c.log_file):
82
 
            f = open(c.log_file, "a")
83
 
        else:
84
 
            f = open(c.log_file, "w")
85
 
        for bug in bugs:
86
 
            data_str = "%s %s %s\n" % (int(t), bug, " ".join(self.tags))
87
 
            f.write(data_str)
88
 
        f.close()
89
 
 
90
 
def add_tags(tags):
91
 
    current_tags = get_tags()
92
 
    for t in tags:
93
 
        if t not in current_tags:
94
 
            current_tags.append(t)
95
 
    set_tags(current_tags)
96
 
 
97
 
def remove_tags(tags):
98
 
    current_tags = get_tags()
99
 
    for t in tags:
100
 
        if t in current_tags:
101
 
            current_tags.remove(t)
102
 
    set_tags(current_tags)
103
 
 
104
 
def set_tags(new_tags):
105
 
    f = open(c.tag_file, "w")
106
 
    f.write("\n".join(new_tags))
107
 
    f.close()
108
 
 
109
 
def show_tags():
110
 
    tags = get_tags()
111
 
    if not tags:
112
 
        print _("No tags set right now.")
113
 
    else:
114
 
        if len(tags) == 1:
115
 
            print _("Your current tag is: %s" % tags[0])
116
 
        else:
117
 
            print _("Your current tags are: %s" % ", ".join(tags))
118
 
 
119
 
def set_teams(new_teams):
120
 
    current_teams = get_teams()
121
 
 
122
 
    lp_teams = [a for a in lpteams.get_teams(c.lplogin)]
123
 
    for t in new_teams:
124
 
        if t not in current_teams:
125
 
            if t in lp_teams:
126
 
                current_teams.append(t)
127
 
            else:
128
 
                print >> sys.stderr, _("You're not in team '%s', not adding.") % t
129
 
    for t in current_teams:
130
 
        if t not in new_teams:
131
 
            current_teams.remove(t)
132
 
        if t not in lp_teams:
133
 
            current_teams.remove(t)
134
 
    try:
135
 
        os.remove(c.team_file)
136
 
    except:
137
 
        pass
138
 
    f = open(c.team_file, "w")
139
 
    f.write("\n".join(current_teams))
140
 
    f.close()
141
 
    return True
142
 
 
143
 
def add_teams(teams):
144
 
    current_teams = get_teams()
145
 
    for t in teams:
146
 
        if t not in current_teams:
147
 
            current_teams.append(t)
148
 
    return set_teams(current_teams)
149
 
 
150
 
def remove_teams(teams):
151
 
    current_teams = get_teams()
152
 
    for t in teams:
153
 
        if t in current_teams:
154
 
            current_teams.remove(t)
155
 
    return set_teams(current_teams)
156
 
 
157
 
def show_teams():
158
 
    teams = get_teams()
159
 
    if not teams:
160
 
        print _("No teams set right now.")
161
 
    else:
162
 
        if len(teams) == 1:
163
 
            print _("Your current team is: %s" % teams[0])
164
 
        else:
165
 
            print _("Your current teams are: %s" % ", ".join(teams))
166