2013-07-08 01:38:01 +00:00
|
|
|
""" ==========================================================
|
2013-07-02 09:21:13 +00:00
|
|
|
File: sublime-wakatime.py
|
2013-07-08 01:38:01 +00:00
|
|
|
Description: Automatic time tracking for Sublime Text 2 and 3.
|
2013-07-02 09:21:13 +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-07-08 01:38:01 +00:00
|
|
|
__version__ = '0.2.1'
|
2013-07-02 09:21:13 +00:00
|
|
|
|
|
|
|
import time
|
|
|
|
import uuid
|
2013-07-09 00:08:13 +00:00
|
|
|
from os.path import expanduser, dirname, realpath, isfile
|
2013-07-02 09:21:13 +00:00
|
|
|
from subprocess import call, Popen
|
|
|
|
|
|
|
|
import sublime
|
|
|
|
import sublime_plugin
|
|
|
|
|
|
|
|
|
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-08 01:38:01 +00:00
|
|
|
API_CLIENT = '%s/packages/wakatime/wakatime.py' % PLUGIN_DIR
|
|
|
|
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-10 07:14:44 +00:00
|
|
|
|
2013-07-09 00:08:13 +00:00
|
|
|
# To be backwards compatible, rename config file
|
|
|
|
if isfile(expanduser('~/.wakatime')):
|
|
|
|
call([
|
|
|
|
'mv',
|
|
|
|
expanduser('~/.wakatime'),
|
|
|
|
expanduser('~/.wakatime.conf')
|
|
|
|
])
|
|
|
|
|
2013-07-08 01:38:01 +00:00
|
|
|
|
2013-07-11 01:43:12 +00:00
|
|
|
# Create config file if it does not already exist
|
|
|
|
if not isfile(expanduser('~/.wakatime.conf')) or True:
|
|
|
|
def got_key(text):
|
|
|
|
if text:
|
|
|
|
cfg = open(expanduser('~/.wakatime.conf'), 'w')
|
|
|
|
cfg.write('api_key=%s' % text)
|
|
|
|
cfg.close()
|
|
|
|
sublime.active_window().show_input_panel('Enter your WakaTi.me api key:', '', got_key, None, None)
|
|
|
|
|
|
|
|
|
2013-07-08 01:38:01 +00:00
|
|
|
def api(targetFile, timestamp, isWrite=False, endtime=None):
|
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:
|
|
|
|
cmd = ['python', API_CLIENT,
|
|
|
|
'--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
|
|
|
]
|
|
|
|
if isWrite:
|
|
|
|
cmd.append('--write')
|
|
|
|
if endtime:
|
|
|
|
cmd.extend(['--endtime', str('%f' % endtime)])
|
|
|
|
Popen(cmd)
|
|
|
|
LAST_ACTION = timestamp
|
|
|
|
if endtime and endtime > LAST_ACTION:
|
|
|
|
LAST_ACTION = endtime
|
|
|
|
LAST_FILE = targetFile
|
2013-07-10 07:14:44 +00:00
|
|
|
LAST_USAGE = LAST_ACTION
|
2013-07-08 01:38:01 +00:00
|
|
|
|
|
|
|
|
|
|
|
def away(now):
|
2013-07-10 07:14:44 +00:00
|
|
|
duration = now - LAST_USAGE
|
|
|
|
units = 'seconds'
|
|
|
|
if duration > 59:
|
|
|
|
duration = int(duration / 60)
|
|
|
|
units = 'minutes'
|
|
|
|
if duration > 59:
|
|
|
|
duration = int(duration / 60)
|
|
|
|
units = 'hours'
|
|
|
|
if duration > 24:
|
|
|
|
duration = int(duration / 24)
|
|
|
|
units = 'days'
|
|
|
|
return sublime\
|
|
|
|
.ok_cancel_dialog("You were away %d %s. Add time to current file?"\
|
|
|
|
% (duration, units), 'Yes, log this time')
|
|
|
|
|
|
|
|
|
|
|
|
def enough_time_passed(now):
|
|
|
|
if now - LAST_ACTION > ACTION_FREQUENCY * 60:
|
|
|
|
return True
|
|
|
|
return False
|
|
|
|
|
|
|
|
|
|
|
|
def should_prompt_user(now):
|
|
|
|
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):
|
|
|
|
now = time.time()
|
|
|
|
targetFile = view.file_name()
|
|
|
|
if enough_time_passed(now) or targetFile != LAST_FILE:
|
|
|
|
if should_prompt_user(now):
|
|
|
|
if away(now):
|
|
|
|
api(targetFile, now, endtime=LAST_ACTION, isWrite=True)
|
|
|
|
else:
|
|
|
|
api(targetFile, now, isWrite=True)
|
|
|
|
else:
|
|
|
|
api(targetFile, now, endtime=LAST_ACTION, isWrite=True)
|
|
|
|
else:
|
|
|
|
api(targetFile, now, isWrite=True)
|
|
|
|
|
|
|
|
|
|
|
|
def handle_normal_action(view):
|
|
|
|
global LAST_USAGE
|
|
|
|
now = time.time()
|
|
|
|
targetFile = view.file_name()
|
|
|
|
if enough_time_passed(now) or targetFile != LAST_FILE:
|
|
|
|
if should_prompt_user(now):
|
|
|
|
if away(now):
|
|
|
|
api(targetFile, now, endtime=LAST_ACTION)
|
|
|
|
else:
|
|
|
|
api(targetFile, now)
|
|
|
|
else:
|
|
|
|
api(targetFile, now, endtime=LAST_ACTION)
|
|
|
|
else:
|
|
|
|
LAST_USAGE = now
|
2013-07-02 09:21:13 +00:00
|
|
|
|
|
|
|
|
|
|
|
class WakatimeListener(sublime_plugin.EventListener):
|
|
|
|
|
|
|
|
def on_post_save(self, view):
|
2013-07-10 07:14:44 +00:00
|
|
|
handle_write_action(view)
|
2013-07-02 09:21:13 +00:00
|
|
|
|
|
|
|
def on_activated(self, view):
|
2013-07-10 07:14:44 +00:00
|
|
|
handle_normal_action(view)
|
2013-07-08 01:38:01 +00:00
|
|
|
|
|
|
|
def on_selection_modified(self, view):
|
2013-07-10 07:14:44 +00:00
|
|
|
handle_normal_action(view)
|
2013-07-02 09:21:13 +00:00
|
|
|
|