-
Notifications
You must be signed in to change notification settings - Fork 15
/
orgtimechart.py
576 lines (524 loc) · 21 KB
/
orgtimechart.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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
import sublime
import sublime_plugin
import datetime
import re
from pathlib import Path
import os
import fnmatch
import OrgExtended.orgparse.node as node
from OrgExtended.orgparse.sublimenode import *
import logging
import sys
import traceback
import OrgExtended.orgdb as db
import OrgExtended.asettings as sets
import OrgExtended.pymitter as evt
import OrgExtended.orgduration as dur
import OrgExtended.orgagenda as ag
import uuid
import subprocess
log = logging.getLogger(__name__)
class OrgTimesheet(ag.TodoView):
def __init__(self, name, setup=True, **kwargs):
super(OrgTimesheet, self).__init__(name, False, **kwargs)
def InsertTableHeadings(self, edit):
self.view.insert(edit, self.view.sel()[0].begin(), "|Name|Estimate|Start|End|Dep|Assigned|Spent|X|\n|-\n")
# Dependencies are marked with the AFTER tag or an ORDERED list.
def GetAfter(self, n):
dep = n.get_property("AFTER", "")
if dep and not dep == "":
file, at = db.Get().FindByAnyId(dep)
if file:
dep = file.At(at)
else:
dep = None
if not dep or dep == "":
t = n
while t.parent is not None and not t.parent.is_root():
orde = t.parent.get_property("ORDERED", None)
print("PROP: " + str(orde))
if orde is not None:
dep = n.get_sibling_and_child_up()
# Chain to parent in ORDERED setup.
if dep is None and t != n:
dep = n.parent
break
t = t.parent
return dep
def GetGlobalProperty(self, name, n, ass):
props = n.list_comment('PROPERTY', None)
if (props):
for i in range(0, len(props), 2):
prop = props[i]
prop = prop.strip()
if (prop.startswith('ASSIGNED') and len(props) > i + 1):
ass = props[i + 1]
return ass
return ass
def GetAssigned(self, n):
ass = sets.Get("timesheetDefaultAssigned", "")
ass = self.GetGlobalProperty("ASSIGNED", n, ass)
ass = n.get_property("ASSIGNED", ass)
return ass
def GetSection(self, n):
ass = sets.Get("timesheetDefaultSection", None)
ass = self.GetGlobalProperty("SECTION", n, ass)
if n.parent is not None:
ass = n.parent.get_property("SECTION", ass)
ass = n.get_property("SECTION", ass)
return ass
def GetClockingData(self, n):
if n.clock:
return n.duration()
return ""
def PreprocessAfter(self):
for entry in self.entries:
n = entry['node']
sec = self.GetSection(n)
if sec:
entry['section'] = sec
dep = self.GetAfter(n)
if dep:
entry['after'] = dep
index = 0
for dp in self.entries:
index += 1
d = dp['node']
if d == dep:
entry['after_offset'] = index
entry['after_name'] = dep.heading
def RenderSheet(self, edit, view):
self.view = view
self.InsertTableHeadings(edit)
newEntries = []
self.PreprocessAfter()
for entry in self.entries:
n = entry['node']
filename = entry['file'].AgendaFilenameTag()
estimate = n.get_property("EFFORT", "")
dt = None
timestamps = n.get_timestamps(active=True, point=True, range=True)
end = ""
start = ""
if timestamps and len(timestamps) > 0:
dt = timestamps[0].start
if n.deadline:
dt = n.deadline.start
if n.scheduled:
dt = n.scheduled.start
if dt:
start = dt.strftime("<%Y-%m-%d>")
if estimate != "":
duration = dur.OrgDuration.Parse(estimate)
endtm = dt + duration.timedelta()
end = endtm.strftime("<%Y-%m-%d>")
pass
else:
start = ""
done = ""
if ag.IsDone(n):
done = "x"
spent = self.GetClockingData(n)
dependenton = entry['after_offset'] if 'after_offset' in entry else ""
# TODO: Adjust index to match table separators
assigned = self.GetAssigned(n)
self.view.insert(edit, self.view.sel()[0].begin(), "|{0:15}|{1:12}|{2}|{3}|{4}|{5}|{6}|{7}|\n".format(n.heading,estimate,start,end,dependenton,assigned,spent,done))
def RenderMermaidGanttFile(self):
tpath = sets.Get("timesheetPath", None)
if tpath is None:
print("ERROR CANNOT RENDER MERMAID WITHOUT timesheetPath in config as destination for file")
return
if not os.path.exists(tpath):
os.makedirs(tpath)
filename = os.path.join(tpath, "schedule.mermaid")
with open(filename, "w") as f:
self.CreateMermaidGanttFile(f)
self.GenerateMermaidGanttChartFromFile(filename)
def GenerateMermaidGanttChartFromFile(self, filename):
# To install on mac or linux:
# npm install @mermaid-js/mermaid-cli
# if sys.platform == 'darwin':
# mmdc = "./node_modules/.bin/mmdc"
# elif sys.platform == 'win32':
# mmdc = "C:\\Users\\ihdav\\node_modules\\.bin\\mmdc.ps1"
print("Trying to render: " + filename)
execs = sets.Get("mermaidPath", None)
tpath = sets.Get("timesheetPath", None)
if execs is None:
print("CANNOT RENDER MERMAID WITHOUT mmdc command line tool. Please install mermaid-cli and fill in mermaidPath")
return
outputFilename = os.path.join(tpath, "project_schedule.png")
# inputFilename = "D:\\Git\\notes\\worklog\\schedule.mermaid"
if sys.platform == 'win32':
commandLine = ["powershell.exe", execs, "-i", filename, "-o", outputFilename, "--width", "2500", "--height", "1024"]
elif sys.platform == 'darwin':
commandLine = [execs, "-i", filename, "-o", outputFilename, "--width", "2500", "--height", "1024"]
try:
startupinfo = subprocess.STARTUPINFO()
startupinfo.dwFlags |= subprocess.STARTF_USESHOWWINDOW
except Exception:
startupinfo = None
view = sublime.active_window().active_view()
fn = view.file_name()
cwd = tpath
if fn is not None:
cwd = os.path.dirname(fn)
popen = subprocess.Popen(commandLine, universal_newlines=True, cwd=cwd, startupinfo=startupinfo, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
# popen.wait()
(o, e) = popen.communicate()
log.debug(o)
log.debug(e)
def RenderGoogleGanttFile(self):
tpath = sets.Get("timesheetPath", None)
if tpath is None:
print("ERROR CANNOT RENDER GOOGLE GANTT WITHOUT timesheetPath in config")
return
if not os.path.exists(tpath):
os.makedirs(tpath)
filename = os.path.join(tpath, "project_schedule.html")
with open(filename, "w") as f:
self.CreateGoogleGanttFile(f)
def CreateGoogleGanttFile(self, f):
self.PreprocessAfter()
import re
idx = 0
f.write("""
<html>
<head>
<!-- <link rel="stylesheet" href="https://fonts.googleapis.com/css2?family=Source%20Sans%20Pro"> -->
<script type="text/javascript" src="https://www.gstatic.com/charts/loader.js"></script>
<style>
text {
font-family: "Sofia" !important;
}
body {
font-family: "Sofia", sans-serif;
}
</style>
<script type="text/javascript">
google.charts.load('current', {'packages':['gantt']});
google.charts.setOnLoadCallback(drawChart);
function daysToMilliseconds(days) {
return days * 24 * 60 * 60 * 1000;
}
function drawChart() {
var data = new google.visualization.DataTable();
data.addColumn('string', 'Task ID');
data.addColumn('string', 'Task Name');
data.addColumn('string', 'Resource');
data.addColumn('date', 'Start Date');
data.addColumn('date', 'End Date');
data.addColumn('number', 'Duration');
data.addColumn('number', 'Percent Complete');
data.addColumn('string', 'Dependencies');
data.addRows([""")
curSection = None
for entry in self.entries:
n = entry['node']
filename = entry['file'].AgendaFilenameTag()
section = entry['section'] if 'section' in entry else None
estimate = n.get_property("EFFORT", "")
dt = None
timestamps = n.get_timestamps(active=True,point=True,range=True)
end = None
start = None
duration = "1"
if estimate != "":
duration = dur.OrgDuration.Parse(estimate).days()
if timestamps and len(timestamps) > 0:
dt = timestamps[0].start
if n.deadline:
dt = n.deadline.start
if n.scheduled:
dt = n.scheduled.start
if dt:
start = dt
if estimate != "":
duration = dur.OrgDuration.Parse(estimate)
end = dt + duration.timedelta()
duration = duration.days()
pass
else:
start = None
done = False
percentDone = 0
if ag.IsDone(n):
done = True
percentDone = 100
resource="active"
spent = self.GetClockingData(n)
dependenton = entry['after_name'] if 'after_name' in entry else None
assigned = self.GetAssigned(n)
idx += 1
if(idx > 0):
#if(done):
# continue
#if(curSection != section and section != None):
# f.write("section {name}\n".format(name=section))
# curSection = section
date = start
dep = dependenton
if assigned and assigned.strip() != None:
resource = assigned.strip()
if done:
resource = "done"
if (date == ""):
date = sets.Get("timesheetDefaultStartDate","2021-05-18")
if start:
start = "new Date({year},{month},{day})".format(year=start.year,month=start.month,day=start.day)
else:
start = "null"
if end:
end = "new Date({year},{month},{day})".format(year=end.year,month=end.month,day=end.day)
else:
end = "null"
line = ""
if idx != 1:
line += ","
if (dep != None and dep != ""):
line += "[\"{name}\",\"{name}\",\"{resource}\", {start},{end},daysToMilliseconds({duration}),{percent},\"{after}\"]\n".format(name=n.heading,idx=idx,after=str(dep),duration=duration,start=start,end=end,percent=percentDone,resource=resource)
else:
line += "[\"{name}\",\"{name}\",\"{resource}\", {start},{end},daysToMilliseconds({duration}),{percent},null]\n".format(name=n.heading,idx=idx,start=start,end=end,duration=duration,percent=percentDone,resource=resource)
f.write(line)
# colorByRowLabel: true
f.write("""]);
var options = {
'font-family': 'Sofia',
height: 1200,
is3D: true,
title: 'Hello World',
gantt: {
trackHeight: 30,
}
};
var chart = new google.visualization.Gantt(document.getElementById('chart_div'));
chart.draw(data, options);
}
</script>
</head>
<body>
<div id="chart_div"></div>
</body>
</html>
""")
# This is REALLY rough it gives us a non functional, really basic view.
def CreateMermaidGanttFile(self,f):
self.PreprocessAfter()
import re
idx = 0
f.write("gantt\n")
f.write("\tdateFormat YYYY-MM-DD\n")
f.write("\taxisFormat %m-%d\n")
f.write("\ttitle Bench Schedule\n")
f.write("\ttodayMarker off\n")
f.write("\texcludes weekends\n")
curSection = None
for entry in self.entries:
n = entry['node']
filename = entry['file'].AgendaFilenameTag()
section = entry['section'] if 'section' in entry else None
estimate = n.get_property("EFFORT", "")
dt = None
timestamps = n.get_timestamps(active=True, point=True, range=True)
end = ""
start = ""
duration = "1d"
if estimate != "":
duration = dur.OrgDuration.Parse(estimate)
if timestamps and len(timestamps) > 0:
dt = timestamps[0].start
if n.deadline:
dt = n.deadline.start
if n.scheduled:
dt = n.scheduled.start
if dt:
start = dt.strftime("%Y-%m-%d")
if estimate != "":
duration = dur.OrgDuration.Parse(estimate)
endtm = dt + duration.timedelta()
end = endtm.strftime("%Y-%m-%d")
pass
else:
start = ""
## spent = self.GetClockingData(n)
dependenton = entry['after_offset'] if 'after_offset' in entry else ""
# TODO: Adjust index to match table separators
assigned = self.GetAssigned(n)
#self.view.insert(edit, self.view.sel()[0].begin(), "|{0:15}|{1:12}|{2}|{3}|{4}|{5}|{6}|{7}|\n".format(n.heading,estimate,start,end,dependenton,assigned,spent,done))
idx += 1
if (idx > 0):
if (curSection != section and section is not None):
f.write("section {name}\n".format(name=section))
curSection = section
date = start
dep = dependenton
prefix = ""
if (ag.IsDone(n)):
prefix = "done,"
if (assigned == 'D'):
prefix = "done,"
if (assigned == 'A'):
prefix += "active,"
if (assigned == 'C'):
prefix += 'crit,'
if (assigned == 'M'):
prefix += 'milestone,'
if (assigned == 'X'):
prefix += 'crit,done,'
if (assigned == 'Y'):
prefix += 'crit,active,'
if (date == ""):
date = sets.Get("timesheetDefaultStartDate", "2023-01-02")
line = ""
if (dep is not None and dep != ""):
line = "\t{name}\t:{prefix}{idx},{start},{duration}\n".format(prefix=prefix,name=n.heading,idx=idx,start="after " + str(dep),duration=duration)
else:
line = "\t{name}\t:{prefix}{idx},{start},{duration}\n".format(prefix=prefix,name=n.heading,idx=idx,start=date,duration=duration)
f.write(line)
def FilterEntry(self, n, filename):
return (ag.IsDone(n) or ag.IsTodo(n)) and not ag.IsProject(n) and not ag.IsArchived(n)
# ================================================================================
class TimesheetRegistry:
def __init__(self):
self.KnownViews = {}
self.AddView("Todos", OrgTimesheet)
def AddView(self,name,cls):
self.KnownViews[name] = cls
# ViewName: <NAME> <ARGS> : <NAME> <ARGS>
def ParseArgs(self, n ):
tokens = n.split(':')
name = tokens[0].strip()
args = {}
i = 1
while(i < len(tokens)):
p = tokens[i].strip()
if(len(p) > 0):
idx = p.find(' ')
if(idx > 0):
pname = p[:idx].strip()
pval = p[idx:].strip()
args[pname] = pval
#print(pname + " -> " + pval)
else:
args[p] = True
i += 1
return (name, args)
def CreateCompositeView(self,views,name="Timesheet"):
vlist = []
for v in views:
n, args = self.ParseArgs(v)
# For timesheets we ONLY have the one view.
# We may need to pull the filter from the other known views in the future?
n = "Todos"
vv = None
if(args == None):
vv = self.KnownViews[n](n, False)
else:
vv = self.KnownViews[n](n, False, **args)
if(vv):
return vv
return None
timesheetRegistry = TimesheetRegistry()
class OrgInsertTimesheetCommand(sublime_plugin.TextCommand):
def run(self, edit, toShow=None, onDone=None):
self.onDone = onDone
self.pos = self.view.sel()[0]
self.views = sets.Get("AgendaCustomViews", {"Default": ["Todos"]})
self.keys = list(self.views.keys())
self.edit = edit
ag.ReloadAllUnsavedBuffers()
if toShow is None:
toShow = "Default"
nameOfShow = toShow
self.views = self.views[nameOfShow]
ts = timesheetRegistry.CreateCompositeView(self.views, nameOfShow)
ts.FilterEntries()
ts.RenderSheet(edit, self.view)
self.view.sel().clear()
self.view.sel().add(self.pos)
self.view.run_command('table_editor_next_field')
evt.EmitIf(self.onDone)
class OrgChooseTimesheetCommand(sublime_plugin.TextCommand):
def on_done_st4(self, index, modifers):
self.on_done(index)
def on_done(self, index):
if (index < 0):
return
key = self.keys[index]
self.view.run_command("org_insert_timesheet", {"toShow": key, "onDone": self.onDone})
def run(self, edit, toShow=None, onDone=None):
self.onDone = onDone
self.pos = self.view.sel()[0]
self.views = sets.Get("AgendaCustomViews", {"Default": ["Todos"]})
self.keys = list(self.views.keys())
self.edit = edit
if (int(sublime.version()) <= 4096):
self.view.window().show_quick_panel(self.keys, self.on_done, -1, -1)
else:
self.view.window().show_quick_panel(self.keys, self.on_done_st4, -1, -1)
class OrgGenerateMermaidGanttChart(sublime_plugin.TextCommand):
def on_done_st4(self, index, modifers):
self.on_done(index)
def on_done(self, index):
if (index < 0):
return
key = self.keys[index]
self.Run(key)
def Run(self, nameOfShow):
ag.ReloadAllUnsavedBuffers()
self.views = self.views[nameOfShow]
print("Creating composite view")
ts = timesheetRegistry.CreateCompositeView(self.views, nameOfShow)
print("Filtering entries")
ts.FilterEntries()
print("Rendering mermaid")
ts.RenderMermaidGanttFile()
print("Done rendering...")
evt.EmitIf(self.onDone)
def run(self, edit, toShow=None, onDone=None):
self.onDone = onDone
self.pos = self.view.sel()[0]
self.views = sets.Get("AgendaCustomViews", {"Default": ["Todos"]})
self.keys = list(self.views.keys())
if toShow is not None:
self.Run(toShow)
return
if (int(sublime.version()) <= 4096):
self.view.window().show_quick_panel(self.keys, self.on_done, -1, -1)
else:
self.view.window().show_quick_panel(self.keys, self.on_done_st4, -1, -1)
class OrgGenerateGoogleGanttChart(sublime_plugin.TextCommand):
def on_done_st4(self, index, modifers):
self.on_done(index)
def on_done(self, index):
if (index < 0):
return
key = self.keys[index]
self.Run(key)
def Run(self, nameOfShow):
ag.ReloadAllUnsavedBuffers()
self.views = self.views[nameOfShow]
ts = timesheetRegistry.CreateCompositeView(self.views, nameOfShow)
ts.FilterEntries()
ts.RenderGoogleGanttFile()
evt.EmitIf(self.onDone)
def run(self, edit, toShow=None, onDone=None):
self.onDone = onDone
self.pos = self.view.sel()[0]
self.views = sets.Get("AgendaCustomViews", {"Default": ["Todos"]})
self.keys = list(self.views.keys())
if toShow is not None:
self.Run(toShow)
return
if (int(sublime.version()) <= 4096):
self.view.window().show_quick_panel(self.keys, self.on_done, -1, -1)
else:
self.view.window().show_quick_panel(self.keys, self.on_done_st4, -1, -1)
# def run(self, edit, onDone=None):
# self.onDone = onDone
# self.views = sets.Get("AgendaCustomViews",{ "Default": ["Calendar", "Day", "Blocked Projects", "Next Tasks", "Loose Tasks"]})
# self.keys = list(self.views.keys())
# if(int(sublime.version()) <= 4096):
# self.view.window().show_quick_panel(self.keys, self.on_done, -1, -1)
# else:
# self.view.window().show_quick_panel(self.keys, self.on_done_st4, -1, -1)