forked from wakatime/sublime-wakatime
/
WakaTime.py
197 lines (161 loc) · 5.34 KB
/
WakaTime.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
""" ==========================================================
File: WakaTime.py
Description: Automatic time tracking for Sublime Text 2 and 3.
Maintainer: WakaTi.me <support@wakatime.com>
Website: https://www.wakati.me/
==========================================================="""
__version__ = '1.3.2'
import sublime
import sublime_plugin
import glob
import os
import platform
import sys
import time
import threading
import uuid
from os.path import expanduser, dirname, realpath, isfile, join, exists
# globals
ACTION_FREQUENCY = 5
ST_VERSION = int(sublime.version())
PLUGIN_DIR = dirname(realpath(__file__))
API_CLIENT = '%s/packages/wakatime/wakatime-cli.py' % PLUGIN_DIR
SETTINGS_FILE = 'WakaTime.sublime-settings'
SETTINGS = {}
LAST_ACTION = 0
LAST_FILE = None
HAS_SSL = False
LOCK = threading.RLock()
# check if we have SSL support
try:
import ssl
HAS_SSL = True
# import wakatime package
sys.path.insert(0, join(PLUGIN_DIR, 'packages', 'wakatime'))
import wakatime
except ImportError:
from subprocess import Popen
def setup_settings_file():
""" Convert ~/.wakatime.conf to WakaTime.sublime-settings
"""
global SETTINGS
# To be backwards compatible, rename config file
SETTINGS = sublime.load_settings(SETTINGS_FILE)
api_key = SETTINGS.get('api_key', '')
if not api_key:
api_key = ''
try:
with open(join(expanduser('~'), '.wakatime.conf')) as old_file:
for line in old_file:
line = line.split('=', 1)
if line[0] == 'api_key':
api_key = str(line[1].strip())
try:
os.remove(join(expanduser('~'), '.wakatime.conf'))
except:
pass
except IOError:
pass
SETTINGS.set('api_key', api_key)
sublime.save_settings(SETTINGS_FILE)
def prompt_api_key():
global SETTINGS
if not SETTINGS.get('api_key'):
def got_key(text):
if text:
SETTINGS.set('api_key', str(text))
sublime.save_settings(SETTINGS_FILE)
window = sublime.active_window()
if window:
window.show_input_panel('Enter your WakaTime api key:', '', got_key, None, None)
return True
else:
print('Error: Could not prompt for api key because no window found.')
return False
def python_binary():
python = 'python'
if platform.system() == 'Windows':
python = 'pythonw'
try:
Popen([python, '--version'])
except:
for path in glob.iglob('/python*'):
if exists(realpath(join(path, 'pythonw.exe'))):
python = realpath(join(path, 'pythonw'))
break
return python
def enough_time_passed(now):
if now - LAST_ACTION > ACTION_FREQUENCY * 60:
return True
return False
def handle_write_action(view):
global LOCK, LAST_FILE, LAST_ACTION
with LOCK:
targetFile = view.file_name()
thread = SendActionThread(targetFile, isWrite=True)
thread.start()
LAST_FILE = targetFile
LAST_ACTION = time.time()
def handle_normal_action(view):
global LOCK, LAST_FILE, LAST_ACTION
with LOCK:
targetFile = view.file_name()
thread = SendActionThread(targetFile)
thread.start()
LAST_FILE = targetFile
LAST_ACTION = time.time()
class SendActionThread(threading.Thread):
def __init__(self, targetFile, isWrite=False, force=False):
threading.Thread.__init__(self)
self.targetFile = targetFile
self.isWrite = isWrite
self.force = force
self.debug = SETTINGS.get('debug')
self.api_key = SETTINGS.get('api_key', '')
self.last_file = LAST_FILE
def run(self):
if self.targetFile:
self.timestamp = time.time()
if self.force or self.isWrite or self.targetFile != self.last_file or enough_time_passed(self.timestamp):
self.send()
def send(self):
if not self.api_key:
print('missing api key')
return
cmd = [
API_CLIENT,
'--file', self.targetFile,
'--time', str('%f' % self.timestamp),
'--plugin', 'sublime-wakatime/%s' % __version__,
'--key', str(bytes.decode(self.api_key.encode('utf8'))),
]
if self.isWrite:
cmd.append('--write')
if self.debug:
cmd.append('--verbose')
print(cmd)
if HAS_SSL:
wakatime.main(cmd)
else:
cmd.insert(0, python_binary())
if platform.system() == 'Windows':
Popen(cmd, shell=False)
else:
with open(join(expanduser('~'), '.wakatime.log'), 'a') as stderr:
Popen(cmd, stderr=stderr)
def plugin_loaded():
setup_settings_file()
after_loaded()
def after_loaded():
if not prompt_api_key():
sublime.set_timeout(after_loaded, 500)
# need to call plugin_loaded because only ST3 will auto-call it
if ST_VERSION < 3000:
plugin_loaded()
class WakatimeListener(sublime_plugin.EventListener):
def on_post_save(self, view):
handle_write_action(view)
def on_activated(self, view):
handle_normal_action(view)
def on_modified(self, view):
handle_normal_action(view)