-
Notifications
You must be signed in to change notification settings - Fork 0
/
gantt.py
160 lines (140 loc) · 4.55 KB
/
gantt.py
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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
from datetime import datetime
from io import StringIO
from .utility import write_output, format_latex
__all__ = ["gantt", "gantt_embedded"]
# Milestones with these prefixes are included when generating Gantt charts.
# Note that some are not DM milestones, but are included for context.
GANTT_MILESTONES = [
"LDM",
"LSST-1200",
"T&SC-1100-0900",
# "COMC-1264",
# "CAMM6995", Dropped following LCR-1288.
"LSST-1220",
# "T&SC-1150-0600",
"LSST-1510",
"LSST-1513",
# "COMC-1664",
"LSST-1520",
"LSST-1540",
"LSST-1560",
"LSST-1620",
]
GANTT_PREAMBLE_EMBEDDED = """
\\begin{ganttchart}[
expand chart=\\textwidth,
title label font=\\sffamily\\bfseries,
milestone label font=\\scriptsize,
progress label text={#1},
milestone progress label node/.append style={right=1.4cm},
y unit chart=0.4cm,
y unit title=0.7cm
]{1}{149}
\\gantttitle{}{6} \\gantttitle{2018}{12} \\gantttitle{2019}{12}
\\gantttitle{2020}{12} \\gantttitle{2021}{12} \\gantttitle{2022}{12}
\\gantttitle{2023}{12} \\gantttitle{2024}{12} \\gantttitle{2025}{12}
\\gantttitle{Operations}{47} \\
\\ganttnewline\n
"""
GANTT_POSTAMBLE_EMBEDDED = """
\\end{ganttchart}
"""
GANTT_PREAMBLE_STANDALONE = """
\\documentclass{article}
\\usepackage[
paperwidth=37cm,
paperheight=28cm, % Manually tweaked to fit chart
left=0mm,
top=0mm,
bottom=0mm,
right=0mm,
noheadfoot,
marginparwidth=0pt,
includemp=false
]{geometry}
\\usepackage{pgfgantt}
\\begin{document}
\\begin{center}
\\begin{ganttchart}[
% vgrid, % disabled for aesthetic reasons
% hgrid, % disabled for aesthetic reasons
expand chart=0.98\\textwidth,
title label font=\\sffamily\\bfseries,
milestone label font=\\sffamily\\bfseries,
progress label text={#1},
milestone progress label node/.append style={right=2.2cm},
milestone progress label font=\\sffamily,
y unit chart=0.55cm,
y unit title=0.8cm
]{1}{126}
\\gantttitle{}{6} \\gantttitle{2018}{12} \\gantttitle{2019}{12}
\\gantttitle{2020}{12} \\gantttitle{2021}{12} \\gantttitle{2022}{12}
\\gantttitle{2023}{12} \\gantttitle{2024}{12} \\gantttitle{2025}{12}
\\gantttitle{Operations}{24} \\
\\ganttnewline\n
"""
GANTT_POSTAMBLE_STANDALONE = """
\\end{ganttchart}
\\end{center}
\\end{document}
"""
def format_gantt(milestones, preamble, postamble, start=datetime(2017, 7, 1)):
def get_month_number(start, date):
# First month is month 1; all other months sequentially.
return 1 + (date.year * 12 + date.month) - (start.year * 12 + start.month)
def get_milestone_name(code):
return code.lower().replace("-", "").replace("&", "")
output = StringIO()
output.write(preamble)
for ms in sorted(milestones, key=lambda x: x.due):
# A comma in the name causes a problem; escape with \\
name = ms.short_name.replace(",", "\\,")
output_string = (
f"\\ganttmilestone[name={get_milestone_name(ms.code)},"
f"progress label text={name}"
f"\\phantom{{#1}},progress=100]{{{ms.code}}}"
f"{{{get_month_number(start, ms.due)}}} \\ganttnewline"
)
output.write(format_latex(output_string))
# format_latex() strips trailing newlines; add one for cosmetic reasons
output.write("\n")
for ms in sorted(milestones, key=lambda x: x.due):
for succ in ms.successors:
if succ in [milestone.code for milestone in milestones]:
output.write(
"\\ganttlink{{{}}}{{{}}}\n".format(
get_milestone_name(ms.code), get_milestone_name(succ)
)
)
output.write(postamble)
return output.getvalue()
def gantt_standalone(milestones):
milestones = [
ms
for ms in milestones
for gantt in GANTT_MILESTONES
if ms.code.startswith(gantt)
]
return format_gantt(
sorted(milestones, key=lambda x: (x.due, x.code)),
GANTT_PREAMBLE_STANDALONE,
GANTT_POSTAMBLE_STANDALONE,
)
def gantt_embedded(milestones):
milestones = [
ms
for ms in milestones
for gantt in GANTT_MILESTONES
if ms.code.startswith(gantt)
]
return format_gantt(
sorted(milestones, key=lambda x: (x.due, x.code)),
GANTT_PREAMBLE_EMBEDDED,
GANTT_POSTAMBLE_EMBEDDED,
)
def gantt(args, milestones):
if args.embedded:
tex_source = gantt_embedded(milestones)
else:
tex_source = gantt_standalone(milestones)
write_output(args.output, tex_source)