-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
188 lines (153 loc) · 7.16 KB
/
main.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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
import calendar
import jpype.imports
from jpype import JClass
from calendar import Calendar, month_abbr, day_abbr
import sys
import datetime as dt
import random
import gantt
from gantt import Gantt, Reference, ReferenceType, Task
import graphviz
import xlwings as xw
from java import fca, bitset, functor, guava, tinkerpop, java_int
jpype.startJVM(classpath=[fca, bitset, functor, guava, tinkerpop, 'classes'], convertStrings=False)
from org.nmdp.ngs.fca import IntervalLattice
def excel_column(index):
ascii = ''
while index > 0:
index -= 1
ascii = chr(index % 26 + ord('A')) + ascii
index //= 26
return ascii
# def indexes_to_excel_range(worksheet, row, column):
if __name__ == '__main__':
project_start = dt.date(2023, 11, 25)
project_end = dt.date(2024, 1, 3)
print(f"project_start = {project_start}")
project_length = (project_end - project_start).days
print(f"project length in days = {project_length}")
cal = Calendar()
lattice = IntervalLattice()
gantt_chart = Gantt(lattice, project_start, project_end)
dates = {}
months = {}
years = {}
for year in range(project_start.year, project_end.year + 1):
print(f"YEAR = {year}")
start_month = 1
end_month = 13
if year == project_start.year:
start_month = project_start.month
if year == project_end.year:
end_month = project_end.month + 1
for month in range(start_month, end_month):
print(f"MONTH = {month}")
for week in Calendar().monthdatescalendar(year, month):
for dt in week:
#print(f"dt = {dt}")
if dt not in dates:
# todo: shift the local coordinates so that day-intervals are relative to the project start date
# todo: this would mean start = len(dates) - [(project_start) - (calendar_start)]
# todo: the effect is that dates (indexes) before the project start would be negative
date = Reference(dt.strftime('%A')[0], java_int(len(dates)), java_int(len(dates) + 1), dt.day, ReferenceType.DAY)
dates[dt] = date
gantt_chart.add_event(date)
#print(f"year = {year} month = {month_abbr[month]} day = {date} weekday = {day_abbr[weekday][0]}")
index_date = min(dates, key=lambda k: k)
year_index = index_date
this_year = index_date.year
this_month = index_date.month
#print(f"THIS YEAR = {this_year}")
#print(f"THIS MONTH = {this_month}")
for global_date, local_date in dates.items():
if global_date.year != index_date.year:
print("CHANGE YEAR")
month = Reference(index_date.strftime('%B'), java_int(dates[index_date].start), java_int(local_date.start), len(months), ReferenceType.MONTH)
months[len(months)] = month
gantt_chart.add_event(month)
print(f"{month.name} {month.start} {month.end} {month.id}")
year = Reference(year_index.year, java_int(dates[year_index].start), java_int(local_date.start), len(years), ReferenceType.YEAR)
years[len(years)] = year
gantt_chart.add_event(year)
print(f"{year.name} {year.start} {year.end} {year.id}")
index_date = global_date
year_index = index_date
if global_date.month != index_date.month:
print("CHANGE MONTH")
month = Reference(index_date.strftime('%B'), java_int(dates[index_date].start), java_int(local_date.start), len(months), ReferenceType.MONTH)
months[len(months)] = month
gantt_chart.add_event(month)
print(f"{month.name} {month.start} {month.end} {month.id}")
index_date = global_date
print(f"{local_date.name} {local_date.start} {local_date.end} {local_date.id}")
last_date = max(dates, key=lambda k: k)
month = Reference(index_date.strftime('%B'), dates[index_date].start, dates[last_date].end, len(months), ReferenceType.MONTH)
months[len(months)] = month
gantt_chart.add_event(month)
print(f"{month.name} {month.start} {month.end} {month.id}")
year = Reference(year_index.year, java_int(dates[year_index].start), java_int(dates[last_date].end), len(years), ReferenceType.YEAR)
years[len(years)] = year
gantt_chart.add_event(year)
print(f"{year.name} {year.start} {year.end} {year.id}")
wb = xw.Book('gantt.xlsx') # connect to a file that is open or in the current working directory
app = xw.App(visible=True)
ws = wb.sheets['test']
n_rows = 10
n_columns = len(dates)
# Format task block
task_labels = ['Name', 'ID', 'Lead', 'Progress']
column_width = len(max(task_labels, key=len, default=0))
task_block_start = 'A5'
task_block_end = excel_column(len(task_labels)) + '5'
ws.range(f"{task_block_start}:{task_block_end}").column_width = column_width
ws.range(f"{task_block_start}:{task_block_end}").value = task_labels
# Format calendar block
calendar_block_start = excel_column(len(task_labels) + 1) + '1'
print(f"index({len(task_labels)} + 1) = {excel_column(len(task_labels))}")
calendar_block_end = excel_column(len(dates)) + '1'
print(f"{calendar_block_start} {calendar_block_end}")
ws.range(f"{calendar_block_start}:{calendar_block_end}").column_width = 2
day_list = [date.name for date in dates.values()]
date_list = [date.id for date in dates.values()]
ws.range('E3').value = day_list
ws.range('E4').value = date_list
for month in months.values():
start_index = excel_column(month.start + 5)
end_index = excel_column(month.end + 4)
print(f"start = {start_index} end = {end_index} {month.name}")
ws.range(f"{start_index}2").value = month.name
ws[f"{start_index}2"].font.color = (255, 255, 255)
ws[f"{start_index}2"].font.bold = True
ws.range(f"{start_index}2").color = (12, 118, 158)
ws.range(f"{start_index}2:{end_index}2").merge()
for year in years. values():
start_index = excel_column(year.start + 5)
end_index = excel_column(year.end + 4)
print(f"start = {start_index} end = {end_index} {year.name}")
ws.range(f"{start_index}1").value = year.name
ws[f"{start_index}1"].font.color = (255, 255, 255)
ws[f"{start_index}1"].font.bold = True
ws.range(f"{start_index}1").color = '#808080'
ws.range(f"{start_index}1:{end_index}1").merge()
ws.range('E2:CX1').api.Borders.Weight = 2
# Format all blocks (entire Gantt)
ws.range('A1:CX10').api.HorizontalAlignment = xw.constants.HAlign.xlHAlignCenter
print(day_list)
print(date_list)
print(f"***")
print(excel_column(5)) # Output: "A"
print(f"***")
print(excel_column(1)) # Output: "B"
print(f"***")
print(excel_column(26)) # Output: "AA"
print(f"***")
print(excel_column(702)) # Output: "ZZ"
print(f"***")
print(excel_column(703)) # Output: "AAA"
print(f"***")
#f = open('lattice.dot', 'w', encoding='utf-8')
#f.write(str(lattice.toString()))
#f.close()
#g = graphviz.Source.from_file('lattice.dot')
#g.view()
sys.exit()