forked from ihdavids/orgextended
-
Notifications
You must be signed in to change notification settings - Fork 0
/
orgclocking.py
167 lines (142 loc) · 4.49 KB
/
orgclocking.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
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 OrgExtended.orgutil.util as util
import OrgExtended.orgutil.navigation as nav
import OrgExtended.orgutil.template as templateEngine
import logging
import sys
import traceback
import OrgExtended.orgdb as db
import OrgExtended.asettings as sets
import OrgExtended.orgproperties as props
import yaml
import OrgExtended.pymitter as evt
log = logging.getLogger(__name__)
class ClockManager:
Clock = None
@staticmethod
def ClockInRecord(file, onode, dt):
parentHeading = ""
if(onode.parent and type(onode.parent) != node.OrgRootNode):
parentHeading = onode.parent.heading
ClockManager.Clock = {
'file': file.filename,
'start': dt,
'heading': onode.get_locator()
}
ClockManager.SaveClock()
@staticmethod
def ClockRunning():
return ClockManager.Clock != None
@staticmethod
def FormatClock(now):
return now.strftime("[%Y-%m-%d %a %H:%M]")
@staticmethod
def FormatDuration(d):
hours = d.seconds/3600
minutes = (d.seconds/60)%60
return "{0:02d}:{1:02d}".format(int(hours),int(minutes))
@staticmethod
def ClockPath():
return os.path.join(sublime.packages_path(), "User","OrgExtended_Clocks.yaml")
@staticmethod
def SaveClock():
f = open(ClockManager.ClockPath(),"w")
data = yaml.dump(ClockManager.Clock, f)
f.close()
@staticmethod
def LoadClock():
cpath = ClockManager.ClockPath()
if(os.path.isfile(cpath)):
stream = open(cpath, 'r')
ClockManager.Clock = yaml.load(stream, Loader=yaml.SafeLoader)
stream.close()
@staticmethod
def ClockIn(view):
if(ClockManager.ClockRunning()):
view.set_status("Clock","CLOCK")
ClockManager.ClockOut(view)
# Handle clock already running
node = db.Get().AtInView(view)
if(node):
file = db.Get().FindInfo(view)
if(file):
dt = datetime.datetime.now()
ClockManager.ClockInRecord(file, node, dt)
props.AddProperty(view, node, "CLOCK", ClockManager.FormatClock(dt) + "--")
@staticmethod
def ClockOut(view):
if(not ClockManager.ClockRunning()):
return
# Eventually we want to navigate to this node
# rather than doing this.
node = db.Get().FindNode(ClockManager.Clock["file"], ClockManager.Clock["heading"])
if(node):
end = datetime.datetime.now()
start = ClockManager.Clock["start"]
duration = end - start
# Should we keep clocking entries less than a minute?
shouldKeep = sets.Get("clockingSubMinuteClocks",True)
if(not shouldKeep and duration.seconds < 60):
props.RemoveProperty(view, node, "CLOCK")
else:
props.UpdateProperty(view, node, "CLOCK", ClockManager.FormatClock(start) + "--" + ClockManager.FormatClock(end) + " => " + ClockManager.FormatDuration(duration))
ClockManager.ClearClock()
else:
log.error("Failed to clock out, couldn't find node")
@staticmethod
def ClearClock():
ClockManager.Clock = None
cpath = ClockManager.ClockPath()
if(os.path.isfile(cpath)):
os.remove(cpath)
@staticmethod
def GetActiveClockFile():
if(not ClockManager.ClockRunning()):
return None
return ClockManager.Clock["file"]
@staticmethod
def GetActiveClockAt():
if(not ClockManager.ClockRunning()):
return None
node = db.Get().FindNode(ClockManager.Clock["file"], ClockManager.Clock["heading"])
if(node):
node.start_row()
# Load the clock cache.
def Load():
ClockManager.LoadClock()
# Clock in a task
class OrgClockInCommand(sublime_plugin.TextCommand):
def run(self,edit,onDone=None):
ClockManager.ClockIn(self.view)
evt.EmitIf(onDone)
# Clock out a task
class OrgClockOutCommand(sublime_plugin.TextCommand):
def run(self,edit,onDone=None):
ClockManager.ClockOut(self.view)
evt.EmitIf(onDone)
# Clear the currently running clock (if there is one)
class OrgClearClockCommand(sublime_plugin.TextCommand):
def run(self,edit):
ClockManager.ClearClock()
# Recalculate all the clock values in a node (Crtl-c Ctrl-c on a clock entry)
class OrgRecalculateClockCommand(sublime_plugin.TextCommand):
def run(self,edit):
node = db.Get().AtInView(self.view)
clockList = copy.copy(node.clock)
print(str(clockList))
print(str(len(clockList)))
props.RemoveAllInstances(self.view, node, "CLOCK")
print(str(clockList))
print(str(len(clockList)))
for clock in clockList:
# File is reloaded have to regrab node
node = db.Get().At(self.view, node.start_row)
props.AddProperty(self.view, node, "CLOCK", clock.format_clock_str())