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-09-05 05:24:56 +00:00
|
|
|
__version__ = '1.3.3'
|
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-08-08 09:17:47 +00:00
|
|
|
import sys
|
2013-07-02 09:21:13 +00:00
|
|
|
import time
|
2013-08-08 09:17:47 +00:00
|
|
|
import threading
|
2013-07-02 09:21:13 +00:00
|
|
|
import uuid
|
2013-07-23 19:11:51 +00:00
|
|
|
from os.path import expanduser, dirname, realpath, isfile, join, exists
|
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
|
|
|
ACTION_FREQUENCY = 5
|
2013-08-08 09:17:47 +00:00
|
|
|
ST_VERSION = int(sublime.version())
|
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-08 09:17:47 +00:00
|
|
|
SETTINGS_FILE = 'WakaTime.sublime-settings'
|
|
|
|
SETTINGS = {}
|
2013-07-08 01:38:01 +00:00
|
|
|
LAST_ACTION = 0
|
|
|
|
LAST_FILE = None
|
2013-08-13 03:30:06 +00:00
|
|
|
HAS_SSL = False
|
2013-08-14 09:36:17 +00:00
|
|
|
LOCK = threading.RLock()
|
2013-07-08 01:38:01 +00:00
|
|
|
|
2013-08-09 01:52:45 +00:00
|
|
|
# check if we have SSL support
|
2013-08-08 09:41:29 +00:00
|
|
|
try:
|
|
|
|
import ssl
|
|
|
|
HAS_SSL = True
|
2013-08-08 09:17:47 +00:00
|
|
|
|
2013-08-13 03:30:06 +00:00
|
|
|
# import wakatime package
|
|
|
|
sys.path.insert(0, join(PLUGIN_DIR, 'packages', 'wakatime'))
|
|
|
|
import wakatime
|
|
|
|
|
|
|
|
except ImportError:
|
|
|
|
from subprocess import Popen
|
2013-08-09 01:52:45 +00:00
|
|
|
|
2013-08-08 09:17:47 +00:00
|
|
|
|
2013-08-07 00:21:32 +00:00
|
|
|
def setup_settings_file():
|
|
|
|
""" Convert ~/.wakatime.conf to WakaTime.sublime-settings
|
|
|
|
"""
|
2013-08-08 09:17:47 +00:00
|
|
|
global SETTINGS
|
2013-08-05 05:11:22 +00:00
|
|
|
# To be backwards compatible, rename config file
|
2013-08-08 09:17:47 +00:00
|
|
|
SETTINGS = sublime.load_settings(SETTINGS_FILE)
|
|
|
|
api_key = SETTINGS.get('api_key', '')
|
2013-08-05 05:15:47 +00:00
|
|
|
if not api_key:
|
2013-08-08 09:17:47 +00:00
|
|
|
api_key = ''
|
2013-08-05 05:15:47 +00:00
|
|
|
try:
|
|
|
|
with open(join(expanduser('~'), '.wakatime.conf')) as old_file:
|
|
|
|
for line in old_file:
|
|
|
|
line = line.split('=', 1)
|
|
|
|
if line[0] == 'api_key':
|
2013-08-08 09:17:47 +00:00
|
|
|
api_key = str(line[1].strip())
|
2013-08-05 05:15:47 +00:00
|
|
|
try:
|
|
|
|
os.remove(join(expanduser('~'), '.wakatime.conf'))
|
|
|
|
except:
|
|
|
|
pass
|
|
|
|
except IOError:
|
|
|
|
pass
|
2013-08-08 09:17:47 +00:00
|
|
|
SETTINGS.set('api_key', api_key)
|
|
|
|
sublime.save_settings(SETTINGS_FILE)
|
2013-08-05 05:11:22 +00:00
|
|
|
|
|
|
|
|
2013-08-14 09:36:17 +00:00
|
|
|
def prompt_api_key():
|
2013-08-08 09:17:47 +00:00
|
|
|
global SETTINGS
|
2013-08-14 09:36:17 +00:00
|
|
|
if not SETTINGS.get('api_key'):
|
2013-08-05 05:11:22 +00:00
|
|
|
def got_key(text):
|
|
|
|
if text:
|
2013-08-14 09:36:17 +00:00
|
|
|
SETTINGS.set('api_key', str(text))
|
2013-08-08 09:17:47 +00:00
|
|
|
sublime.save_settings(SETTINGS_FILE)
|
2013-08-07 00:21:32 +00:00
|
|
|
window = sublime.active_window()
|
2013-08-08 09:17:47 +00:00
|
|
|
if window:
|
2013-08-14 09:36:17 +00:00
|
|
|
window.show_input_panel('Enter your WakaTime api key:', '', got_key, None, None)
|
|
|
|
return True
|
2013-07-23 22:10:19 +00:00
|
|
|
else:
|
2013-08-08 09:17:47 +00:00
|
|
|
print('Error: Could not prompt for api key because no window found.')
|
2013-08-14 09:36:17 +00:00
|
|
|
return False
|
2013-07-10 07:14:44 +00:00
|
|
|
|
2013-08-09 01:52:45 +00:00
|
|
|
|
2013-08-08 09:41:29 +00:00
|
|
|
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
|
2013-07-10 07:14:44 +00:00
|
|
|
|
2013-08-09 01:52:45 +00:00
|
|
|
|
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 handle_write_action(view):
|
2013-08-14 09:36:17 +00:00
|
|
|
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()
|
2013-07-10 07:14:44 +00:00
|
|
|
|
|
|
|
|
|
|
|
def handle_normal_action(view):
|
2013-08-14 09:36:17 +00:00
|
|
|
global LOCK, LAST_FILE, LAST_ACTION
|
|
|
|
with LOCK:
|
|
|
|
targetFile = view.file_name()
|
|
|
|
thread = SendActionThread(targetFile)
|
|
|
|
thread.start()
|
|
|
|
LAST_FILE = targetFile
|
|
|
|
LAST_ACTION = time.time()
|
2013-08-08 09:17:47 +00:00
|
|
|
|
|
|
|
|
|
|
|
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
|
2013-08-14 08:49:08 +00:00
|
|
|
self.debug = SETTINGS.get('debug')
|
2013-08-14 09:36:17 +00:00
|
|
|
self.api_key = SETTINGS.get('api_key', '')
|
2013-08-25 23:54:48 +00:00
|
|
|
self.last_file = LAST_FILE
|
2013-08-08 09:17:47 +00:00
|
|
|
|
|
|
|
def run(self):
|
|
|
|
if self.targetFile:
|
|
|
|
self.timestamp = time.time()
|
2013-08-25 23:54:48 +00:00
|
|
|
if self.force or self.isWrite or self.targetFile != self.last_file or enough_time_passed(self.timestamp):
|
2013-08-08 09:17:47 +00:00
|
|
|
self.send()
|
|
|
|
|
|
|
|
def send(self):
|
2013-08-14 08:49:08 +00:00
|
|
|
if not self.api_key:
|
|
|
|
print('missing api key')
|
2013-08-08 09:17:47 +00:00
|
|
|
return
|
|
|
|
cmd = [
|
|
|
|
API_CLIENT,
|
|
|
|
'--file', self.targetFile,
|
|
|
|
'--time', str('%f' % self.timestamp),
|
|
|
|
'--plugin', 'sublime-wakatime/%s' % __version__,
|
2013-08-14 08:49:08 +00:00
|
|
|
'--key', str(bytes.decode(self.api_key.encode('utf8'))),
|
2013-08-08 09:17:47 +00:00
|
|
|
]
|
|
|
|
if self.isWrite:
|
|
|
|
cmd.append('--write')
|
2013-08-14 08:49:08 +00:00
|
|
|
if self.debug:
|
2013-08-09 01:52:45 +00:00
|
|
|
cmd.append('--verbose')
|
|
|
|
print(cmd)
|
2013-08-13 03:30:06 +00:00
|
|
|
if HAS_SSL:
|
|
|
|
wakatime.main(cmd)
|
|
|
|
else:
|
2013-08-08 09:41:29 +00:00
|
|
|
cmd.insert(0, python_binary())
|
|
|
|
if platform.system() == 'Windows':
|
|
|
|
Popen(cmd, shell=False)
|
|
|
|
else:
|
|
|
|
with open(join(expanduser('~'), '.wakatime.log'), 'a') as stderr:
|
2013-08-13 03:30:06 +00:00
|
|
|
Popen(cmd, stderr=stderr)
|
2013-08-08 09:17:47 +00:00
|
|
|
|
|
|
|
|
|
|
|
def plugin_loaded():
|
|
|
|
setup_settings_file()
|
2013-08-14 09:36:17 +00:00
|
|
|
after_loaded()
|
|
|
|
|
|
|
|
|
|
|
|
def after_loaded():
|
|
|
|
if not prompt_api_key():
|
|
|
|
sublime.set_timeout(after_loaded, 500)
|
2013-08-08 09:17:47 +00:00
|
|
|
|
|
|
|
|
|
|
|
# need to call plugin_loaded because only ST3 will auto-call it
|
|
|
|
if ST_VERSION < 3000:
|
|
|
|
plugin_loaded()
|
2013-07-02 09:21:13 +00:00
|
|
|
|
|
|
|
|
|
|
|
class WakatimeListener(sublime_plugin.EventListener):
|
|
|
|
|
|
|
|
def on_post_save(self, view):
|
2013-08-14 09:36:17 +00:00
|
|
|
handle_write_action(view)
|
2013-07-02 09:21:13 +00:00
|
|
|
|
|
|
|
def on_activated(self, view):
|
2013-08-14 09:36:17 +00:00
|
|
|
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-08-14 09:36:17 +00:00
|
|
|
handle_normal_action(view)
|