Debian Codegraph @

bin/lp-milestone2ical

raw
1 #!/usr/bin/python3
2 #
3 # Author: Rodney Dawes <[email protected]>
4 #
5 # Copyright 2009 Canonical Ltd.
6 #
7 # This program is free software: you can redistribute it and/or modify it
8 # under the terms of the GNU General Public License version 3, as published
9 # by the Free Software Foundation.
10 #
11 # This program is distributed in the hope that it will be useful, but
12 # WITHOUT ANY WARRANTY; without even the implied warranties of
13 # MERCHANTABILITY, SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR
14 # PURPOSE. See the GNU General Public License for more details.
15 #
16 # You should have received a copy of the GNU General Public License along
17 # with this program. If not, see <http://www.gnu.org/licenses/>.
18
19
20
21 import os
22 import sys
23
24 from xdg.BaseDirectory import xdg_cache_home
25
26 from threading import Thread
27
28 from launchpadlib.launchpad import Launchpad, EDGE_SERVICE_ROOT
29 from launchpadlib.credentials import Credentials
30
31 class MSMain(object):
32
33 def __init__(self, project):
34 self.project = project
35
36 self.cachedir = os.path.join(xdg_cache_home, "lptools")
37 if not os.path.isdir(self.cachedir):
38 os.makedirs(self.cachedir)
39
40 self.launchpad = None
41 self.thread = None
42 self.buffer = []
43
44 def __start_calendar(self):
45 self.buffer.append("BEGIN:VCALENDAR")
46 self.buffer.append("VERSION:2.0")
47 self.buffer.append("PRODID:-//Canonical/lptools/NONSGML v1.0//EN")
48
49 def __end_calendar(self):
50 self.buffer.append("END:VCALENDAR")
51
52 def __convert_to_ical(self, item):
53 for milestone in item.all_milestones:
54 if not milestone.date_targeted:
55 continue
56 self.buffer.append("BEGIN:VEVENT")
57 date = milestone.date_targeted.strftime("%Y%m%dT%H%M%SZ")
58 self.buffer.append("DTSTART:%s" % date)
59 self.buffer.append("SUMMARY:%s" % milestone.name)
60 self.buffer.append("END:VEVENT")
61
62 def __login_and_go(self):
63 credsfile = os.path.join(self.cachedir, "credentials")
64
65 if os.path.exists(credsfile):
66 creds = Credentials()
67
68 with file(credsfile) as f:
69 creds.load(f)
70 self.launchpad = Launchpad(creds, EDGE_SERVICE_ROOT)
71 else:
72 self.launchpad = Launchpad.get_token_and_login(
73 'lptools',
74 EDGE_SERVICE_ROOT,
75 self.cachedir)
76 with file(credsfile, "w") as f:
77 self.launchpad.credentials.save(f)
78
79 self.__start_calendar()
80 try:
81 lp_project = self.launchpad.project_groups[self.project]
82 except KeyError:
83 lp_project = self.launchpad.projects[self.project]
84 finally:
85 self.__convert_to_ical(lp_project)
86 self.__end_calendar()
87 print(self.calendar())
88
89 def run(self):
90 self.thread = Thread(target=self.__login_and_go).start()
91
92 def stop(self):
93 self.thread.join()
94
95 def calendar(self):
96 return "\n".join(self.buffer)
97
98 if __name__ == "__main__":
99 try:
100 project = sys.argv[1]
101 except IndexError:
102 print("Usage: %s <project>" % sys.argv[0])
103 exit(1)
104
105 try:
106 mt = MSMain(project=sys.argv[1])
107 mt.run()
108 except KeyboardInterrupt:
109 mt.stop()