2013-07-08 01:38:01 +00:00
|
|
|
""" ==========================================================
|
2013-08-05 05:30:24 +00:00
|
|
|
File: WakaTime.py
|
2013-07-08 01:38:01 +00:00
|
|
|
Description: Automatic time tracking for Sublime Text 2 and 3.
|
2013-07-11 02:01:06 +00:00
|
|
|
Maintainer: WakaTi.me <support@wakatime.com>
|
2013-07-08 01:38:01 +00:00
|
|
|
Website: https://www.wakati.me/
|
|
|
|
==========================================================="""
|
2013-07-02 09:21:13 +00:00
|
|
|
|
2013-08-07 00:26:59 +00:00
|
|
|
__version__ = '1.0.0'
|
2013-07-02 09:21:13 +00:00
|
|
|
|
2013-07-23 19:09:58 +00:00
|
|
|
import sublime
|
|
|
|
import sublime_plugin
|
|
|
|
|
2013-07-23 22:18:31 +00:00
|
|
|
import glob
|
2013-08-05 05:11:22 +00:00
|
|
|
import os
|
2013-07-23 19:09:58 +00:00
|
|
|
import platform
|
2013-07-02 09:21:13 +00:00
|
|
|
import time
|
|
|
|
import uuid
|
2013-07-23 19:11:51 +00:00
|
|
|
from os.path import expanduser, dirname, realpath, isfile, join, exists
|
2013-08-05 05:11:22 +00:00
|
|
|
from subprocess import call, Popen
|
2013-07-02 09:21:13 +00:00
|
|
|
|
|
|
|
|
2013-07-08 01:38:01 +00:00
|
|
|
# globals
|
2013-07-10 07:14:44 +00:00
|
|
|
AWAY_MINUTES = 10
|
|
|
|
ACTION_FREQUENCY = 5
|
2013-07-02 09:21:13 +00:00
|
|
|
PLUGIN_DIR = dirname(realpath(__file__))
|
2013-07-20 22:28:38 +00:00
|
|
|
API_CLIENT = '%s/packages/wakatime/wakatime-cli.py' % PLUGIN_DIR
|
2013-08-07 00:21:32 +00:00
|
|
|
SETTINGS = 'WakaTime.sublime-settings'
|
2013-07-08 01:38:01 +00:00
|
|
|
LAST_ACTION = 0
|
2013-07-10 07:14:44 +00:00
|
|
|
LAST_USAGE = 0
|
2013-07-08 01:38:01 +00:00
|
|
|
LAST_FILE = None
|
2013-07-11 18:27:27 +00:00
|
|
|
BUSY = False
|
2013-07-08 01:38:01 +00:00
|
|
|
|
2013-07-10 07:14:44 +00:00
|
|
|
|
2013-08-07 00:21:32 +00:00
|
|
|
def setup_settings_file():
|
|
|
|
""" Convert ~/.wakatime.conf to WakaTime.sublime-settings
|
|
|
|
"""
|
2013-08-05 05:11:22 +00:00
|
|
|
# To be backwards compatible, rename config file
|
|
|
|
settings = sublime.load_settings(SETTINGS)
|
|
|
|
api_key = settings.get('api_key', '')
|
2013-08-05 05:15:47 +00:00
|
|
|
if not 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 = line[1].strip()
|
|
|
|
try:
|
|
|
|
os.remove(join(expanduser('~'), '.wakatime.conf'))
|
|
|
|
except:
|
|
|
|
pass
|
|
|
|
except IOError:
|
|
|
|
pass
|
2013-08-07 00:21:32 +00:00
|
|
|
settings.set('api_key', str(api_key))
|
2013-08-05 05:11:22 +00:00
|
|
|
sublime.save_settings(SETTINGS)
|
|
|
|
|
|
|
|
|
2013-08-07 00:21:32 +00:00
|
|
|
def get_api_key():
|
|
|
|
"""If api key not set, prompt user to enter one then save
|
|
|
|
to WakaTime.sublime-settings.
|
|
|
|
"""
|
2013-08-05 05:11:22 +00:00
|
|
|
settings = sublime.load_settings(SETTINGS)
|
2013-08-07 00:21:32 +00:00
|
|
|
api_key = settings.get('api_key', '')
|
2013-08-05 05:11:22 +00:00
|
|
|
if not api_key:
|
|
|
|
def got_key(text):
|
|
|
|
if text:
|
2013-08-07 00:21:32 +00:00
|
|
|
settings = sublime.load_settings(SETTINGS)
|
|
|
|
settings.set('api_key', str(text))
|
2013-08-05 05:11:22 +00:00
|
|
|
sublime.save_settings(SETTINGS)
|
2013-08-07 00:21:32 +00:00
|
|
|
window = sublime.active_window()
|
|
|
|
if window is not None:
|
|
|
|
window.show_input_panel('Enter your WakaTi.me api key:', '', got_key, None, None)
|
|
|
|
return sublime.load_settings(SETTINGS).get('api_key', '')
|
|
|
|
else:
|
|
|
|
return api_key
|
2013-07-11 01:43:12 +00:00
|
|
|
|
|
|
|
|
2013-07-23 22:10:19 +00:00
|
|
|
def python_binary():
|
|
|
|
python = 'python'
|
|
|
|
if platform.system() == 'Windows':
|
2013-08-03 00:00:26 +00:00
|
|
|
python = 'pythonw'
|
2013-07-23 22:10:19 +00:00
|
|
|
try:
|
|
|
|
Popen([python, '--version'])
|
|
|
|
except:
|
|
|
|
for path in glob.iglob('/python*'):
|
2013-08-03 00:00:26 +00:00
|
|
|
if exists(realpath(join(path, 'pythonw.exe'))):
|
|
|
|
python = realpath(join(path, 'pythonw'))
|
2013-07-23 22:10:19 +00:00
|
|
|
break
|
|
|
|
return python
|
|
|
|
|
|
|
|
|
2013-07-17 01:26:52 +00:00
|
|
|
def api(targetFile, timestamp, isWrite=False, endtime=0):
|
2013-07-10 07:14:44 +00:00
|
|
|
global LAST_ACTION, LAST_USAGE, LAST_FILE
|
2013-07-08 01:38:01 +00:00
|
|
|
if not targetFile:
|
|
|
|
targetFile = LAST_FILE
|
|
|
|
if targetFile:
|
2013-07-23 22:10:19 +00:00
|
|
|
cmd = [python_binary(), API_CLIENT,
|
2013-07-08 01:38:01 +00:00
|
|
|
'--file', targetFile,
|
|
|
|
'--time', str('%f' % timestamp),
|
|
|
|
'--plugin', 'sublime-wakatime/%s' % __version__,
|
2013-07-10 07:14:44 +00:00
|
|
|
#'--verbose',
|
2013-07-08 01:38:01 +00:00
|
|
|
]
|
2013-08-07 00:21:32 +00:00
|
|
|
api_key = get_api_key()
|
2013-08-05 05:11:22 +00:00
|
|
|
if api_key:
|
|
|
|
cmd.extend(['--key', str(api_key)])
|
2013-07-08 01:38:01 +00:00
|
|
|
if isWrite:
|
|
|
|
cmd.append('--write')
|
|
|
|
if endtime:
|
|
|
|
cmd.extend(['--endtime', str('%f' % endtime)])
|
2013-07-16 11:02:30 +00:00
|
|
|
#print(cmd)
|
2013-07-23 22:10:19 +00:00
|
|
|
if platform.system() == 'Windows':
|
2013-08-05 05:11:22 +00:00
|
|
|
Popen(cmd, shell=False)
|
2013-07-23 22:10:19 +00:00
|
|
|
else:
|
|
|
|
with open(join(expanduser('~'), '.wakatime.log'), 'a') as stderr:
|
|
|
|
Popen(cmd, stderr=stderr)
|
2013-07-17 01:26:52 +00:00
|
|
|
LAST_ACTION = timestamp
|
|
|
|
if endtime and endtime > LAST_ACTION:
|
|
|
|
LAST_ACTION = endtime
|
|
|
|
LAST_FILE = targetFile
|
|
|
|
LAST_USAGE = LAST_ACTION
|
|
|
|
else:
|
|
|
|
LAST_USAGE = timestamp
|
2013-07-08 01:38:01 +00:00
|
|
|
|
|
|
|
|
|
|
|
def away(now):
|
2013-07-10 07:14:44 +00:00
|
|
|
duration = now - LAST_USAGE
|
2013-07-12 06:35:49 +00:00
|
|
|
minutes = ''
|
|
|
|
units = 'second'
|
|
|
|
if duration >= 60:
|
2013-07-10 07:14:44 +00:00
|
|
|
duration = int(duration / 60)
|
2013-07-12 06:35:49 +00:00
|
|
|
units = 'minute'
|
|
|
|
if duration >= 60:
|
|
|
|
remainder = duration % 60
|
|
|
|
if remainder > 0:
|
|
|
|
minutes = ' and %d minute' % remainder
|
|
|
|
if remainder > 1:
|
|
|
|
minutes = minutes + 's'
|
|
|
|
duration = int(duration / 60)
|
|
|
|
units = 'hour'
|
|
|
|
if duration > 1:
|
|
|
|
units = units + 's'
|
2013-07-10 07:14:44 +00:00
|
|
|
return sublime\
|
2013-07-12 06:35:49 +00:00
|
|
|
.ok_cancel_dialog("You were away %d %s%s. Add time to current file?"\
|
|
|
|
% (duration, units, minutes), 'Yes, log this time')
|
2013-07-10 07:14:44 +00:00
|
|
|
|
|
|
|
|
|
|
|
def enough_time_passed(now):
|
2013-07-17 01:26:52 +00:00
|
|
|
if now - LAST_ACTION > ACTION_FREQUENCY * 60:
|
2013-07-10 07:14:44 +00:00
|
|
|
return True
|
|
|
|
return False
|
|
|
|
|
|
|
|
|
|
|
|
def should_prompt_user(now):
|
2013-08-05 05:11:22 +00:00
|
|
|
if not sublime.load_settings(SETTINGS).get('prompt_after_away', False):
|
|
|
|
return False
|
2013-07-10 07:14:44 +00:00
|
|
|
if not LAST_USAGE:
|
2013-07-08 01:38:01 +00:00
|
|
|
return False
|
2013-07-10 07:14:44 +00:00
|
|
|
duration = now - LAST_USAGE
|
2013-07-08 01:38:01 +00:00
|
|
|
if duration > AWAY_MINUTES * 60:
|
2013-07-10 07:14:44 +00:00
|
|
|
return True
|
|
|
|
return False
|
2013-07-08 01:38:01 +00:00
|
|
|
|
|
|
|
|
2013-07-10 07:14:44 +00:00
|
|
|
def handle_write_action(view):
|
2013-07-17 01:26:52 +00:00
|
|
|
global LAST_USAGE, BUSY
|
2013-07-16 16:17:21 +00:00
|
|
|
BUSY = True
|
2013-07-10 07:14:44 +00:00
|
|
|
targetFile = view.file_name()
|
2013-07-17 01:26:52 +00:00
|
|
|
now = time.time()
|
|
|
|
if enough_time_passed(now) or (LAST_FILE and targetFile != LAST_FILE):
|
|
|
|
if should_prompt_user(now):
|
|
|
|
if away(now):
|
2013-07-16 16:17:21 +00:00
|
|
|
api(targetFile, now, endtime=LAST_ACTION, isWrite=True)
|
2013-07-17 01:26:52 +00:00
|
|
|
else:
|
|
|
|
api(targetFile, now, isWrite=True)
|
2013-07-10 07:14:44 +00:00
|
|
|
else:
|
2013-07-17 01:26:52 +00:00
|
|
|
api(targetFile, now, endtime=LAST_ACTION, isWrite=True)
|
|
|
|
else:
|
|
|
|
api(targetFile, now, isWrite=True)
|
2013-07-16 16:17:21 +00:00
|
|
|
BUSY = False
|
2013-07-10 07:14:44 +00:00
|
|
|
|
|
|
|
|
|
|
|
def handle_normal_action(view):
|
2013-07-11 18:27:27 +00:00
|
|
|
global LAST_USAGE, BUSY
|
2013-07-16 16:17:21 +00:00
|
|
|
BUSY = True
|
2013-07-10 07:14:44 +00:00
|
|
|
targetFile = view.file_name()
|
2013-07-17 01:26:52 +00:00
|
|
|
now = time.time()
|
|
|
|
if enough_time_passed(now) or (LAST_FILE and targetFile != LAST_FILE):
|
|
|
|
if should_prompt_user(now):
|
|
|
|
if away(now):
|
2013-07-16 16:17:21 +00:00
|
|
|
api(targetFile, now, endtime=LAST_ACTION)
|
2013-07-17 01:26:52 +00:00
|
|
|
else:
|
|
|
|
api(targetFile, now)
|
2013-07-10 07:14:44 +00:00
|
|
|
else:
|
2013-07-17 01:26:52 +00:00
|
|
|
api(targetFile, now, endtime=LAST_ACTION)
|
|
|
|
else:
|
|
|
|
LAST_USAGE = now
|
2013-07-16 16:17:21 +00:00
|
|
|
BUSY = False
|
2013-07-02 09:21:13 +00:00
|
|
|
|
|
|
|
|
|
|
|
class WakatimeListener(sublime_plugin.EventListener):
|
|
|
|
|
|
|
|
def on_post_save(self, view):
|
2013-07-17 01:26:52 +00:00
|
|
|
if view.file_name():
|
|
|
|
#print(['saved', view.file_name()])
|
|
|
|
handle_write_action(view)
|
2013-07-02 09:21:13 +00:00
|
|
|
|
|
|
|
def on_activated(self, view):
|
2013-07-17 01:26:52 +00:00
|
|
|
if view.file_name() and not BUSY:
|
|
|
|
#print(['activated', view.file_name()])
|
|
|
|
handle_normal_action(view)
|
2013-07-08 01:38:01 +00:00
|
|
|
|
2013-07-16 11:02:30 +00:00
|
|
|
def on_modified(self, view):
|
2013-07-17 01:26:52 +00:00
|
|
|
if view.file_name() and not BUSY:
|
|
|
|
#print(['modified', view.file_name()])
|
|
|
|
handle_normal_action(view)
|
2013-07-02 09:21:13 +00:00
|
|
|
|
2013-08-07 00:21:32 +00:00
|
|
|
|
|
|
|
def plugin_loaded():
|
|
|
|
get_api_key()
|
|
|
|
setup_settings_file()
|
|
|
|
if int(sublime.version()) < 3000:
|
|
|
|
plugin_loaded()
|