2013-07-08 05:38:01 +04:00
|
|
|
""" ==========================================================
|
2013-08-05 09:30:24 +04:00
|
|
|
File: WakaTime.py
|
2013-07-08 05:38:01 +04:00
|
|
|
Description: Automatic time tracking for Sublime Text 2 and 3.
|
2014-03-06 01:55:08 +04:00
|
|
|
Maintainer: WakaTime <support@wakatime.com>
|
2014-03-13 04:03:14 +04:00
|
|
|
License: BSD, see LICENSE for more details.
|
2014-03-15 00:36:20 +04:00
|
|
|
Website: https://wakatime.com/
|
2013-07-08 05:38:01 +04:00
|
|
|
==========================================================="""
|
2013-07-02 13:21:13 +04:00
|
|
|
|
2014-03-06 01:55:08 +04:00
|
|
|
__version__ = '1.6.5'
|
2013-07-02 13:21:13 +04:00
|
|
|
|
2013-07-23 23:09:58 +04:00
|
|
|
import sublime
|
|
|
|
import sublime_plugin
|
|
|
|
|
2013-07-24 02:18:31 +04:00
|
|
|
import glob
|
2013-08-05 09:11:22 +04:00
|
|
|
import os
|
2013-07-23 23:09:58 +04:00
|
|
|
import platform
|
2013-08-08 13:17:47 +04:00
|
|
|
import sys
|
2013-07-02 13:21:13 +04:00
|
|
|
import time
|
2013-08-08 13:17:47 +04:00
|
|
|
import threading
|
2013-07-02 13:21:13 +04:00
|
|
|
import uuid
|
2013-07-23 23:11:51 +04:00
|
|
|
from os.path import expanduser, dirname, realpath, isfile, join, exists
|
2013-07-02 13:21:13 +04:00
|
|
|
|
|
|
|
|
2013-07-08 05:38:01 +04:00
|
|
|
# globals
|
2013-11-28 15:16:43 +04:00
|
|
|
ACTION_FREQUENCY = 2
|
2013-08-08 13:17:47 +04:00
|
|
|
ST_VERSION = int(sublime.version())
|
2013-07-02 13:21:13 +04:00
|
|
|
PLUGIN_DIR = dirname(realpath(__file__))
|
2013-07-21 02:28:38 +04:00
|
|
|
API_CLIENT = '%s/packages/wakatime/wakatime-cli.py' % PLUGIN_DIR
|
2013-08-08 13:17:47 +04:00
|
|
|
SETTINGS_FILE = 'WakaTime.sublime-settings'
|
|
|
|
SETTINGS = {}
|
2013-11-28 15:16:43 +04:00
|
|
|
LAST_ACTION = {
|
|
|
|
'time': 0,
|
|
|
|
'file': None,
|
|
|
|
'is_write': False,
|
|
|
|
}
|
2013-08-13 07:30:06 +04:00
|
|
|
HAS_SSL = False
|
2013-08-14 13:36:17 +04:00
|
|
|
LOCK = threading.RLock()
|
2013-07-08 05:38:01 +04:00
|
|
|
|
2013-08-09 05:52:45 +04:00
|
|
|
# check if we have SSL support
|
2013-08-08 13:41:29 +04:00
|
|
|
try:
|
|
|
|
import ssl
|
2013-09-30 17:58:12 +04:00
|
|
|
import socket
|
|
|
|
socket.ssl
|
2013-08-08 13:41:29 +04:00
|
|
|
HAS_SSL = True
|
2013-09-30 17:58:12 +04:00
|
|
|
except (ImportError, AttributeError):
|
2013-09-23 03:15:43 +04:00
|
|
|
from subprocess import Popen
|
2013-08-08 13:17:47 +04:00
|
|
|
|
2013-09-23 03:15:43 +04:00
|
|
|
if HAS_SSL:
|
2013-08-13 07:30:06 +04:00
|
|
|
# import wakatime package
|
|
|
|
sys.path.insert(0, join(PLUGIN_DIR, 'packages', 'wakatime'))
|
|
|
|
import wakatime
|
|
|
|
|
2013-08-08 13:17:47 +04:00
|
|
|
|
2013-08-14 13:36:17 +04:00
|
|
|
def prompt_api_key():
|
2013-08-08 13:17:47 +04:00
|
|
|
global SETTINGS
|
2013-08-14 13:36:17 +04:00
|
|
|
if not SETTINGS.get('api_key'):
|
2013-08-05 09:11:22 +04:00
|
|
|
def got_key(text):
|
|
|
|
if text:
|
2013-08-14 13:36:17 +04:00
|
|
|
SETTINGS.set('api_key', str(text))
|
2013-08-08 13:17:47 +04:00
|
|
|
sublime.save_settings(SETTINGS_FILE)
|
2013-08-07 04:21:32 +04:00
|
|
|
window = sublime.active_window()
|
2013-08-08 13:17:47 +04:00
|
|
|
if window:
|
2013-12-13 19:08:34 +04:00
|
|
|
window.show_input_panel('[WakaTime] Enter your wakatime.com api key:', '', got_key, None, None)
|
2013-08-14 13:36:17 +04:00
|
|
|
return True
|
2013-07-24 02:10:19 +04:00
|
|
|
else:
|
2013-12-13 19:08:34 +04:00
|
|
|
print('[WakaTime] Error: Could not prompt for api key because no window found.')
|
2013-08-14 13:36:17 +04:00
|
|
|
return False
|
2013-07-10 11:14:44 +04:00
|
|
|
|
2013-08-09 05:52:45 +04:00
|
|
|
|
2013-08-08 13:41:29 +04:00
|
|
|
def python_binary():
|
|
|
|
if platform.system() == 'Windows':
|
|
|
|
try:
|
2013-10-13 19:26:31 +04:00
|
|
|
Popen(['pythonw', '--version'])
|
|
|
|
return 'pythonw'
|
2013-08-08 13:41:29 +04:00
|
|
|
except:
|
2013-10-13 19:26:31 +04:00
|
|
|
for path in glob.iglob('/python*'):
|
|
|
|
if exists(realpath(join(path, 'pythonw.exe'))):
|
|
|
|
return realpath(join(path, 'pythonw'))
|
|
|
|
return None
|
|
|
|
return 'python'
|
2013-07-10 11:14:44 +04:00
|
|
|
|
2013-08-09 05:52:45 +04:00
|
|
|
|
2013-11-28 15:16:43 +04:00
|
|
|
def enough_time_passed(now, last_time):
|
|
|
|
if now - last_time > ACTION_FREQUENCY * 60:
|
2013-07-10 11:14:44 +04:00
|
|
|
return True
|
|
|
|
return False
|
|
|
|
|
|
|
|
|
2013-10-28 08:30:10 +04:00
|
|
|
def handle_action(view, is_write=False):
|
2013-11-28 15:16:43 +04:00
|
|
|
global LOCK, LAST_ACTION
|
2013-08-14 13:36:17 +04:00
|
|
|
with LOCK:
|
2013-10-28 08:30:10 +04:00
|
|
|
target_file = view.file_name()
|
2013-11-28 15:16:43 +04:00
|
|
|
if target_file:
|
|
|
|
thread = SendActionThread(target_file, is_write=is_write)
|
|
|
|
thread.start()
|
|
|
|
LAST_ACTION = {
|
|
|
|
'file': target_file,
|
|
|
|
'time': time.time(),
|
|
|
|
'is_write': is_write,
|
|
|
|
}
|
2013-08-08 13:17:47 +04:00
|
|
|
|
|
|
|
|
|
|
|
class SendActionThread(threading.Thread):
|
|
|
|
|
2013-10-28 08:30:10 +04:00
|
|
|
def __init__(self, target_file, is_write=False, force=False):
|
2013-08-08 13:17:47 +04:00
|
|
|
threading.Thread.__init__(self)
|
2013-10-28 08:30:10 +04:00
|
|
|
self.target_file = target_file
|
|
|
|
self.is_write = is_write
|
2013-08-08 13:17:47 +04:00
|
|
|
self.force = force
|
2013-08-14 12:49:08 +04:00
|
|
|
self.debug = SETTINGS.get('debug')
|
2013-08-14 13:36:17 +04:00
|
|
|
self.api_key = SETTINGS.get('api_key', '')
|
2013-10-28 08:30:10 +04:00
|
|
|
self.ignore = SETTINGS.get('ignore', [])
|
2013-11-28 15:16:43 +04:00
|
|
|
self.last_action = LAST_ACTION
|
2013-08-08 13:17:47 +04:00
|
|
|
|
|
|
|
def run(self):
|
2013-10-28 08:30:10 +04:00
|
|
|
if self.target_file:
|
2013-08-08 13:17:47 +04:00
|
|
|
self.timestamp = time.time()
|
2013-11-28 15:16:43 +04:00
|
|
|
if self.force or (self.is_write and not self.last_action['is_write']) or self.target_file != self.last_action['file'] or enough_time_passed(self.timestamp, self.last_action['time']):
|
2013-08-08 13:17:47 +04:00
|
|
|
self.send()
|
|
|
|
|
|
|
|
def send(self):
|
2013-08-14 12:49:08 +04:00
|
|
|
if not self.api_key:
|
2013-12-13 19:08:34 +04:00
|
|
|
print('[WakaTime] Error: missing api key.')
|
2013-08-08 13:17:47 +04:00
|
|
|
return
|
2013-09-05 18:39:33 +04:00
|
|
|
ua = 'sublime/%d sublime-wakatime/%s' % (ST_VERSION, __version__)
|
2013-08-08 13:17:47 +04:00
|
|
|
cmd = [
|
|
|
|
API_CLIENT,
|
2013-10-28 08:30:10 +04:00
|
|
|
'--file', self.target_file,
|
2013-08-08 13:17:47 +04:00
|
|
|
'--time', str('%f' % self.timestamp),
|
2013-09-05 18:39:33 +04:00
|
|
|
'--plugin', ua,
|
2013-08-14 12:49:08 +04:00
|
|
|
'--key', str(bytes.decode(self.api_key.encode('utf8'))),
|
2013-08-08 13:17:47 +04:00
|
|
|
]
|
2013-10-28 08:30:10 +04:00
|
|
|
if self.is_write:
|
2013-08-08 13:17:47 +04:00
|
|
|
cmd.append('--write')
|
2013-10-28 08:30:10 +04:00
|
|
|
for pattern in self.ignore:
|
|
|
|
cmd.extend(['--ignore', pattern])
|
2013-08-14 12:49:08 +04:00
|
|
|
if self.debug:
|
2013-08-09 05:52:45 +04:00
|
|
|
cmd.append('--verbose')
|
2013-08-13 07:30:06 +04:00
|
|
|
if HAS_SSL:
|
2013-09-21 21:34:40 +04:00
|
|
|
if self.debug:
|
|
|
|
print(cmd)
|
2013-09-30 18:04:24 +04:00
|
|
|
code = wakatime.main(cmd)
|
|
|
|
if code != 0:
|
2013-12-13 19:08:34 +04:00
|
|
|
print('[WakaTime] Error: Response code %d from wakatime package.' % code)
|
2013-08-13 07:30:06 +04:00
|
|
|
else:
|
2013-10-13 08:52:40 +04:00
|
|
|
python = python_binary()
|
2013-10-13 19:26:31 +04:00
|
|
|
if python:
|
2013-10-13 08:52:40 +04:00
|
|
|
cmd.insert(0, python)
|
|
|
|
if self.debug:
|
|
|
|
print(cmd)
|
|
|
|
if platform.system() == 'Windows':
|
|
|
|
Popen(cmd, shell=False)
|
|
|
|
else:
|
|
|
|
with open(join(expanduser('~'), '.wakatime.log'), 'a') as stderr:
|
|
|
|
Popen(cmd, stderr=stderr)
|
2013-10-13 19:26:31 +04:00
|
|
|
else:
|
2013-12-13 19:08:34 +04:00
|
|
|
print('[WakaTime] Error: Unable to find python binary.')
|
2013-08-08 13:17:47 +04:00
|
|
|
|
|
|
|
|
|
|
|
def plugin_loaded():
|
2013-10-28 08:30:10 +04:00
|
|
|
global SETTINGS
|
|
|
|
SETTINGS = sublime.load_settings(SETTINGS_FILE)
|
2013-08-14 13:36:17 +04:00
|
|
|
after_loaded()
|
|
|
|
|
|
|
|
|
|
|
|
def after_loaded():
|
|
|
|
if not prompt_api_key():
|
|
|
|
sublime.set_timeout(after_loaded, 500)
|
2013-08-08 13:17:47 +04:00
|
|
|
|
|
|
|
|
|
|
|
# need to call plugin_loaded because only ST3 will auto-call it
|
|
|
|
if ST_VERSION < 3000:
|
|
|
|
plugin_loaded()
|
2013-07-02 13:21:13 +04:00
|
|
|
|
|
|
|
|
|
|
|
class WakatimeListener(sublime_plugin.EventListener):
|
|
|
|
|
|
|
|
def on_post_save(self, view):
|
2013-10-28 08:30:10 +04:00
|
|
|
handle_action(view, is_write=True)
|
2013-07-02 13:21:13 +04:00
|
|
|
|
|
|
|
def on_activated(self, view):
|
2013-10-28 08:30:10 +04:00
|
|
|
handle_action(view)
|
2013-07-08 05:38:01 +04:00
|
|
|
|
2013-07-16 15:02:30 +04:00
|
|
|
def on_modified(self, view):
|
2013-10-28 08:30:10 +04:00
|
|
|
handle_action(view)
|