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
|
|
|
|
2015-04-01 02:19:30 +03:00
|
|
|
|
2015-08-29 01:06:49 +03:00
|
|
|
__version__ = '4.0.15'
|
2013-07-02 13:21:13 +04:00
|
|
|
|
2015-04-01 02:19:30 +03: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
|
2015-08-01 01:19:56 +03:00
|
|
|
import urllib
|
2015-02-24 09:20:27 +03:00
|
|
|
import webbrowser
|
2015-03-20 11:14:53 +03:00
|
|
|
from datetime import datetime
|
2015-04-01 02:19:30 +03:00
|
|
|
from subprocess import Popen
|
|
|
|
|
2013-07-02 13:21:13 +04:00
|
|
|
|
2013-07-08 05:38:01 +04:00
|
|
|
# globals
|
2015-05-06 22:25:52 +03:00
|
|
|
HEARTBEAT_FREQUENCY = 2
|
2013-08-08 13:17:47 +04:00
|
|
|
ST_VERSION = int(sublime.version())
|
2015-04-01 02:19:30 +03:00
|
|
|
PLUGIN_DIR = os.path.dirname(os.path.realpath(__file__))
|
|
|
|
API_CLIENT = os.path.join(PLUGIN_DIR, 'packages', 'wakatime', 'cli.py')
|
2013-08-08 13:17:47 +04:00
|
|
|
SETTINGS_FILE = 'WakaTime.sublime-settings'
|
|
|
|
SETTINGS = {}
|
2015-05-06 22:25:52 +03:00
|
|
|
LAST_HEARTBEAT = {
|
2013-11-28 15:16:43 +04:00
|
|
|
'time': 0,
|
|
|
|
'file': None,
|
|
|
|
'is_write': False,
|
|
|
|
}
|
2013-08-14 13:36:17 +04:00
|
|
|
LOCK = threading.RLock()
|
2015-03-10 00:32:27 +03:00
|
|
|
PYTHON_LOCATION = None
|
2013-07-08 05:38:01 +04:00
|
|
|
|
2014-12-24 21:01:56 +03:00
|
|
|
|
2015-04-01 02:19:30 +03:00
|
|
|
# add wakatime package to path
|
|
|
|
sys.path.insert(0, os.path.join(PLUGIN_DIR, 'packages'))
|
2013-08-08 13:41:29 +04:00
|
|
|
try:
|
2015-04-01 02:19:30 +03:00
|
|
|
from wakatime.base import parseConfigFile
|
|
|
|
except ImportError:
|
|
|
|
pass
|
2013-08-13 07:30:06 +04:00
|
|
|
|
2013-08-08 13:17:47 +04:00
|
|
|
|
2014-12-24 21:01:56 +03:00
|
|
|
def createConfigFile():
|
|
|
|
"""Creates the .wakatime.cfg INI file in $HOME directory, if it does
|
|
|
|
not already exist.
|
|
|
|
"""
|
|
|
|
configFile = os.path.join(os.path.expanduser('~'), '.wakatime.cfg')
|
|
|
|
try:
|
2014-12-27 03:18:52 +03:00
|
|
|
with open(configFile) as fh:
|
2014-12-24 21:01:56 +03:00
|
|
|
pass
|
|
|
|
except IOError:
|
|
|
|
try:
|
2014-12-27 03:18:52 +03:00
|
|
|
with open(configFile, 'w') as fh:
|
2014-12-24 21:01:56 +03:00
|
|
|
fh.write("[settings]\n")
|
|
|
|
fh.write("debug = false\n")
|
|
|
|
fh.write("hidefilenames = false\n")
|
|
|
|
except IOError:
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
2013-08-14 13:36:17 +04:00
|
|
|
def prompt_api_key():
|
2013-08-08 13:17:47 +04:00
|
|
|
global SETTINGS
|
2014-12-24 21:01:56 +03:00
|
|
|
|
|
|
|
createConfigFile()
|
|
|
|
|
|
|
|
default_key = ''
|
2015-03-10 01:23:29 +03:00
|
|
|
try:
|
|
|
|
configs = parseConfigFile()
|
|
|
|
if configs is not None:
|
|
|
|
if configs.has_option('settings', 'api_key'):
|
|
|
|
default_key = configs.get('settings', 'api_key')
|
|
|
|
except:
|
|
|
|
pass
|
2014-12-24 21:01:56 +03:00
|
|
|
|
2014-06-18 21:10:28 +04:00
|
|
|
if SETTINGS.get('api_key'):
|
|
|
|
return True
|
|
|
|
else:
|
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:
|
2014-12-24 21:01:56 +03:00
|
|
|
window.show_input_panel('[WakaTime] Enter your wakatime.com api key:', default_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():
|
2015-03-10 00:32:27 +03:00
|
|
|
global PYTHON_LOCATION
|
|
|
|
if PYTHON_LOCATION is not None:
|
|
|
|
return PYTHON_LOCATION
|
|
|
|
paths = [
|
|
|
|
"pythonw",
|
|
|
|
"python",
|
|
|
|
"/usr/local/bin/python",
|
|
|
|
"/usr/bin/python",
|
|
|
|
]
|
|
|
|
for path in paths:
|
2013-08-08 13:41:29 +04:00
|
|
|
try:
|
2015-03-10 00:32:27 +03:00
|
|
|
Popen([path, '--version'])
|
|
|
|
PYTHON_LOCATION = path
|
|
|
|
return path
|
2013-08-08 13:41:29 +04:00
|
|
|
except:
|
2015-03-10 00:32:27 +03:00
|
|
|
pass
|
|
|
|
for path in glob.iglob('/python*'):
|
2015-04-01 02:19:30 +03:00
|
|
|
path = os.path.realpath(os.path.join(path, 'pythonw'))
|
2015-03-10 00:32:27 +03:00
|
|
|
try:
|
|
|
|
Popen([path, '--version'])
|
|
|
|
PYTHON_LOCATION = path
|
|
|
|
return path
|
|
|
|
except:
|
|
|
|
pass
|
|
|
|
return None
|
2013-07-10 11:14:44 +04:00
|
|
|
|
2013-08-09 05:52:45 +04:00
|
|
|
|
2015-04-03 01:04:05 +03:00
|
|
|
def obfuscate_apikey(command_list):
|
|
|
|
cmd = list(command_list)
|
2015-04-01 23:01:10 +03:00
|
|
|
apikey_index = None
|
|
|
|
for num in range(len(cmd)):
|
|
|
|
if cmd[num] == '--key':
|
|
|
|
apikey_index = num + 1
|
|
|
|
break
|
|
|
|
if apikey_index is not None and apikey_index < len(cmd):
|
|
|
|
cmd[apikey_index] = '********-****-****-****-********' + cmd[apikey_index][-4:]
|
|
|
|
return cmd
|
|
|
|
|
|
|
|
|
2015-05-06 22:27:39 +03:00
|
|
|
def enough_time_passed(now, last_heartbeat, is_write):
|
|
|
|
if now - last_heartbeat['time'] > HEARTBEAT_FREQUENCY * 60:
|
2013-07-10 11:14:44 +04:00
|
|
|
return True
|
2015-05-06 22:27:39 +03:00
|
|
|
if is_write and now - last_heartbeat['time'] > 2:
|
2015-05-06 22:22:42 +03:00
|
|
|
return True
|
2013-07-10 11:14:44 +04:00
|
|
|
return False
|
|
|
|
|
|
|
|
|
2015-04-03 02:41:55 +03:00
|
|
|
def find_folder_containing_file(folders, current_file):
|
|
|
|
"""Returns absolute path to folder containing the file.
|
|
|
|
"""
|
|
|
|
|
|
|
|
parent_folder = None
|
|
|
|
|
|
|
|
current_folder = current_file
|
|
|
|
while True:
|
2015-02-06 04:40:45 +03:00
|
|
|
for folder in folders:
|
2015-04-03 02:41:55 +03:00
|
|
|
if os.path.realpath(os.path.dirname(current_folder)) == os.path.realpath(folder):
|
|
|
|
parent_folder = folder
|
|
|
|
break
|
|
|
|
if parent_folder is not None:
|
|
|
|
break
|
|
|
|
if not current_folder or os.path.dirname(current_folder) == current_folder:
|
|
|
|
break
|
|
|
|
current_folder = os.path.dirname(current_folder)
|
|
|
|
|
|
|
|
return parent_folder
|
|
|
|
|
|
|
|
|
|
|
|
def find_project_from_folders(folders, current_file):
|
|
|
|
"""Find project name from open folders.
|
|
|
|
"""
|
|
|
|
|
|
|
|
folder = find_folder_containing_file(folders, current_file)
|
2015-04-08 00:20:21 +03:00
|
|
|
return os.path.basename(folder) if folder else None
|
2014-06-18 21:35:59 +04:00
|
|
|
|
|
|
|
|
2015-05-07 00:06:06 +03:00
|
|
|
def is_view_active(view):
|
|
|
|
if view:
|
|
|
|
active_window = sublime.active_window()
|
|
|
|
if active_window:
|
|
|
|
active_view = active_window.active_view()
|
|
|
|
if active_view:
|
|
|
|
return active_view.buffer_id() == view.buffer_id()
|
|
|
|
return False
|
|
|
|
|
|
|
|
|
2015-05-06 22:25:52 +03:00
|
|
|
def handle_heartbeat(view, is_write=False):
|
2015-03-25 21:08:27 +03:00
|
|
|
window = view.window()
|
|
|
|
if window is not None:
|
|
|
|
target_file = view.file_name()
|
2015-04-03 02:41:55 +03:00
|
|
|
project = window.project_data() if hasattr(window, 'project_data') else None
|
2015-03-25 21:08:27 +03:00
|
|
|
folders = window.folders()
|
2015-05-06 22:25:52 +03:00
|
|
|
thread = SendHeartbeatThread(target_file, view, is_write=is_write, project=project, folders=folders)
|
2015-03-25 21:08:27 +03:00
|
|
|
thread.start()
|
2013-08-08 13:17:47 +04:00
|
|
|
|
|
|
|
|
2015-05-06 22:25:52 +03:00
|
|
|
class SendHeartbeatThread(threading.Thread):
|
2013-08-08 13:17:47 +04:00
|
|
|
|
2015-03-20 11:14:53 +03:00
|
|
|
def __init__(self, target_file, view, is_write=False, project=None, folders=None, force=False):
|
2013-08-08 13:17:47 +04:00
|
|
|
threading.Thread.__init__(self)
|
2015-03-20 11:14:53 +03:00
|
|
|
self.lock = LOCK
|
2013-10-28 08:30:10 +04:00
|
|
|
self.target_file = target_file
|
|
|
|
self.is_write = is_write
|
2014-06-18 21:10:28 +04:00
|
|
|
self.project = project
|
2014-06-18 21:35:59 +04:00
|
|
|
self.folders = folders
|
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', [])
|
2015-05-06 22:25:52 +03:00
|
|
|
self.last_heartbeat = LAST_HEARTBEAT.copy()
|
2015-05-07 02:34:15 +03:00
|
|
|
self.cursorpos = view.sel()[0].begin() if view.sel() else None
|
2015-03-20 11:14:53 +03:00
|
|
|
self.view = view
|
2013-08-08 13:17:47 +04:00
|
|
|
|
|
|
|
def run(self):
|
2015-03-20 11:14:53 +03:00
|
|
|
with self.lock:
|
|
|
|
if self.target_file:
|
|
|
|
self.timestamp = time.time()
|
2015-05-06 22:27:39 +03:00
|
|
|
if self.force or self.target_file != self.last_heartbeat['file'] or enough_time_passed(self.timestamp, self.last_heartbeat, self.is_write):
|
2015-03-20 11:14:53 +03:00
|
|
|
self.send_heartbeat()
|
2013-08-08 13:17:47 +04:00
|
|
|
|
2015-03-20 11:14:53 +03:00
|
|
|
def send_heartbeat(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')
|
2015-04-03 02:41:55 +03:00
|
|
|
if self.project and self.project.get('name'):
|
2015-05-16 01:32:03 +03:00
|
|
|
cmd.extend(['--alternate-project', self.project.get('name')])
|
2014-06-18 21:35:59 +04:00
|
|
|
elif self.folders:
|
2015-04-03 02:41:55 +03:00
|
|
|
project_name = find_project_from_folders(self.folders, self.target_file)
|
2014-06-18 21:35:59 +04:00
|
|
|
if project_name:
|
2015-05-16 01:32:03 +03:00
|
|
|
cmd.extend(['--alternate-project', project_name])
|
2015-05-07 02:34:15 +03:00
|
|
|
if self.cursorpos is not None:
|
|
|
|
cmd.extend(['--cursorpos', '{0}'.format(self.cursorpos)])
|
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')
|
2015-04-01 02:19:30 +03:00
|
|
|
if python_binary():
|
|
|
|
cmd.insert(0, python_binary())
|
2015-03-10 01:25:40 +03:00
|
|
|
if self.debug:
|
2015-04-01 23:01:10 +03:00
|
|
|
print('[WakaTime] %s' % ' '.join(obfuscate_apikey(cmd)))
|
2015-04-01 02:19:30 +03:00
|
|
|
if platform.system() == 'Windows':
|
|
|
|
Popen(cmd, shell=False)
|
2015-03-20 11:14:53 +03:00
|
|
|
else:
|
2015-04-01 02:19:30 +03:00
|
|
|
with open(os.path.join(os.path.expanduser('~'), '.wakatime.log'), 'a') as stderr:
|
|
|
|
Popen(cmd, stderr=stderr)
|
|
|
|
self.sent()
|
2013-08-13 07:30:06 +04:00
|
|
|
else:
|
2015-04-01 02:19:30 +03:00
|
|
|
print('[WakaTime] Error: Unable to find python binary.')
|
2013-08-08 13:17:47 +04:00
|
|
|
|
2015-03-20 11:14:53 +03:00
|
|
|
def sent(self):
|
2015-03-24 20:05:23 +03:00
|
|
|
sublime.set_timeout(self.set_status_bar, 0)
|
2015-05-06 22:25:52 +03:00
|
|
|
sublime.set_timeout(self.set_last_heartbeat, 0)
|
2015-03-20 11:14:53 +03:00
|
|
|
|
2015-03-24 20:05:23 +03:00
|
|
|
def set_status_bar(self):
|
|
|
|
if SETTINGS.get('status_bar_message'):
|
2015-06-21 20:42:31 +03:00
|
|
|
self.view.set_status('wakatime', datetime.now().strftime(SETTINGS.get('status_bar_message_fmt')))
|
2015-03-24 20:05:23 +03:00
|
|
|
|
2015-05-06 22:25:52 +03:00
|
|
|
def set_last_heartbeat(self):
|
|
|
|
global LAST_HEARTBEAT
|
|
|
|
LAST_HEARTBEAT = {
|
2015-03-20 11:14:53 +03:00
|
|
|
'file': self.target_file,
|
|
|
|
'time': self.timestamp,
|
|
|
|
'is_write': self.is_write,
|
|
|
|
}
|
|
|
|
|
2013-08-08 13:17:47 +04:00
|
|
|
|
|
|
|
def plugin_loaded():
|
2013-10-28 08:30:10 +04:00
|
|
|
global SETTINGS
|
2014-06-18 21:10:28 +04:00
|
|
|
print('[WakaTime] Initializing WakaTime plugin v%s' % __version__)
|
2014-10-14 22:02:52 +04:00
|
|
|
|
2015-04-01 02:19:30 +03:00
|
|
|
if not python_binary():
|
2015-08-01 01:19:56 +03:00
|
|
|
print('[WakaTime] Warning: Python binary not found.')
|
|
|
|
if platform.system() == 'Windows':
|
|
|
|
install_python()
|
|
|
|
else:
|
|
|
|
sublime.error_message("Unable to find Python binary!\nWakaTime needs Python to work correctly.\n\nGo to https://www.python.org/downloads")
|
|
|
|
return
|
2014-10-14 22:02:52 +04:00
|
|
|
|
2013-10-28 08:30:10 +04:00
|
|
|
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
|
|
|
|
|
|
|
|
2015-08-01 01:19:56 +03:00
|
|
|
def install_python():
|
|
|
|
print('[WakaTime] Downloading and installing python...')
|
|
|
|
url = 'https://www.python.org/ftp/python/3.4.3/python-3.4.3.msi'
|
|
|
|
if platform.architecture()[0] == '64bit':
|
|
|
|
url = 'https://www.python.org/ftp/python/3.4.3/python-3.4.3.amd64.msi'
|
|
|
|
python_msi = os.path.join(os.path.expanduser('~'), 'python.msi')
|
2015-08-01 01:34:24 +03:00
|
|
|
try:
|
|
|
|
urllib.urlretrieve(url, python_msi)
|
|
|
|
except AttributeError:
|
|
|
|
urllib.request.urlretrieve(url, python_msi)
|
2015-08-01 01:19:56 +03:00
|
|
|
args = [
|
|
|
|
'msiexec',
|
|
|
|
'/i',
|
|
|
|
python_msi,
|
|
|
|
'/norestart',
|
|
|
|
'/qb!',
|
|
|
|
]
|
|
|
|
Popen(args)
|
|
|
|
|
|
|
|
|
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):
|
2015-05-06 22:25:52 +03:00
|
|
|
handle_heartbeat(view, is_write=True)
|
2013-07-02 13:21:13 +04:00
|
|
|
|
2015-04-13 02:45:16 +03:00
|
|
|
def on_selection_modified(self, view):
|
2015-05-07 00:06:06 +03:00
|
|
|
if is_view_active(view):
|
2015-05-07 00:00:33 +03:00
|
|
|
handle_heartbeat(view)
|
2013-07-08 05:38:01 +04:00
|
|
|
|
2013-07-16 15:02:30 +04:00
|
|
|
def on_modified(self, view):
|
2015-05-07 00:06:06 +03:00
|
|
|
if is_view_active(view):
|
2015-05-07 00:00:33 +03:00
|
|
|
handle_heartbeat(view)
|
2015-02-24 01:59:10 +03:00
|
|
|
|
|
|
|
|
|
|
|
class WakatimeDashboardCommand(sublime_plugin.ApplicationCommand):
|
2015-02-24 09:20:27 +03:00
|
|
|
|
2015-02-24 01:59:10 +03:00
|
|
|
def run(self):
|
|
|
|
webbrowser.open_new_tab('https://wakatime.com/dashboard')
|